<?php

extract($_GET);
extract($_POST);
$jamb_services = Jamb::find_all();
// $jamb_services = Jamb::find_by_query("SELECT * FROM jamb WHERE status=0");
$jamb_services = array_reverse($jamb_services);
// if (isset($jamb_id)) {

//     // $selected_jamb_services = Jamb::find_by_id($jamb_id);
//     $selected_jamb_services = Jamb::find_by_query("SELECT * FROM jamb WHERE id=$jamb_id AND status=0");
//     $selected_jamb_services = array_shift($selected_jamb_services);
// }
// if (isset($done)) {

//     $jamb = Jamb::find_by_id($jamb_service_id);
//     $jamb->status = 1;
//     $jamb->save();
// }
// if (isset($failed)) {

//     $jamb = Jamb::find_by_id($jamb_service_id);
//     $jamb->status = 2;
//     $jamb->save();
// }
?>
<!-- } else {

        echo '<script>iziToast.warning({
                    title: "Caution!",
                    message: "You cannot transfer fund to yourself",
                    position: "topRight",
                    balloon: true
                });</script>';

    } -->


<div class="p-2 sm:ml-64 mb-14">
    <nav class="md:max-w-max flex px-5 py-2 mt-20 mx-2 text-cyan-700 border border-cyan-200 rounded-lg bg-cyan-50 dark:bg-cyan-800 dark:border-cyan-700"
        aria-label="Breadcrumb">
        <ol class="inline-flex items-center space-x-1 md:space-x-3">
            <li class="inline-flex items-center">
                <a href="<?= BASE_URL ?>"
                    class="inline-flex items-center text-sm font-medium text-gray-700 hover:text-blue-600 dark:text-gray-400 dark:hover:text-white">
                    <svg class="w-3 h-3 mr-2.5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg"
                        fill="currentColor" viewBox="0 0 20 20">
                        <path
                            d="m19.707 9.293-2-2-7-7a1 1 0 0 0-1.414 0l-7 7-2 2a1 1 0 0 0 1.414 1.414L2 10.414V18a2 2 0 0 0 2 2h3a1 1 0 0 0 1-1v-4a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1v4a1 1 0 0 0 1 1h3a2 2 0 0 0 2-2v-7.586l.293.293a1 1 0 0 0 1.414-1.414Z" />
                    </svg>
                    Home
                </a>
            </li>
            <li>
                <div class="flex items-center">
                    <svg class="w-3 h-3 mx-1 text-gray-400" aria-hidden="true" xmlns="http://www.w3.org/2000/svg"
                        fill="none" viewBox="0 0 6 10">
                        <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                            d="m1 9 4-4-4-4" />
                    </svg>
                    <a href="<?= BASE_URL ?>/users"
                        class="ml-1 text-sm font-medium text-gray-700 hover:text-blue-600 md:ml-2 dark:text-gray-400 dark:hover:text-white">JAMB Requests</a>
                </div>
            </li>
            
        </ol>
    </nav>
    <div class="p-2 rounded-lg dark:border-gray-700">

        <!-- Main modal -->
        <div id="crypto-modal" tabindex="-1" aria-hidden="true"
            class="fixed top-0 left-0 right-0 z-50 hidden w-full p-4 overflow-x-hidden overflow-y-auto md:inset-0 h-[calc(100%-1rem)] max-h-full backdrop-blur transition-all">
            <div class="relative w-full max-w-md max-h-full">
                <!-- Modal content -->
                <div class="relative rounded-lg shadow bg-cyan-700">
                    <button type="button"
                        class="absolute top-3 right-2.5 bg-transparent  rounded-lg text-sm w-8 h-8 ml-auto inline-flex justify-center items-center hover:bg-gray-600 hover:text-white"
                        data-modal-hide="crypto-modal">
                        <svg class="w-3 h-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none"
                            viewBox="0 0 14 14">
                            <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                d="m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6" />
                        </svg>
                        <span class="sr-only">Close modal</span>
                    </button>
                    <!-- Modal header -->
                    <div class="px-6 py-2 border-b rounded-t dark:border-gray-600">
                        <h3 class="text-base font-semibold lg:text-xl text-white">
                            Wallet Top-up
                        </h3>
                    </div>
                    <!-- Modal body -->
                    <div class="p-4">
                        <h3 class="text-xl font-medium text-gray-900 dark:text-white">Minimun Top-up Amount is ₦500</h3>
                        <p class="p-2 bg-pale-200 rounded text-sm font-normal text-gray-500 dark:text-gray-400">Note:
                            Every Funding will be Charge from your Wallet Balance and will be recoded.</p>
                        <form class="space-y-6" id="formm" method="POST" action="" role="form">
                            <div>
                                <label for="amount" class="block mb-2 text-sm font-medium text-white">Enter the amount
                                    you will like to top-up.</label>
                                <input type="number" name="amount" id="amount"
                                    class="border text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 bg-gray-600 border-gray-500 placeholder-gray-400 text-white"
                                    placeholder="e.g 500" required>
                            </div>

                            <button id="pay_btn" type="submit" name="update_wallet"
                                class="w-full text-white focus:ring-4 focus:outline-none  font-medium rounded-lg text-sm px-5 py-2.5 text-center bg-blue-600 hover:bg-blue-700 focus:ring-blue-800">Fund
                                Wallet</button>

                        </form>
                    </div>
                </div>
            </div>
        </div>




        <div class="grid grid-cols-1 gap-4 mb-4 ">

            <div
                class="relative flex flex-col h-full min-w-0 break-words bg-sky-800 border-0 shadow-soft-xl rounded bg-clip-border">
                <div
                    class="relative py-3 px-3 flex flex-col min-w-0 break-words bg-sky-800 border-0 shadow-soft-xl rounded bg-clip-border">
                    <!-- <div class="p-6 mt-0 bg-white rounded-t-2xl"> -->
                    <h6 class="text-sm text-white">Recent Services Performed</h6>
                    <hr class="h-px bg-transparent bg-gradient-to-r from-transparent via-black/40 to-transparent" />
                    <!-- </div> -->
                    <div class="table-responsive text-gray-400" style="font-size: 12px !important;">
                        <table class="table table-flush text-white datatable text-sm " datatable id="datatable-search"
                            style="font-size: 12px !important; padding: 0;">
                            <thead class="thead-light">
                                <tr>
                                    <th>#</th>
                                    <!-- <th>User ID</th> -->
                                    <th>User Email</th>
                                    <th>Service</th>
                                    <th>Sub-service</th>
                                    <th>Type</th>
                                    <th>Profile Code / JAMB Reg. No.</th>
                                    <th>Year</th>
                                    <th>Name</th>
                                    <th>Status</th>
                                    <th>Document (1)</th>
                                    <th>Document (2)</th>
                                    <th>Date Submitted</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                // $jamb_services = Jamb::find_by_query("SELECT * FROM jamb WHERE status = 0");
                                
                                $i = 1;
                                foreach ($jamb_services as $jamb_service):

                                    ?>
                                    <tr class='hover:bg-sky-300 hover:text-sky-950'>

                                        <td>
                                            <?= $i ?>
                                        </td>
                                        <!-- <td>
                                            <?= $jamb_service->user_id ?>
                                        </td> -->
                                        <td>
                                            <?= User::find_by_id($jamb_service->user_id)->email ?>
                                        </td>
                                        <td>
                                            <?= Service::find_by_id($jamb_service->service_id)->service_name ?>
                                        </td>
                                        <td>
                                            <?= $jamb_service->sub_service ?>
                                        </td>
                                        <td>
                                            <?= $jamb_service->type ?>
                                        </td>
                                        <td>
                                            <?= $jamb_service->profile_code ?>
                                        </td>
                                        <td>
                                            <?= $jamb_service->jamb_year ?>
                                        </td>
                                        <td>
                                            <?= $jamb_service->fullname ?>
                                        </td>

                                        <td>

                                            <?php

                                            if ($jamb_service->status == 1) {
                                                echo '<span class="badge badge-lg bg-gradient-to-tl from-green-600 to-lime-400">Done</span>';
                                            } else if ($jamb_service->status == 0) {
                                                echo '<span
                                        class="badge badge-lg bg-gradient-to-tl from-yellow-600 to-yellow-400">Pending</span>';
                                            } else {
                                                echo '<span
                                            class="badge badge-lg bg-red-500">Failed</span>';
                                            } ?>
                                        </td>
                                        <td>
                                            <?php if ($jamb_service->doc_1 != ''): ?>
                                                <button data-file='<?= $jamb_service->doc_1 ?>'
                                                    class="p-1 rounded bg-blue-500 download_doc">Download</button>
                                            <?php endif; ?>

                                        </td>
                                        <td>
                                            <?php if ($jamb_service->doc_1 != ''): ?>
                                                <button data-file='<?= $jamb_service->doc_2 ?>'
                                                    class="p-1 rounded bg-blue-500 download_doc">Download</button>
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <?= $jamb_service->submitted_date ?>
                                        </td>
                                        <td>
                                            <?php
                                            if ($jamb_service->status == 0): ?>

                                                <button data-id="<?= $jamb_service->id ?>" data-type="done"
                                                    class="rounded p-1 bg-green-500 change_status">Done</button>
                                                <button data-id="<?= $jamb_service->id ?>" data-type="failed"
                                                    class="rounded p-1 bg-red-500 change_status">Failed</button>
                                            <?php endif ?>
                                        </td>
                                    </tr>
                                    <?php $i++;
                                endforeach; ?>
                            <tbody>
                        </table>
                    </div>
                </div>
            </div>

        </div>


    </div>



