@extends('layouts.app')
@section('content')
<?php
    $isEdit = !empty($details['id'] ?? null);
    $districtKey = $details['uuid'] ?? $details['id'] ?? null;
?>
<div class="pcoded-content">
    <div class="pcoded-inner-content">

        <div class="main-body">
            <div class="page-wrapper">
                <!-- Page-header start -->
                <div class="page-header card page-body">
                    <div class="row align-items-end">
                        <div class="col-lg-8">
                            <div class="page-header-title">
                                <i class="icofont icofont icofont icofont-file-document bg-c-pink"></i>
                                <div class="d-inline">
                                    <h4>Districts</h4>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-4">
                            <div class="page-header-breadcrumb">
                                <ul class="breadcrumb-title">
                                    <li class="breadcrumb-item">
                                        <a href="index.html">
                                            <i class="icofont icofont-home"></i>
                                        </a>
                                    </li>
                                    <li class="breadcrumb-item"><a href="#!">Location</a>
                                    </li>
                                    <li class="breadcrumb-item"><a href="#!">Districts</a>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                        <hr>
                        <div class="row">
                            <div class="col-sm-12">
                                @if (session('success_message'))
                                    <div class="alert alert-success alert-dismissible fade show" role="alert">
                                        {{ session('success_message') }}
                                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                            <span aria-hidden="true">&times;</span>
                                        </button>
                                    </div>
                                @endif

                                @if (session('error_message'))
                                    <div class="alert alert-danger alert-dismissible fade show" role="alert">
                                        {{ session('error_message') }}
                                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                            <span aria-hidden="true">&times;</span>
                                        </button>
                                    </div>
                                @endif

                                @if ($errors->any())
                                    <div class="alert alert-danger alert-dismissible fade show" role="alert">
                                        <ul class="mb-0">
                                            @foreach ($errors->all() as $error)
                                                <li>{{ $error }}</li>
                                            @endforeach
                                        </ul>
                                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                            <span aria-hidden="true">&times;</span>
                                        </button>
                                    </div>
                                @endif

                                <form method="POST" action="<?= $isEdit ? url('/location/districts/update/'.$districtKey) : url('/location/districts/store') ?>">
                                    @csrf
                                    <div class="form-group row">
                                        <label class="col-sm-2 col-form-label">Name</label>
                                        <div class="col-sm-10">
                                            <input type="text" name="name" class="form-control" placeholder="District Name" value="{{ old('name', $details['name'] ?? '') }}">
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label class="col-sm-2 col-form-label">Code</label>
                                        <div class="col-sm-4">
                                            <input type="text" name="code" class="form-control" placeholder="District Code" value="{{ old('code', $details['code'] ?? '') }}">
                                        </div>
                                        <label class="col-sm-2 col-form-label">Region</label>
                                        <div class="col-sm-4">
                                           <select name="regions_id" class="form-control" required="required">
                                               <option value=""> Select One</option>
                                               @foreach($regions as $region)
                                                   <option value="{{ $region->id }}" {{ (string) old('regions_id', $details['regions_id'] ?? '') === (string) $region->id ? 'selected' : '' }}>{{ $region->name }}</option>
                                               @endforeach
                                           </select>
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label class="col-sm-2 col-form-label">Latitude</label>
                                        <div class="col-sm-10">
                                            <input type="text" name="latitude" class="form-control" placeholder="Latitude" value="{{ old('latitude', $details['latitude'] ?? ($details['latitudes'] ?? '')) }}" >
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label class="col-sm-2 col-form-label">Longitude</label>
                                        <div class="col-sm-10">
                                            <input type="text" name="longitude" class="form-control" placeholder="Longitude" value="{{ old('longitude', $details['longitude'] ?? ($details['longitudes'] ?? '')) }}" >
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label class="col-sm-2 col-form-label">Notes</label>
                                        <div class="col-sm-10">
                                            <textarea rows="5" cols="5" class="form-control"
                                            placeholder="Details" name="notes">{{ old('notes', $details['notes'] ?? '') }}</textarea>
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label class="col-sm-10 col-form-label"></label>
                                        <button type="submit" class="col-sm-2 btn btn-primary">{{ $isEdit ? 'Update' : 'Submit' }}</button>
                                    </div>
                                </form>
                            </div>
                        </div>

                </div>
                <!-- Page-header end -->
            </div>
        </div>
        <div id="styleSelector">

        </div>
    </div>
</div>
@endsection
