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

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

$property_id = $_GET['property_id'] ?? '';

$sql = "SELECT p.name as property_name,
               COUNT(DISTINCT u.id) as total_units,
               COUNT(DISTINCT CASE WHEN u.status = 'occupied' THEN u.id END) as occupied_units,
               COUNT(DISTINCT CASE WHEN u.status = 'vacant' THEN u.id END) as vacant_units,
               ROUND((COUNT(DISTINCT CASE WHEN u.status = 'occupied' THEN u.id END) / COUNT(DISTINCT u.id)) * 100, 2) as occupancy_rate
        FROM properties p
        LEFT JOIN units u ON p.id = u.property_id
        WHERE 1=1";

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

if (!empty($property_id)) {
    $sql .= " AND p.id = ?";
    $params[] = $property_id;
    $types .= 'i';
}

$sql .= " GROUP BY p.id ORDER BY p.name";

// Get properties for filter
$sql_properties = "SELECT * FROM properties ORDER BY name";
$result_properties = $conn->query($sql_properties);

if (!empty($params)) {
    $stmt = $conn->prepare($sql);
    $stmt->bind_param($types, ...$params);
    $stmt->execute();
    $result = $stmt->get_result();
} else {
    $result = $conn->query($sql);
}
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Occupancy Report')); ?>
    <?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' => 'Reports', 'title' => 'Occupancy Report')); ?>

                    <div class="row mb-3">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">
                                    <form method="GET" class="row g-3">
                                        <div class="col-md-4">
                                            <label class="form-label">Property</label>
                                            <select class="form-select" name="property_id" onchange="this.form.submit()">
                                                <option value="">All Properties</option>
                                                <?php while($prop = $result_properties->fetch_assoc()): ?>
                                                    <option value="<?php echo $prop['id']; ?>" 
                                                            <?php echo $property_id == $prop['id'] ? 'selected' : ''; ?>>
                                                        <?php echo htmlspecialchars($prop['name']); ?>
                                                    </option>
                                                <?php endwhile; ?>
                                            </select>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-header">
                                    <h4 class="card-title mb-0">Occupancy Report</h4>
                                </div>
                                <div class="card-body">
                                    <div class="table-responsive">
                                        <table class="table table-nowrap align-middle mb-0">
                                            <thead>
                                                <tr>
                                                    <th>Property</th>
                                                    <th>Total Units</th>
                                                    <th>Occupied</th>
                                                    <th>Vacant</th>
                                                    <th>Occupancy Rate</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if ($result && $result->num_rows > 0): ?>
                                                    <?php while($row = $result->fetch_assoc()): ?>
                                                        <tr>
                                                            <td>
                                                                <span class="fw-semibold"><?php echo htmlspecialchars($row['property_name']); ?></span>
                                                            </td>
                                                            <td><?php echo $row['total_units']; ?></td>
                                                            <td>
                                                                <span class="badge bg-soft-success text-success">
                                                                    <?php echo $row['occupied_units']; ?>
                                                                </span>
                                                            </td>
                                                            <td>
                                                                <span class="badge bg-soft-danger text-danger">
                                                                    <?php echo $row['vacant_units']; ?>
                                                                </span>
                                                            </td>
                                                            <td>
                                                                <div class="d-flex align-items-center">
                                                                    <div class="flex-grow-1 me-2">
                                                                        <div class="progress">
                                                                            <div class="progress-bar bg-success" role="progressbar" 
                                                                                 style="width: <?php echo $row['occupancy_rate']; ?>%">
                                                                            </div>
                                                                        </div>
                                                                    </div>
                                                                    <span class="fw-semibold"><?php echo $row['occupancy_rate']; ?>%</span>
                                                                </div>
                                                            </td>
                                                        </tr>
                                                    <?php endwhile; ?>
                                                <?php else: ?>
                                                    <tr>
                                                        <td colspan="5" class="text-center py-4">
                                                            <div class="text-muted">No data found</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>
