false, 'message' => 'Invalid ID']);
exit;
}
header('Location: admin_shipments.php'); exit;
}
$errors = [];
$flash = null;
$stmt = db()->prepare("SELECT * FROM shipments WHERE id = ?");
$stmt->execute([$id]);
$shipment = $stmt->fetch();
if (!$shipment) {
if ($isAjax) {
echo json_encode(['success' => false, 'message' => 'Shipment not found']);
exit;
}
header('Location: admin_shipments.php'); exit;
}
// Fetch all countries for dropdowns
$countries = [];
try {
$countries = db()->query("SELECT * FROM countries ORDER BY name_en ASC")->fetchAll();
} catch (Throwable $e) {}
// Helper to find country_id by city name
function find_country_id_by_city_name($cityName) {
if (!$cityName) return null;
$stmt = db()->prepare("SELECT country_id FROM cities WHERE name_en = ? LIMIT 1");
$stmt->execute([$cityName]);
return $stmt->fetchColumn() ?: null;
}
// Pre-fetch country IDs for existing cities
$origin_country_id = find_country_id_by_city_name($shipment['origin_city']);
$destination_country_id = find_country_id_by_city_name($shipment['destination_city']);
// Fetch initial city lists for the pre-selected countries
$origin_cities_list = [];
if ($origin_country_id) {
$stmt = db()->prepare("SELECT * FROM cities WHERE country_id = ? ORDER BY name_en ASC");
$stmt->execute([$origin_country_id]);
$origin_cities_list = $stmt->fetchAll();
}
$destination_cities_list = [];
if ($destination_country_id) {
$stmt = db()->prepare("SELECT * FROM cities WHERE country_id = ? ORDER BY name_en ASC");
$stmt->execute([$destination_country_id]);
$destination_cities_list = $stmt->fetchAll();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$shipper_name = trim($_POST['shipper_name'] ?? '');
$shipper_company = trim($_POST['shipper_company'] ?? '');
// These are now selected from dropdowns, but the value is still the city name
$origin_city = trim($_POST['origin_city'] ?? '');
$destination_city = trim($_POST['destination_city'] ?? '');
$cargo_description = trim($_POST['cargo_description'] ?? '');
$weight_tons = (float)($_POST['weight_tons'] ?? 0);
$pickup_date = trim($_POST['pickup_date'] ?? '');
$delivery_date = trim($_POST['delivery_date'] ?? '');
$payment_method = trim($_POST['payment_method'] ?? 'thawani');
$status = trim($_POST['status'] ?? 'posted');
if ($shipper_name === '') $errors[] = "Shipper name is required.";
if ($origin_city === '') $errors[] = "Origin city is required.";
if ($destination_city === '') $errors[] = "Destination city is required.";
if (empty($errors)) {
$updateSql = "
UPDATE shipments
SET shipper_name = ?, shipper_company = ?, origin_city = ?, destination_city = ?,
cargo_description = ?, weight_tons = ?, pickup_date = ?, delivery_date = ?,
payment_method = ?, status = ?
WHERE id = ?
";
db()->prepare($updateSql)->execute([
$shipper_name, $shipper_company, $origin_city, $destination_city,
$cargo_description, $weight_tons, $pickup_date, $delivery_date,
$payment_method, $status, $id
]);
$flash = "Shipment updated successfully.";
if ($isAjax) {
header('Content-Type: application/json');
echo json_encode(['success' => true, 'message' => $flash]);
exit;
}
// Refresh data
$stmt->execute([$id]);
$shipment = $stmt->fetch();
// Re-fetch derived data for the form
$origin_country_id = find_country_id_by_city_name($shipment['origin_city']);
$destination_country_id = find_country_id_by_city_name($shipment['destination_city']);
$origin_cities_list = [];
if ($origin_country_id) {
$stmt = db()->prepare("SELECT * FROM cities WHERE country_id = ? ORDER BY name_en ASC");
$stmt->execute([$origin_country_id]);
$origin_cities_list = $stmt->fetchAll();
}
$destination_cities_list = [];
if ($destination_country_id) {
$stmt = db()->prepare("SELECT * FROM cities WHERE country_id = ? ORDER BY name_en ASC");
$stmt->execute([$destination_country_id]);
$destination_cities_list = $stmt->fetchAll();
}
}
if ($isAjax && $errors) {
header('Content-Type: application/json');
echo json_encode(['success' => false, 'message' => implode('
', $errors)]);
exit;
}
}
// -- OUTPUT START --
if (!$isAjax):
render_header('Edit Shipment', 'admin', true);
?>