<?php include 'includes/header.php'; ?>
<?php include 'includes/sidebar.php'; ?>
<?php require 'includes/db_connect.php'; ?>

<div class="container mt-4">
    <h4 class="text-orange">Track Mail With Tracking Number</h4>

    <form method="post" class="row g-3 mb-4">
        <div class="col-md-6">
            <input type="text" name="tracking_no" class="form-control" placeholder="Enter Tracking Number" required>
        </div>
        <div class="col-md-2">
            <button type="submit" name="track" class="btn btn-warning text-white">Track</button>
        </div>
    </form>

    <?php
    if (isset($_POST['track'])) {
        $tracking_no = trim($_POST['tracking_no']);
        $stmt = $conn->prepare("SELECT * FROM mails WHERE tracking_no = ?");
        $stmt->bind_param("s", $tracking_no);
        $stmt->execute();
        $result = $stmt->get_result();

        if ($result->num_rows > 0) {
            $mail = $result->fetch_assoc();
            ?>
            <div class="card mb-4">
                <div class="card-header bg-orange text-white">
                    <strong>Mail Details</strong>
                </div>
                <div class="card-body">
                    <p><strong>Subject:</strong> <?= htmlspecialchars($mail['subject']); ?></p>
                    <p><strong>Summary:</strong> <?= htmlspecialchars($mail['summary']); ?></p>
                    <p><strong>Type:</strong> <?= htmlspecialchars($mail['mail_type']); ?></p>
                    <p><strong>Status:</strong> <span class="badge bg-warning text-dark"><?= htmlspecialchars($mail['status']); ?></span></p>
                    <p><strong>Last Department:</strong> <?= htmlspecialchars($mail['department']); ?></p>
                    <p><strong>Date Minuted:</strong> <?= htmlspecialchars($mail['date_minuted']); ?></p>
                    <p><strong>Registered Date:</strong> <?= htmlspecialchars($mail['reg_date']); ?></p>
                </div>
            </div>

            <h5 class="text-orange">Movement History</h5>
            <table class="table table-bordered">
                <thead class="bg-orange text-white">
                    <tr>
                        <th>Date</th>
                        <th>Moved By</th>
                        <th>Moved To</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $history = $conn->prepare("SELECT h.*, s.name as staff_id FROM mail_history h 
                        LEFT JOIN staff s ON h.staff_id = s.id WHERE h.mail_id = ?");
                    $history->bind_param("i", $mail['id']);
                    $history->execute();
                    $history_result = $history->get_result();

                    if ($history_result->num_rows > 0) {
                        while ($row = $history_result->fetch_assoc()) {
                            echo "<tr>
                                <td>{$row['action_date']}</td>
                                <td>{$row['staff_id']}</td>
                                <td>{$row['moved_to']}</td>
                                <td>{$row['action']}</td>
                            </tr>";
                        }
                    } else {
                        echo "<tr><td colspan='4' class='text-center'>No History Found.</td></tr>";
                    }
                    ?>
                </tbody>
            </table>

            <?php
        } else {
            echo "<div class='alert alert-danger'>No mail found with Tracking Number: <strong>$tracking_no</strong></div>";
        }
    }
    ?>
</div>

<?php include 'includes/footer.php'; ?>
