33 lines
976 B
PHP
33 lines
976 B
PHP
<?php
|
|
session_start();
|
|
require_once '../includes/db.php';
|
|
require_once '../includes/api_helper.php';
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
jsonResponse(false, 'Unauthorized', [], 401);
|
|
}
|
|
|
|
$vehicle_id = $_GET['vehicle_id'] ?? null;
|
|
|
|
if (!$vehicle_id) {
|
|
jsonResponse(false, 'Missing vehicle_id');
|
|
}
|
|
|
|
try {
|
|
$stmt = $pdo->prepare("SELECT * FROM maintenance_records WHERE vehicle_id = ? ORDER BY repair_date DESC, id DESC");
|
|
$stmt->execute([$vehicle_id]);
|
|
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Fetch documents for each record
|
|
foreach ($records as &$record) {
|
|
$docStmt = $pdo->prepare("SELECT * FROM maintenance_documents WHERE maintenance_record_id = ? ORDER BY id ASC");
|
|
$docStmt->execute([$record['id']]);
|
|
$record['documents'] = $docStmt->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
jsonResponse(true, 'Success', $records);
|
|
} catch (PDOException $e) {
|
|
jsonResponse(false, 'Database Error: ' . $e->getMessage());
|
|
}
|
|
?>
|