prepare("INSERT INTO maintenance_records (vehicle_id, repair_date, description, mileage, cost, garage) VALUES (?, ?, ?, ?, ?, ?)"); $stmt->execute([$vehicle_id, $repair_date, $description, $mileage, $cost, $garage]); $maintenance_record_id = $pdo->lastInsertId(); // Handle file uploads if any $upload_msg = ''; if (isset($_FILES['documents']) && !empty($_FILES['documents']['name'][0])) { $uploadDir = '../uploads/maintenance/'; if (!is_dir($uploadDir)) mkdir($uploadDir, 0755, true); $successFiles = 0; foreach ($_FILES['documents']['tmp_name'] as $key => $tmp_name) { if ($_FILES['documents']['error'][$key] === UPLOAD_ERR_OK) { $original_name = basename($_FILES['documents']['name'][$key]); $file_size = $_FILES['documents']['size'][$key]; $file_type = mime_content_type($tmp_name); $ext = strtolower(pathinfo($original_name, PATHINFO_EXTENSION)); if ($ext === 'pdf' && $file_type === 'application/pdf' && $file_size <= 10 * 1024 * 1024) { $new_filename = uniqid('maint_') . '_' . time() . '.pdf'; $dest_path = $uploadDir . $new_filename; if (move_uploaded_file($tmp_name, $dest_path)) { $docStmt = $pdo->prepare("INSERT INTO maintenance_documents (maintenance_record_id, original_name, file_name, file_path) VALUES (?, ?, ?, ?)"); $docStmt->execute([$maintenance_record_id, $original_name, $new_filename, 'uploads/maintenance/' . $new_filename]); $successFiles++; } } } } if ($successFiles > 0) $upload_msg = " (และแนบเอกสาร $successFiles ไฟล์)"; } // Fetch vehicle plate for notification $vehStmt = $pdo->prepare("SELECT license_plate FROM vehicles WHERE id = ?"); $vehStmt->execute([$vehicle_id]); $veh = $vehStmt->fetch(); $plate = $veh ? $veh['license_plate'] : "ID {$vehicle_id}"; notifyEvent('maintenance', "🛠 แจ้งเตือนซ่อมบำรุง\nมีการบันทึกประวัติการซ่อมรถ ทะเบียน: {$plate}\nรายละเอียด: {$description}"); jsonResponse(true, 'บันทึกประวัติการซ่อมสำเร็จ' . $upload_msg); } catch (PDOException $e) { jsonResponse(false, 'Database Error: ' . $e->getMessage()); } ?>