<?php
require_once 'includes/auth.php';
require_once 'includes/functions.php';

$page_title = 'Unit Sewa - ANATL Portal';

$units = [];
$result = $conn->query('SELECT * FROM rental_units WHERE is_available = 1');
if ($result) {
    while ($row = $result->fetch_assoc()) {
        $units[] = $row;
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $page_title ?></title>
    <link rel="stylesheet" href="<?= BASE_URL ?>/assets/css/style.css">
</head>
<body>
<?php require 'includes/header.php'; ?>

<div class="main">
    <div class="container">
        <h1 style="color: #003A70; margin-bottom: 32px;">Unit Sewa</h1>
        <p style="color: #5A6A7A; margin-bottom: 32px;">Pilihan unit bisnis premium di Bandara Internasional Dili</p>

        <div class="grid grid--2">
            <?php if ($units): ?>
                <?php foreach ($units as $unit): ?>
                <div class="card">
                    <div style="height: 200px; background: linear-gradient(135deg, #003A70 0%, #005BAA 100%); border-radius: 8px; margin-bottom: 16px;"></div>
                    <h3 style="color: #003A70; margin-bottom: 8px;"><?= esc($unit['name']) ?></h3>
                    <p style="color: #5A6A7A; margin-bottom: 12px; font-size: 14px;">
                        📍 <?= esc($unit['location']) ?><br>
                        📐 <?= number_format($unit['area_m2'], 2) ?> m²
                    </p>
                    <p style="color: #003A70; font-size: 20px; font-weight: 600; margin-bottom: 12px;">
                        Rp <?= number_format($unit['price_monthly'], 0, ',', '.') ?>/bulan
                    </p>
                    <p style="color: #1A2332; margin-bottom: 16px; line-height: 1.6;">
                        <?= esc(substr($unit['description'], 0, 100)) ?>...
                    </p>
                    <?php if (is_logged_in() && get_role() === 'tenant'): ?>
                        <a href="<?= BASE_URL ?>/tenant/pengajuan.php" class="btn btn--primary">Ajukan Sewa</a>
                    <?php else: ?>
                        <a href="<?= BASE_URL ?>/login.php" class="btn btn--primary">Masuk untuk Ajukan</a>
                    <?php endif; ?>
                </div>
                <?php endforeach; ?>
            <?php else: ?>
                <div style="grid-column: 1 / -1; text-align: center; padding: 48px;">
                    <p style="color: #5A6A7A;">Tidak ada unit yang tersedia saat ini</p>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php require 'includes/footer.php'; ?>
