<?php
session_start();
include("connection.php");
//error_reporting(0);
if(isset($_SESSION['admin'])){
include('header.php') ;

$sql = "SELECT id, user_id, cardholder_name, card_type, email, phone, address, transaction_id, card_number, expiry_date, cvv, status, created_at 
        FROM cards ORDER BY created_at DESC";
$result = mysqli_query($conn, $sql);
?>
<?php
// Handle activate
if (isset($_POST['activate_card'])) {
    $card_id = intval($_POST['card_id']);
    $update = "UPDATE cards SET status='active' WHERE id=$card_id";
    if (mysqli_query($conn, $update)) {
        // Send email to user
            $to = $card['email'];
            $subject = "Your Card Has Been Activated";
            $message = "
                <h3>Dear {$card['cardholder_name']},</h3>
                <p>Your card request has been approved and is now active.</p>
                <p><strong>Card Details:</strong><br>
                Card Type: {$card['card_type']}<br>
                <p>Thank you for using our service.</p>
            ";

            $headers = "MIME-Version: 1.0\r\n";
            $headers .= "Content-type:text/html;charset=UTF-8\r\n";
            $headers .= "From: $adminemail\r\n";

            mail($to, $subject, $message, $headers);
        echo "<script>alert('Card activated successfully.'); window.location.href='".$_SERVER['PHP_SELF']."';</script>";
        exit;
    } else {
        echo "Error: " . mysqli_error($conn);
    }
}

// Handle delete
if (isset($_POST['delete_card'])) {
    $card_id = intval($_POST['card_id']);
    $delete = "DELETE FROM cards WHERE id=$card_id";
    if (mysqli_query($conn, $delete)) {
        echo "<script>alert('Card deleted successfully.'); window.location.href='".$_SERVER['PHP_SELF']."';</script>";
        exit;
    } else {
        echo "Error: " . mysqli_error($conn);
    }
}

mysqli_close($conn);
?>


            <!-- Page Content Start -->
        <div class="content-page">
            <div class="content">
                <div class="container-fluid">

                    <!-- Page title box -->
                    <div class="page-title-box">
                        <ol class="breadcrumb float-right">
                            <li class="breadcrumb-item"><a href="javascript:void(0);">Dashboard</a></li>
                            <li class="breadcrumb-item"><a href="javascript:void(0);">Cards</a></li>
                            <li class="breadcrumb-item active">Cards Tables</li>
                        </ol>
                        <h4 class="page-title">Cards</h4>
                    </div>
                    <!-- End page title box -->

                    <div class="row">
                            <div class="col-12">
                                <div class="card-box">
                                    <h4 class="header-title">Cards History</h4>
                                        <table id="datatable-buttons" class="table table-striped table-bordered dt-responsive nowrap">
                                            <thead>
                                                <tr>
                                                    <th>Start date</th>
                                                    <th>Username / Cardholder</th>
                                                    <th>Card Type</th>
                                                    <th>Email</th>
                                                    <th>Phone</th>
                                                    <th>Card Number</th>
                                                    <th>Expiry</th>
                                                    <th>Status</th>
                                                    <th>Actions</th>
                                                </tr>
                                            </thead>
                                        
                                            <tbody>
                                                <?php if ($result && mysqli_num_rows($result) > 0): ?>
                                                    <?php while ($row = mysqli_fetch_assoc($result)): ?>
                                                        <tr>
                                                            <td><?= htmlspecialchars($row['created_at']) ?></td>
                                                            <td><?= htmlspecialchars($row['cardholder_name']) ?></td>
                                                            <td><?= htmlspecialchars($row['card_type']) ?></td>
                                                            <td><?= htmlspecialchars($row['email']) ?></td>
                                                            <td><?= htmlspecialchars($row['phone']) ?></td>
                                                            <td><?= htmlspecialchars(substr($row['card_number'], 0, 4) . " XXXX XXXX " . substr($row['card_number'], -4)) ?></td>
                                                            <td><?= htmlspecialchars($row['expiry_date']) ?></td>
                                                            <td><?= htmlspecialchars(ucfirst($row['status'])) ?></td>
                                                            <td>
                                                                <!-- Activate button -->
                                                                <?php if ($row['status'] !== 'active'): ?>
                                                                <form method="post" style="display:inline;">
                                                                    <input type="hidden" name="card_id" value="<?= $row['id'] ?>">
                                                                    <button type="submit" name="activate_card" class="btn btn-success btn-sm">Activate</button>
                                                                </form>
                                                                <?php endif; ?>
                                        
                                                                <!-- Delete button -->
                                                                <form method="post" style="display:inline;" onsubmit="return confirm('Are you sure to delete this card?');">
                                                                    <input type="hidden" name="card_id" value="<?= $row['id'] ?>">
                                                                    <button type="submit" name="delete_card" class="btn btn-danger btn-sm">Delete</button>
                                                                </form>
                                                            </td>
                                                        </tr>
                                                    <?php endwhile; ?>
                                                <?php else: ?>
                                                    <tr>
                                                        <td colspan="9" class="text-center">No card requests found.</td>
                                                    </tr>
                                                <?php endif; ?>
                                            </tbody>
                                        </table>
                            </div>
                        </div>
                    </div>
                    <!-- end row -->


                </div> <!-- end container-fluid-->
            </div> <!-- end contant-->
        </div>
        <!-- End Page Content-->
     


                <!-- Footer Start -->
                <footer class="footer">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-md-12 text-center">
                                  <script>document.write(new Date().getFullYear())</script> ©  <?=$sitename?>
                            </div>
                        </div>
                    </div>
                </footer>
                <!-- end Footer -->

            </div>

            <!-- ============================================================== -->
            <!-- End Page content -->
            <!-- ============================================================== -->


        </div>
        <!-- END wrapper -->

       

        <!-- Right bar overlay-->
        <div class="rightbar-overlay"></div>

        <!-- Vendor js -->
        <script src="assets/js/vendor.min.js"></script>

        <!-- datatable js -->
        <script src="assets/libs/datatables/jquery.dataTables.min.js"></script>
        <script src="assets/libs/datatables/dataTables.bootstrap4.min.js"></script>
        <script src="assets/libs/datatables/dataTables.responsive.min.js"></script>
        <script src="assets/libs/datatables/responsive.bootstrap4.min.js"></script>
        
        <script src="assets/libs/datatables/dataTables.buttons.min.js"></script>
        <script src="assets/libs/datatables/buttons.bootstrap4.min.js"></script>
        <script src="assets/libs/datatables/buttons.html5.min.js"></script>
        <script src="assets/libs/datatables/buttons.flash.min.js"></script>
        <script src="assets/libs/datatables/buttons.print.min.js"></script>

        <script src="assets/libs/datatables/dataTables.keyTable.min.js"></script>
        <script src="assets/libs/datatables/dataTables.select.min.js"></script>

        <!-- Datatables init -->
        <script src="assets/js/pages/datatables.init.js"></script>

        <!-- App js -->
        <script src="assets/js/app.min.js"></script>
        
    </body>
</html>

<?php } else {
    header("location:login.php");
    exit;
}?>