<?php
include('header.php');
mysqli_set_charset($conn, "utf8");

// ✅ Admin check
if (!isset($_SESSION['userid']) || $_SESSION['userid'] != "ADMIN") {
    echo "<script>window.location.href='index.php';</script>";
    exit;
}

if (isset($_POST['delete']) && $_POST['delete'] === 'ahkwebsolutions') {
    $hospital = base64_decode($_POST['hospital']);
    $hospital_safe = mysqli_real_escape_string($conn, $hospital);

    // अब सभी records delete होंगे उस hospital के लिए
    $deleteQuery = "DELETE FROM `death` WHERE hospital = '$hospital_safe'";

    if (mysqli_query($conn, $deleteQuery)) {
        $deleted = mysqli_affected_rows($conn);
        echo "<script>
                alert('$deleted record(s) deleted successfully for hospital: $hospital_safe');
                window.location.href='".$_SERVER['PHP_SELF']."';
              </script>";
        exit;
    } else {
        echo "<script>alert('Failed to delete records');</script>";
    }
}



// ✅ Pagination setup
$limit = 50; // per page records
$page  = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $limit;

// ✅ Total records count
$countRes = mysqli_query($conn, "SELECT COUNT(*) AS total FROM death");
$totalRow = mysqli_fetch_assoc($countRes);
$total    = $totalRow['total'];
$pages    = ceil($total / $limit);

// ✅ Fetch records for current page
$res = mysqli_query($conn, "SELECT * FROM `death` ORDER BY regdatelocal DESC LIMIT $start, $limit");
?>

<div class="container mt-4">
    <h3 class="mb-3 text-center">death Records (Admin Only)</h3>
    <div class="table-responsive">
        <table class="table table-bordered table-hover shadow-sm bg-white">
        <thead class="thead-dark">
            <tr>
                <th>#</th>
                <th>State</th>
                 <th>Hospital Name</th>
                <th>Hospital Local</th>
               
              
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
        <?php
        if (mysqli_num_rows($res) > 0) {
            $i = $start + 1; // pagination के हिसाब से numbering सही होगी
            while ($row = mysqli_fetch_assoc($res)) {
                ?>
                <tr>
                    <td><?php echo $i++; ?></td>
                    <td><?php echo htmlspecialchars($row['state']); ?></td>
                   
                    <td><?php echo htmlspecialchars($row['hospitalname']); ?></td>
                     <td><?php echo htmlspecialchars($row['hospitalnamelocal']); ?></td>
                   
                    <td>
                    <form method="POST" onsubmit="return confirm('Are you sure you want to delete records for this hospital?');">
    <input type="hidden" name="hospital" value="<?php echo base64_encode($row['hospital']); ?>">
    <input type="hidden" name="delete" value="ahkwebsolutions">
    <button type="submit" class="btn btn-sm btn-danger">
        <i class="bi bi-trash"></i> Delete
    </button>
</form>

                    </td>
                </tr>
                <?php
            }
        } else {
            echo "<tr><td colspan='10' class='text-center'>No Records Found</td></tr>";
        }
        ?>
        </tbody>
    </table>

    <!-- ✅ Pagination अब table के नीचे center में -->
<div class="mt-3 d-flex justify-content-center">
    <nav aria-label="Page navigation">
        <ul class="pagination">
            <?php for ($i = 1; $i <= $pages; $i++): ?>
                <li class="page-item <?php if($i == $page) echo 'active'; ?>">
                    <a class="page-link" href="?page=<?php echo $i; ?>"><?php echo $i; ?></a>
                </li>
            <?php endfor; ?>
        </ul>
    </nav>
</div>



<!-- ✅ Custom CSS -->
<style>
.container {
    max-width: 100% !important;  /* container को full width कर दो */
    padding: 0 10px;             /* थोड़ा side gap safe रखने के लिए */
}

.table {
    width: 100% !important;      /* table full width */
    table-layout: auto;          /* content के हिसाब से adjust */
    margin: 0 auto;              /* center में रखे */
}

.table td, .table th {
    white-space: nowrap;         /* unnecessary wrapping रोकने के लिए */
}

.table {
        background: #fff;
        border-radius: 8px;
        overflow: hidden;
    }
    .table thead {
        background: #343a40;
        color: #fff;
    }
    .table-hover tbody tr:hover {
        background-color: #f1f1f1;
        transition: 0.3s;
    }
    .btn-danger {
        border-radius: 5px;
        padding: 4px 10px;
        font-size: 14px;
    }

    .pagination {
    display: flex;
    justify-content: center;  /* center में लाएगा */
    list-style: none;
    padding: 0;
    margin: 20px 0;
}
.pagination li {
    margin: 0 5px;
}
.pagination a {
    display: block;
    padding: 6px 12px;
    border: 1px solid #ccc;
    color: #007bff;
    text-decoration: none;
    border-radius: 4px;
}
.pagination .active a {
    background: #007bff;
    color: #fff;
    border-color: #007bff;
}

</style>

<!-- ✅ Bootstrap Icons (for trash icon) -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">

