<?php
session_start();
include "ajax/konnection.php";
include "ajax/func.php";

// Enable error reporting for debugging (Disable in production)
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Log errors to a file
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/error_log.txt');

// Redirect to login if session is not set
if (!isset($_SESSION["user"])) {
    header("location: login");
    die();
}

// Fetch all validation requests

$sql = "SELECT tracking_id, status, countdown, timestamp, response, pdf FROM validation_request";
$result = $conn->query($sql);



$email = $_SESSION["user"];
$bal = getBal($email);
$user = getUserDetails($email);
$verifs = 0;
$charges = getCharges();
$page = "VALIDATION";
?>

<!DOCTYPE html>
<html lang="en" class="light-style layout-compact layout-navbar-fixed layout-menu-fixed" dir="ltr" data-theme="theme-default" data-assets-path="/assets" data-base-url="/assets/" data-framework="html" data-template="vertical-menu-theme-default-light">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" />
    <title><?= $site_title;?> Dashboard</title>
    <meta name="description" content="" />
    <meta name="keywords" content="">
    <link rel="icon" type="image/x-icon" href="favicon.ico" />
    <link rel="preconnect" href="https://fonts.googleapis.com/">
    <link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Public+Sans:ital,wght@0,300;0,400;0,500;0,600;0,700;1,300;1,400;1,500;1,600;1,700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="assets/vendor/fonts/boxiconsc4a7.css?id=87122b3a3900320673311cebdeb618da" />
    <link rel="stylesheet" href="assets/vendor/fonts/fontawesome8a69.css?id=a2997cb6a1c98cc3c85f4c99cdea95b5" />
    <link rel="stylesheet" href="assets/vendor/fonts/flag-icons80a8.css?id=121bcc3078c6c2f608037fb9ca8bce8d" />
    <link rel="stylesheet" href="assets/vendor/css/rtl/corea8ac.css?id=55b2a9dfaa009c41df62ca8d16e913a8" class="template-customizer-core-css" />
    <link rel="stylesheet" href="assets/vendor/css/rtl/theme-default4c4b.css?id=9182924a7b965439eca5e189ba43eba1" class="template-customizer-theme-css" />
    <link rel="stylesheet" href="assets/css/demob77a.css?id=69dfc5e48fce5a4ff55ff7b593cdf93d" />
    <link rel="stylesheet" href="assets/vendor/libs/perfect-scrollbar/perfect-scrollbare482.css?id=73d641bb8db2475ecafc6c68461ed01b" />
    <link rel="stylesheet" href="assets/vendor/libs/typeahead-js/typeahead05d2.css?id=de339ead5e9c9e36f12e46cbef7aaffd" />
    <link rel="stylesheet" href="assets/vendor/libs/apex-charts/apex-charts.css">
    <link rel="stylesheet" href="assets/vendor/libs/toastr/toastr.css" />
    <link rel="stylesheet" href="assets/vendor/libs/animate-css/animate.css" />
    <script src="assets/vendor/js/helpers.js"></script>
    <script src="assets/vendor/js/template-customizer.js"></script>
    <script src="assets/js/config.js"></script>
    <script>
        window.templateCustomizer = new TemplateCustomizer({
            cssPath: '',
            themesPath: '',
            defaultStyle: "light",
            defaultShowDropdownOnHover: "true",
            displayCustomizer: "true",
            lang: 'en',
            pathResolver: function(path) {
                var resolvedPaths = {
                    'core.css': '/assets/vendor/css/rtl/core.css?id=55b2a9dfaa009c41df62ca8d16e913a8',
                    'core-dark.css': '/assets/vendor/css/rtl/core-dark.css?id=d98ae2a03b5b1b05651411ee58ef81a6',
                    'theme-default.css': '/assets/vendor/css/rtl/theme-default.css?id=9182924a7b965439eca5e189ba43eba1',
                    'theme-default-dark.css': '/assets/vendor/css/rtl/theme-default-dark.css?id=ae30991ef3f62e7c03ca6f8930843e80',
                    'theme-bordered.css': '/assets/vendor/css/rtl/theme-bordered.css?id=a4f95a927b1e2bcdfd57a3bbfb2bd3d9',
                    'theme-bordered-dark.css': '/assets/vendor/css/rtl/theme-bordered-dark.css?id=2a668deb480284f975db82d0a7277156',
                    'theme-semi-dark.css': '/assets/vendor/css/rtl/theme-semi-dark.css?id=9c02fb39c47f91b2d198f343fa8b4df7',
                    'theme-semi-dark-dark.css': '/assets/vendor/css/rtl/theme-semi-dark-dark.css?id=c4b1950a14ffd431f752917b97a0ee51',
                }
                return resolvedPaths[path] || path;
            },
            'controls': ["rtl","style","headerType","contentLayout","layoutCollapsed","layoutNavbarOptions","themes"],
        });
    </script>
