<?php
require_once('hethong/ketnoi.php');
if(!isset($_SESSION['session'])) { header('location: /landing-page'); exit(); }
$session = $_SESSION['session'];
$check_user = $ketnoi->query("SELECT * FROM `users` WHERE `session` = '$session' ")->fetch_array();
if(empty($check_user['id'])) { header('location: /landing-page'); exit(); }
$user = $check_user;

// Kiểm tra quyền admin (giả sử có cột role hoặc is_admin)
$is_admin = isset($user['role']) && $user['role'] === 'admin';

// Xử lý hành động
$action = isset($_GET['action']) ? $_GET['action'] : '';
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if($action === 'approve' && $id && $is_admin){
    $ketnoi->query("UPDATE `history_subdomain` SET `status` = 'active', `approved_at` = NOW() WHERE `id` = '$id'");
    header('location: quan-ly-subdomain.php?msg=approved');
    exit();
}

if($action === 'reject' && $id && $is_admin){
    $ketnoi->query("UPDATE `history_subdomain` SET `status` = 'rejected', `rejected_at` = NOW() WHERE `id` = '$id'");
    header('location: quan-ly-subdomain.php?msg=rejected');
    exit();
}

if($action === 'delete' && $id){
    // Chỉ admin hoặc chủ sở hữu mới được xóa
    if($is_admin) {
        $ketnoi->query("DELETE FROM `history_subdomain` WHERE `id` = '$id'");
    } else {
        $ketnoi->query("DELETE FROM `history_subdomain` WHERE `id` = '$id' AND `username` = '".$user['username']."'");
    }
    header('location: quan-ly-subdomain.php?msg=deleted');
    exit();
}

// Lấy danh sách subdomain
$where = $is_admin ? "1=1" : "`username` = '".$user['username']."'";
$subdomains = $ketnoi->query("SELECT * FROM `history_subdomain` WHERE $where ORDER BY `id` DESC");

