<?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>
/* ===================== FULL WIDTH CONTAINER ===================== */
.container{
    max-width:100%!important;
    padding:0 15px;
}

/* ===================== LUXURY TABLE ===================== */
.table{
    width:100%;
    background:#ffffff;
    border-radius:14px;
    overflow:hidden;
    box-shadow:0 15px 35px rgba(0,0,0,.15);
    border:none;
}

/* ===================== TABLE HEADER ===================== */
.table thead{
    background:linear-gradient(135deg,#1e3c72,#2a5298);
}

.table thead th{
    color:#fff;
    font-weight:700;
    text-align:center;
    padding:14px;
    border:none;
    white-space:nowrap;
}

/* ===================== TABLE BODY ===================== */
.table tbody td{
    text-align:center;
    padding:12px;
    vertical-align:middle;
    border-top:1px solid #eef1ff;
    font-size:14px;
    color:#2c3e50;
    white-space:nowrap;
}

/* ===================== ROW HOVER ===================== */
.table-hover tbody tr{
    transition:all .25s ease;
}
.table-hover tbody tr:hover{
    background:#f3f6ff;
    transform:scale(1.01);
}

/* ===================== DELETE BUTTON ===================== */
.table .btn-danger{
    background:linear-gradient(135deg,#ff416c,#ff4b2b);
    border:none;
    border-radius:20px;
    padding:6px 14px;
    font-size:13px;
    font-weight:600;
    box-shadow:0 5px 15px rgba(255,75,43,.4);
    transition:.25s;
}

.table .btn-danger:hover{
    transform:translateY(-2px);
    box-shadow:0 8px 22px rgba(255,75,43,.6);
}

/* ===================== PAGINATION ===================== */
.pagination{
    display:flex;
    justify-content:center;
    margin-top:20px;
}

.pagination .page-item{
    margin:0 4px;
}

.pagination .page-link{
    border-radius:20px;
    border:none;
    padding:8px 14px;
    font-weight:600;
    color:#396afc;
    background:#eef1ff;
    transition:.25s;
}

.pagination .page-link:hover{
    background:#396afc;
    color:#fff;
}

.pagination .active .page-link{
    background:linear-gradient(135deg,#11998e,#38ef7d);
    color:#fff;
}

/* ===================== RESPONSIVE ===================== */
@media(max-width:768px){
    .table thead{
        font-size:13px;
    }
    .table tbody td{
        font-size:12px;
    }
}

</style>

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

