@extends('layout.app')


@section('content')
    <div class="card">
        <div class="card-body">
            <h2>Upazila List</h2>
            <div class="float-right mb-2">
                <a href="{{ route('location.index') }}" class="btn btn-primary btn-sm">District</a>
                <a href="{{ route('thanaList') }}" class="ml-1 btn btn-primary btn-sm">Thana</a>
                @can('add-upazila')
                    <button type="button" class="ml-1 btn btn-primary btn-sm" data-toggle="modal"
                        data-target="#addUpazilaModal">Add Upazila</button>
                @endcan
            </div>
            <div class="table-responsive">
                <table id="dataTable" class="display">
                    <thead class="bg-gray-50">
                        <tr>
                            <th scope="col" class="text-center">#</th>
                            <th scope="col" class="text-center">District</th>
                            <th scope="col" class="text-center">Upazila Name</th>
                            <th scope="col" class="text-center">Upazila Name(Bengali)</th>
                            <th scope="col" class="text-center">Coverage Status</th>
                            <th scope="col" class="text-center"></th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($upazilas as $r)
                            <tr>
                                <td class="text-center">{{ $r->id }}</td>
                                <td class="text-center">{{ $r->district->name ?? '' }}</td>
                                <td class="text-center">{{ $r->name }}</td>
                                <td class="text-center">{{ $r->bn_name }}</td>
                                <td class="text-center">{{ $r->coverage_status }}</td>
                                <td class="d-flex">
                                    <a href="{{ route('upazilaEdit', $r->id) }}" class="btn btn-primary btn-sm">Edit</a>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Add Upazila Modal -->
    <div class="modal fade" id="addUpazilaModal" tabindex="-1" role="dialog" aria-labelledby="addUpazilaModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="addUpazilaModalLabel">Add Upazila</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <form action="{{ route('upazilaStore') }}" method="POST">
                    @csrf
                    <div class="modal-body">
                        <div class="form-group">
                            <label for="modal_district_id">District</label>
                            <select name="district_id" id="modal_district_id" class="form-control select2" required>
                                <option value="">Select District</option>
                                @foreach ($districts as $item)
                                    <option value="{{ $item->id }}">{{ $item->name }} -- {{ $item->bn_name }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="modal_name">Upazila Name (English)</label>
                            <input type="text" name="name" id="modal_name" class="form-control"
                                placeholder="Upazila name" required>
                        </div>
                        <div class="form-group">
                            <label for="modal_bn_name">Upazila Name (Bengali)</label>
                            <input type="text" name="bn_name" id="modal_bn_name" class="form-control"
                                placeholder="Upazila name (Bengali)">
                        </div>
                        <div class="form-group">
                            <label for="modal_coverage_status">Coverage Status</label>
                            <select name="coverage_status" id="modal_coverage_status" class="form-control">
                                <option value="">Select Coverage Status</option>
                                <option value="Coverage">Coverage</option>
                                <option value="Not Coverage">Not Coverage</option>
                            </select>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-primary">Save Upazila</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endsection

@push('js')
    <script>
        $(function() {
            $('#addUpazilaModal').on('shown.bs.modal', function() {
                if (!$('#modal_district_id').hasClass('select2-hidden-accessible')) {
                    $('#modal_district_id').select2({
                        dropdownParent: $('#addUpazilaModal'),
                        width: '100%'
                    });
                }
            });
            $('#addUpazilaModal').on('hidden.bs.modal', function() {
                if ($('#modal_district_id').hasClass('select2-hidden-accessible')) {
                    $('#modal_district_id').select2('destroy');
                }
            });
        });
    </script>
@endpush
