add countries to edit shipments
This commit is contained in:
parent
2d3486ba46
commit
022932f8fe
@ -7,21 +7,7 @@ ensure_schema();
|
||||
|
||||
$errors = [];
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'update_status') {
|
||||
$shipmentId = (int) ($_POST['shipment_id'] ?? 0);
|
||||
$status = $_POST['status'] ?? 'posted';
|
||||
|
||||
$allowed = ['posted', 'offered', 'confirmed', 'in_transit', 'delivered'];
|
||||
if ($shipmentId <= 0 || !in_array($status, $allowed, true)) {
|
||||
$errors[] = t('error_invalid');
|
||||
} else {
|
||||
$stmt = db()->prepare("UPDATE shipments SET status = :status WHERE id = :id");
|
||||
$stmt->execute([':status' => $status, ':id' => $shipmentId]);
|
||||
set_flash('success', t('success_status'));
|
||||
header('Location: ' . url_with_lang('admin_dashboard.php'));
|
||||
exit;
|
||||
}
|
||||
}
|
||||
// Removed inline status update logic as per UI cleanup
|
||||
|
||||
$shipments = [];
|
||||
try {
|
||||
@ -142,18 +128,9 @@ render_header(t('admin_dashboard'), 'admin');
|
||||
</td>
|
||||
<td><span class="badge <?= e($row['status']) ?> rounded-pill px-3 py-2"><?= e(status_label($row['status'])) ?></span></td>
|
||||
<td class="text-end">
|
||||
<form class="d-flex gap-2 justify-content-end" method="post">
|
||||
<input type="hidden" name="action" value="update_status">
|
||||
<input type="hidden" name="shipment_id" value="<?= e($row['id']) ?>">
|
||||
<select class="form-select form-select-sm" name="status" style="width: auto;">
|
||||
<option value="posted" <?= $row['status'] === 'posted' ? 'selected' : '' ?>><?= e(t('status_posted')) ?></option>
|
||||
<option value="offered" <?= $row['status'] === 'offered' ? 'selected' : '' ?>><?= e(t('status_offered')) ?></option>
|
||||
<option value="confirmed" <?= $row['status'] === 'confirmed' ? 'selected' : '' ?>><?= e(t('status_confirmed')) ?></option>
|
||||
<option value="in_transit" <?= $row['status'] === 'in_transit' ? 'selected' : '' ?>><?= e(t('status_in_transit')) ?></option>
|
||||
<option value="delivered" <?= $row['status'] === 'delivered' ? 'selected' : '' ?>><?= e(t('status_delivered')) ?></option>
|
||||
</select>
|
||||
<button class="btn btn-sm btn-primary px-3 rounded-pill" type="submit"><i class="bi bi-check2"></i> Save</button>
|
||||
</form>
|
||||
<a href="<?= e(url_with_lang('shipment_detail.php', ['id' => $row['id']])) ?>" class="btn btn-sm btn-outline-primary rounded-pill px-3">
|
||||
View Details
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
|
||||
@ -19,9 +19,44 @@ if (!$shipment) {
|
||||
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'] ?? '');
|
||||
@ -50,9 +85,27 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
]);
|
||||
$flash = "Shipment updated successfully.";
|
||||
|
||||
// Refresh
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -102,13 +155,62 @@ render_header('Edit Shipment', 'admin');
|
||||
<input type="text" name="shipper_company" class="form-control" value="<?= e((string)$shipment['shipper_company']) ?>">
|
||||
</div>
|
||||
|
||||
<!-- Origin Selection -->
|
||||
<div class="col-md-6">
|
||||
<label class="form-label">Origin Country</label>
|
||||
<select class="form-select" id="origin_country">
|
||||
<option value="">Select Country</option>
|
||||
<?php foreach ($countries as $c): ?>
|
||||
<option value="<?= e($c['id']) ?>" <?= $c['id'] == $origin_country_id ? 'selected' : '' ?>>
|
||||
<?= e($c['name_en']) ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<label class="form-label">Origin City</label>
|
||||
<input type="text" name="origin_city" class="form-control" value="<?= e($shipment['origin_city']) ?>" required>
|
||||
<select class="form-select" name="origin_city" id="origin_city" required>
|
||||
<option value="">Select City</option>
|
||||
<!-- If shipment has a city but we couldn't match a country, preserve the value as a fallback option -->
|
||||
<?php if (!$origin_country_id && $shipment['origin_city']): ?>
|
||||
<option value="<?= e($shipment['origin_city']) ?>" selected><?= e($shipment['origin_city']) ?></option>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php foreach ($origin_cities_list as $city): ?>
|
||||
<option value="<?= e($city['name_en']) ?>" <?= $city['name_en'] === $shipment['origin_city'] ? 'selected' : '' ?>>
|
||||
<?= e($city['name_en']) ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<!-- Destination Selection -->
|
||||
<div class="col-md-6">
|
||||
<label class="form-label">Destination Country</label>
|
||||
<select class="form-select" id="destination_country">
|
||||
<option value="">Select Country</option>
|
||||
<?php foreach ($countries as $c): ?>
|
||||
<option value="<?= e($c['id']) ?>" <?= $c['id'] == $destination_country_id ? 'selected' : '' ?>>
|
||||
<?= e($c['name_en']) ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<label class="form-label">Destination City</label>
|
||||
<input type="text" name="destination_city" class="form-control" value="<?= e($shipment['destination_city']) ?>" required>
|
||||
<select class="form-select" name="destination_city" id="destination_city" required>
|
||||
<option value="">Select City</option>
|
||||
<!-- Fallback for unmapped city -->
|
||||
<?php if (!$destination_country_id && $shipment['destination_city']): ?>
|
||||
<option value="<?= e($shipment['destination_city']) ?>" selected><?= e($shipment['destination_city']) ?></option>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php foreach ($destination_cities_list as $city): ?>
|
||||
<option value="<?= e($city['name_en']) ?>" <?= $city['name_en'] === $shipment['destination_city'] ? 'selected' : '' ?>>
|
||||
<?= e($city['name_en']) ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="col-md-12">
|
||||
@ -156,4 +258,44 @@ render_header('Edit Shipment', 'admin');
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php render_footer(); ?>
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
function setupCityLoader(countrySelectId, citySelectId) {
|
||||
const countrySelect = document.getElementById(countrySelectId);
|
||||
const citySelect = document.getElementById(citySelectId);
|
||||
|
||||
countrySelect.addEventListener('change', function() {
|
||||
const countryId = this.value;
|
||||
citySelect.innerHTML = '<option value="">Loading...</option>';
|
||||
citySelect.disabled = true;
|
||||
|
||||
if (countryId) {
|
||||
fetch('api/get_cities.php?country_id=' + countryId)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
citySelect.innerHTML = '<option value="">Select City</option>';
|
||||
data.forEach(city => {
|
||||
const option = document.createElement('option');
|
||||
option.value = city.name_en; // Using name as value for DB compatibility
|
||||
option.textContent = city.name_en;
|
||||
citySelect.appendChild(option);
|
||||
});
|
||||
citySelect.disabled = false;
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error fetching cities:', error);
|
||||
citySelect.innerHTML = '<option value="">Error loading cities</option>';
|
||||
});
|
||||
} else {
|
||||
citySelect.innerHTML = '<option value="">Select City</option>';
|
||||
citySelect.disabled = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
setupCityLoader('origin_country', 'origin_city');
|
||||
setupCityLoader('destination_country', 'destination_city');
|
||||
});
|
||||
</script>
|
||||
|
||||
<?php render_footer(); ?>
|
||||
Loading…
x
Reference in New Issue
Block a user