@extends('layout.app')


@section('content')
    <div class="card">
        <div class="card-body">
            <h2>Thana List</h2>
            <div class="float-right mb-2">
                <a href="{{ route('location.index') }}" class="btn btn-primary btn-sm">District</a>
                <a href="{{ route('upazilaList') }}" class="btn btn-primary btn-sm">Upazila</a>
                @can('add-thana')
                    <button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#addThanaModal">Add
                        Thana</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 Name</th>
                            <th scope="col" class="text-center">Upazila Name</th>
                            <th scope="col" class="text-center">Thana Name</th>
                            <th scope="col" class="text-center">Thana Name(Bengali)</th>
                            <th scope="col" class="text-center">Coverage Status</th>
                            <th scope="col" class="text-center"></th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($thanas as $r)
                            <tr>
                                <td class="text-center">{{ $r->id }}</td>
                                <td class="text-center">{{ $r->upazila->district->name ?? '' }}</td>
                                <td class="text-center">{{ $r->upazila->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('thanaEdit', $r->id) }}" class="btn btn-primary btn-sm">Edit</a>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Add Thana Modal -->
    <div class="modal fade" id="addThanaModal" tabindex="-1" role="dialog" aria-labelledby="addThanaModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="addThanaModalLabel">Add Thana</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <form action="{{ route('thanaStore') }}" method="POST">
                    @csrf
                    <div class="modal-body">
                        <div class="form-group">
                            <label for="modal_upazila_id">Upazila</label>
                            <select name="upazila_id" id="modal_upazila_id" class="form-control select2" required>
                                <option value="">Select Upazila</option>
                                @foreach ($upazilas as $item)
                                    <option value="{{ $item->id }}">{{ $item->name }} -- {{ $item->bn_name }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="modal_name">Thana Name (English)</label>
                            <input type="text" name="name" id="modal_name" class="form-control"
                                placeholder="Thana name" required>
                        </div>
                        <div class="form-group">
                            <label for="modal_bn_name">Thana Name (Bengali)</label>
                            <input type="text" name="bn_name" id="modal_bn_name" class="form-control"
                                placeholder="Thana 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 Thana</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endsection

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