<?php
require_once("auth.php");
require_once("../api/config/db.php");

$village = $conn->query("SELECT village_name FROM villages WHERE id='$village_id' LIMIT 1")->fetch_assoc();

$totalSubAdmins = $conn->query("SELECT COUNT(*) c FROM users WHERE village_id='$village_id' AND role='sub_admin'")->fetch_assoc()['c'] ?? 0;

$pendingSubAdmins = $conn->query("SELECT COUNT(*) c FROM users WHERE village_id='$village_id' AND role='sub_admin' AND status='pending'")->fetch_assoc()['c'] ?? 0;

$approvedSubAdmins = $conn->query("SELECT COUNT(*) c FROM users WHERE village_id='$village_id' AND role='sub_admin' AND status='approved'")->fetch_assoc()['c'] ?? 0;

$totalVisitors = $conn->query("SELECT COUNT(DISTINCT visitor_id) c FROM visitor_logs WHERE village_id='$village_id'")->fetch_assoc()['c'] ?? 0;

$todayVisitors = $conn->query("SELECT COUNT(*) c FROM visitor_logs WHERE village_id='$village_id' AND DATE(created_at)=CURDATE()")->fetch_assoc()['c'] ?? 0;

$suspiciousPending = $conn->query("
    SELECT COUNT(*) c 
    FROM suspicious_requests sr
    LEFT JOIN visitors v ON sr.visitor_id = v.id
    WHERE sr.status='pending'
")->fetch_assoc()['c'] ?? 0;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Village Admin Dashboard</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>
*{box-sizing:border-box}
body{margin:0;font-family:Arial,sans-serif;background:#f1f5f9;color:#0f172a}
.app{max-width:520px;margin:0 auto;min-height:100vh;background:#f8fafc;padding-bottom:90px}
.header{background:linear-gradient(135deg,#052e16,#15803d,#22c55e);color:white;padding:24px 18px 34px;border-bottom-left-radius:30px;border-bottom-right-radius:30px}
.header h2{margin:0;font-size:25px}
.header p{margin:7px 0 0;color:#dcfce7}
.card{background:white;margin:16px;padding:18px;border-radius:24px;box-shadow:0 10px 30px rgba(15,23,42,.08)}
.welcome{background:linear-gradient(135deg,#16a34a,#22c55e);color:white}
.welcome h3{margin:0 0 8px;font-size:22px}
.welcome p{margin:0;color:#ecfdf5}
.grid{display:grid;grid-template-columns:1fr 1fr;gap:14px;margin:16px}
.stat{border-radius:22px;padding:18px;color:white;box-shadow:0 12px 28px rgba(0,0,0,.12)}
.stat span{font-size:13px}
.stat h2{margin:10px 0 0;font-size:32px}
.green{background:linear-gradient(135deg,#16a34a,#86efac)}
.blue{background:linear-gradient(135deg,#2563eb,#38bdf8)}
.orange{background:linear-gradient(135deg,#f97316,#facc15)}
.red{background:linear-gradient(135deg,#dc2626,#fb7185)}
.purple{background:linear-gradient(135deg,#7c3aed,#c084fc)}
.dark{background:linear-gradient(135deg,#0f172a,#334155)}
.actions{display:grid;gap:12px}
.actions a{display:block;background:#f1f5f9;color:#0f172a;padding:16px;border-radius:18px;font-weight:800;text-decoration:none}
.nav{position:fixed;bottom:0;left:50%;transform:translateX(-50%);max-width:520px;width:100%;background:white;display:grid;grid-template-columns:repeat(5,1fr);padding:10px 4px 12px;box-shadow:0 -10px 30px rgba(0,0,0,.08);z-index:10}
.nav a{text-align:center;text-decoration:none;color:#64748b;font-size:20px}
.nav span{display:block;font-size:11px;font-weight:700;margin-top:3px}
@media(max-width:480px){.app{max-width:100%}}
</style>
</head>

<body>
<div class="app">

    <div class="header">
        <h2>Village Admin</h2>
        <p><?= htmlspecialchars($admin_name) ?></p>
    </div>

    <div class="card welcome">
        <h3>Welcome 👋</h3>
        <p><?= htmlspecialchars($village['village_name'] ?? 'Your Village') ?></p>
    </div>

    <div class="grid">
        <div class="stat green">
            <span>Today Visitors</span>
            <h2><?= $todayVisitors ?></h2>
        </div>

        <div class="stat blue">
            <span>Total Visitors</span>
            <h2><?= $totalVisitors ?></h2>
        </div>

        <div class="stat orange">
            <span>Pending Sub Admins</span>
            <h2><?= $pendingSubAdmins ?></h2>
        </div>

        <div class="stat purple">
            <span>Approved Sub Admins</span>
            <h2><?= $approvedSubAdmins ?></h2>
        </div>

        <div class="stat red">
            <span>Suspicious Pending</span>
            <h2><?= $suspiciousPending ?></h2>
        </div>

        <div class="stat dark">
            <span>Total Sub Admins</span>
            <h2><?= $totalSubAdmins ?></h2>
        </div>
    </div>

    <div class="card">
        <h3>Quick Actions</h3>
        <div class="actions">
            <a href="pending-sub-admins.php">👥 Pending Sub Admins</a>
            <a href="approved-sub-admins.php">✅ Approved Sub Admins</a>
            <a href="visitors.php">🧾 Village Visitors</a>
            <a href="suspicious-requests.php">⚠️ Suspicious Requests</a>
            <a href="profile.php">👤 My Profile</a>
        </div>
    </div>

    <div class="nav">
        <a href="dashboard.php">🏠<span>Home</span></a>
        <a href="pending-sub-admins.php">👥<span>Pending</span></a>
        <a href="visitors.php">🧾<span>Visitors</span></a>
        <a href="suspicious-requests.php">⚠️<span>Alerts</span></a>
        <a href="profile.php">👤<span>Profile</span></a>
    </div>

</div>
</body>
</html>