@extends('layout.app')


@section('content')
    <div class="card">
        <div class="card-body">
            <h2>District List</h2>
            <div class="float-right mb-2">
                <a href="{{ route('upazilaList') }}" class="btn btn-primary btn-sm">Upazila</a>
                <a href="{{ route('thanaList') }}" class="ml-1 btn btn-primary btn-sm">Thana</a>
                @can('add-district')
                    <button type="button" class="ml-1 btn btn-primary btn-sm" data-toggle="modal"
                        data-target="#addDistrictModal">Add District</button>
                @endcan
                @can('add-division')

                <button type="button" class="ml-1 btn btn-primary btn-sm" data-toggle="modal"
                    data-target="#addDivisionModal">Add Division</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">Division</th>
                            <th scope="col" class="text-center"> Name</th>
                            <th scope="col" class="text-center">District Name(Bengali)</th>
                            <th scope="col" class="text-center"></th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($districts as $r)
                            <tr>
                                <td class="text-center">{{ $r->id }}</td>
                                <td class="text-center">{{ $r->division->name ?? '' }}</td>
                                <td class="text-center">{{ $r->name }}</td>
                                <td class="text-center">{{ $r->bn_name }}</td>
                                <td class="d-flex">
                                    <a href="{{ route('location.edit', $r->id) }}" class="btn btn-primary btn-sm">Edit</a>
                                    @can('district_delete')
                                        <form action="{{ route('district.destroy', $r->id) }}" method="POST">
                                            @method('DELETE')
                                            @csrf
                                            <button class=" btn btn-danger form-control">Delete</button>
                                        </form>
                                    @endcan
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Add District Modal -->
    <div class="modal fade" id="addDistrictModal" tabindex="-1" role="dialog" aria-labelledby="addDistrictModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="addDistrictModalLabel">Add District</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <form action="{{ route('location.store') }}" method="POST">
                    @csrf
                    <div class="modal-body">
                        <div class="form-group">
                            <label for="modal_division_id">Division</label>
                            <select name="division_id" id="modal_division_id" class="form-control" required>
                                <option value="">Select Division</option>
                                @foreach ($divisions as $item)
                                    <option value="{{ $item->id }}">{{ $item->name }} -- {{ $item->bn_name }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="modal_name">District Name (English)</label>
                            <input type="text" name="name" id="modal_name" class="form-control"
                                placeholder="District name" required>
                        </div>
                        <div class="form-group">
                            <label for="modal_bn_name">District Name (Bengali)</label>
                            <input type="text" name="bn_name" id="modal_bn_name" class="form-control"
                                placeholder="District name (Bengali)">
                        </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 District</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Add Division Modal -->
    <div class="modal fade" id="addDivisionModal" tabindex="-1" role="dialog" aria-labelledby="addDivisionModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="addDivisionModalLabel">Add Division</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <form action="{{ route('divisionStore') }}" method="POST">
                    @csrf
                    <div class="modal-body">
                        <div class="form-group">
                            <label for="division_modal_name">Division Name (English)</label>
                            <input type="text" name="name" id="division_modal_name" class="form-control"
                                placeholder="Division name" required>
                        </div>
                        <div class="form-group">
                            <label for="division_modal_bn_name">Division Name (Bengali)</label>
                            <input type="text" name="bn_name" id="division_modal_bn_name" class="form-control"
                                placeholder="Division name (Bengali)">
                        </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 Division</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endsection
