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

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

// Get tenant's active lease
$user_id = $_SESSION['user_id'];
$sql = "SELECT l.*, u.unit_number, u.floor_level, u.bedrooms, u.bathrooms,
               p.name as property_name, p.location_area, p.street_address
        FROM leases l
        JOIN units u ON l.unit_id = u.id
        JOIN properties p ON u.property_id = p.id
        JOIN lease_tenants lt ON l.id = lt.lease_id AND lt.is_primary = 1
        WHERE lt.tenant_id = (SELECT tenant_id FROM tenants WHERE user_id = ?)
        AND l.status = 'active'
        ORDER BY l.start_date DESC
        LIMIT 1";

$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$lease = $result->fetch_assoc();
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'My Lease')); ?>
    <?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' => 'My Home', 'title' => 'Lease Information')); ?>

                    <?php if ($lease): ?>
                        <div class="row">
                            <div class="col-md-8">
                                <div class="card">
                                    <div class="card-header">
                                        <h4 class="card-title mb-0">Lease Details</h4>
                                    </div>
                                    <div class="card-body">
                                        <div class="row mb-3">
                                            <div class="col-md-6">
                                                <p class="mb-2"><strong>Property:</strong> <?php echo htmlspecialchars($lease['property_name']); ?></p>
                                                <p class="mb-2"><strong>Unit:</strong> <?php echo htmlspecialchars($lease['unit_number']); ?></p>
                                                <p class="mb-2"><strong>Location:</strong> <?php echo htmlspecialchars($lease['location_area']); ?></p>
                                            </div>
                                            <div class="col-md-6">
                                                <p class="mb-2"><strong>Lease Period:</strong> <?php echo date('M d, Y', strtotime($lease['start_date'])); ?> to <?php echo date('M d, Y', strtotime($lease['end_date'])); ?></p>
                                                <p class="mb-2"><strong>Rent Amount:</strong> KES <?php echo number_format($lease['rent_amount'], 2); ?> / <?php echo $lease['payment_frequency']; ?></p>
                                                <p class="mb-2"><strong>Due Day:</strong> <?php echo $lease['due_day']; ?> of each month</p>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="card">
                                    <div class="card-header">
                                        <h4 class="card-title mb-0">Quick Actions</h4>
                                    </div>
                                    <div class="card-body">
                                        <a href="pay_rent.php" class="btn btn-primary w-100 mb-2">
                                            <i class="ri-money-dollar-circle-line me-1"></i>Pay Rent
                                        </a>
                                        <a href="my_invoices.php" class="btn btn-info w-100 mb-2">
                                            <i class="ri-receipt-line me-1"></i>View Invoices
                                        </a>
                                        <a href="report_issue.php" class="btn btn-warning w-100">
                                            <i class="ri-tools-line me-1"></i>Report Issue
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php else: ?>
                        <div class="row">
                            <div class="col-12">
                                <div class="card">
                                    <div class="card-body text-center py-5">
                                        <i class="ri-file-line fs-48 text-muted mb-3"></i>
                                        <h5>No Active Lease</h5>
                                        <p class="text-muted">You don't have an active lease at the moment.</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endif; ?>

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

    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