</div>
<script>




    $(document).on("click", ".download_doc", function (e) {

        e.preventDefault()

        var file_name = $(this).data('file')
        var file_path = "<?= ROOT_URL ?>/jamb_docs/" + file_name


        download(file_path, file_name)



    })

    function download(url, filename) {

        //window.open(url)
        //return false
        var link = document.createElement('a');
        link.href = url;
        link.download = filename;
        link.target = "_blank"; // Open the link in a new tab/window
        link.style.display = "none"; // Hide the link
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);

    }
</script>

<script>
    $(document).on('click', '.change_status', function () {

        var id = $(this).data('id')
        var type = $(this).data('type')

        var data = {
            id,
            type
        }

        $.ajax({
            url: "../main_includes/components/change_status.php",
            data: data,
            method: "POST",
            success: function (res) {


                if (res.response_code == '00') {

                    // iziToast.success({
                    //     title: 'OK',
                    //     message: res.message,
                    //     position: "topRight",
                    //     balloon: true
                    // });

                    window.location.reload()
                    
                } else {
                    $("#personal_detail").fadeOut("slow");
                    window.location.reload()

                    // iziToast.error({
                    //     title: 'Error',
                    //     message: res.message,
                    //     position: "topRight",
                    //     balloon: true
                    // });
                }

            }

        })

    })
</script>