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

require_login('tenant');
$page_title = 'Kontrak - ANATL Portal';
$user_id = $_SESSION['user_id'];

$contracts = [];
$result = $conn->query("
    SELECT rc.*, ra.business_name, ru.name as unit_name
    FROM rental_contracts rc
    JOIN rental_applications ra ON rc.application_id = ra.id
    JOIN rental_units ru ON ra.unit_id = ru.id
    WHERE ra.user_id = $user_id
    ORDER BY rc.created_at DESC
");
if ($result) {
    while ($row = $result->fetch_assoc()) {
        $contracts[] = $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="portal">
        <div class="portal__sidebar">
            <div class="sidebar__title">Menu Tenant</div>
            <a href="<?= BASE_URL ?>/tenant/index.php" class="sidebar-item">📊 Dashboard</a>
            <a href="<?= BASE_URL ?>/tenant/pengajuan.php" class="sidebar-item">📝 Pengajuan</a>
            <a href="<?= BASE_URL ?>/tenant/kontrak.php" class="sidebar-item sidebar-item--active">📄 Kontrak</a>
            <a href="<?= BASE_URL ?>/tenant/maintenance.php" class="sidebar-item">🔧 Maintenance</a>
        </div>

        <div class="portal__content">
            <h1 style="color: #003A70; margin-bottom: 32px;">Kontrak</h1>

            <?php if (empty($contracts)): ?>
            <div class="empty-state">
                <div class="empty-state__message">Belum ada kontrak yang aktif</div>
            </div>
            <?php else: ?>

            <div class="grid grid--1">
                <?php foreach ($contracts as $contract): ?>
                <div class="card">
                    <div class="card__header">
                        <div>
                            <div class="card__title"><?= esc($contract['unit_name']) ?></div>
                            <p style="color: #5A6A7A; font-size: 14px; margin-top: 4px;">
                                <?= esc($contract['business_name']) ?>
                            </p>
                        </div>
                    </div>

                    <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 24px;">
                        <div>
                            <p style="color: #5A6A7A; font-size: 12px;">Mulai</p>
                            <p style="font-weight: 600;"><?= format_date($contract['start_date']) ?></p>
                        </div>
                        <div>
                            <p style="color: #5A6A7A; font-size: 12px;">Berakhir</p>
                            <p style="font-weight: 600;"><?= format_date($contract['end_date']) ?></p>
                        </div>
                    </div>

                    <a href="#" class="btn btn--primary">📥 Download Kontrak</a>
                </div>
                <?php endforeach; ?>
            </div>

            <?php endif; ?>
        </div>
    </div>
</div>

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