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

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

// Get stats
$result = $conn->query("SELECT COUNT(*) as count FROM rental_applications WHERE user_id = $user_id");
$stats['apps'] = $result->fetch_assoc()['count'];

$result = $conn->query("SELECT COUNT(*) as count FROM rental_contracts WHERE application_id IN (SELECT id FROM rental_applications WHERE user_id = $user_id)");
$stats['contracts'] = $result->fetch_assoc()['count'];

$result = $conn->query("SELECT COUNT(*) as count FROM maintenance_requests WHERE user_id = $user_id");
$stats['maintenance'] = $result->fetch_assoc()['count'];
?>
<!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 sidebar-item--active">📊 Dashboard</a>
            <a href="<?= BASE_URL ?>/tenant/pengajuan.php" class="sidebar-item">📝 Pengajuan</a>
            <a href="<?= BASE_URL ?>/tenant/kontrak.php" class="sidebar-item">📄 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;">Dashboard Tenant</h1>

            <div class="grid grid--3" style="margin-bottom: 48px;">
                <div class="stat-card">
                    <div style="font-size: 24px;">📝</div>
                    <div class="stat-card__value"><?= $stats['apps'] ?></div>
                    <div class="stat-card__label">Pengajuan</div>
                </div>

                <div class="stat-card">
                    <div style="font-size: 24px;">📄</div>
                    <div class="stat-card__value"><?= $stats['contracts'] ?></div>
                    <div class="stat-card__label">Kontrak Aktif</div>
                </div>

                <div class="stat-card">
                    <div style="font-size: 24px;">🔧</div>
                    <div class="stat-card__value"><?= $stats['maintenance'] ?></div>
                    <div class="stat-card__label">Maintenance</div>
                </div>
            </div>

            <div class="card">
                <div class="card__header">
                    <div class="card__title">Quick Actions</div>
                </div>
                <div class="grid grid--2">
                    <a href="<?= BASE_URL ?>/tenant/pengajuan.php" class="btn btn--primary">Pengajuan Sewa</a>
                    <a href="<?= BASE_URL ?>/tenant/kontrak.php" class="btn btn--primary">Lihat Kontrak</a>
                    <a href="<?= BASE_URL ?>/tenant/maintenance.php" class="btn btn--secondary">Lapor Maintenance</a>
                </div>
            </div>
        </div>
    </div>
</div>

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