<?php
session_start();
include('include/config.php');

if(strlen($_SESSION['alogin'])==0) {
    header("Location: index.php");
    exit();
}

// ✅ Fetch general counts
$notProcessedCount = mysqli_num_rows(mysqli_query($bd, "SELECT * FROM tblcomplaints WHERE status IS NULL"));
$pendingCount = mysqli_num_rows(mysqli_query($bd, "SELECT * FROM tblcomplaints WHERE status='in Process'"));
$closedCount = mysqli_num_rows(mysqli_query($bd, "SELECT * FROM tblcomplaints WHERE status='closed'"));
$totalUsers = mysqli_num_rows(mysqli_query($bd, "SELECT * FROM users"));

// ✅ Fetch yearly complaint counts
$yearQuery = mysqli_query($bd, "SELECT YEAR(regDate) as year, COUNT(*) as total FROM tblcomplaints GROUP BY YEAR(regDate) ORDER BY YEAR(regDate) DESC");
$yearlyCounts = [];
while($row = mysqli_fetch_assoc($yearQuery)) {
    $yearlyCounts[$row['year']] = $row['total'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Admin Dashboard</title>

<!-- Bootstrap 5 & Icons -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">

<style>
body { font-family: 'Poppins', sans-serif; background: #f7f8fa; }
.wrapper { display: flex; min-height: 100vh; }
.sidebar { width: 250px; background: linear-gradient(180deg, #ff7b00, #ff9500); color: #fff; padding-top: 25px; }
.sidebar a { color: #fff; display: block; padding: 12px 20px; text-decoration: none; }
.sidebar a:hover, .sidebar a.active { background: rgba(255,255,255,0.2); }
.content { flex: 1; padding: 30px; }
.card { border-radius: 12px; box-shadow: 0 4px 15px rgba(0,0,0,0.08); }
.card .card-body { text-align: center; }
.card i { font-size: 2rem; margin-bottom: 10px; }
@media(max-width:991px){ .wrapper { flex-direction: column; } .sidebar { width: 100%; } }
</style>
</head>
<body>

<?php include('include/header.php'); ?>

<div class="wrapper">
    <!-- Sidebar -->
    <div class="sidebar">
        <?php include('include/sidebar.php'); ?>
    </div>

    <!-- Content -->
    <div class="content">
        <h3 class="mb-4">Dashboard</h3>
        <div class="row g-4">

            <!-- Not Processed -->
            <div class="col-md-3 col-sm-6">
                <div class="card text-warning">
                    <div class="card-body">
                        <i class="bi bi-hourglass-split"></i>
                        <h5 class="card-title mt-2">Not Processed</h5>
                        <h3><?php echo $notProcessedCount; ?></h3>
                    </div>
                </div>
            </div>

            <!-- Pending -->
            <div class="col-md-3 col-sm-6">
                <div class="card text-info">
                    <div class="card-body">
                        <i class="bi bi-hourglass-top"></i>
                        <h5 class="card-title mt-2">Pending</h5>
                        <h3><?php echo $pendingCount; ?></h3>
                    </div>
                </div>
            </div>

            <!-- Closed -->
            <div class="col-md-3 col-sm-6">
                <div class="card text-success">
                    <div class="card-body">
                        <i class="bi bi-check-circle"></i>
                        <h5 class="card-title mt-2">Closed</h5>
                        <h3><?php echo $closedCount; ?></h3>
                    </div>
                </div>
            </div>

            <!-- Total Users -->
            <div class="col-md-3 col-sm-6">
                <div class="card text-primary">
                    <div class="card-body">
                        <i class="bi bi-people"></i>
                        <h5 class="card-title mt-2">Total Users</h5>
                        <h3><?php echo $totalUsers; ?></h3>
                    </div>
                </div>
            </div>

        </div>

        <!-- Yearly Complaint Counts -->
        <h4 class="mt-5 mb-3">Complaints by Year</h4>
        <div class="row g-4">
            <?php foreach($yearlyCounts as $year => $count) { ?>
            <div class="col-md-3 col-sm-6">
                <div class="card text-secondary">
                    <div class="card-body">
                        <i class="bi bi-calendar-event"></i>
                        <h5 class="card-title mt-2"><?php echo $year; ?></h5>
                        <h3><?php echo $count; ?></h3>
                    </div>
                </div>
            </div>
            <?php } ?>
        </div>
    </div>
</div>

<!-- Footer -->
<?php include('include/footer.php'); ?>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
