@extends('layouts.main')

@section('breadcrumbs')
    {{ Breadcrumbs::render('pembayaran') }}
@endsection

@section('content')
    <div class="grid">
        <div class="min-w-full border card border-agi-100 card-grid" data-datatable="false" data-datatable-page-size="10"
            data-datatable-state-save="false" id="pembayaran-table" data-api-url="{{ route('pembayaran.lebih.datatables') }}">
            <div class="flex-wrap py-5 card-header bg-agi-50">
                <h3 class="card-title">
                    Daftar Pembayaran Lebih Bayar
                </h3>
                <div class="flex flex-wrap gap-2 lg:gap-5">
                    <div class="flex">
                        <label class="input input-sm"> <i class="ki-filled ki-magnifier"> </i>
                            <input placeholder="Search Lebih Bayar" id="search" type="text" value="">
                        </label>
                    </div>
                    <div class="flex flex-wrap gap-2.5">
                        <div class="h-[24px] border border-r-gray-200"></div>
                        <a class="btn btn-sm btn-light" href="#"> Export to Excel </a>
                    </div>
                </div>
            </div>
            <div class="card-body">
                <div class="scrollable-x-auto">
                    <table class="table text-sm font-medium text-gray-700 align-middle table-auto table-border"
                        data-datatable-table="true">
                        <thead>
                            <tr>
                                <th class="w-14">
                                    <input class="checkbox checkbox-sm" data-datatable-check="true" type="checkbox" />
                                </th>
                                <th class="min-w-[150px]" data-datatable-column="nomor_registrasi">
                                    <span class="sort"> <span class="sort-label"> Nomor Registrasi </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px]" data-datatable-column="nomor_tiket">
                                    <span class="sort"> <span class="sort-label"> Nomor Tiket </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px]" data-datatable-column="tanggal_permohonan">
                                    <span class="sort"> <span class="sort-label"> Tanggal Permohonan </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px]" data-datatable-column="user_id">
                                    <span class="sort"> <span class="sort-label"> User Pemohon </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px]" data-datatable-column="branch_id">
                                    <span class="sort"> <span class="sort-label"> Cabang Pemohon </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px]" data-datatable-column="debitur_id">
                                    <span class="sort"> <span class="sort-label"> Debitur </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px] text-center" data-datatable-column="status_bayar">
                                    <span class="sort"> <span class="sort-label"> Status Bayar </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px] text-center" data-datatable-column="nominal_lebih_bayar">
                                    <span class="sort"> <span class="sort-label"> Nominal </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>

                                <th class="min-w-[50px] text-center" data-datatable-column="actions">Action</th>
                            </tr>
                        </thead>
                    </table>
                </div>
                <div
                    class="flex-col gap-3 justify-center font-medium text-gray-600 card-footer md:justify-between md:flex-row text-2sm">
                    <div class="flex gap-2 items-center">
                        Show
                        <select class="w-16 select select-sm" data-datatable-size="true" name="perpage"> </select> per
                        page
                    </div>
                    <div class="flex gap-4 items-center">
                        <span data-datatable-info="true"> </span>
                        <div class="pagination" data-datatable-pagination="true">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

@push('scripts')
    <script type="text/javascript">
        function pembayaranOtorisator(id) {
            // alert('hai id = ' + id);
            Swal.fire({
                title: 'Apakah Anda yakin?',
                text: `Untuk melakukan approve Pembayaran!`,
                icon: 'warning',
                input: 'textarea', // Menambahkan input textarea
                inputLabel: 'Keterangan',
                inputPlaceholder: 'Masukkan keterangan...',
                inputAttributes: {
                    'aria-label': 'Masukkan keterangan'
                },
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Ya, Lanjutkan!',
                cancelButtonText: 'Lain kali',
            }).then((result) => {
                if (result.isConfirmed) {

                    let useURL = "{{ URL::to('pembayaran') }}" + "/" + id;
                    const keterangan = result.value || ''; // Ambil pesan dari textarea

                    var input_data = new Object();
                    input_data._method = 'PUT';
                    input_data.id = id;
                    input_data.keterangan = keterangan;

                    $.ajaxSetup({
                        headers: {
                            'X-CSRF-TOKEN': '{{ csrf_token() }}'
                        },
                    });
                    $.ajax({
                        url: useURL,
                        type: 'PUT',
                        data: input_data,
                        dataType: "json",
                        success: (response) => {
                            if ('success' == response.status) {
                                Swal.fire('Berhasil!', response.message, 'success').then(() => {
                                    window.location.reload();
                                });
                            } else if ('error' == response.status) {
                                Swal.fire('Error!', response.message, 'error').then(() => {
                                    window.location.reload();
                                });
                            }

                            console.log(response);
                        },
                        error: (error) => {
                            console.error('Error:', error);
                            Swal.fire('Gagal!', 'Terjadi kesalahan saat melakukan otorisator.',
                                'error');
                        }
                    });
                }
            });
        }
    </script>
    <script type="module">
        const element = document.querySelector('#pembayaran-table');
        const searchInput = document.getElementById('search');

        const apiUrl = element.getAttribute('data-api-url');
        const dataTableOptions = {
            apiEndpoint: apiUrl,
            pageSize: 5,
            columns: {
                select: {
                    render: (item, data, context) => {
                        const checkbox = document.createElement('input');
                        checkbox.className = 'checkbox checkbox-sm';
                        checkbox.type = 'checkbox';
                        checkbox.value = data.id.toString();
                        checkbox.setAttribute('data-datatable-row-check', 'true');
                        return checkbox.outerHTML.trim();
                    },
                },
                nomor_registrasi: {
                    title: 'Nomor Registrasi',
                    render: (item, data) => {
                        return `${data.permohonan?.nomor_registrasi}`;
                    },
                },
                nomor_tiket: {
                    title: 'Nomor Tiket',
                },
                tanggal_permohonan: {
                    title: 'Tanggal Permohonan',
                    render: (item, data) => {
                        return `${data.permohonan?.tanggal_permohonan}`;
                    },
                },
                user_id: {
                    title: 'User Pemohon',
                    render: (item, data) => {
                        return `${data.pemohon?.name}`;
                    },
                },
                branch_id: {
                    title: 'Cabang Pemohon',
                    render: (item, data) => {
                        if (data.branch) {
                            return `${data.branch.code} - ${data.branch.name}`;
                        }
                        return '-';
                    },
                },
                debitur_id: {
                    title: 'Debitur',
                    render: (item, data) => {
                        return `${data.debiture?.name}`;
                    },
                },
                status_bayar: {
                    title: 'Status Bayar',
                    render: (item, data) => {
                        return `<span class="font-bold text-green-600 uppercase text-md">
                            Lebih Bayar
                            </span>`;
                    },
                },
                nominal_lebih_bayar: {
                    title: 'Lebih Bayar'
                },
                actions: {
                    title: 'Status',
                    render: (item, data) => {
                        return `<div class="flex gap-2 justify-center">
                        <a class="btn btn-icon btn-clear btn-warning" href="pembayaran/${data.id}/lebih" title="Lakukan Pembayaran">
                            <i class="ki-outline ki-credit-cart"></i>
                        </a>
                        </div>`;
                    },
                }
            },
        };

        let dataTable = new KTDataTable(element, dataTableOptions);
        // Custom search functionality
        searchInput.addEventListener('input', function() {
            const searchValue = this.value.trim();
            dataTable.search(searchValue, true);

        });
    </script>
@endpush
