@extends('layouts.admin')
@section('content')
<!-- start page title -->
<div class="row">
    <div class="col-12">
        <div class="page-title-box d-sm-flex align-items-center justify-content-between bg-galaxy-transparent">
            <h4 class="mb-sm-0">Emirates</h4>

            <div class="page-title-right">
                <ol class="breadcrumb m-0">
                    <li class="breadcrumb-item"><a href="javascript: void(0);">Home</a></li>
                    <li class="breadcrumb-item active">Emirates</li>
                </ol>
            </div>
        </div>
    </div>
</div>
<!-- end page title -->

<div class="row">
    <div class="col-lg-12">
        <div class="card" id="orderList">
            <div class="card-header border-0">
                <div class="row align-items-center gy-3">
                    <div class="col-sm">
                        <h5 class="card-title mb-0">Emirate</h5>
                    </div>
                    <div class="col-sm-auto">
                        <div class="d-flex gap-1 flex-wrap">
                            <a href="{{ route('admin.emirates.create') }}" class="btn btn-success add-btn"><i class="ri-add-line align-bottom me-1"></i>Create Emirate</a>
                        </div>
                    </div>
                </div>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table id="EmirateTable" class="table table-bordered nowrap align-middle" style="width: 100%;">
                        <thead class="">
                            <tr class="text-uppercase">
                                <th style="width: 40px;">Sl. No</th>
                                <th>Country</th>
                                <th>Name</th>
                                <th>Name (Arabic)</th>
                                <th>Status</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody class="list form-check-all">
                            
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <!--end col-->
</div>
<!--end row-->

<div id="removeemirate" class="modal fade zoomIn" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" id="NotificationModalbtn-close"></button>
            </div>
            <div class="modal-body">
                <div class="mt-2 text-center">
                    <lord-icon src="https://cdn.lordicon.com/gsqxdxog.json" trigger="loop" colors="primary:#f7b84b,secondary:#f06548" style="width: 100px; height: 100px;"></lord-icon>
                    <div class="mt-4 pt-2 fs-15 mx-4 mx-sm-5">
                        <h4>Are you sure ?</h4>
                        <p class="text-muted mx-4 mb-0">Are you sure you want to remove this emirate.</p>
                    </div>
                </div>
                <div class="d-flex gap-2 justify-content-center mt-4 mb-2">
                    <button type="button" class="btn w-sm btn-light" data-bs-dismiss="modal">Close</button>
                    <button type="button" class="btn w-sm btn-danger" id="delete-notification">Yes, Delete It!</button>
                </div>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>
<!-- /.modal -->

@endsection
@section('script')
<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.5/dist/jquery.validate.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.5/dist/additional-methods.min.js"></script>
<script>
    $(document).ready(function() {
        $('#EmirateTable').DataTable({
            responsive: true, 
            paging: true,     
            searching: true,  
            ordering: true,   
            lengthChange: true,
            serverSide: true,
            ajax: {
            url: "{{ route('admin.getEmirateList') }}",
        },
            columns: [
                { data: 'id', name: 'id' },
                { data: 'country_name', name: 'country_name' , orderable: false},
                { data: 'emirate_name', name: 'emirate_name'},
                { data: 'emirate_name_ar', name: 'emirate_name_ar' , orderable: false},
                { data: 'status', name: 'status' , orderable: false},
                { data: 'action', name: 'action' }
            ],
            columnDefs: [
                {
                    orderable: false,
                    targets: [0, 5]
                },
                {
                    "targets": 0,
                    "render": function(data, type, row, meta) {
                        const pageInfo = $('#EmirateTable').DataTable().page.info();
                        const startIndex = pageInfo.start;
                        return startIndex + meta.row + 1;
                    }
                }
            ],
        });
    });

</script>
<script>
    $('#removeemirate').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget)
        var id = button.data('emirate-id')
        var modal = $(this)
        modal.find('#delete-notification').attr('data-id', id)
    })
</script>
<script>
    $(document).on('click', '#delete-notification', function () {
        var id = $(this).data('id');
        $.ajax({
            url: "{{ route('admin.emirates.destroy', ':id') }}".replace(':id', id),
            type: 'POST',
            processData: false,
            contentType: false,
            headers: 
            {
                "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content")
            },
            success: function (response) {
                location.reload();
            },
            error: function (xhr, status, error) {
                Swal.fire({
                    title: "Error!",
                    text: "Something went wrong. Please try again later.",
                    icon: "error",
                    confirmButtonText: "OK"
                });
            }
        })  
    })
</script>
@endsection
