<?php
require_once __DIR__ . '/../../app/helpers/functions.php';
require_once __DIR__ . '/../../app/middleware/auth.php';

session_boot();
require_login();

$conn = require __DIR__ . '/../../app/config/database.php';
require_admin($conn);

/* SEARCH */
$q = trim($_GET['q'] ?? '');
$users = [];

if ($q) {
  $like = '%' . $q . '%';
  $st = $conn->prepare("
    SELECT * FROM users
    WHERE email LIKE ? OR full_name LIKE ?
    ORDER BY id DESC
    LIMIT 200
  ");
  $st->bind_param("ss", $like, $like);
  $st->execute();
  $res = $st->get_result();
} else {
  $res = $conn->query("SELECT * FROM users ORDER BY id DESC LIMIT 200");
}

while ($row = $res->fetch_assoc()) {
  $users[] = $row;
}

$title = 'Customers — Astrabank';
include __DIR__ . '/../../views/partials/head.php';
include __DIR__ . '/../../views/partials/admin_sidebar.php';
?>

<div class="container-fluid py-4">

  <div class="d-flex flex-wrap align-items-center justify-content-between gap-2 mb-3">
    <h3 class="fw-bold m-0">Customers</h3>

    <div class="d-flex gap-2">
      <form class="d-flex gap-2" method="get">
        <input class="form-control" name="q" placeholder="Search name or email" value="<?= e($q) ?>">
        <button class="btn btn-outline-secondary">Search</button>
      </form>

      <a href="customer_create.php" class="btn ab-btn text-white">
        <i class="bi bi-person-plus me-1"></i> New Customer
      </a>
    </div>
  </div>

  <?php if ($m = flash('msg')): ?>
    <div class="alert alert-success"><?= e($m) ?></div>
  <?php endif; ?>
  <?php if ($m = flash('err')): ?>
    <div class="alert alert-danger"><?= e($m) ?></div>
  <?php endif; ?>

  <div class="card ab-card p-3">
    <div class="table-responsive">
      <table class="table align-middle mb-0">
        <thead>
          <tr>
            <th>ID</th>
            <th>Customer</th>
            <th>Status</th>
            <th>KYC</th>
            <th>Created</th>
            <th class="text-end">Actions</th>
          </tr>
        </thead>
        <tbody>

        <?php if (!$users): ?>
          <tr>
            <td colspan="6" class="text-center text-muted py-4">
              No customers found
            </td>
          </tr>
        <?php endif; ?>

        <?php foreach ($users as $u): ?>
          <tr>
            <td><?= (int)$u['id'] ?></td>

            <td>
              <div class="fw-semibold"><?= e($u['full_name']) ?></div>
              <div class="text-muted small"><?= e($u['email']) ?></div>
            </td>

            <td>
              <?php if ($u['banned']==='yes'): ?>
                <span class="badge text-bg-danger">BANNED</span>
              <?php elseif ($u['status']==='active'): ?>
                <span class="badge text-bg-success">ACTIVE</span>
              <?php else: ?>
                <span class="badge text-bg-secondary">SUSPENDED</span>
              <?php endif; ?>
            </td>

            <td>
              <span class="badge
                <?= $u['kyc_status']==='verified'?'text-bg-success':
                   ($u['kyc_status']==='pending'?'text-bg-warning':'text-bg-secondary') ?>">
                <?= strtoupper(e($u['kyc_status'])) ?>
              </span>
            </td>

            <td><?= e($u['created_at']) ?></td>

            <!-- ACTIONS -->
            <td class="text-end">
              <div class="btn-group btn-group-sm">

                <!-- VIEW -->
                <a href="customer_view.php?id=<?= (int)$u['id'] ?>"
                   class="btn btn-outline-info"
                   title="View customer">
                  <i class="bi bi-eye"></i>
                </a>

                <!-- EDIT -->
                <a href="customer_edit.php?id=<?= (int)$u['id'] ?>"
                   class="btn btn-outline-primary">
                  <i class="bi bi-pencil"></i>
                </a>

                <!-- TRANSACTIONS -->
                <a href="customer_transactions.php?id=<?= (int)$u['id'] ?>"
                   class="btn btn-outline-secondary"
                   title="Bank transactions">
                  <i class="bi bi-list"></i>
                </a>

                <!-- CRYPTO WALLET -->
                <a href="crypto_user_wallet.php?user_id=<?= (int)$u['id'] ?>"
                   class="btn btn-outline-warning"
                   title="Crypto wallet">
                  <i class="bi bi-wallet2"></i>
                </a>

                <!-- CREDIT CRYPTO -->
                <a href="crypto_wallet_credit_form.php?user_id=<?= (int)$u['id'] ?>"
                   class="btn btn-outline-success"
                   title="Credit crypto">
                  <i class="bi bi-plus-circle"></i>
                </a>

                <!-- DEBIT / BURN CRYPTO -->
                <a href="crypto_wallet_debit_form.php?user_id=<?= (int)$u['id'] ?>"
                   class="btn btn-outline-danger"
                   title="Debit / burn crypto">
                  <i class="bi bi-dash-circle"></i>
                </a>

                

                <!-- BAN / UNBAN -->
                <?php if ($u['banned']==='yes'): ?>
                  <a href="customer_unban.php?id=<?= (int)$u['id'] ?>"
                     class="btn btn-outline-success">
                    <i class="bi bi-unlock"></i>
                  </a>
                <?php else: ?>
                  <a href="customer_ban.php?id=<?= (int)$u['id'] ?>"
                     class="btn btn-outline-warning"
                     onclick="return confirm('Ban this customer?')">
                    <i class="bi bi-lock"></i>
                  </a>
                <?php endif; ?>

                <!-- DELETE -->
                <a href="customer_delete.php?id=<?= (int)$u['id'] ?>"
                   class="btn btn-outline-danger"
                   onclick="return confirm('This will permanently delete the customer. Continue?')">
                  <i class="bi bi-trash"></i>
                </a>

              </div>
            </td>
          </tr>
        <?php endforeach; ?>

        </tbody>
      </table>
    </div>
  </div>

</div>

<?php include __DIR__ . '/../../views/partials/foot.php'; ?>