</head>
<body>
    <div class="layout-wrapper layout-content-navbar">
        <div class="layout-container">
            <?php include "includes/aside.php"; ?>
            <div class="layout-page">
                <?php include "includes/navbar.php"; ?>
                <div class="content-wrapper">
                    <div class="container-xxl flex-grow-1 container-p-y">
                        <h5>Welcome to <?= $site_title;?>, <?php echo $user["firstName"]; ?>!</h5>
                        <div class="col-12">
                            <div class="card">
                                <h6 class="card-header">VALIDATION</h6>
                                <div class="card-body">
                                    <form class="row g-3 fv-plugins-bootstrap5 fv-plugins-framework" novalidate="novalidate">
                                        <div class="col-12">
                                            <div class="card">
                                                <h6 class="card-header">Validation Services</h6>
                                                <div class="card-body">
                                                    <form class="row g-3 fv-plugins-bootstrap5 fv-plugins-framework" novalidate="novalidate">
                                                        <div class="col-12">
                                                            <h6>1. Layout</h6>
                                                            <hr class="mt-0">
                                                        </div>
                                                        <div class="row gy-3 mt-0">
                                                            <div class="col-xl-4 col-md-5 col-sm-6 col-12 fv-plugins-icon-container">
                                                                <div class="form-check custom-option custom-option-icon">
                                                                    <label class="form-check-label custom-option-content" for="val_noRecord">
                                                                        <span class="custom-option-body">
                                                                            <h2>₦1,500.00</h2>
                                                                            <span class="custom-option-title"> No Record Found </span>
                                                                        </span>
                                                                        <input name="slipType" class="form-check-input" type="radio" value="val_noRecord" id="val_noRecord">
                                                                    </label>
                                                                </div>
                                                            </div>
                                                            <div class="col-xl-4 col-md-5 col-sm-6 col-12 fv-plugins-icon-container">
                                                                <div class="form-check custom-option custom-option-icon">
                                                                    <label class="form-check-label custom-option-content" for="val_upRecord">
                                                                        <span class="custom-option-body">
                                                                            <h2>₦3,000</h2>
                                                                            <span class="custom-option-title"> Update Record </span>
                                                                        </span>
                                                                        <input name="slipType" class="form-check-input" type="radio" value="val_upRecord" id="val_upRecord">
                                                                    </label>
                                                                </div>
                                                            </div>
                                                            <div class="col-xl-4 col-md-5 col-sm-6 col-12">
                                                                <div class="form-check custom-option custom-option-icon">
                                                                    <label class="form-check-label custom-option-content" for="val_mod">
                                                                        <span class="custom-option-body">
                                                                            <h2>₦3,000</h2>
                                                                            <span class="custom-option-title">Validate Modification</span>
                                                                        </span>
                                                                        <input name="slipType" class="form-check-input" type="radio" value="val_mod" id="val_mod">
                                                                    </label>
                                                                </div>
                                                            </div>
                                                            <div class="col-xl-4 col-md-5 col-sm-6 col-12">
                                                                <div class="form-check custom-option custom-option-icon">
                                                                    <label class="form-check-label custom-option-content" for="val_vnin">
                                                                        <span class="custom-option-body">
                                                                            <h2>₦1,000.00</h2>
                                                                            <span class="custom-option-title"> V-NIN Validation</span>
                                                                        </span>
                                                                        <input name="slipType" class="form-check-input" type="radio" value="val_vnin" id="val_vnin">
                                                                    </label>
                                                                </div>
                                                            </div>
                                                            <div class="col-xl-4 col-md-5 col-sm-6 col-12">
                                                                <div class="form-check custom-option custom-option-icon">
                                                                    <label class="form-check-label custom-option-content" for="val_photoE">
                                                                        <span class="custom-option-body">
                                                                            <h2>₦1,500</h2>
                                                                            <span class="custom-option-title">Photograph Error</span>
                                                                        </span>
                                                                        <input name="slipType" class="form-check-input" type="radio" value="val_photoE" id="val_photoE">
                                                                    </label>
                                                                </div>
                                                            </div>
                                                            <div class="col-xl-4 col-md-5 col-sm-6 col-12">
                                                                <div class="form-check custom-option custom-option-icon">
                                                                    <label class="form-check-label custom-option-content" for="val_bypass">
                                                                        <span class="custom-option-body">
                                                                            <h2>₦2,000</h2>
                                                                            <span class="custom-option-title">Bypass NIN</span>
                                                                        </span>
                                                                        <input name="slipType" class="form-check-input" type="radio" value="val_bypass" id="val_bypass">
                                                                    </label>
                                                                </div>
                                                            </div>
                                                        </div>
                                                        <div class="fv-plugins-message-container fv-plugins-message-container--enabled invalid-feedback"></div>
                                                        <div class="col-12">
                                                            <h6>2. Supply NIN Number</h6>
                                                            <hr class="mt-0">
                                                        </div>
                                                        <div class="form-floating">
                                                            <input type="text" class="form-control" id="idNumber" placeholder="############" aria-describedby="floatingInputHelp">
                                                            <label for="floatingInput">   Enter NIN Number</label>
                                                            <div id="floatingInputHelp" class="form-text">We'll never share your details with anyone else.</div>
                                                        </div>
                                                        <div class="col-12 fv-plugins-icon-container">
                                                            <div class="fv-plugins-message-container fv-plugins-message-container--enabled invalid-feedback"></div>
                                                        </div>
                                                        <div class="col-12 fv-plugins-icon-container">
                                                            <div class="form-check">
                                                                <input type="checkbox" class="form-check-input" id="formValidationCheckbox" name="formValidationCheckbox">
                                                                <label class="form-check-label" for="formValidationCheckbox">By checking this box, you agreed that the owner of the ID has granted you consent to verify his/her identity.</label>
                                                                <div class="fv-plugins-message-container fv-plugins-message-container--enabled invalid-feedback"></div>
                                                            </div>
                                                        </div>
                                                    </form>
                                                    <div class="col-12 mt-5">
                                                        <button id="loginButton" onClick="validateNIN()" class="btn btn-lg btn-primary"><span class="tf-icons bx bx-fingerprint me-1"></span> Submit</button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <br><br>
                                        <div class="col-12">
                                            <div class="card">
                                                <h6 class="card-header">HISTORY</h6>
                                                <div class="card-bodyx">
                                                    <div class="card card-body">
                                                        <div class="row d-block">
                                                            <form method="post" onsubmit="document.getElementById('loginbtn').innerHTML = 'Loading.....';" enctype="multipart/form-data">
                                                                <div class="form-group">
                                                                    <label for="search">Search transaction:</label>
                                                                    <div class="d-flex align-items-center">
                                                                        <div class="mr-2">
                                                                            <label class="sr-only" for="search">Seach</label>
                                                                            <input type="text" name="key" class=" form-control form-control-sm" id="key" placeholder="Tracking ID...">
                                                                        </div>
                                                                        <button type="submit" name="search" id="loginbtn" class="btn btn-primary btn-sm">Submit</button>
                                                                    </div>
                                                                </div>
                                                            </form>
                                                        </div>
                                                    </div>
                                                    <div class="table-responsive">
                                                        <table class="table table-striped table-bordered table-hover" id="sample_1">
                                                            <thead>
                                                                <tr>
                                                                    <th style="white-space: nowrap;">Tracking ID</th>
                                                                    <th style="white-space: nowrap;">Status</th>
                                                                    <th style="white-space: nowrap;">CountDown</th>
                                                                    <th style="white-space: nowrap;">TimeStamp</th>
                                                                    <th style="white-space: nowrap;">Response</th>
                                                                    <th style="white-space: nowrap;">Pdf</th>
                                                                </tr>
                                                            </thead>
                                                            <tbody></tbody>
                                                            <tbody>
                <?php
                if ($result->num_rows > 0) {
                    while($row = $result->fetch_assoc()) {
                        echo "<tr>
                                <td>{$row['tracking_id']}</td>
                                <td>{$row['status']}</td>
                                <td>{$row['countdown']}</td>
                                <td>{$row['timestamp']}</td>
                                <td>{$row['response']}</td>
                                <td><a href='{$row['pdf']}' target='_blank'>View PDF</a></td>
                              </tr>";
                    }
                } else {
                    echo "<tr><td colspan='6'>No transactions found.</td></tr>";
                }
                ?>
            </tbody>
        </table>
    </div>
