<?php include '../db.php'; ?>

<h2>Hosting List</h2>
<a href="add.php">+ Add Hosting</a> | 
<a href="index.php?filter=expired">Expired Hostings</a> | 
<a href="index.php">All Hostings</a>
<br><br>

<table border="1" cellpadding="5">
<tr>
    <th>Client</th><th>Plan</th><th>Provider</th><th>Start Date</th><th>Expiry Date</th><th>Status</th><th>Actions</th>
</tr>

<?php
$filter = $_GET['filter'] ?? '';
$where = "";

if ($filter == 'expired') {
    $today = date('Y-m-d');
    $where = "WHERE expiry_date < '$today'";
}

$stmt = $conn->query("
    SELECT h.*, c.name as client_name 
    FROM hostings h 
    JOIN clients c ON h.client_id = c.id 
    $where 
    ORDER BY expiry_date ASC
");

foreach ($stmt->fetchAll() as $h) {
    echo "<tr>
        <td>{$h['client_name']}</td>
        <td>{$h['plan']}</td>
        <td>{$h['provider']}</td>
        <td>{$h['start_date']}</td>
        <td>{$h['expiry_date']}</td>
        <td>{$h['status']}</td>
        <td>
            <a href='edit.php?id={$h['id']}'>Edit</a> |
            <a href='delete.php?id={$h['id']}' onclick=\"return confirm('Delete this hosting?')\">Delete</a> |
            <a href='print.php?id={$h['id']}'>Print</a>
        </td>
    </tr>";
}
?>
</table>
