<?php
require_once "../config/auth.php";
require_once "../config/db.php";
require_once "../config/helpers.php";

$pageTitle = "Renewals";
require_once "../includes/header.php";
require_once "../includes/sidebar.php";
require_once "../includes/topbar.php";

// ================= DATE =================
$today  = date('Y-m-d');
$next30 = date('Y-m-d', strtotime('+30 days'));
$next60 = date('Y-m-d', strtotime('+60 days'));

// ================= 0–30 DAYS =================
$renew30 = $conn->query("
SELECT s.*, c.name as customer_name
FROM services s
JOIN customers c ON s.customer_id = c.id
WHERE s.renewal_date BETWEEN '$today' AND '$next30'
ORDER BY s.renewal_date ASC
");

// ================= 30–60 DAYS (NO DUPLICATE) =================
$renew60 = $conn->query("
SELECT s.*, c.name as customer_name
FROM services s
JOIN customers c ON s.customer_id = c.id
WHERE s.renewal_date > '$next30' 
AND s.renewal_date <= '$next60'
ORDER BY s.renewal_date ASC
");
?>

<div class="main-content">
<div class="col-lg-12">

<div class="topbar">
    <h5>Renewal Alerts</h5>
</div>

<div class="p-4">

<!-- ================= 0–30 DAYS ================= -->
<div class="card mb-4 shadow-sm">
<div class="card-header bg-danger text-white">
    Expiring in Next 30 Days
</div>

<div class="table-responsive">
<table class="table table-bordered table-striped">

<thead class="table-dark">
<tr>
<th>Customer</th>
<th>Service</th>
<th>Type</th>
<th>Amount</th>
<th>Renewal Date</th>
<th>Days Left</th>
<th>Action</th>
</tr>
</thead>

<tbody>

<?php if($renew30->num_rows > 0){ ?>
<?php while($row = $renew30->fetch_assoc()){ ?>

<?php
$days = floor((strtotime($row['renewal_date']) - strtotime($today)) / 86400);
?>

<tr>
<td><?= e($row['customer_name']) ?></td>
<td><?= e($row['service_name']) ?></td>
<td><?= ucfirst($row['type']) ?></td>
<td>₹<?= number_format($row['amount'],2) ?></td>
<td><?= $row['renewal_date'] ?></td>

<td class="<?= ($days <= 5) ? 'text-danger fw-bold' : 'text-warning' ?>">
    <?= $days ?> days
</td>

<td>
<a href="../actions/whatsapp_renewal.php?id=<?= $row['id'] ?>" 
class="btn btn-success btn-sm" target="_blank">
WhatsApp
</a>
</td>

</tr>

<?php } ?>
<?php } else { ?>
<tr><td colspan="7" class="text-center">No renewals</td></tr>
<?php } ?>

</tbody>
</table>
</div>
</div>

<!-- ================= 30–60 DAYS ================= -->
<div class="card shadow-sm">
<div class="card-header bg-warning">
    Expiring in 30–60 Days
</div>

<div class="table-responsive">
<table class="table table-bordered table-striped">

<thead class="table-dark">
<tr>
<th>Customer</th>
<th>Service</th>
<th>Type</th>
<th>Amount</th>
<th>Renewal Date</th>
<th>Days Left</th>
<th>Action</th>
</tr>
</thead>

<tbody>

<?php if($renew60->num_rows > 0){ ?>
<?php while($row = $renew60->fetch_assoc()){ ?>

<?php
$days = floor((strtotime($row['renewal_date']) - strtotime($today)) / 86400);
?>

<tr>
<td><?= e($row['customer_name']) ?></td>
<td><?= e($row['service_name']) ?></td>
<td><?= ucfirst($row['type']) ?></td>
<td>₹<?= number_format($row['amount'],2) ?></td>
<td><?= $row['renewal_date'] ?></td>

<td class="text-muted"><?= $days ?> days</td>

<td>
<a href="../actions/whatsapp_renewal.php?id=<?= $row['id'] ?>" 
class="btn btn-success btn-sm" target="_blank">
WhatsApp
</a>
</td>

</tr>

<?php } ?>
<?php } else { ?>
<tr><td colspan="7" class="text-center">No renewals</td></tr>
<?php } ?>

</tbody>
</table>
</div>
</div>

</div>
</div>
</div>

<?php require_once "../includes/footer.php"; ?>