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

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

$days_ahead = $_GET['days'] ?? 60;

$sql = "SELECT l.*, u.unit_number, p.name as property_name, p.property_code,
               t.first_name, t.last_name, t.email, t.phone
        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
        JOIN tenants t ON lt.tenant_id = t.tenant_id
        WHERE l.lease_status = 'ACTIVE'
        AND l.end_date BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL ? DAY)
        ORDER BY l.end_date ASC";

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

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

                    <div class="row mb-3">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">
                                    <form method="GET" class="row g-3">
                                        <div class="col-md-3">
                                            <label class="form-label">Expiring Within (Days)</label>
                                            <select class="form-select" name="days" onchange="this.form.submit()">
                                                <option value="30" <?php echo $days_ahead == 30 ? 'selected' : ''; ?>>30 Days</option>
                                                <option value="60" <?php echo $days_ahead == 60 ? 'selected' : ''; ?>>60 Days</option>
                                                <option value="90" <?php echo $days_ahead == 90 ? 'selected' : ''; ?>>90 Days</option>
                                            </select>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-header">
                                    <h4 class="card-title mb-0">Leases Expiring Soon</h4>
                                </div>
                                <div class="card-body">
                                    <div class="table-responsive">
                                        <table class="table table-nowrap align-middle mb-0">
                                            <thead>
                                                <tr>
                                                    <th>Lease Code</th>
                                                    <th>Tenant</th>
                                                    <th>Property/Unit</th>
                                                    <th>Start Date</th>
                                                    <th>End Date</th>
                                                    <th>Days Remaining</th>
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if ($result && $result->num_rows > 0): ?>
                                                    <?php while($lease = $result->fetch_assoc()): 
                                                        $end_date = strtotime($lease['end_date']);
                                                        $today = strtotime('today');
                                                        $days_left = round(($end_date - $today) / (60 * 60 * 24));
                                                    ?>
                                                        <tr>
                                                            <td>
                                                                <span class="fw-semibold"><?php echo htmlspecialchars($lease['lease_code']); ?></span>
                                                            </td>
                                                            <td>
                                                                <div>
                                                                    <h5 class="fs-14 mb-0">
                                                                        <?php echo htmlspecialchars($lease['first_name'] . ' ' . $lease['last_name']); ?>
                                                                    </h5>
                                                                    <p class="text-muted mb-0 small"><?php echo htmlspecialchars($lease['email']); ?></p>
                                                                </div>
                                                            </td>
                                                            <td>
                                                                <div>
                                                                    <h5 class="fs-14 mb-0"><?php echo htmlspecialchars($lease['property_name']); ?></h5>
                                                                    <p class="text-muted mb-0 small">Unit: <?php echo htmlspecialchars($lease['unit_number']); ?></p>
                                                                </div>
                                                            </td>
                                                            <td><?php echo date('M d, Y', strtotime($lease['start_date'])); ?></td>
                                                            <td>
                                                                <span class="<?php echo $days_left <= 15 ? 'text-danger' : 'text-warning'; ?>">
                                                                    <?php echo date('M d, Y', $end_date); ?>
                                                                </span>
                                                            </td>
                                                            <td>
                                                                <span class="badge bg-soft-<?php echo $days_left <= 15 ? 'danger' : 'warning'; ?>">
                                                                    <?php echo $days_left; ?> days
                                                                </span>
                                                            </td>
                                                            <td>
                                                                <a href="create_lease.php?renew=<?php echo $lease['lease_id']; ?>" 
                                                                   class="btn btn-primary btn-sm">
                                                                    <i class="ri-refresh-line me-1"></i>Renew
                                                                </a>
                                                            </td>
                                                        </tr>
                                                    <?php endwhile; ?>
                                                <?php else: ?>
                                                    <tr>
                                                        <td colspan="7" class="text-center py-4">
                                                            <div class="text-muted">
                                                                <i class="ri-checkbox-circle-line fs-48 text-success mb-3"></i>
                                                                <p class="mt-2">No leases expiring in the next <?php echo $days_ahead; ?> days</p>
                                                            </div>
                                                        </td>
                                                    </tr>
                                                <?php endif; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

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

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