<?php 
$pageTitle = 'Properties';
include __DIR__ . '/../layouts/header.php'; 
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h2><i class="bi bi-house-door me-2"></i>Properties</h2>
    <a href="<?= $this->baseUrl('/rent/properties/create') ?>" class="btn btn-primary">
        <i class="bi bi-plus-circle"></i> Add Property
    </a>
</div>

<?php if (isset($_SESSION['success'])): ?>
    <div class="alert alert-success alert-dismissible fade show">
        <?= htmlspecialchars($_SESSION['success']); unset($_SESSION['success']); ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
<?php endif; ?>
<?php if (isset($_SESSION['error'])): ?>
    <div class="alert alert-danger alert-dismissible fade show">
        <?= htmlspecialchars($_SESSION['error']); unset($_SESSION['error']); ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
<?php endif; ?>

<div class="card">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead class="table-light">
                    <tr>
                        <th>Name</th>
                        <th>Tenant</th>
                        <th>Monthly Rent</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($properties)): ?>
                        <tr>
                            <td colspan="5" class="text-center py-4 text-muted">
                                <i class="bi bi-house fs-1 d-block mb-2"></i>
                                No properties yet. <a href="<?= $this->baseUrl('/rent/properties/create') ?>">Add your first property</a>.
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($properties as $p): ?>
                        <tr>
                            <td><strong><?= htmlspecialchars($p['name']) ?></strong></td>
                            <td><?= htmlspecialchars($p['tenant_name'] ?: '—') ?></td>
                            <td><?= number_format($p['monthly_rent'], 2) ?> KES</td>
                            <td>
                                <span class="badge bg-<?= $p['status'] === 'occupied' ? 'success' : 'secondary' ?>">
                                    <?= ucfirst($p['status']) ?>
                                </span>
                            </td>
                            <td>
                                <a href="<?= $this->baseUrl('/rent/payments/create?property_id=' . $p['id']) ?>" 
                                   class="btn btn-sm btn-success" title="Record Payment">
                                    <i class="bi bi-cash"></i>
                                </a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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