<?php
include('../includes/session.php');
include('../includes/config.php');
include('../template/ahkweb/header.php');

$appliedby = $udata['phone'];

if ($udata['type'] == 'admin') {
    $res = mysqli_query($ahk_conn, "SELECT name, mobile, email, gender, application_type, application_mode, phone, p_order_id, status, ack_no, date FROM nsdl_requests ORDER BY id DESC");
} else {
    $res = mysqli_query($ahk_conn, "SELECT name, mobile, email, gender, application_type, application_mode, phone, p_order_id, status, ack_no, date FROM nsdl_requests WHERE phone='$appliedby' ORDER BY id DESC");
}
?>
<div class="page-wrapper">
<div class="page-content">
    <div class="card radius-10 shadow-lg border-0">
        <div class="card-body">
            <h4 class="mb-3 text-primary fw-bold">
                <i class="bi bi-file-earmark-text"></i> NSDL Application History
            </h4>
            <hr>
            <div class="table-responsive">
                <table id="example2" class="table table-striped table-bordered align-middle mb-0" style="width:100%">
                    <thead class="table-primary">
                        <tr>
                            <th>#</th>
                            <th>Name</th>
                            <th>Mobile</th>
                            <th>Email</th>
                            <th>Gender</th>
                            <th>Application </th>
                            <th> Order ID</th>
                            <th>Status</th>
                            <th>Ack No</th>
                            <th>Date</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $x = 0;
                        while($data = mysqli_fetch_assoc($res)){
                            $x++;
                            // status badge
                            $statusBadge = "<span class='badge bg-secondary'>{$data['status']}</span>";
                            if ($data['status'] == 'SUCCESS') {
                                $statusBadge = "<span class='badge bg-success'>Success</span>";
                            } elseif ($data['status'] == 'Pending') {
                                $statusBadge = "<span class='badge bg-warning text-dark'>Pending</span>";
                            } elseif ($data['status'] == 'REFUNDED') {
                                $statusBadge = "<span class='badge bg-danger'>Refunded</span>";
                            }

                            echo "<tr>
                                <td>{$x}</td>
                                <td>{$data['name']}</td>
                                <td>{$data['mobile']}</td>
                                <td>{$data['email']}</td>
                                <td>{$data['gender']}</td>
                                <td>{$data['application_type']} <span class='text-muted'>({$data['application_mode']})</span></td>
                                <td>{$data['p_order_id']}</td>
                                <td>{$statusBadge}</td>
                                <td>{$data['ack_no']}</td>
                                <td>".date("d-M-Y h:i A", strtotime($data['date']))."</td>
                            </tr>";
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
</div>

<?php 
include('../template/ahkweb/footer.php');
?>

<!-- Bootstrap & Plugins JS -->
<script src="../template/ahkweb/assets/js/bootstrap.bundle.min.js"></script>
<script src="../template/ahkweb/assets/js/jquery.min.js"></script>
<script src="../template/ahkweb/assets/plugins/simplebar/js/simplebar.min.js"></script>
<script src="../template/ahkweb/assets/plugins/metismenu/js/metisMenu.min.js"></script>
<script src="../template/ahkweb/assets/plugins/perfect-scrollbar/js/perfect-scrollbar.js"></script>
<script src="../template/ahkweb/assets/js/index.js"></script>
<script src="../template/ahkweb/assets/js/app.js"></script>

<!-- Datatables -->
<script src="../template/ahkweb/assets/plugins/datatable/js/jquery.dataTables.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/dataTables.bootstrap5.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/dataTables.buttons.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/buttons.bootstrap5.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/jszip.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/pdfmake.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/vfs_fonts.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/buttons.html5.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/buttons.print.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/buttons.colVis.min.js"></script>

<script>
$(document).ready(function() {
    var table = $('#example2').DataTable({
        lengthChange: true,
        pageLength: 10,
        order: [[0, "desc"]],
        buttons: [ 
            { extend: 'copy', className: 'btn btn-sm btn-secondary' },
            { extend: 'excel', className: 'btn btn-sm btn-success' },
            { extend: 'pdf', className: 'btn btn-sm btn-danger' },
            { extend: 'print', className: 'btn btn-sm btn-primary' },
            { extend: 'colvis', className: 'btn btn-sm btn-warning' }
        ]
    });

    table.buttons().container()
        .appendTo('#example2_wrapper .col-md-6:eq(0)');
});
</script>
