query("SELECT id, name FROM properties ORDER BY name ASC")->fetchAll(PDO::FETCH_ASSOC); $tenants = $db->query("SELECT id, name FROM tenants ORDER BY name ASC")->fetchAll(PDO::FETCH_ASSOC); $stmt = $db->prepare("SELECT * FROM maintenance_requests WHERE id = ?"); $stmt->execute([$id]); $request = $stmt->fetch(PDO::FETCH_ASSOC); if (!$request) { header("Location: index.php?page=maintenance&error=Request not found"); exit; } } catch (PDOException $e) { $error = "Error fetching data: " . $e->getMessage(); } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $property_id = $_POST['property_id'] ?? null; $tenant_id = $_POST['tenant_id'] ?? null; $description = $_POST['description'] ?? ''; $reported_date = $_POST['reported_date'] ?? ''; $completed_date = !empty($_POST['completed_date']) ? $_POST['completed_date'] : null; $status = $_POST['status'] ?? 'Open'; if ($property_id && $description && $reported_date) { try { $stmt = $db->prepare("UPDATE maintenance_requests SET property_id = ?, tenant_id = ?, description = ?, reported_date = ?, completed_date = ?, status = ? WHERE id = ?"); $stmt->execute([$property_id, $tenant_id, $description, $reported_date, $completed_date, $status, $id]); header("Location: index.php?page=maintenance&success=Request updated"); exit; } catch (PDOException $e) { $error = "Error: " . $e->getMessage(); } } else { $error = "Please fill all required fields."; } } include 'templates/header.php'; ?>