<?php
require_once __DIR__ . '/../../config/config.php';
require_once __DIR__ . '/../../includes/auth.php';

requireAdmin();
$db = getDBConnection();

// Toggle status (Active / Suspended)
if (isset($_GET['action']) && $_GET['action'] === 'toggle_status') {
    $id = (int)$_GET['id'];
    $current = sanitize($_GET['current']);
    $newStatus = ($current === 'active') ? 'suspended' : 'active';
    
    $stmt = $db->prepare("UPDATE users SET status = ? WHERE id = ? AND role != 'admin'");
    $stmt->execute([$newStatus, $id]);
    $_SESSION['flash_success'] = 'เปลี่ยนสถานะสมาชิกเรียบร้อยแล้ว';
    header('Location: users.php');
    exit;
}

$users = $db->query("SELECT u.*, COUNT(o.id) as order_count FROM users u LEFT JOIN orders o ON u.id = o.user_id GROUP BY u.id ORDER BY u.id DESC")->fetchAll();

require_once __DIR__ . '/../../includes/admin_header.php';
?>

<style>
  /* ปุ่มระงับบัญชี - Dark Mode Friendly */
  .btn-suspend {
    color: #ff6b6b !important;
    background-color: rgba(220, 53, 69, 0.12) !important;
    border: 1px solid #dc3545 !important;
    transition: all 0.2s ease-in-out;
  }
  .btn-suspend:hover {
    color: #ffffff !important;
    background-color: rgba(220, 53, 69, 0.7) !important;
    border-color: #ff4d4d !important;
    box-shadow: 0 0 8px rgba(220, 53, 69, 0.5);
  }
</style>

<h3 class="fw-bold mb-4">👥 จัดการสมาชิก (Users Management)</h3>

<div class="card card-custom p-4">
  <div class="table-responsive">
    <table class="table table-hover align-middle">
      <thead>
        <tr>
          <th>ID</th>
          <th>ชื่อ - นามสกุล</th>
          <th>อีเมล</th>
          <th>เบอร์โทร</th>
          <th>บทบาท</th>
          <th>จำนวนออเดอร์</th>
          <th>สถานะ</th>
          <th>การจัดการ</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($users as $u): ?>
          <tr>
            <td>#<?php echo $u['id']; ?></td>
            <td class="fw-bold"><?php echo sanitize($u['fullname']); ?></td>
            <td><?php echo sanitize($u['email']); ?></td>
            <td><?php echo sanitize($u['phone'] ?: '-'); ?></td>
            <td>
              <span class="badge <?php echo $u['role'] === 'admin' ? 'bg-danger' : 'bg-secondary'; ?>">
                <?php echo strtoupper($u['role']); ?>
              </span>
            </td>
            <td><span class="badge bg-info text-dark"><?php echo $u['order_count']; ?> ออเดอร์</span></td>
            <td>
              <span class="badge <?php echo $u['status'] === 'active' ? 'bg-success' : 'bg-warning text-dark'; ?>">
                <?php echo strtoupper($u['status']); ?>
              </span>
            </td>
            <td>
              <?php if ($u['role'] !== 'admin'): ?>
                <a href="users.php?action=toggle_status&id=<?php echo $u['id']; ?>&current=<?php echo $u['status']; ?>" 
                   class="btn btn-sm <?php echo $u['status'] === 'active' ? 'btn-suspend' : 'btn-outline-success'; ?>"
                   onclick="return confirm('ยืนยันการเปลี่ยนสถานะผู้ใช้?')">
                  <?php echo $u['status'] === 'active' ? '🚫 ระงับบัญชี' : '✅ ปลดระงับ'; ?>
                </a>
              <?php else: ?>
                <span class="text-muted small">Admin Root</span>
              <?php endif; ?>
            </td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>

<?php require_once __DIR__ . '/../../includes/admin_footer.php'; ?>
