109 lines
4.7 KiB
PHP
109 lines
4.7 KiB
PHP
<?php
|
|
require_once 'session.php';
|
|
|
|
require_once 'db/config.php';
|
|
|
|
$id = $_GET['id'] ?? null;
|
|
if (!$id) {
|
|
header("Location: index.php?page=maintenance");
|
|
exit;
|
|
}
|
|
|
|
$properties = $tenants = [];
|
|
$request = null;
|
|
|
|
try {
|
|
$db = db();
|
|
$properties = $db->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';
|
|
?>
|
|
|
|
<div class="container mt-4">
|
|
<h2>Edit Maintenance Request</h2>
|
|
|
|
<?php if (!empty($error)): ?>
|
|
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
|
|
<?php endif; ?>
|
|
|
|
<form action="edit_request.php?id=<?php echo $id; ?>" method="post">
|
|
<div class="form-group">
|
|
<label for="property_id">Property</label>
|
|
<select class="form-control" id="property_id" name="property_id" required>
|
|
<option value="">Select Property</option>
|
|
<?php foreach ($properties as $prop): ?>
|
|
<option value="<?php echo $prop['id']; ?>" <?php echo ($prop['id'] == $request['property_id']) ? 'selected' : ''; ?>><?php echo htmlspecialchars($prop['name']); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="tenant_id">Tenant (Optional)</label>
|
|
<select class="form-control" id="tenant_id" name="tenant_id">
|
|
<option value="">Select Tenant</option>
|
|
<?php foreach ($tenants as $tenant): ?>
|
|
<option value="<?php echo $tenant['id']; ?>" <?php echo ($tenant['id'] == $request['tenant_id']) ? 'selected' : ''; ?>><?php echo htmlspecialchars($tenant['name']); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="description">Description</label>
|
|
<textarea class="form-control" id="description" name="description" rows="4" required><?php echo htmlspecialchars($request['description']); ?></textarea>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="reported_date">Reported Date</label>
|
|
<input type="date" class="form-control" id="reported_date" name="reported_date" value="<?php echo htmlspecialchars($request['reported_date']); ?>" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="completed_date">Completed Date</label>
|
|
<input type="date" class="form-control" id="completed_date" name="completed_date" value="<?php echo htmlspecialchars($request['completed_date']); ?>">
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="status">Status</label>
|
|
<select class="form-control" id="status" name="status">
|
|
<option value="Open" <?php echo ($request['status'] == 'Open') ? 'selected' : ''; ?>>Open</option>
|
|
<option value="In Progress" <?php echo ($request['status'] == 'In Progress') ? 'selected' : ''; ?>>In Progress</option>
|
|
<option value="Closed" <?php echo ($request['status'] == 'Closed') ? 'selected' : ''; ?>>Closed</option>
|
|
</select>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Update Request</button>
|
|
<a href="index.php?page=maintenance" class="btn btn-secondary">Cancel</a>
|
|
</form>
|
|
</div>
|
|
|
|
<?php include 'templates/footer.php'; ?>
|