<?php
session_start();
include 'layouts/dbconn.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$user_id = $_SESSION['user_id'];
$status_filter = $_GET['status'] ?? '';

$sql = "SELECT mr.*, p.name as property_name, u.unit_number
        FROM maintenance_requests mr
        LEFT JOIN properties p ON mr.property_id = p.id
        LEFT JOIN units u ON mr.unit_id = u.id
        WHERE mr.requester_id = ?";

$params = [$user_id];
$types = 'i';

if (!empty($status_filter)) {
    $sql .= " AND mr.status = ?";
    $params[] = $status_filter;
    $types .= 's';
}

$sql .= " ORDER BY mr.created_at DESC";

$stmt = $conn->prepare($sql);
$stmt->bind_param($types, ...$params);
$stmt->execute();
$result = $stmt->get_result();
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'My Maintenance Requests')); ?>
    <?php include 'layouts/head-css.php'; ?>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>

        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <?php includeFileWithVariables('layouts/page-title.php', array('pagetitle' => 'Maintenance', 'title' => 'My Maintenance Requests')); ?>

                    <div class="row mb-3">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <form method="GET" class="d-flex gap-2">
                                            <select class="form-select" name="status" style="width: 200px;" onchange="this.form.submit()">
                                                <option value="">All Status</option>
                                                <option value="Open" <?php echo $status_filter == 'Open' ? 'selected' : ''; ?>>Open</option>
                                                <option value="Assigned" <?php echo $status_filter == 'Assigned' ? 'selected' : ''; ?>>Assigned</option>
                                                <option value="In Progress" <?php echo $status_filter == 'In Progress' ? 'selected' : ''; ?>>In Progress</option>
                                                <option value="Completed" <?php echo $status_filter == 'Completed' ? 'selected' : ''; ?>>Completed</option>
                                            </select>
                                        </form>
                                        <a href="report_issue.php" class="btn btn-primary">
                                            <i class="ri-add-line align-bottom me-1"></i>New Request
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-header">
                                    <h4 class="card-title mb-0">My Requests</h4>
                                </div>
                                <div class="card-body">
                                    <div class="table-responsive">
                                        <table class="table table-nowrap align-middle mb-0">
                                            <thead>
                                                <tr>
                                                    <th>Ticket #</th>
                                                    <th>Property/Unit</th>
                                                    <th>Category</th>
                                                    <th>Priority</th>
                                                    <th>Subject</th>
                                                    <th>Status</th>
                                                    <th>Created</th>
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if ($result && $result->num_rows > 0): ?>
                                                    <?php while($request = $result->fetch_assoc()): ?>
                                                        <tr>
                                                            <td>
                                                                <span class="fw-semibold"><?php echo htmlspecialchars($request['ticket_number']); ?></span>
                                                            </td>
                                                            <td>
                                                                <div>
                                                                    <h5 class="fs-14 mb-0"><?php echo htmlspecialchars($request['property_name'] ?? 'N/A'); ?></h5>
                                                                    <p class="text-muted mb-0 small">Unit: <?php echo htmlspecialchars($request['unit_number'] ?? 'N/A'); ?></p>
                                                                </div>
                                                            </td>
                                                            <td>
                                                                <span class="badge bg-soft-info text-info">
                                                                    <?php echo htmlspecialchars($request['category']); ?>
                                                                </span>
                                                            </td>
                                                            <td>
                                                                <?php
                                                                $priority_colors = [
                                                                    'Emergency' => 'danger',
                                                                    'High' => 'warning',
                                                                    'Medium' => 'info',
                                                                    'Low' => 'secondary'
                                                                ];
                                                                $priority_color = $priority_colors[$request['priority']] ?? 'secondary';
                                                                ?>
                                                                <span class="badge bg-soft-<?php echo $priority_color; ?> text-<?php echo $priority_color; ?>">
                                                                    <?php echo htmlspecialchars($request['priority']); ?>
                                                                </span>
                                                            </td>
                                                            <td><?php echo htmlspecialchars($request['subject']); ?></td>
                                                            <td>
                                                                <?php
                                                                $status_badges = [
                                                                    'Open' => 'badge-soft-info',
                                                                    'Assigned' => 'badge-soft-warning',
                                                                    'In Progress' => 'badge-soft-primary',
                                                                    'Completed' => 'badge-soft-success'
                                                                ];
                                                                $badge_class = $status_badges[$request['status']] ?? 'badge-soft-secondary';
                                                                ?>
                                                                <span class="badge <?php echo $badge_class; ?>">
                                                                    <?php echo htmlspecialchars($request['status']); ?>
                                                                </span>
                                                            </td>
                                                            <td><?php echo date('M d, Y', strtotime($request['created_at'])); ?></td>
                                                            <td>
                                                                <a href="maintenance_request_details.php?id=<?php echo $request['id']; ?>" 
                                                                   class="btn btn-soft-secondary btn-sm">
                                                                    <i class="ri-eye-line"></i>
                                                                </a>
                                                            </td>
                                                        </tr>
                                                    <?php endwhile; ?>
                                                <?php else: ?>
                                                    <tr>
                                                        <td colspan="8" class="text-center py-4">
                                                            <div class="text-muted">
                                                                <i class="ri-inbox-line fs-48"></i>
                                                                <p class="mt-2">No maintenance requests found</p>
                                                                <a href="report_issue.php" class="btn btn-primary btn-sm mt-2">
                                                                    <i class="ri-add-line me-1"></i>Submit First Request
                                                                </a>
                                                            </div>
                                                        </td>
                                                    </tr>
                                                <?php endif; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>

    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
