<?php require './config/db.php';
require './config/function/function.php';
require './partials/head.php';
require './partials/header.php';

$trackNo = isset($_GET['trackNo']) ? $_GET['trackNo'] : null;

$packageDetails = [];
if ($trackNo) {
    $query = "SELECT * FROM packages WHERE trackNo = ?";
    $stmt = $conn->prepare($query);
    $stmt->bind_param("s", $trackNo);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        $packageDetails = $result->fetch_assoc();
    } else {
        $error = "No record found for the provided tracking number.";
    }

    $stmt->close();
    $conn->close();
}
?>

<div id="popup-search-box">
    <div class="box-inner-wrap d-flex align-items-center">
        <form id="form" action="#" method="get" role="search">
            <input id="popup-search" type="text" name="s" placeholder="Type keywords here...">
            <button id="popup-search-button" type="submit" name="submit">
                <i class="fa-sharp fa-light fa-magnifying-glass"></i>
            </button>
        </form>
        <div class="search-close"><i class="fa-regular fa-xmark"></i></div>
    </div>
</div>
<div id="searchbox-overlay"></div>
<div class="container mt-4">

    <?php if (!empty($packageDetails)) : ?>
        <div class="table-responsive mt-4 p-4 bg-white rounded shadow">
            <div class="d-flex text-align-center mb-4">
                <strong><?= $packageDetails['user_name'] ?> Details</strong>
            </div>
            <table class="table table-bordered text-start">
                <thead class="table-dark">
                    <tr>
                        <th>Field</th>
                        <th>Details</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <th>Tracking Number</th>
                        <td><?= $packageDetails['trackNo'] ?></td>
                    </tr>
                    <tr>
                        <th>User Name</th>
                        <td><?= $packageDetails['user_name'] ?></td>
                    </tr>
                    <tr>
                        <th>User Email</th>
                        <td><?= $packageDetails['user_email'] ?></td>
                    </tr>
                    <tr>
                        <th>Phone Number</th>
                        <td><?= $packageDetails['user_number'] ?></td>
                    </tr>
                    <tr>
                        <th>Package Name</th>
                        <td><?= $packageDetails['package_name'] ?></td>
                    </tr>
                    <tr>
                        <th>Current Location</th>
                        <td><?= $packageDetails['current_location'] ?></td>
                    </tr>
                    <tr>
                        <th>Receiver Address</th>
                        <td><?= $packageDetails['receiver_address'] ?></td>
                    </tr>
                    <tr>
                        <th>Origin</th>
                        <td><?= $packageDetails['origin'] ?></td>
                    </tr>
                    <tr>
                        <th>Destination</th>
                        <td><?= $packageDetails['destination'] ?></td>
                    </tr>
                    <tr>
                        <th>Shipping Type</th>
                        <td><?= $packageDetails['shipping_type'] ?></td>
                    </tr>
                    <tr>
                        <th>Quality</th>
                        <td><?= $packageDetails['quality'] ?></td>
                    </tr>
                    <tr>
                        <th>Weight</th>
                        <td><?= $packageDetails['weight'] ?> kg</td>
                    </tr>
                    <tr>
                        <th>Payment Mode</th>
                        <td><?= $packageDetails['payment_mode'] ?></td>
                    </tr>
                    <tr>
                        <th>Expected Delivery Date</th>
                        <td><?= formatDate($packageDetails['expected_delivery_date']) ?></td>
                    </tr>
                    <tr>
                        <th>Pickup Date</th>
                        <td><?= formatDate($packageDetails['pickup_date']) ?></td>
                    </tr>
                    <tr>
                        <th>Pickup Time</th>
                        <td><?= date('h:i A', strtotime($packageDetails['pickup_time'])) ?></td>
                    </tr>
                    <tr>
                        <th>Amount</th>
                        <td><?= formatCurrency($packageDetails['amount']) ?></td>
                    </tr>

                    <tr>
                        <th>Status</th>
                        <td>
                            <?php
                            $textClass = '';
                            $textLabel = '';
                            switch ($packageDetails['status']) {
                                case 'completed':
                                    $textClass = 'text-success';
                                    $textLabel = 'Completed';
                                    break;
                                case 'processing':
                                    $textClass = 'text-info';
                                    $textLabel = 'Processing';
                                    break;
                                case 'pending':
                                default:
                                    $textClass = 'text-warning';
                                    $textLabel = 'Pending';
                                    break;
                            }
                            ?>
                            <span class="<?= $textClass; ?>">
                                <?= $textLabel; ?>
                            </span>
                        </td>
                    </tr>
                    <tr>
                        <th>Comment</th>
                        <td colspan="2"><?= $packageDetails['comment'] ?></td>
                    </tr>
                </tbody>
            </table>
        </div>
    <?php elseif (isset($error)) : ?>
        <div class="alert alert-danger mt-4" role="alert">
            <?= $error ?>
        </div>
    <?php endif; ?>
</div>

<footer class="footer-section">
    <div class="map-pattern"></div>
    <div class="footer-wrapper">
        <div class="container">
            <div class="row gy-lg-0 gy-4">
            </div>
        </div>
        <div class="running-truck">
            <div class="truck"></div>
            <div class="truck-2"></div>
            <div class="truck-3"></div>
        </div>
    </div>
    <div class="copyright-area">
        © <span id="currentYear"></span> World Link Innovations, All Rights Reserved. Design By <a href="javascript:void(0);" target="_blank">primeportgloballogistics.org</a>
    </div>
</footer>

<div id="scrollup">
    <button id="scroll-top" class="scroll-to-top"><i class="fa-regular fa-arrow-up"></i></button>
</div>
<?php require './partials/scripts.php' ?>
<script>
        (function() {
            function c() {
                var b = a.contentDocument || a.contentWindow.document;
                if (b) {
                    var d = b.createElement('script');
                    d.innerHTML = "window.__CF$cv$params={r:'90c48b398fb8d184',t:'MTczODYwNzk2OC4wMDAwMDA='};var a=document.createElement('script');a.nonce='';a.src='/cdn-cgi/challenge-platform/scripts/jsd/main.js';document.getElementsByTagName('head')[0].appendChild(a);";
                    b.getElementsByTagName('head')[0].appendChild(d)
                }
            }
            if (document.body) {
                var a = document.createElement('iframe');
                a.height = 1;
                a.width = 1;
                a.style.position = 'absolute';
                a.style.top = 0;
                a.style.left = 0;
                a.style.border = 'none';
                a.style.visibility = 'hidden';
                document.body.appendChild(a);
                if ('loading' !== document.readyState) c();
                else if (window.addEventListener) document.addEventListener('DOMContentLoaded', c);
                else {
                    var e = document.onreadystatechange || function() {};
                    document.onreadystatechange = function(b) {
                        e(b);
                        'loading' !== document.readyState && (document.onreadystatechange = e, c())
                    }
                }
            }
        })();
    </script>
</body>

</html>