false, 'message' => 'Invalid ID']);
exit;
}
header('Location: admin_shippers.php');
exit;
}
$errors = [];
$flash = null;
// Fetch Shipper Profile
$stmt = db()->prepare("
SELECT u.id, u.email, u.full_name, u.status, u.role,
p.company_name, p.phone, p.address_line, p.country_id, p.city_id
FROM users u
LEFT JOIN shipper_profiles p ON u.id = p.user_id
WHERE u.id = ? AND u.role = 'shipper'
");
$stmt->execute([$userId]);
$shipper = $stmt->fetch();
if (!$shipper) {
if ($isAjax) {
echo json_encode(['success' => false, 'message' => 'Shipper not found']);
exit;
}
header('Location: admin_shippers.php');
exit;
}
$countries = db()->query("SELECT id, name_en, name_ar FROM countries ORDER BY name_en ASC")->fetchAll();
$cities = db()->query("SELECT id, country_id, name_en, name_ar FROM cities ORDER BY name_en ASC")->fetchAll();
if ($_SERVER['REQUEST_METHOD'] === 'POST') { validate_csrf_token();
$fullName = trim($_POST['full_name'] ?? '');
$email = trim($_POST['email'] ?? '');
$phone = trim($_POST['phone'] ?? '');
$countryId = (int)($_POST['country_id'] ?? 0);
$cityId = (int)($_POST['city_id'] ?? 0);
$addressLine = trim($_POST['address_line'] ?? '');
$companyName = trim($_POST['company_name'] ?? '');
$status = trim($_POST['status'] ?? '');
$password = $_POST['password'] ?? '';
if ($fullName === '') $errors[] = 'Full name is required.';
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) $errors[] = 'Valid email is required.';
if ($phone === '') $errors[] = 'Phone number is required.';
if ($companyName === '') $errors[] = 'Company name is required.';
if (!in_array($status, ['pending', 'active', 'rejected'], true)) $errors[] = 'Invalid status.';
if ($countryId <= 0 || $cityId <= 0) {
$errors[] = 'Please select country and city.';
} else {
$cityCheck = db()->prepare("SELECT COUNT(*) FROM cities WHERE id = ? AND country_id = ?");
$cityCheck->execute([$cityId, $countryId]);
if ((int)$cityCheck->fetchColumn() === 0) {
$errors[] = 'Selected city does not belong to selected country.';
}
}
if (!$errors) {
try {
db()->beginTransaction();
$stmtUser = db()->prepare("UPDATE users SET full_name = ?, email = ?, status = ? WHERE id = ? AND role = 'shipper'");
$stmtUser->execute([$fullName, $email, $status, $userId]);
if ($password !== '') {
$stmtPass = db()->prepare("UPDATE users SET password = ? WHERE id = ? AND role = 'shipper'");
$stmtPass->execute([password_hash($password, PASSWORD_DEFAULT), $userId]);
}
$stmtProfile = db()->prepare("
UPDATE shipper_profiles
SET company_name = ?, phone = ?, address_line = ?, country_id = ?, city_id = ?
WHERE user_id = ?
");
$stmtProfile->execute([$companyName, $phone, $addressLine, $countryId, $cityId, $userId]);
db()->commit();
$flash = 'Shipper profile updated successfully.';
if ($isAjax) {
header('Content-Type: application/json');
echo json_encode(['success' => true, 'message' => $flash]);
exit;
}
// Refresh data
$shipper['full_name'] = $fullName;
$shipper['email'] = $email;
$shipper['status'] = $status;
$shipper['company_name'] = $companyName;
$shipper['phone'] = $phone;
$shipper['address_line'] = $addressLine;
$shipper['country_id'] = $countryId;
$shipper['city_id'] = $cityId;
} catch (Throwable $e) {
db()->rollBack();
if (stripos($e->getMessage(), 'Duplicate entry') !== false) {
$errors[] = 'This email is already in use by another account.';
} else {
$errors[] = 'Failed to update shipper profile. Please try again.';
}
}
}
if ($isAjax && $errors) {
header('Content-Type: application/json');
echo json_encode(['success' => false, 'message' => implode('
', $errors)]);
exit;
}
}
// -- OUTPUT START --
if (!$isAjax):
render_header('Edit Shipper', 'admin', true);
?>