<?php 
require_once("header.php");

$id = $_GET['id'] ?? null;
$success = "";

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
    try {
        $pdo->beginTransaction();

        // Prepare update statement for `packages`
        $updatePackage = $pdo->prepare("
            UPDATE packages SET  
                pack = 0,
                delivery_status = :delivery_status,
                consignment_status = :consignment_status,
                billing_name = :billing_name,
                billing_cost = :billing_cost,
                current_location = :current_location,
                final_destination = :final_destination,
                consignment_quantity = :consignment_quantity,
                arrival_date = :arrival_date,
                departure_date = :departure_date,
                arrival_time = :arrival_time,
                parcel_name = :parcel_name,
                parcel_details = :parcel_details,
                tracking_code = :tracking_code,
                recv_name = :recv_name,
                recv_email = :recv_email,
                recv_address = :recv_address,
                recv_phone = :recv_phone,
                recv_location = :recv_location,
                sdr_name = :sdr_name,
                sdr_address = :sdr_address,
                sdr_phone = :sdr_phone,
                sdr_location = :sdr_location
            WHERE id = :id
        ");

        $updatePackage->execute([
            ':delivery_status' => $_POST['delivery_status'],
            ':consignment_status' => $_POST['consignment_status'],
            ':billing_name' => $_POST['billing_name'],
            ':billing_cost' => $_POST['billing_cost'],
            ':current_location' => $_POST['current_location'],
            ':final_destination' => $_POST['final_destination'],
            ':consignment_quantity' => $_POST['consignment_quantity'],
            ':arrival_date' => $_POST['arrival_date'],
            ':departure_date' => $_POST['departure_date'],
            ':arrival_time' => $_POST['arrival_time'],
            ':parcel_name' => $_POST['parcel_name'],
            ':parcel_details' => $_POST['parcel_details'],
            ':tracking_code' => $_POST['tracking_code'],
            ':recv_name' => $_POST['recv_name'],
            ':recv_email' => $_POST['recv_email'],
            ':recv_address' => $_POST['recv_address'],
            ':recv_phone' => $_POST['recv_phone'],
            ':recv_location' => $_POST['recv_location'],
            ':sdr_name' => $_POST['sdr_name'],
            ':sdr_address' => $_POST['sdr_address'],
            ':sdr_phone' => $_POST['sdr_phone'],
            ':sdr_location' => $_POST['sdr_location'],
            ':id' => $id
        ]);

        // Mark previous location as not active
        $pdo->prepare("UPDATE locations SET green = 0 WHERE trackid = :id")
            ->execute([':id' => $id]);

        // Insert new location
        $insertLocation = $pdo->prepare("
    INSERT INTO locations (
        trackid, comp_name, comp_location, comp_phone, ship_cost, sub_cost, total_cost,
        delivery_status, consignment_status, billing_name, billing_cost,
        current_location, current_address, final_destination, consignment_quantity,
        arrival_date, arrival_time, departure_date, parcel_name, parcel_details,
        tracking_code, recv_name, recv_email, recv_address,
        recv_phone, recv_location, sdr_name, sdr_address, sdr_phone, sdr_location, green, date
    ) VALUES (
        :trackid, :comp_name, :comp_location, :comp_phone, :ship_cost, :sub_cost, :total_cost,
        :delivery_status, :consignment_status, :billing_name, :billing_cost,
        :current_location, :current_address, :final_destination, :consignment_quantity,
        :arrival_date, :arrival_time, :departure_date, :parcel_name, :parcel_details,
        :tracking_code, :recv_name, :recv_email, :recv_address,
        :recv_phone, :recv_location, :sdr_name, :sdr_address, :sdr_phone, :sdr_location, 1, :date
    )
");

$insertLocation->execute([
    ':trackid' => $_POST['trackid'],
    ':comp_name' => $_POST['comp_name'],
    ':comp_location' => $_POST['comp_location'],
    ':comp_phone' => $_POST['comp_phone'],
    ':ship_cost' => $_POST['ship_cost'],
    ':sub_cost' => $_POST['sub_cost'],
    ':total_cost' => $_POST['total_cost'],
    ':delivery_status' => $_POST['delivery_status'],
    ':consignment_status' => $_POST['consignment_status'],
    ':billing_name' => $_POST['billing_name'],
    ':billing_cost' => $_POST['billing_cost'],
    ':current_location' => $_POST['current_location'],
    ':current_address' => $_POST['current_address'],
    ':final_destination' => $_POST['final_destination'],
    ':consignment_quantity' => $_POST['consignment_quantity'],
    ':arrival_date' => $_POST['arrival_date'],
    ':arrival_time' => $_POST['arrival_time'],
    ':departure_date' => $_POST['departure_date'],
    ':parcel_name' => $_POST['parcel_name'],
    ':parcel_details' => $_POST['parcel_details'],
    ':tracking_code' => $_POST['tracking_code'],
    ':recv_name' => $_POST['recv_name'],
    ':recv_email' => $_POST['recv_email'],
    ':recv_address' => $_POST['recv_address'],
    ':recv_phone' => $_POST['recv_phone'],
    ':recv_location' => $_POST['recv_location'],
    ':sdr_name' => $_POST['sdr_name'],
    ':sdr_address' => $_POST['sdr_address'],
    ':sdr_phone' => $_POST['sdr_phone'],
    ':sdr_location' => $_POST['sdr_location'],
    ':date' => date('Y-m-d H:i:s') // this is the missing field
]);


        $pdo->commit();
        $success = "<div class='alert alert-success'>Parcel location updated successfully. <meta http-equiv='refresh' content='5'></div>";

    } catch (PDOException $e) {
        $pdo->rollBack();
        $success = "<div class='alert alert-danger'>Error: {$e->getMessage()}</div>";
    }
}

if (!empty($success)) echo $success;
?>

    
    
  
    
  
 
   

        <div wire:id="0ho7JEz65LVMGIzD4ceZ" wire:initial-data="{&quot;fingerprint&quot;:{&quot;id&quot;:&quot;0ho7JEz65LVMGIzD4ceZ&quot;,&quot;name&quot;:&quot;admin.manage-users&quot;,&quot;locale&quot;:&quot;en&quot;,&quot;path&quot;:&quot;admin\/dashboard\/manageusers&quot;,&quot;method&quot;:&quot;GET&quot;,&quot;v&quot;:&quot;acj&quot;},&quot;effects&quot;:{&quot;listeners&quot;:[],&quot;path&quot;:&quot;https:\/\/xtbtrading.live\/trade\/admin\/dashboard\/manageusers&quot;},&quot;serverMemo&quot;:{&quot;children&quot;:[],&quot;errors&quot;:[],&quot;htmlHash&quot;:&quot;ed570425&quot;,&quot;data&quot;:{&quot;pagenum&quot;:10,&quot;searchvalue&quot;:&quot;&quot;,&quot;orderby&quot;:&quot;id&quot;,&quot;orderdirection&quot;:&quot;desc&quot;,&quot;selectPage&quot;:false,&quot;selectAll&quot;:false,&quot;checkrecord&quot;:[],&quot;selected&quot;:&quot;&quot;,&quot;action&quot;:&quot;Delete&quot;,&quot;username&quot;:null,&quot;fullname&quot;:null,&quot;email&quot;:null,&quot;password&quot;:null,&quot;message&quot;:null,&quot;subject&quot;:null,&quot;plan&quot;:null,&quot;datecreated&quot;:null,&quot;topamount&quot;:null,&quot;toptype&quot;:null,&quot;topcolumn&quot;:&quot;Bonus&quot;,&quot;page&quot;:1,&quot;paginators&quot;:{&quot;page&quot;:1}},&quot;dataMeta&quot;:[],&quot;checksum&quot;:&quot;9e82a6adabccc53607a584c57da8f89b4130352eb62c7aa9cc50f9455ca3c494&quot;}}">
    <div class="main-panel">
        <div class="content bg-light">
            <div class="page-inner">
                <div class="mt-2 mb-4">
                    <h1 class="title1 text-dark">Edit Location</h1>
                </div>
                <div>
    </div>                <div>
    </div>                
               
               
               
               
                <div class="mb-5 row">
                    <div class="col-md-12 ">
                        <!--<div class="card shadow p-4 bg-light">-->
                           
                           
                           
                           
                           
                            <div class="card-header">
                                <div class="row">
                                    
                                    
                                    <div class="col-6">
                                             
                                             
                                             
                                              <div class="card-body">
                                <div class="" data-example-id="hoverable-table">
                                    <table class="">
                                        <thead>
                                            <tr>
                                               
                                               
                                                <th><a class="btn btn-primary btn-sm float-right" type="button" href="list_parcel.php">
                                                    <span class="fa fa-gift-plus" data-fa-transform="shrink-3 down-2">&nbsp;Parcel List</span>
                                                   
                                                </a></th>
                                               
                                               
                                              
                                               
                                            </tr>
                                        </thead>
                                        </table>
                                        </div>
                                        </div>
                                        
                                       
                                                                            </div>
                                </div>
                            </div>
                            
                            
                            
 
 
           <center>
    <legend>CHANGE PACKAGE LOCATION</legend>
    </center>
        	<?php
                        if(isset($error4)){
                            echo $error4;
                        }elseif(isset($error2)){
                            echo $error2;
                        }elseif(isset($error)){
                            echo $error;
                        }elseif(isset($sucess)){
                            echo $sucess;
                        }
                        ?>   
          <br>
          
         
         
         
         
         <!--=======================================================-->
 <?php

// === Fetch data to prefill the form ===
$stmt = $pdo->prepare("SELECT * FROM packages WHERE id = :id");
$stmt->execute([':id' => $id]);
$ordrow = $stmt->fetch();

if (!$ordrow) {
    die("Package not found.");
}

if (!empty($success)) echo $success;
?>

<form method="POST" enctype="multipart/form-data">
    <input type="hidden" name="action" value="update_location">
    <input type="hidden" name="trackid" value="<?= htmlspecialchars($ordrow['id']) ?>">

    <div class="form-group">
        <label for="delivery_status" style="color:black !important;">Delivery Status</label>
        <select name="delivery_status" class="form-control">
            <option selected value="<?= htmlspecialchars($ordrow['delivery_status']) ?>">
                <?= htmlspecialchars($ordrow['delivery_status']) ?>
            </option>
            <option value="Ready">Ready</option>
            <option value="Finished">Finished</option>
            <option value="In-Transit">In-Transit</option>
            <option value="On-Hold">On-Hold</option>
            <option value="Landed">Landed</option>
            <option value="Delayed">Delayed</option>
            <option value="Custom Clearance">Custom Clearance</option>
            <option value="On Route">On Route</option>
            <option value="Delivered">Delivered</option>
            <option value="Custom Carrier">Custom Carrier</option>
            <option value="Clearance">Clearance</option>
            <option value="Permit">Permit</option>
        </select>
    </div>

    <div class="form-group">
        <label for="consignment_status" style="color:black !important;">Consignment Status</label>
        <input type="text" name="consignment_status" class="form-control"
            value="<?= htmlspecialchars($ordrow['consignment_status']) ?>">
    </div>

    <div class="form-group">
        <label for="billing_name" style="color:black !important;">Billing Name</label>
        <input type="text" name="billing_name" class="form-control"
            value="<?= htmlspecialchars($ordrow['billing_name']) ?>">
    </div>

    <div class="form-group">
        <label for="billing_cost" style="color:black !important;">Billing Cost</label>
        <input type="text" name="billing_cost" class="form-control"
            value="<?= htmlspecialchars($ordrow['billing_cost']) ?>">
    </div>

    <div class="form-group">
        <label for="current_address" style="color:black !important;">Current Address</label>
        <input type="text" name="current_address" class="form-control"
            value="<?= htmlspecialchars($ordrow['sdr_address']) ?>">
    </div>

    <div class="form-group">
    <label for="current_location" style="color:black !important;">Current Location</label>
    <select name="current_location" class="form-control">
        <?php
        $countries = [
            "Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Antigua and Barbuda", "Argentina", "Armenia",
            "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",
            "Belize", "Benin", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei",
            "Bulgaria", "Burkina Faso", "Burundi", "Cabo Verde", "Cambodia", "Cameroon", "Canada", "Central African Republic",
            "Chad", "Chile", "China", "Colombia", "Comoros", "Congo (Congo-Brazzaville)", "Costa Rica", "Croatia", "Cuba",
            "Cyprus", "Czech Republic", "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic",
            "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Eswatini (fmr. Swaziland)", "Ethiopia",
            "Fiji", "Finland", "France", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Greece", "Grenada", "Guatemala",
            "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras", "Hungary", "Iceland", "India", "Indonesia", "Iran",
            "Iraq", "Ireland", "Israel", "Italy", "Ivory Coast", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati",
            "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania",
            "Luxembourg", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Mauritania",
            "Mauritius", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", "Montenegro", "Morocco", "Mozambique",
            "Myanmar (formerly Burma)", "Namibia", "Nauru", "Nepal", "Netherlands", "New Zealand", "Nicaragua", "Niger",
            "Nigeria", "North Korea", "North Macedonia", "Norway", "Oman", "Pakistan", "Palau", "Palestine State", "Panama",
            "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Qatar", "Romania", "Russia", "Rwanda",
            "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines", "Samoa", "San Marino",
            "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore",
            "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Korea", "South Sudan", "Spain",
            "Sri Lanka", "Sudan", "Suriname", "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand",
            "Timor-Leste", "Togo", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Tuvalu", "Uganda",
            "Ukraine", "United Arab Emirates", "United Kingdom", "United States of America", "Uruguay", "Uzbekistan", "Vanuatu",
            "Vatican City", "Venezuela", "Vietnam", "Yemen", "Zambia", "Zimbabwe"
        ];

        $selectedCountry = htmlspecialchars($ordrow['current_location']);
        foreach ($countries as $country) {
            $selected = ($country === $selectedCountry) ? 'selected' : '';
            echo "<option value=\"$country\" $selected>$country</option>";
        }
        ?>
    </select>
</div>

    <!-- Hidden fields for other values -->
    <?php
    $fields = [
        'comp_name', 'comp_location', 'comp_phone', 'ship_cost',
        'sub_cost', 'total_cost', 'final_destination', 'consignment_quantity',
        'departure_date', 'arrival_date', 'parcel_name', 'parcel_details',
        'tracking_code', 'recv_name', 'recv_email', 'recv_address',
        'recv_phone', 'recv_location', 'sdr_name', 'sdr_address',
        'sdr_phone', 'sdr_location'
    ];

    foreach ($fields as $field) {
        echo "<input type='hidden' name='{$field}' value='" . htmlspecialchars($ordrow[$field]) . "'>\n";
    }
    ?>

    <div class="form-group">
        <label for="arrival_time" style="color:black !important;">Current Time</label>
        <input type="text" name="arrival_time" class="form-control" value="<?= date('g:iA') ?>">
    </div>

    <button type="submit" class="btn btn-success">Update Location</button>
</form>

            <hr>
            
            
  
            
        </div></div></div>
<!--=====================================-->

</div>

 <?php
require ("footer.php")
?>