86 lines
3.5 KiB
PHP
86 lines
3.5 KiB
PHP
<?php
|
|
require_once 'session.php';
|
|
|
|
require_once 'db/config.php';
|
|
|
|
$properties = $tenants = [];
|
|
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);
|
|
} 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'] ?? '';
|
|
$status = $_POST['status'] ?? 'Open';
|
|
|
|
if ($property_id && $description && $reported_date) {
|
|
try {
|
|
$stmt = $db->prepare("INSERT INTO maintenance_requests (property_id, tenant_id, description, reported_date, status) VALUES (?, ?, ?, ?, ?)");
|
|
$stmt->execute([$property_id, $tenant_id, $description, $reported_date, $status]);
|
|
header("Location: index.php?page=maintenance&success=Request added");
|
|
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>Add Maintenance Request</h2>
|
|
|
|
<?php if (!empty($error)): ?>
|
|
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
|
|
<?php endif; ?>
|
|
|
|
<form action="add_request.php" 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 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 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></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" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="status">Status</label>
|
|
<select class="form-control" id="status" name="status">
|
|
<option value="Open">Open</option>
|
|
<option value="In Progress">In Progress</option>
|
|
<option value="Closed">Closed</option>
|
|
</select>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Add Request</button>
|
|
<a href="index.php?page=maintenance" class="btn btn-secondary">Cancel</a>
|
|
</form>
|
|
</div>
|
|
|
|
<?php include 'templates/footer.php'; ?>
|