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

if (!isset($_SESSION['username'])) {
    header("Location: login.php");
    exit;
}

$username = $_SESSION['username'];

// Handle Search Query
$search_query = "";
if (isset($_POST['search_btn']) && !empty($_POST['search_val'])) {
    $val = mysqli_real_escape_string($ahk_conn, $_POST['search_val']);
    $search_query = " AND (aadhaar LIKE '%$val%' OR pan LIKE '%$val%') ";
}

// Fetch Records
$sql = "SELECT * FROM a2p_verify_hkb WHERE username = '$username' $search_query ORDER BY id DESC";
$result = mysqli_query($ahk_conn, $sql);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>PAN Find History</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        .section-header {
            font-size: 14px;
            font-weight: 800;
            color: #3182ce;
            text-transform: uppercase;
            letter-spacing: 1.5px;
            margin: 20px auto;
            display: table;
            background: #ebf8ff;
            padding: 8px 20px;
            border-radius: 50px;
        }
        .table-card {
            border-radius: 15px;
            overflow: hidden;
            border: none;
            box-shadow: 0 4px 12px rgba(0,0,0,0.05);
        }
        .thead-custom {
            background-color: #f8f9fa;
            border-bottom: 2px solid #dee2e6;
        }
        .pan-badge {
            background-color: #e3f2fd;
            color: #0d47a1;
            font-weight: bold;
            padding: 4px 8px;
            border-radius: 4px;
            font-family: 'Courier New', monospace;
        }
    </style>
</head>
<body class="bg-light">

<div class="container-fluid mt-4">
    <span class="section-header">PAN Transaction History</span>

    <div class="row mb-4 justify-content-center">
        <div class="col-md-6">
            <form method="POST" class="input-group">
                <input type="text" name="search_val" class="form-control" placeholder="Search by Aadhaar or PAN Number..." value="<?= isset($_POST['search_val']) ? $_POST['search_val'] : '' ?>">
                <button class="btn btn-primary" name="search_btn" type="submit">
                    <i class="fas fa-search"></i> Search
                </button>
                <a href="history_pan.php" class="btn btn-outline-secondary">Reset</a>
            </form>
        </div>
    </div>

    <div class="card table-card">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="thead-custom">
                    <tr>
                        <th class="ps-4">ID</th>
                        <th>Aadhaar Number</th>
                        <th>PAN Number</th>
                        <th>Application No</th>
                        <th>Date & Time</th>
                        <th class="text-center">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(mysqli_num_rows($result) > 0): ?>
                        <?php while($row = mysqli_fetch_assoc($result)): ?>
                        <tr>
                            <td class="ps-4 text-muted">#<?= $row['id'] ?></td>
                            <td><i class="fas fa-fingerprint text-secondary me-2"></i> <?= $row['aadhaar'] ?></td>
                            <td><span class="pan-badge text-uppercase"><?= $row['pan'] ?></span></td>
                            <td class="small text-muted"><?= $row['application_no'] ?></td>
                            <td><?= date('d M Y, h:i A', strtotime($row['date'])) ?></td>
                            <td class="text-center">
                                <button class="btn btn-sm btn-light border" onclick="copyToClipboard('<?= $row['pan'] ?>')">
                                    <i class="fas fa-copy text-primary"></i> Copy
                                </button>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="6" class="text-center py-5 text-muted">
                                <i class="fas fa-folder-open fa-3x mb-3 d-block"></i>
                                No records found for your account.
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<script>
function copyToClipboard(text) {
    navigator.clipboard.writeText(text).then(function() {
        alert('PAN Number ' + text + ' copied to clipboard!');
    }, function(err) {
        console.error('Could not copy text: ', err);
    });
}
</script>

<?php include('footer.php'); ?>
</body>
</html>