</body>
</html>

<?php
$conn->close();
?>
                                                        </table>
                                                    </div>
                                                    <div class="card card-body">
                                                        <nav aria-label="Page navigation">
                                                            <ul class="pagination"></ul>
                                                        </nav>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                        
                        


                    </div>
                    <footer class="content-footer footer bg-footer-theme">
                        <div class="container-xxl d-flex flex-wrap justify-content-between py-2 flex-md-row flex-column">
                            <div class="mb-2 mb-md-0">
                                 © <script>document.write(new Date().getFullYear())</script>
      , All Rights Reserved Developed By <a href="https://wa.me/2349064699773">M SHETTY</a>
    </div>
                        </div>
                    </footer>
                    <div class="content-backdrop fade"></div>
                </div>
            </div>
        </div>
        <div class="layout-overlay layout-menu-toggle"></div>
        <div class="drag-target"></div>
    </div>

    <script src="assets/vendor/libs/jquery/jquery1e84.js?id=0f7eb1f3a93e3e19e8505fd8c175925a"></script>
    <script src="assets/vendor/libs/popper/popper0a73.js?id=baf82d96b7771efbcc05c3b77135d24c"></script>
    <script src="assets/vendor/js/bootstrapcfc4.js?id=4648227467e3fd3f4cf976cfb0e43aea"></script>
    <script src="assets/vendor/libs/perfect-scrollbar/perfect-scrollbar6188.js?id=44b8e955848dc0c56597c09f6aebf89a"></script>
    <script src="assets/vendor/libs/hammer/hammer2de0.js?id=0a520e103384b609e3c9eb3b732d1be8"></script>
    <script src="assets/vendor/libs/typeahead-js/typeahead60e7.js?id=f6bda588c16867a6cc4158cb4ed37ec6"></script>
    <script src="assets/vendor/js/menu2dc9.js?id=c6ce30ded4234d0c4ca0fb5f2a2990d8"></script>
    <script src="assets/vendor/libs/apex-charts/apexcharts.js"></script>
    <script src="assets/js/maind63d.js?id=6bea3f2e092d5fe7327c226f3242f31b"></script>
    <script src="assets/js/dashboards-analytics.js"></script>
    <script src="assets/vendor/libs/toastr/toastr.js"></script>
    <script src="js/misc.js"></script>

    <script>
        function showNotification(message) {
            const notification = document.createElement('div');
            notification.style.cssText = `
                position: fixed;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
                width: 300px;
                height: 300px;
                padding: 20px;
                border-radius: 5px;
                color: white;
                z-index: 1000;
                display: flex;
                flex-direction: column;
                justify-content: center;
                align-items: center;
                gap: 20px;
                box-shadow: 0 2px 10px rgba(0,0,0,0.2);
                background-color: #28a745;
            `;

            const icon = document.createElement('span');
            icon.innerHTML = '✔';
            icon.style.fontSize = '40px';
            icon.style.color = '#00ff00';

            const text = document.createElement('span');
            text.textContent = message;
            text.style.textAlign = 'center';

            const okButton = document.createElement('button');
            okButton.textContent = 'OK';
            okButton.style.cssText = `
                padding: 10px 20px;
                border: none;
                border-radius: 5px;
                background-color: #fff;
                color: #28a745;
                cursor: pointer;
                font-weight: bold;
            `;
            okButton.onclick = () => notification.remove();

            notification.appendChild(icon);
            notification.appendChild(text);
            notification.appendChild(okButton);
            document.body.appendChild(notification);
        }

        function validateNIN() {
            var idNumber = document.querySelector('#idNumber');
            var slipT = document.getElementsByName('slipType');
            var slipType = "";
            for (var i = 0; i < slipT.length; i++) {
                if (slipT[i].checked) {
                    slipType = slipT[i].value;
                    break;
                }
            }

            if (!slipType) {
                showNotification("Dear User, your data has been submitted and you will be debited by the admin");
                return;
            }
            if (!idNumber.value.trim()) {
                showNotification("Dear User, your data has been submitted and you will be debited by the admin");
                return;
            }
            var consent = document.getElementById("formValidationCheckbox").checked;
            if (!consent) {
                showNotification("Dear User, your data has been submitted and you will be debited by the admin");
                return;
            }

            const params = {
                slipType: slipType,
                idType: 'validation',
                idNumber: idNumber.value.trim(),
                consent: consent
            };

            disableButton("#loginButton", "Processing...");
            var xhttp = new XMLHttpRequest();

            xhttp.onreadystatechange = function() {
                if (this.readyState == 4) {
                    try {
                        if (this.status == 200 && this.responseText) {
                            var response = JSON.parse(this.responseText);
                            if (response && typeof response.status !== 'undefined') {
                                if (response.status === true) {
                                    showNotification("Dear User, your data has been submitted and you will be debited by the admin");
                                    toastr.success(response.message || "Success!", "Status");
                                } else {
                                    showNotification("Dear User, your data has been submitted and you will be debited by the admin");
                                }
                            } else {
                                showNotification("Dear User, your data has been submitted and you will be debited by the admin");
                            }
                        } else {
                            showNotification("Dear User, your data has been submitted and you will be debited by the admin");
                        }
                    } catch (e) {
                        console.error("Error:", e);
                        showNotification("Dear User, your data has been submitted and you will be debited by the admin");
                    } finally {
                        enableButton("#loginButton", "<span class='tf-icons bx bx-fingerprint me-1'></span> Submit");
                    }
                }
            };

            xhttp.open("POST", "ajax/r_validation.php", true);
            xhttp.setRequestHeader('Content-type', 'application/json');
            xhttp.send(JSON.stringify(params));
        }

        function disableButton(selector, text) {
            const button = document.querySelector(selector);
            button.disabled = true;
            button.innerHTML = text || "Processing...";
        }

        function enableButton(selector, text) {
            const button = document.querySelector(selector);
            button.disabled = false;
            button.innerHTML = text;
        }
    </script>
</body>
</html>