<?php
// ====================== track/mf.php ======================
require_once('dbconnection.php');

$id = 0;
if(isset($_GET['id'])) {
    $id = (int)$_GET['id'];
}

if($id <= 0) {
    die("<h2>Invalid Request</h2>");
}

$query = "SELECT * FROM tracking WHERE id = $id";
$result = mysqli_query($dbcon, $query);

if(mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);
} else {
    die("<h2>Tracking record not found!</h2>");
}

// ====================== UPDATE FORM SUBMITTED ======================
if(isset($_POST['update_status'])) {
    $status      = mysqli_real_escape_string($dbcon, $_POST['status']);
    $clocation   = mysqli_real_escape_string($dbcon, $_POST['clocation']);
    $cost        = mysqli_real_escape_string($dbcon, $_POST['cost']);
    $pickdate    = mysqli_real_escape_string($dbcon, $_POST['pickdate']);
    $deliverydate= mysqli_real_escape_string($dbcon, $_POST['deliverydate']);
    $move        = (int)$_POST['move'];
    $msg         = mysqli_real_escape_string($dbcon, $_POST['msg']);

    $day  = date("l");
    $date = date("jS \of F Y");
    $time = date("h:i:s A");

    // Insert into move history
    $q1 = "INSERT INTO move (userid, day, date, time, trackingno, status, clocation) 
           VALUES(0, '$day', '$date', '$time', '".$row['trackingno']."', '$status', '$clocation')";
    mysqli_query($dbcon, $q1);

    // Update main tracking record
    $q2 = "UPDATE tracking SET 
           status = '$status',
           clocation = '$clocation',
           cost = '$cost',
           pickdate = '$pickdate',
           deliverydate = '$deliverydate',
           move = '$move',
           msg = '$msg'
           WHERE id = $id";

    if(mysqli_query($dbcon, $q2)) {
        echo "<script>alert('Tracking Updated Successfully!'); window.location='mf.php?id=$id';</script>";
        exit();
    }
}
?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
    <meta charset="utf-8">
    <title>Tracking Details - <?php echo htmlspecialchars($row['trackingno']); ?></title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="stylesheets/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="stylesheets/style.css">
    
    <style>
        body {font-family: Arial, sans-serif; padding: 20px; background: #f4f4f4;}
        .box {background: white; padding: 25px; border: 1px solid #ddd; border-radius: 8px; margin-bottom: 20px;}
        table {width: 100%; border-collapse: collapse; margin: 10px 0;}
        th, td {padding: 10px; border: 1px solid #ccc; vertical-align: top;}
        th {background: #0066cc; color: white;}
        input, textarea {width: 100%; padding: 8px; margin: 5px 0;}
    </style>
</head>
<body>

<div class="box">
    <h2>Tracking Details - <?php echo htmlspecialchars($row['trackingno']); ?></h2>

    <table>
        <tr><th colspan="2">Sender Information</th></tr>
        <tr><td><strong>Sender</strong></td><td><?php echo htmlspecialchars($row['sender']); ?></td></tr>
        <tr><td><strong>Sender Email</strong></td><td><?php echo htmlspecialchars($row['semail']); ?></td></tr>
        <tr><td><strong>Sender Phone</strong></td><td><?php echo htmlspecialchars($row['senderphone']); ?></td></tr>
    </table>

    <table>
        <tr><th colspan="2">Receiver Information</th></tr>
        <tr><td><strong>Receiver</strong></td><td><?php echo htmlspecialchars($row['receiver']); ?></td></tr>
        <tr><td><strong>Receiver Email</strong></td><td><?php echo htmlspecialchars($row['remail']); ?></td></tr>
        <tr><td><strong>Receiver Address</strong></td><td><?php echo nl2br(htmlspecialchars($row['address'])); ?></td></tr>
        <tr><td><strong>Receiver Phone</strong></td><td><?php echo htmlspecialchars($row['recphone']); ?></td></tr>
    </table>

    <table>
        <tr><th colspan="2">Shipment Details</th></tr>
        <tr><td><strong>Package Type</strong></td><td><?php echo htmlspecialchars($row['packagetype']); ?></td></tr>
        <tr><td><strong>Weight</strong></td><td><?php echo htmlspecialchars($row['weight']); ?></td></tr>
        <tr><td><strong>Cost</strong></td><td><?php echo htmlspecialchars($row['cost']); ?></td></tr>
        <tr><td><strong>Transit Type</strong></td><td><?php echo htmlspecialchars($row['transtype']); ?></td></tr>
    </table>

    <table>
        <tr><th colspan="2">Current Status</th></tr>
        <tr><td><strong>Status</strong></td><td><?php echo htmlspecialchars($row['status']); ?></td></tr>
        <tr><td><strong>Current Location</strong></td><td><?php echo htmlspecialchars($row['clocation']); ?></td></tr>
        <tr><td><strong>Dispatch Date</strong></td><td><?php echo htmlspecialchars($row['dispdate']); ?></td></tr>
        <tr><td><strong>Pickup Date</strong></td><td><?php echo htmlspecialchars($row['pickdate']); ?></td></tr>
    </table>
</div>

<!-- ====================== FULL UPDATE FORM ====================== -->
<div class="box">
    <h3>Update Tracking Information</h3>
    <form method="post" action="">
        <strong>Status:</strong><br>
        <input type="text" name="status" value="<?php echo htmlspecialchars($row['status']); ?>"><br><br>

        <strong>Current Location:</strong><br>
        <input type="text" name="clocation" value="<?php echo htmlspecialchars($row['clocation']); ?>"><br><br>

        <strong>Cost:</strong><br>
        <input type="text" name="cost" value="<?php echo htmlspecialchars($row['cost']); ?>"><br><br>

        <strong>Pickup Date:</strong><br>
        <input type="date" name="pickdate" value="<?php echo htmlspecialchars($row['pickdate']); ?>"><br><br>

        <strong>Delivery Date:</strong><br>
        <input type="date" name="deliverydate" value="<?php echo htmlspecialchars($row['deliverydate']); ?>"><br><br>

        <strong>Move Stage:</strong><br>
        <input type="number" name="move" value="<?php echo (int)$row['move']; ?>"><br><br>

        <strong>Message / Note:</strong><br>
        <textarea name="msg" rows="5"><?php echo htmlspecialchars($row['msg']); ?></textarea><br><br>

        <input type="submit" name="update_status" value="UPDATE TRACKING" style="padding:12px 25px; background:#0066cc; color:white; font-size:16px;">
    </form>
</div>

<br>
<a href="admin.php" style="font-size:18px; color:blue;">← Back to Admin Panel</a>

</body>
</html>