<?php
//Developer: core tech web designTechnology
//Whatsapp: +2349162671360
//Telegram: @jeditech
include ('include/header.php');
$page_title = WEB_NAME . " | Banned Users";

// Fetch banned users (status = 2)
try {
    $stmt = $pdo->prepare("SELECT * FROM users WHERE status = 2 ORDER BY id DESC");
    $stmt->execute();
    $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $users = [];
    error_log("Error fetching banned users: " . $e->getMessage());
}
?>
  <title>Banned Users - <?php echo $site_data['web_name']; ?></title>
  
  <link href="vendor/datatables/dataTables.bootstrap4.min.css" rel="stylesheet">
             <!-- Container Fluid-->
            <div class="container-fluid" id="container-wrapper">
              <div class="d-sm-flex align-items-center justify-content-between mb-4">
                <h1 class="h3 mb-0 text-gray-800">Banned Users</h1>
                <ol class="breadcrumb">
                  <li class="breadcrumb-item"><a href="dashboard.php">Home</a></li>
                  <li class="breadcrumb-item"><a href="all_users.php">All Users</a></li>
                  <li class="breadcrumb-item active" aria-current="page">Banned Users</li>
                </ol>
              </div>

              <!-- Row -->
              <div class="row">
                <div class="col-lg-12">
                  <div class="card mb-4">
                    <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                      <h6 class="m-0 font-weight-bold text-danger">Banned Users</h6>
                      <a href="all_users.php" class="btn btn-sm btn-primary">
                        <i class="fas fa-arrow-left"></i> Back to All Users
                      </a>
                    </div>
                    <div class="table-responsive p-3">
                      <table class="table align-items-center table-flush table-hover" id="dataTable">
                        <thead class="thead-light">
                          <tr>
                            <th>#</th>
                            <th>Username</th>
                            <th>Email</th>
                            <th>Main Balance</th>
                            <th>Referral Balance</th>
                            <th>Register Date</th>
                            <th>Status</th>
                            <th>Actions</th>
                          </tr>
                        </thead>
                        <tbody>
                          <?php
                          $i = 1;
                          foreach ($users as $data) {
                            $user_id = $data['id'];
                            $is_admin = ($data['is_admin'] == 1);
                            ?>
                            <tr>
                              <td><?php echo $i; ?></td>
                              <td>
                                <strong><?php echo htmlspecialchars($data['username']); ?></strong>
                                <?php if($is_admin): ?>
                                  <span class="badge badge-primary ml-1">Admin</span>
                                <?php endif; ?>
                              </td>
                              <td><?php echo htmlspecialchars($data['email']); ?></td>
                              <td><strong>NGN<?php echo number_format($data['main_balance'], 2); ?></strong></td>
                              <td>NGN<?php echo number_format($data['referral_balance'], 2); ?></td>
                              <td>
                                <small><?php echo date('M d, Y', strtotime($data['created_at'])); ?></small>
                              </td>
                              <td><span class="badge badge-danger">Banned</span></td>
                              <td>
                                <button type="button" 
                                        class="btn btn-sm btn-success" 
                                        onclick="unbanUser(<?php echo $user_id; ?>, '<?php echo htmlspecialchars($data['username'], ENT_QUOTES); ?>')"
                                        title="Unban User">
                                  <i class="fas fa-check-circle"></i> Unban
                                </button>
                                <button type="button" 
                                        class="btn btn-sm btn-danger" 
                                        onclick="deleteUser(<?php echo $user_id; ?>, '<?php echo htmlspecialchars($data['username'], ENT_QUOTES); ?>', '<?php echo htmlspecialchars($data['email'], ENT_QUOTES); ?>')"
                                        title="Delete User">
                                  <i class="fas fa-trash"></i> Delete
                                </button>
                              </td>
                            </tr>
                            <?php
                            $i++;
                          }
                          ?>
                        </tbody>
                      </table>
                    </div>
                  </div>
                </div>
              </div>
              
          <script src="vendor/datatables/jquery.dataTables.min.js"></script>
          <script src="vendor/datatables/dataTables.bootstrap4.min.js"></script>
          <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
          
          <script>
            $(document).ready(function () {
              $('#dataTable').DataTable({
                ordering: true,
                pageLength: 25
              });
            });
            
            // Unban user function
            function unbanUser(userId, userName) {
              Swal.fire({
                title: 'Unban User?',
                html: `Restore full access for <strong>${userName}</strong>?`,
                icon: 'question',
                showCancelButton: true,
                confirmButtonColor: '#1cc88a',
                cancelButtonColor: '#858796',
                confirmButtonText: 'Yes, Unban User'
              }).then((result) => {
                if (result.isConfirmed) {
                  performAction('ajax/unban_user.php', userId);
                }
              });
            }
            
            // Delete user function
            function deleteUser(userId, userName, userEmail) {
              Swal.fire({
                title: 'Delete User Account?',
                html: `
                  <div style="text-align: left;">
                    <p><strong>Warning:</strong> This will permanently delete:</p>
                    <ul style="text-align: left; margin-left: 20px;">
                      <li>User: <strong>${userName}</strong></li>
                      <li>Email: <strong>${userEmail}</strong></li>
                      <li>All user data</li>
                    </ul>
                    <p style="color: #e74a3b;"><strong>This cannot be undone!</strong></p>
                  </div>
                `,
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#e74a3b',
                cancelButtonColor: '#858796',
                confirmButtonText: 'Yes, Delete',
                focusCancel: true
              }).then((result) => {
                if (result.isConfirmed) {
                  performAction('ajax/delete_user.php', userId);
                }
              });
            }
            
            // Generic action performer
            function performAction(url, userId) {
              Swal.fire({
                title: 'Processing...',
                allowOutsideClick: false,
                didOpen: () => { Swal.showLoading(); }
              });
              
              $.ajax({
                url: url,
                type: 'POST',
                data: { user_id: userId },
                dataType: 'json',
                success: function(data) {
                  Swal.close();
                  if(data.status == '1') {
                    Swal.fire({
                      title: 'Success!',
                      text: data.msg,
                      icon: 'success',
                      confirmButtonColor: '#1cc88a'
                    }).then(() => { location.reload(); });
                  } else {
                    Swal.fire('Error!', data.msg, 'error');
                  }
                },
                error: function() {
                  Swal.fire('Error!', 'An error occurred. Please try again.', 'error');
                }
              });
            }
          </script>

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