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

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

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

$sql = "SELECT u.*, p.name as property_name, p.property_code, ut.name as unit_type_name
        FROM units u
        LEFT JOIN properties p ON u.property_id = p.id
        LEFT JOIN unit_types ut ON u.unit_type_id = ut.id
        WHERE u.status = 'vacant'";

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

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

$sql .= " ORDER BY p.name, u.unit_number ASC";

// 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' => 'Vacant Units')); ?>
    <?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' => 'Management', 'title' => 'Vacant Units')); ?>

                    <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">Available Units</h4>
                                </div>
                                <div class="card-body">
                                    <div class="table-responsive">
                                        <table class="table table-nowrap align-middle mb-0">
                                            <thead>
                                                <tr>
                                                    <th>Unit Number</th>
                                                    <th>Property</th>
                                                    <th>Type</th>
                                                    <th>Bedrooms</th>
                                                    <th>Bathrooms</th>
                                                    <th>Rent Amount</th>
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if ($result && $result->num_rows > 0): ?>
                                                    <?php while($unit = $result->fetch_assoc()): ?>
                                                        <tr>
                                                            <td>
                                                                <span class="fw-semibold"><?php echo htmlspecialchars($unit['unit_number']); ?></span>
                                                            </td>
                                                            <td>
                                                                <div>
                                                                    <h5 class="fs-14 mb-0"><?php echo htmlspecialchars($unit['property_name']); ?></h5>
                                                                    <p class="text-muted mb-0 small"><?php echo htmlspecialchars($unit['property_code'] ?? ''); ?></p>
                                                                </div>
                                                            </td>
                                                            <td><?php echo htmlspecialchars($unit['unit_type_name'] ?? 'N/A'); ?></td>
                                                            <td><?php echo $unit['bedrooms'] ?? 0; ?></td>
                                                            <td><?php echo $unit['bathrooms'] ?? 0; ?></td>
                                                            <td>
                                                                <span class="fw-semibold">KES <?php echo number_format($unit['rent_amount'], 2); ?></span>
                                                            </td>
                                                            <td>
                                                                <a href="create_lease.php?unit_id=<?php echo $unit['id']; ?>" 
                                                                   class="btn btn-primary btn-sm">
                                                                    <i class="ri-file-add-line me-1"></i>Create Lease
                                                                </a>
                                                            </td>
                                                        </tr>
                                                    <?php endwhile; ?>
                                                <?php else: ?>
                                                    <tr>
                                                        <td colspan="7" class="text-center py-4">
                                                            <div class="text-muted">
                                                                <i class="ri-checkbox-circle-line fs-48 text-success mb-3"></i>
                                                                <p class="mt-2">No vacant units available</p>
                                                            </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>
