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

$msg = '';

// Handle UID or Status update
if (isset($_POST['update_entry'])) {
    $id = intval($_POST['history_id']);
    $uid = trim($_POST['uid']);
    $status = intval($_POST['status']);

    if ($uid === '') {
        $msg = "UID cannot be empty.";
    } else {
        $uid_safe = mysqli_real_escape_string($ahk_conn, $uid);
        mysqli_query($ahk_conn, "UPDATE eid_history SET uid='$uid_safe', status='$status' WHERE id='$id'");
        $msg = "Entry updated successfully!";
    }
}

// Fetch EID history where status is not completed
$result = mysqli_query($ahk_conn, "SELECT * FROM eid_history WHERE status != 2 ORDER BY id DESC");
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>EID History Management</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: #0d6efd; color: #fff; }
.badge-process { background: #ffc107; color: #212529; }
</style>
</head>
<body>
<div class="container mt-5">
    <h4 class="text-center mb-4 text-primary">EID Update History</h4>

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

    <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>EID No</th>
                     <th>DOB</th>
                      <th>Pin Code</th>
                    <th>Date</th>
                    <th>Status</th>
                    <th>UID</th>
                    <th>Update UID & 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['eid_no']) ?></td>
                        <td><?= htmlspecialchars($row['birth_date']) ?></td>
                        <td><?= htmlspecialchars($row['pin_code']) ?></td>
                        <td><?= htmlspecialchars($row['date']) ?></td>
                        <td>
                            <?php
                            $status_badge = ['0'=>'badge-pending','1'=>'badge-process'];
                            $status_text = ['0'=>'Pending','1'=>'In Process'];
                            $status_class = isset($status_badge[$row['status']]) ? $status_badge[$row['status']] : 'badge-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>
                            <?php if (!empty($row['uid'])): ?>
                                <span class="badge bg-success"><?= htmlspecialchars($row['uid']) ?></span>
                            <?php else: ?>
                                <span class="badge bg-secondary">Not Available</span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <form method="POST" class="d-flex gap-1">
                                <input type="text" name="uid" value="<?= htmlspecialchars($row['uid']) ?>" placeholder="Enter UID" class="form-control form-control-sm" required>
                                <select name="status" class="form-select form-select-sm">
                                    <option value="1" <?= $row['status']==1?'selected':''; ?>>Pending</option>
                                    <option value="2" <?= $row['status']==2?'selected':''; ?>>In Process</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="7" class="text-muted">No pending or in-process EID records found.</td>
                </tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>
