@@ -283,7 +291,9 @@ document.addEventListener('DOMContentLoaded', function() {
form.addEventListener('submit', function(e) {
e.preventDefault();
const submitBtn = form.querySelector('button[type="submit"]');
+ let originalBtnText = '';
if(submitBtn) {
+ originalBtnText = submitBtn.innerHTML;
submitBtn.disabled = true;
submitBtn.innerHTML = '
= e(t('loading')) ?>';
}
@@ -297,7 +307,7 @@ document.addEventListener('DOMContentLoaded', function() {
} else {
if(submitBtn) {
submitBtn.disabled = false;
- submitBtn.textContent = '= e(t('save_changes')) ?>';
+ submitBtn.innerHTML = originalBtnText || '= e(t('save_changes')) ?>';
}
const errDiv = form.querySelector('#form-errors');
if(errDiv) {
@@ -312,7 +322,7 @@ document.addEventListener('DOMContentLoaded', function() {
console.error(err);
if(submitBtn) {
submitBtn.disabled = false;
- submitBtn.textContent = '= e(t('save_changes')) ?>';
+ submitBtn.innerHTML = originalBtnText || '= e(t('save_changes')) ?>';
}
alert('An error occurred while saving.');
});
diff --git a/admin_truck_owner_edit.php b/admin_truck_owner_edit.php
index d24c173..8b6deab 100644
--- a/admin_truck_owner_edit.php
+++ b/admin_truck_owner_edit.php
@@ -8,6 +8,7 @@ $isAjax = isset($_GET['ajax']) && $_GET['ajax'] === '1';
if ($userId <= 0) {
if ($isAjax) {
+ header('Content-Type: application/json');
echo json_encode(['success' => false, 'message' => 'Invalid ID']);
exit;
}
@@ -23,7 +24,8 @@ $stmt = db()->prepare("
SELECT u.id, u.email, u.full_name, u.status, u.role,
p.phone, p.address_line, p.country_id, p.city_id,
p.bank_account, p.bank_name, p.bank_branch,
- p.id_card_path, p.is_company, p.ctr_number, p.notes
+ p.id_card_path, p.truck_pic_path, p.registration_path,
+ p.is_company, p.ctr_number, p.notes
FROM users u
LEFT JOIN truck_owner_profiles p ON u.id = p.user_id
WHERE u.id = ? AND u.role = 'truck_owner'
@@ -37,6 +39,7 @@ $ownerTrucks = $trucks->fetchAll();
if (!$owner) {
if ($isAjax) {
+ header('Content-Type: application/json');
echo json_encode(['success' => false, 'message' => 'Owner not found']);
exit;
}
@@ -52,10 +55,66 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { validate_csrf_token();
$truckId = (int)$_POST['truck_id'];
db()->prepare("UPDATE trucks SET is_approved = 1 WHERE id = ? AND user_id = ?")->execute([$truckId, $userId]);
$flash = 'Truck approved successfully.';
+ if ($isAjax) { echo json_encode(['success' => true, 'message' => $flash]); exit; }
} elseif (isset($_POST['reject_truck'])) {
$truckId = (int)$_POST['truck_id'];
db()->prepare("UPDATE trucks SET is_approved = 0 WHERE id = ? AND user_id = ?")->execute([$truckId, $userId]);
$flash = 'Truck status set to unapproved.';
+ if ($isAjax) { echo json_encode(['success' => true, 'message' => $flash]); exit; }
+ } elseif (isset($_POST['add_truck'])) {
+ $truckType = trim($_POST['truck_type'] ?? '');
+ $loadCapacity = (float)($_POST['load_capacity'] ?? 0);
+ $plateNo = trim($_POST['plate_no'] ?? '');
+ $regExpiry = $_POST['registration_expiry_date'] ?? null;
+ $insExpiry = $_POST['insurance_expiry_date'] ?? null;
+
+ if ($truckType === '') $errors[] = 'Truck type is required.';
+ if ($loadCapacity <= 0) $errors[] = 'Valid load capacity is required.';
+ if ($plateNo === '') $errors[] = 'Plate number is required.';
+
+ $truckPicPath = null;
+ $regPath = null;
+
+ if (empty($errors)) {
+ // Handle File Uploads
+ $uploadDir = 'uploads/trucks/';
+ if (!is_dir($uploadDir)) mkdir($uploadDir, 0777, true);
+
+ if (isset($_FILES['truck_pic']) && $_FILES['truck_pic']['error'] === UPLOAD_ERR_OK) {
+ $ext = pathinfo($_FILES['truck_pic']['name'], PATHINFO_EXTENSION);
+ $filename = 'truck_' . uniqid() . '.' . $ext;
+ if (move_uploaded_file($_FILES['truck_pic']['tmp_name'], $uploadDir . $filename)) {
+ $truckPicPath = $uploadDir . $filename;
+ }
+ }
+ if (isset($_FILES['registration_doc']) && $_FILES['registration_doc']['error'] === UPLOAD_ERR_OK) {
+ $ext = pathinfo($_FILES['registration_doc']['name'], PATHINFO_EXTENSION);
+ $filename = 'reg_' . uniqid() . '.' . $ext;
+ if (move_uploaded_file($_FILES['registration_doc']['tmp_name'], $uploadDir . $filename)) {
+ $regPath = $uploadDir . $filename;
+ }
+ }
+
+ try {
+ $stmt = db()->prepare("
+ INSERT INTO trucks (user_id, truck_type, load_capacity, plate_no, truck_pic_path, registration_path, registration_expiry_date, insurance_expiry_date, is_approved)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1)
+ ");
+ $stmt->execute([
+ $userId, $truckType, $loadCapacity, $plateNo, $truckPicPath, $regPath,
+ $regExpiry ?: null, $insExpiry ?: null
+ ]);
+ $flash = 'Truck added successfully.';
+ if ($isAjax) { echo json_encode(['success' => true, 'message' => $flash]); exit; }
+ } catch (Throwable $e) {
+ $errors[] = 'Database error: ' . $e->getMessage();
+ }
+ }
+
+ if (!empty($errors) && $isAjax) {
+ echo json_encode(['success' => false, 'message' => implode('. ', $errors)]); exit;
+ }
+
} else {
$fullName = trim($_POST['full_name'] ?? '');
$email = trim($_POST['email'] ?? '');
@@ -105,38 +164,85 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { validate_csrf_token();
db()->commit();
$flash = 'Truck Owner profile updated successfully.';
+
+ if ($isAjax) {
+ echo json_encode(['success' => true, 'message' => $flash]);
+ exit;
+ }
} catch (Throwable $e) {
db()->rollBack();
$errors[] = 'Failed to update truck owner profile. Please try again.';
+ if ($isAjax) {
+ echo json_encode(['success' => false, 'message' => implode('. ', $errors)]);
+ exit;
+ }
}
+ } else {
+ if ($isAjax) {
+ echo json_encode(['success' => false, 'message' => implode('. ', $errors)]);
+ exit;
+ }
}
}
}
// -- OUTPUT START --
-if (!$isAjax):
+if (!$isAjax) {
render_header('Edit Truck Owner', 'admin', true);
+ echo '
+
';
+ render_admin_sidebar('truck_owners');
+ echo '
+
';
+}
?>
-
-
-
+
+
+
← = e(t('back')) ?>
+
= e(t('edit_owner')) ?>
-
-
-
← = e(t('back')) ?>
-
= e(t('edit_owner')) ?>
-
+
+
+
+
+
-
-
= e($flash) ?>
-
-
-
= e(implode(' ', $errors)) ?>
-
+
+
= e($flash) ?>
+
+
+
= e(implode(' ', $errors)) ?>
+
+
+
+
-
+
+
+
+
= e(t('id_card_front') ?: 'ID Card Documents') ?>
+
+
+
+
+
+
+
+
+
= e(t('no_documents') ?: 'No documents uploaded') ?>
+
+
+
+
+
= e(t('other_documents') ?: 'Other Profile Documents') ?>
+
+
-
+
+
+
+
+
+
-
\ No newline at end of file
+
+
';
+ render_footer();
+}
+?>
\ No newline at end of file
diff --git a/admin_truck_owners.php b/admin_truck_owners.php
index 00959ed..ed20cac 100644
--- a/admin_truck_owners.php
+++ b/admin_truck_owners.php
@@ -42,9 +42,10 @@ $whereClause = "u.role = 'truck_owner'";
$params = [];
if ($q !== '') {
- $whereClause .= " AND (u.full_name LIKE ? OR u.email LIKE ? OR p.plate_no LIKE ? OR p.truck_type LIKE ?)";
+ // Search only by user details as trucks are now one-to-many
+ $whereClause .= " AND (u.full_name LIKE ? OR u.email LIKE ? OR p.phone LIKE ?)";
$likeQ = "%$q%";
- $params = array_merge($params, [$likeQ, $likeQ, $likeQ, $likeQ]);
+ $params = array_merge($params, [$likeQ, $likeQ, $likeQ]);
}
if ($status !== '' && in_array($status, ['active', 'pending', 'rejected'])) {
@@ -64,13 +65,13 @@ $stmt->execute($params);
$total = (int)$stmt->fetchColumn();
$totalPages = (int)ceil($total / $limit);
-// Fetch truck owners
+// Fetch truck owners with truck count
$sql = "
SELECT u.id, u.email, u.full_name, u.status, u.created_at,
- p.phone, p.truck_type, p.load_capacity, p.plate_no,
- p.id_card_path, p.truck_pic_path, p.registration_path,
+ p.phone, p.id_card_path,
c.name_en AS country_name,
- ci.name_en AS city_name
+ ci.name_en AS city_name,
+ (SELECT COUNT(*) FROM trucks t WHERE t.user_id = u.id) as truck_count
FROM users u
LEFT JOIN truck_owner_profiles p ON u.id = p.user_id
LEFT JOIN countries c ON p.country_id = c.id
@@ -96,6 +97,14 @@ render_header(t('manage_truck_owners'), 'admin', true);
= e(t('truck_owners')) ?>
= e(t('review_registrations')) ?>
+
@@ -135,7 +144,7 @@ render_header(t('manage_truck_owners'), 'admin', true);
ID
= e(t('name_email')) ?>
- = e(t('truck_info')) ?>
+ = e(t('trucks')) ?>
= e(t('documents')) ?>
= e(t('status')) ?>
= e(t('action')) ?>
@@ -151,9 +160,10 @@ render_header(t('manage_truck_owners'), 'admin', true);
= e((string)$owner['phone']) ?>
- = e(t('truck_type')) ?>: = e((string)$owner['truck_type']) ?>
- = e(t('cap')) ?>: = e((string)$owner['load_capacity']) ?>t
- = e(t('truck_plate')) ?>: = e((string)$owner['plate_no']) ?>
+
+ = e((string)$owner['truck_count']) ?>
+ = e(t('trucks')) ?>
+
@@ -229,12 +239,12 @@ render_header(t('manage_truck_owners'), 'admin', true);
-
+
@@ -249,8 +259,6 @@ render_header(t('manage_truck_owners'), 'admin', true);
@@ -267,27 +275,13 @@ $pic = $owner['truck_pic_path'];
+
+
= e(t('no_documents')) ?>
+
-
-
= e(t('truck_reg')) ?>
-
-
-
-
-
-
+
+ = e(t('view_truck_docs_in_edit')) ?>
-
-
= e(t('truck_picture')) ?>
-
-
-
-
= e(t('no_picture')) ?>
-