// Thống kê
$stats_result = $ketnoi->query("
    SELECT 
        COUNT(*) as total,
        SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending,
        SUM(CASE WHEN status = 'active' THEN 1 ELSE 0 END) as active,
        SUM(CASE WHEN status = 'rejected' THEN 1 ELSE 0 END) as rejected
    FROM `history_subdomain` 
    WHERE $where
");
$stats = $stats_result ? $stats_result->fetch_assoc() : ['total'=>0, 'pending'=>0, 'active'=>0, 'rejected'=>0];
?>
<!DOCTYPE html>
<html lang="vi">
<head>
<?php require $_SERVER['DOCUMENT_ROOT'].'/hethong/head2.php';?>
<title>Quản Lý Subdomain | <?=$lxtdev['ten_web'];?></title>
<?php require $_SERVER['DOCUMENT_ROOT'].'/hethong/nav.php';?>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
<link href="https://fonts.googleapis.com/css2?family=Be+Vietnam+Pro:ital,wght@0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,400&family=JetBrains+Mono:wght@400;500;700&display=swap" rel="stylesheet">
<script src="js/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<style>
:root{
  --bg: #f0f3f8;
  --surface: #ffffff;
  --s2: #f8f9fc;
  --s3: #eef0f7;
  --border: rgba(0,0,0,.07);
  --border2: rgba(0,0,0,.04);
  --h: #0b0d17;
  --b: #3d4464;
  --m: #8891b0;
  --v1: #5452ee;
  --v2: #8b5cf6;
  --vg: linear-gradient(135deg,#5452ee,#8b5cf6);
  --g1: #059669;
  --g2: #10b981;
  --gg: linear-gradient(135deg,#059669,#10b981);
  --a1: #d97706;
  --a2: #f59e0b;
  --ag: linear-gradient(135deg,#d97706,#f59e0b);
  --s1: #0284c7;
  --s2c: #0ea5e9;
  --sg: linear-gradient(135deg,#0284c7,#0ea5e9);
  --r1: #dc2626;
  --r2: #ef4444;
  --rg: linear-gradient(135deg,#dc2626,#ef4444);
  --rd: 12px;
  --rdm: 18px;
  --rdl: 24px;
  --rdxl: 32px;
  --sh0: 0 1px 3px rgba(0,0,0,.05);
  --sh1: 0 4px 16px rgba(0,0,0,.06);
  --sh2: 0 8px 32px rgba(0,0,0,.08);
  --ease: cubic-bezier(.16,1,.3,1);
  --fs-hero-title: clamp(1.5rem, 4vw, 2.6rem);
}

*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}

body{
  font-family:'Be Vietnam Pro',sans-serif;
  background:var(--bg);
  color:var(--b);
  -webkit-font-smoothing:antialiased;
}

.page{
  padding: clamp(12px, 3vw, 24px) clamp(12px, 3vw, 28px);
  max-width:1380px;
  margin:0 auto;
}

/* ── HERO ── */
.hero{
  position:relative;overflow:hidden;
  background:linear-gradient(135deg,#0b0d17,#1a1d2e);
  border-radius:var(--rdxl);
  padding: clamp(24px, 5vw, 48px) clamp(18px, 4vw, 52px);
  margin-bottom:clamp(16px, 3vw, 24px);
  display:grid;
  grid-template-columns:1fr auto;
  gap:clamp(16px, 3vw, 32px);
  align-items:center;
}
.hero-bg{
  position:absolute;inset:0;
  background:
    radial-gradient(ellipse 65% 80% at 100% -20%,rgba(84,82,238,.5) 0%,transparent 55%),
    radial-gradient(ellipse 50% 60% at -5%  110%,rgba(139,92,246,.3) 0%,transparent 55%);
  z-index:0;
}
.hero-grid{
  position:absolute;inset:0;z-index:0;
  background-image:
    linear-gradient(rgba(255,255,255,.025) 1px,transparent 1px),
    linear-gradient(90deg,rgba(255,255,255,.025) 1px,transparent 1px);
  background-size:48px 48px;
}
.hero>*{position:relative;z-index:2}

.hero-eyebrow{
  display:inline-flex;align-items:center;gap:8px;
  border:1px solid rgba(255,255,255,.12);
  background:rgba(255,255,255,.05);
  border-radius:50px;
  padding:6px 16px;
  font-size:0.65rem;font-weight:700;letter-spacing:1.3px;
  color:rgba(255,255,255,.6);text-transform:uppercase;
  margin-bottom:1rem;
  backdrop-filter:blur(8px);
}
.hero-dot{
  width:7px;height:7px;border-radius:50%;
  background:#10b981;
  animation:pulse-dot 2s ease-in-out infinite;
}
@keyframes pulse-dot{
  0%,100%{box-shadow:0 0 0 0 rgba(16,185,129,.6)}
  50%{box-shadow:0 0 0 6px rgba(16,185,129,0)}
}

.hero-title{
  font-size:var(--fs-hero-title);font-weight:900;line-height:1.18;
  color:#fff;letter-spacing:-.5px;margin-bottom:10px;
}
.hero-title .grad{
  background:linear-gradient(90deg,#a5b4fc,#c084fc);
  -webkit-background-clip:text;-webkit-text-fill-color:transparent;
}
.hero-sub{
  font-size:0.85rem;color:rgba(255,255,255,.45);
  font-weight:400;margin-bottom:1.5rem;line-height:1.6;
  max-width:480px;
}

/* ── STATS ── */
.stats-row{
  display:grid;
  grid-template-columns:repeat(4,1fr);
  gap:clamp(12px, 2vw, 16px);
  margin-bottom:clamp(16px, 3vw, 24px);
}
.stat-box{
  background:var(--surface);
  border:1px solid var(--border);
  border-radius:var(--rdm);
  padding:clamp(14px, 3vw, 20px);
  box-shadow:var(--sh0);
  text-align:center;
  transition:transform .25s var(--ease),box-shadow .25s;
}
.stat-box:hover{
  transform:translateY(-3px);
  box-shadow:var(--sh1);
}
.stat-box .num{
  font-size:clamp(1.4rem, 3vw, 2rem);
  font-weight:900;
  font-family:'JetBrains Mono',monospace;
  color:var(--h);
  display:block;
}
.stat-box .lbl{
  font-size:0.65rem;font-weight:700;color:var(--m);
  text-transform:uppercase;letter-spacing:.5px;margin-top:4px;
}
.stat-box.pending .num{color:var(--a1)}
.stat-box.active .num{color:var(--g1)}
.stat-box.rejected .num{color:var(--r1)}
.stat-box.total .num{color:var(--v1)}

/* ── TABLE ── */
.table-wrap{
  background:var(--surface);
  border:1px solid var(--border);
  border-radius:var(--rdl);
  padding:clamp(16px, 3vw, 24px);
  box-shadow:var(--sh0);
  overflow:hidden;
}
.sec-hd{
  display:flex;align-items:center;justify-content:space-between;
  margin-bottom:clamp(12px, 2vw, 16px);
  flex-wrap:wrap;gap:10px;
}
.sec-title{
  font-size:0.8125rem;font-weight:800;color:var(--h);
  letter-spacing:.3px;display:flex;align-items:center;gap:8px;