<x-admin.layout>
    <x-slot name="title">Rejected List</x-slot>
    <x-slot name="heading">Rejected List</x-slot>

    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-header">
                </div>
                <div class="card-body">
                    <div class="table-responsive">

                        <table id="buttons-datatables" class="table table-bordered nowrap align-middle" style="width:100%">
                            <thead>
                                <tr>
                                    <th>Sr.No</th>
                                    <th>Application.No</th>
                                    <th>Applicant Name</th>
                                    <th>Service ID</th>
                                    <th>Date</th>
                                    {{-- <th>Remark</th> --}}
                                    {{-- <th>Status</th> --}}
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($approvedItems as $pro )
                                    <tr data-id="{{ $pro->status()->where('service_id', $pro->service_id)->first()?->id }}">
                                        <td>{{ $loop->iteration }}</td>
                                        <td>{{ $pro->application_no }}</td>
                                        <td>{{ $user->where('id', $pro->user_id)->first()?->name }}</td>
                                        <td
                                        title="{{ $serviceName->where('service_id', $pro->service_id)->first()?->service_name }}">
                                        {{ \Illuminate\Support\Str::limit($serviceName->where('service_id', $pro->service_id)->first()?->service_name, 30) }}
                                        </td>
                                        <td>{{ isset($pro->created_at) ? date('d-m-Y h:i A', strtotime($pro->created_at)) : 'N/A' }}</td>
                                        {{-- <td>
                                            @if (Auth::check() && Auth::user()->hasRole('Clerk'))
                                            @php
                                                $remark = $statusdata->where('service_id', $pro->service_id)->where('table_id', $pro->id)->value('clerk_remark');
                                            @endphp
                                            @if($remark)
                                                {{ $remark }}
                                            @else
                                                -
                                            @endif

                                        @elseif(Auth::check() && Auth::user()->hasRole('HOD'))
                                            @php
                                                $remark = $statusdata->where('service_id', $pro->service_id)->where('table_id', $pro->id)->value('hod_remark');
                                            @endphp
                                            @if($remark)
                                                {{ $remark }}
                                            @else
                                            -
                                            @endif

                                        @elseif(Auth::check() && Auth::user()->hasRole('Deputy_Co'))
                                            @php
                                                $remark = $statusdata->where('service_id', $pro->service_id)->where('table_id', $pro->id)->value('deputy_coremark');
                                            @endphp
                                            @if($remark)
                                                {{ $remark }}
                                            @else
                                            -
                                            @endif
                                        @endif

                                        </td> --}}
                                        {{-- <td>
                                            @if ($pro->status()->where('service_id', $pro->service_id)->first()?->id)
                                            <a href="{{ array_key_exists($pro->service_id, $editRoute) && $editRoute[$pro->service_id] != '' ? route($editRoute[$pro->service_id], encrypt($pro->id)) : '#' }}"
                                               class="btn btn-primary">View</a>
                                            @else
                                                -
                                            @endif
                                        </td> --}}
                                    </tr>
                                @endforeach
                            </tbody>

                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

</x-admin.layout>

{{-- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@11.4.15/dist/sweetalert2.min.css">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.4.15/dist/sweetalert2.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function() {
        $(document).on('click', '.approveBtn', function() {
            var itemId = $(this).data('id');
            if (!itemId) {
                Swal.fire('Error!', 'Service ID not found!', 'error');
                return;
            }

            Swal.fire({
                title: 'Are you sure?',
                text: 'Do you want to approve this item?',
                icon: 'warning',
                showCancelButton: true,
                confirmButtonText: 'Yes, approve it!',
                cancelButtonText: 'No, cancel!',
            }).then((result) => {
                if (result.isConfirmed) {
                    $.ajax({
                        url: '/approve-item/' + itemId,
                        method: 'POST',
                        data: {
                            _token: '{{ csrf_token() }}',
                        },
                        success: function(response) {
                            Swal.fire('Approved!', response.message, 'success');
                            $('#viewDetailsModal').modal('hide');
                            location.reload();
                        },
                        error: function() {
                            Swal.fire('Error!', 'Something went wrong, please try again.', 'error');
                        }
                    });
                }
            });
        });
    });
</script>
<script>
    $(document).ready(function() {
        $(document).on('click', '.rejectBtn', function() {
            var itemId = $(this).data('id');
            if (!itemId) {
                Swal.fire('Error!', 'Service ID not found!', 'error');
                return;
            }

            Swal.fire({
                title: 'Are you sure?',
                text: 'Do you want to reject this item?',
                icon: 'warning',
                showCancelButton: true,
                confirmButtonText: 'Yes, reject it!',
                cancelButtonText: 'No, cancel!',
            }).then((result) => {
                if (result.isConfirmed) {
                    $.ajax({
                        url: '/reject-item/' + itemId,
                        method: 'POST',
                        data: {
                            _token: '{{ csrf_token() }}',
                        },
                        success: function(response) {
                            Swal.fire('Rejected!', response.message, 'success');
                            $('#').modal('hide');
                            location.reload();
                        },
                        error: function() {
                            Swal.fire('Error!', 'Something went wrong, please try again.', 'error');
                        }
                    });
                }
            });
        });
    });
</script> --}}



