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

// ===== FETCH (SECURE) =====
$stmt = $conn->prepare("SELECT * FROM dmanual WHERE username=? ORDER BY id DESC");
$stmt->bind_param("s", $s_phone);
$stmt->execute();
$res = $stmt->get_result();

// ===== DELETE =====
if(isset($_POST['delete']) && $_POST['delete']=="ahkwebsolutions"){
    $id = base64_decode($_POST['id']);

    $stmt = $conn->prepare("DELETE FROM dmanual WHERE id=?");
    $stmt->bind_param("i",$id);

    if($stmt->execute()){
        echo "<script>Swal.fire('Success','Deleted Successfully','success')</script>";
    } else {
        echo "<script>Swal.fire('Error','Delete Failed','error')</script>";
    }
}
?>

<!-- ===== LUXURY CSS FIX ===== -->
<style>
#example2{
    width:100%;
    border-collapse:separate;
    border-spacing:0;
    background:#fff;
    border-radius:14px;
    overflow:hidden;
    box-shadow:0 12px 30px rgba(0,0,0,.15);
}

#example2 thead th{
    background:linear-gradient(135deg,#0f2027,#203a43,#2c5364);
    color:#fff;
    text-align:center;
    padding:12px;
}

#example2 tbody tr{background:#f8faff;}
#example2 tbody tr:nth-child(even){background:#eef3ff;}
#example2 tbody tr:hover{background:#dbe6ff;}

#example2 td{
    padding:10px;
    border-top:1px solid #dbe2ff;
    text-align:center;
    font-size:13px;
}

#example2 td:nth-child(2){
    text-align:left;
}

/* IMPORTANT FIX */
#example2 td:nth-child(4){
    white-space:nowrap;
}

/* BUTTON */
#example2 .btn{
    border-radius:20px;
    padding:5px 10px;
    font-size:12px;
    margin:2px;
}

.btn-primary{background:linear-gradient(135deg,#396afc,#2948ff);color:#fff;}
.btn-success{background:linear-gradient(135deg,#11998e,#38ef7d);color:#fff;}
.btn-danger{background:linear-gradient(135deg,#ff416c,#ff4b2b);color:#fff;}
.btn-dark{background:linear-gradient(135deg,#232526,#414345);color:#fff;}
</style>

<div class="card">
<div class="payment-form">

<table id="example2" class="table table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>User</th>
<th>Action</th>
<th>Delete</th>
</tr>
</thead>

<tbody>

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

$id = base64_encode($row['id']);
$secureId = urlencode($id)."iJS7JY2FTHajLHr6KJtx9EI%3D";
?>

<tr>

<td><?= htmlspecialchars($row['id']) ?></td>

<td><?= htmlspecialchars($row['name']) ?></td>

<td><?= htmlspecialchars($_SESSION['name']) ?></td>

<td>
 <a target="_blank" href="deathCertificatePdf/delhi-print?id=<?= $secureId ?>" class="btn btn-primary">PDF</a>   
    
<a target="_blank" href="deathCertificatePdf/print?id=<?= $secureId ?>" class="btn btn-primary">PDF</a>

<a target="_blank" href="verifyCertificote?id=<?= $secureId ?>" class="btn btn-dark">View</a>

<a href="deathedit?id=<?= $secureId ?>" class="btn btn-success">Edit</a>
</td>

<td>
<form method="post">
<input type="hidden" name="id" value="<?= $id ?>">
<input type="hidden" name="delete" value="ahkwebsolutions">
<button type="button" class="btn btn-danger" onclick="deleteRecord(this)">Delete</button>
</form>
</td>

</tr>

<?php
}
}
?>

</tbody>
</table>

</div>
</div>

<!-- ===== JS ===== -->
<script src="plugins/jquery/jquery.min.js"></script>
<script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script>

<script src="plugins/datatables/jquery.dataTables.min.js"></script>
<script src="plugins/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/8.11.8/sweetalert2.all.min.js"></script>

<script>
$(function(){
    $('#example2').DataTable({
        responsive:true
    });
});

function deleteRecord(btn){
    Swal.fire({
        title:'Delete?',
        icon:'warning',
        showCancelButton:true
    }).then((res)=>{
        if(res.value){
            btn.closest('form').submit();
        }
    });
}
</script>

<?php include('userfooter.php'); ?>