false, 'message' => 'Invalid ID']); exit; } header('Location: admin_truck_owners.php'); exit; } $errors = []; $flash = null; // Fetch Truck Owner Profile $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.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' "); $stmt->execute([$userId]); $owner = $stmt->fetch(); $trucks = db()->prepare("SELECT * FROM trucks WHERE user_id = ?"); $trucks->execute([$userId]); $ownerTrucks = $trucks->fetchAll(); if (!$owner) { if ($isAjax) { header('Content-Type: application/json'); echo json_encode(['success' => false, 'message' => 'Owner not found']); exit; } header('Location: admin_truck_owners.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(); if (isset($_POST['approve_truck'])) { $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'] ?? ''); $phone = trim($_POST['phone'] ?? ''); $countryId = (int)($_POST['country_id'] ?? 0); $cityId = (int)($_POST['city_id'] ?? 0); $addressLine = trim($_POST['address_line'] ?? ''); $status = trim($_POST['status'] ?? ''); $password = $_POST['password'] ?? ''; $bankAccount = trim($_POST['bank_account'] ?? ''); $bankName = trim($_POST['bank_name'] ?? ''); $bankBranch = trim($_POST['bank_branch'] ?? ''); $isCompany = isset($_POST['is_company']) ? 1 : 0; $ctrNumber = trim($_POST['ctr_number'] ?? ''); $notes = trim($_POST['notes'] ?? ''); 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 (!in_array($status, ['pending', 'active', 'rejected'], true)) $errors[] = 'Invalid status.'; if ($countryId <= 0 || $cityId <= 0) { $errors[] = 'Please select country and city.'; } if (!$errors) { try { db()->beginTransaction(); $stmtUser = db()->prepare("UPDATE users SET full_name = ?, email = ?, status = ? WHERE id = ? AND role = 'truck_owner'"); $stmtUser->execute([$fullName, $email, $status, $userId]); if ($password !== '') { $stmtPass = db()->prepare("UPDATE users SET password = ? WHERE id = ? AND role = 'truck_owner'"); $stmtPass->execute([password_hash($password, PASSWORD_DEFAULT), $userId]); } $stmtProfile = db()->prepare(" UPDATE truck_owner_profiles SET phone = ?, address_line = ?, country_id = ?, city_id = ?, bank_account = ?, bank_name = ?, bank_branch = ?, is_company = ?, ctr_number = ?, notes = ? WHERE user_id = ? "); $stmtProfile->execute([$phone, $addressLine, $countryId, $cityId, $bankAccount, $bankName, $bankBranch, $isCompany, $ctrNumber, $notes, $userId]); 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) { render_header('Edit Truck Owner', 'admin', true); echo '
'; render_admin_sidebar('truck_owners'); echo '
'; } ?>
'; render_footer(); } ?>