<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

$pageTitle = "Dashboard";

// 🔐 AUTH (this must define $user)
require_once("includes/auth.php");

// DB
require_once("../api/config/db.php");

// ✅ Safe user values
$user_id = intval($user['id'] ?? 0);
$village_id = intval($user['village_id'] ?? 0);
$user_name = $user['name'] ?? 'User';

// 📊 STATS
$todayVisitors = 0;
$totalVisitors = 0;
$myRequests = 0;
$pendingRequests = 0;

// Today visitors
$res = $conn->query("SELECT COUNT(*) c FROM visitor_logs WHERE checked_by='$user_id' AND DATE(created_at)=CURDATE()");
if ($res) {
    $row = $res->fetch_assoc();
    $todayVisitors = $row['c'] ?? 0;
}

// Total visitors
$res = $conn->query("SELECT COUNT(*) c FROM visitor_logs WHERE checked_by='$user_id'");
if ($res) {
    $row = $res->fetch_assoc();
    $totalVisitors = $row['c'] ?? 0;
}

// My requests
$res = $conn->query("SELECT COUNT(*) c FROM suspicious_requests WHERE requested_by='$user_id'");
if ($res) {
    $row = $res->fetch_assoc();
    $myRequests = $row['c'] ?? 0;
}

// Pending requests
$res = $conn->query("SELECT COUNT(*) c FROM suspicious_requests WHERE requested_by='$user_id' AND status='pending'");
if ($res) {
    $row = $res->fetch_assoc();
    $pendingRequests = $row['c'] ?? 0;
}

// Village name
$village_name = "Your Village";
$res = $conn->query("SELECT village_name FROM villages WHERE id='$village_id' LIMIT 1");
if ($res && $res->num_rows > 0) {
    $village = $res->fetch_assoc();
    $village_name = $village['village_name'];
}
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Sub 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;background:#f1f5f9;color:#0f172a}

.app{
    max-width:520px;
    margin:0 auto;
    min-height:100vh;
    background:#f8fafc;
    padding-bottom:90px;
}

/* HEADER */
.header{
    background:linear-gradient(135deg,#07111f,#123c88,#0ea5e9);
    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:#cbd5f5}

/* WELCOME */
.welcome-card{
    background:linear-gradient(135deg,#2563eb,#06b6d4);
    color:white;
    margin:16px;
    padding:18px;
    border-radius:24px;
}
.welcome-card h3{margin:0 0 6px}

/* STATS */
.stats-grid{
    display:grid;
    grid-template-columns:1fr 1fr;
    gap:14px;
    margin:16px;
}

.stat-card{
    border-radius:22px;
    padding:18px;
    color:white;
    box-shadow:0 12px 28px rgba(0,0,0,.12);
}

.stat-card span{font-size:13px}
.stat-card h2{margin:10px 0 0;font-size:30px}

.blue{background:linear-gradient(135deg,#2563eb,#38bdf8)}
.green{background:linear-gradient(135deg,#16a34a,#86efac)}
.orange{background:linear-gradient(135deg,#f97316,#facc15)}
.red{background:linear-gradient(135deg,#dc2626,#fb7185)}

/* ACTIONS */
.section-card{
    background:white;
    margin:16px;
    padding:18px;
    border-radius:24px;
    box-shadow:0 10px 30px rgba(15,23,42,.08);
}

.section-card h3{margin:0 0 14px}

.quick-actions{
    display:grid;
    gap:12px;
}

.quick-actions a{
    display:block;
    background:#f1f5f9;
    padding:16px;
    border-radius:18px;
    font-weight:800;
    text-decoration:none;
    color:#0f172a;
}

/* NAV */
.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);
}

.nav a{
    text-align:center;
    text-decoration:none;
    color:#64748b;
    font-size:20px;
}

.nav span{
    display:block;
    font-size:11px;
    font-weight:700;
}
</style>
</head>

<body>

<div class="app">

<!-- HEADER -->
<div class="header">
    <h2>Sub Admin</h2>
    <p><?= htmlspecialchars($user_name) ?></p>
</div>

<!-- WELCOME -->
<div class="welcome-card">
    <h3>Hello 👋</h3>
    <p><?= htmlspecialchars($village_name) ?></p>
</div>

<!-- STATS -->
<div class="stats-grid">

    <div class="stat-card blue">
        <span>Today Visitors</span>
        <h2><?= $todayVisitors ?></h2>
    </div>

    <div class="stat-card green">
        <span>Total Entries</span>
        <h2><?= $totalVisitors ?></h2>
    </div>

    <div class="stat-card orange">
        <span>My Requests</span>
        <h2><?= $myRequests ?></h2>
    </div>

    <div class="stat-card red">
        <span>Pending</span>
        <h2><?= $pendingRequests ?></h2>
    </div>

</div>

<!-- ACTIONS -->
<div class="section-card">
    <h3>Quick Actions</h3>

    <div class="quick-actions">
        <a href="add-visitor.php">➕ Add Visitor</a>
        <a href="search-visitor.php">🔍 Search Visitor</a>
        <a href="my-visitors.php">📋 My Visitors</a>
        <a href="suspicious-requests.php">⚠️ Suspicious Requests</a>
        <a href="profile.php">👤 Profile</a>
        <a href="logout.php">🚪 Logout</a>
    </div>
</div>

<!-- NAV -->
<div class="nav">
    <a href="dashboard.php">🏠<span>Home</span></a>
    <a href="add-visitor.php">➕<span>Add</span></a>
    <a href="search-visitor.php">🔍<span>Search</span></a>
    <a href="my-visitors.php">📋<span>List</span></a>
    <a href="profile.php">👤<span>Profile</span></a>
</div>

</div>

</body>
</html>