@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">Mother Certificate Verification</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">Mother Certificate Verification</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">Users Waiting for Mother Certificate Verification</h5>
                    </div>
                </div>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table id="MotherCertTable" class="table table-bordered nowrap align-middle" style="width: 100%;">
                        <thead class="">
                            <tr class="text-uppercase">
                                <th>Sl No</th>
                                <th>Profile Image</th>
                                <th>User ID</th>
                                <th>Name</th>
                                <th>Mobile Number</th>
                                <th>Nationality</th>
                                <th>Sect</th>
                                <th>Verify Mother Certificate</th>
                                <th>Created Date</th>
                                <th>Actions</th>
                            </tr>
                        </thead> 
                        <tbody class="list form-check-all">
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

@endsection

@section('script')
<script>
    $(document).ready(function() {
        var table = $('#MotherCertTable').DataTable({
            responsive: true, 
            paging: true,     
            searching: true,  
            ordering: true,   
            lengthChange: true, 
            serverSide: true,
            ajax: {
                url: "{{ route('admin.getMotherCertificateVerificationList') }}",
                type:'GET',
            },
            columns: [
                { data: 'id', name: 'id' },
                { data: 'user_image', name: 'user_image' },
                { data: 'user_number', name: 'user_number' },
                { data: 'name', name: 'name' },
                { data: 'phone', name: 'phone' },
                { data: 'nationality_name', name: 'nationality_name' },
                { data: 'sect_name', name: 'sect_name' },
                { data: 'mother_certificate_verified ', name: 'mother_certificate_verified ' },
                { data: 'created_at', name: 'created_at' },
                { data: 'action', name: 'action' }
            ],
            columnDefs: [
                {
                    orderable: false,
                    targets: [0, 1, 7, 9]
                },
                {
                    "targets": 0,
                    "render": function(data, type, row, meta) {
                        const pageInfo = $('#MotherCertTable').DataTable().page.info();
                        return pageInfo.start + meta.row + 1;
                    }
                }
            ],
        });

        $(document).on('change', '.verify-mother-cert', function() {
            var url = $(this).data('url');
            var id = $(this).data('id');
            var isChecked = $(this).is(':checked');

            if (isChecked) {
                Swal.fire({
                    title: 'Are you sure?',
                    text: "You want to verify this mother certificate?",
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: 'Yes, verify it!'
                }).then((result) => {
                    if (result.isConfirmed) {
                        $.ajax({
                            url: url,
                            type: 'POST',
                            data: {
                                _token: '{{ csrf_token() }}'
                            },
                            success: function(response) {
                                if (response.status == 'success') {
                                    Swal.fire(
                                        'Verified!',
                                        response.message,
                                        'success'
                                    );
                                    table.ajax.reload();
                                } else {
                                    Swal.fire(
                                        'Error!',
                                        response.message,
                                        'error'
                                    );
                                    table.ajax.reload();
                                }
                            },
                            error: function() {
                                Swal.fire(
                                    'Error!',
                                    'Something went wrong.',
                                    'error'
                                );
                                table.ajax.reload();
                            }
                        });
                    } else {
                        $(this).prop('checked', false);
                    }
                });
            }
        });
    });
</script>
@endsection
