<?php
include('con.php');

$msg = '';

/* ================= UPDATE ENTRY ================= */
if (isset($_POST['update_entry'])) {

    $id     = intval($_POST['history_id']);
    $panno  = strtoupper(trim($_POST['panno']));
    $status = intval($_POST['status']);

    if ($panno == '') {

        $msg = "PAN number cannot be empty.";

    } else {

        $panno_safe = mysqli_real_escape_string($ahk_conn, $panno);

        mysqli_query(
            $ahk_conn,
            "UPDATE aaa2p 
             SET panno='$panno_safe', status='$status' 
             WHERE id='$id'"
        );

        $msg = "Record updated successfully!";
    }
}

/* ================= FETCH DATA ================= */
$result = mysqli_query(
    $ahk_conn,
    "SELECT * FROM aaa2p 
     WHERE status != 1 
     ORDER BY id DESC"
);
?>

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="UTF-8">

<title>PAN Update History</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<style>

.badge-status{
    padding:5px 12px;
    border-radius:50px;
    font-size:0.85rem;
    font-weight:500;
}

.badge-pending{
    background:#ffc107;
    color:#000;
}

.badge-approved{
    background:#198754;
    color:#fff;
}

.badge-rejected{
    background:#dc3545;
    color:#fff;
}

</style>

</head>

<body class="bg-light">

<div class="container mt-5">

<h4 class="text-center mb-4 text-primary">
Aadhaar To PAN Update History
</h4>

<?php if($msg): ?>

<script>
Swal.fire({
    icon:'success',
    title:'Notice',
    text:<?= json_encode($msg); ?>
});
</script>

<?php endif; ?>

<div class="card shadow">

<div class="card-body">

<div class="table-responsive">

<table class="table table-bordered table-striped text-center align-middle">

<thead class="table-dark">

<tr>
    <th>ID</th>
    <th>Name</th>
    <th>Username</th>
    <th>Father Name</th>
    <th>Aadhaar No</th>
    <th>DOB</th>
    <th>PAN No</th>
    <th>Status</th>
    <th>Date</th>
    <th>Update PAN & Status</th>
</tr>

</thead>

<tbody>

<?php if(mysqli_num_rows($result) > 0): ?>

<?php while($row = mysqli_fetch_assoc($result)): ?>

<tr>

<td><?= $row['id']; ?></td>

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

<td><?= htmlspecialchars($row['username']); ?></td>
<td><?= htmlspecialchars($row['fathername']); ?></td>

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

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

<td>

<?php if(!empty($row['panno'])): ?>

<span class="badge bg-success">
<?= htmlspecialchars($row['panno']); ?>
</span>

<?php else: ?>

<span class="badge bg-secondary">
Not Available
</span>

<?php endif; ?>

</td>

<td>

<?php

$status_badge = [
    '0' => 'badge-pending',
    '1' => 'badge-approved',
    '2' => 'badge-rejected'
];

$status_text = [
    '0' => 'Pending',
    '1' => 'Approved',
    '2' => 'Rejected'
];

$status_class = isset($status_badge[$row['status']])
    ? $status_badge[$row['status']]
    : 'bg-secondary';

$status_label = isset($status_text[$row['status']])
    ? $status_text[$row['status']]
    : 'Unknown';

?>

<span class="badge badge-status <?= $status_class; ?>">
<?= $status_label; ?>
</span>

</td>

<td><?= $row['date_time']; ?></td>

<td>

<form method="POST" class="d-flex gap-1">

<input type="text"
name="panno"
value="<?= htmlspecialchars($row['panno']); ?>"
placeholder="Enter PAN No"
class="form-control form-control-sm"
style="min-width:140px;"
required>

<select name="status"
class="form-select form-select-sm">

<option value="0"
<?= $row['status']==0 ? 'selected' : ''; ?>>
Pending
</option>

<option value="1"
<?= $row['status']==1 ? 'selected' : ''; ?>>
Approved
</option>

<option value="2"
<?= $row['status']==2 ? 'selected' : ''; ?>>
Rejected
</option>

</select>

<input type="hidden"
name="history_id"
value="<?= $row['id']; ?>">

<button type="submit"
name="update_entry"
class="btn btn-sm btn-primary">

Update

</button>

</form>

</td>

</tr>

<?php endwhile; ?>

<?php else: ?>

<tr>
<td colspan="9" class="text-muted">
No records found.
</td>
</tr>

<?php endif; ?>

</tbody>

</table>

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

</div>

</body>
</html>