<?php
session_name('mini_admin_session');
session_start();
error_reporting(0);
include('includes/dbconnection.php');
if (strlen($_SESSION['sid']==0)) {
header('location:logoutmini.php');
} 
$admin = $_SESSION['sid'];

$sql = "SELECT * FROM tblusers WHERE id = ?";
$stmt = $dbh->prepare($sql);
$stmt->execute([$admin]);

if ($stmt->rowCount() > 0) {
    $fecthemail = $stmt->fetch(PDO::FETCH_ASSOC);
    $request = $fecthemail['id'];
}


$id = $_SESSION['sid'];

// Get admin details
$sql1 = "SELECT * FROM tblusers WHERE id = '$id'";
$result = mysqli_query($con, $sql1);
$row = mysqli_fetch_assoc($result);

// Assign variables
$status     = isset($row['status']) ? (int)$row['status'] : 0;
$firstname  = $row['name'] ?? '';
$lastname   = $row['lastname'] ?? '';
$permission = $row['permission'] ?? '';
$userimage  = !empty($row['userimage']) && file_exists("staff_images/".$row['userimage'])
                ? "staff_images/" . htmlspecialchars($row['userimage'])
                : "default.png";
?>
<!DOCTYPE html>
<html>
<?php @include("includes/head.php"); ?>
</body>

<body class="hold-transition sidebar-mini layout-fixed">
    <div class="wrapper">
        <?php @include("includes/header2.php"); ?>
        <?php @include("includes/sidebar2.php"); ?>
        
        <div class="content-wrapper">

<!-- Admin Header -->
<div id="pageHeader" 
     class="content-header py-4 rounded shadow-sm mb-4 sticky-top" 
     style="background: linear-gradient(135deg, var(--dark), var(--primary)); color: #fff; z-index: 1020; transition: all 0.3s ease;">
    <div class="container-fluid">
        <div class="d-flex justify-content-between align-items-center flex-wrap">
            
            <!-- Profile & Welcome -->
            <div class="d-flex align-items-center mb-2 mb-md-0">
                <div style="width:60px; height:60px; border-radius:50%; overflow:hidden; background:#fff; display:flex; align-items:center; justify-content:center; margin-right:15px; transition: all 0.3s ease;">
                    <?php if ($userimage !== "default.png"): ?>
                        <img src="<?php echo $userimage; ?>" 
                             alt="Admin Picture" 
                             style="width:100%; height:100%; object-fit:cover;" 
                             class="img-circle img-fluid">
                    <?php else: ?>
                        <i class="fas fa-user-circle" style="font-size:60px; color:var(--primary);"></i>
                    <?php endif; ?>
                </div>
                <div>
                    <h4 class="mb-1 fw-bold" style="transition: all 0.3s ease;">
                        <?php echo htmlspecialchars($firstname . ' ' . $lastname); ?>,
                    </h4>
                    <h6 class="mb-0">Welcome to your dashboard</h6>
                    <small style="opacity:0.9;">Manage payments, students, events and more.</small>
                </div>
            </div>  

<!-- Admin Status -->
<div>
    <h6 class="fw-bold mb-2 student-status-heading">Admin Status</h6>
    <?php 
        $admissionStatusOptions = [
            2 => ['text' => 'Active', 'class' => 'bg-success'],
            1 => ['text' => 'Active', 'class' => 'bg-success'],
        ];
        $admissionStatus = $admissionStatusOptions[$status] ?? ['text' => 'Unknown', 'class' => 'bg-secondary'];
    ?>
    <div>
        <span class="badge <?= $admissionStatus['class']; ?> d-block p-2" style="font-size:1rem;">
            <?= htmlspecialchars($admissionStatus['text']); ?>
        </span>
    </div>
</div>

        </div>
    </div>
</div>

<style>
    .student-status-heading {
        position: relative;
        display: inline-block;
        color: #fff;
        padding-bottom: 4px;
    }
    .student-status-heading::after {
        content: "";
        position: absolute;
        left: 0;
        bottom: 0;
        height: 2px;
        width: 0;
        background-color: rgba(255,255,255,0.3);
        transition: width 0.6s ease-in-out;
    }
    body.loaded .student-status-heading::after {
        width: 100%;
    }
</style>

<script>
    window.addEventListener("load", function() {
        document.body.classList.add("loaded");
    });

    // Shrink header on scroll
    window.addEventListener('scroll', function() {
        let header = document.getElementById('pageHeader');
        let profilePic = header.querySelector("div[style*='width:60px']");
        let nameTag = header.querySelector("h4");

        if (window.scrollY > 50) {
            header.style.padding = "0.5rem 0";
            profilePic.style.width = "40px";
            profilePic.style.height = "40px";
            nameTag.style.fontSize = "1rem";
        } else {
            header.style.padding = "1rem 0";
            profilePic.style.width = "60px";
            profilePic.style.height = "60px";
            nameTag.style.fontSize = "1.25rem";
        }
    });
</script>

            <!-- Main Content -->
            <section class="content px-6">
                <div class="container-fluid">
                    <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 gap-4 mb-10">
                        <!-- Card UI -->

                        <?php
    $dashboard_items = [
        // Students
        ["query" => "SELECT * FROM students WHERE status =1", "label" => "FULLY ENROLLED", "icon" => "fas fa-user-graduate", "bg" => "bg-info", "link" => "student_list1.php"],
        ["query" => "SELECT * FROM students WHERE status =2", "label" => "PROVISIONAL STUDENTS", "icon" => "fas fa-user-tie", "bg" => "bg-primary", "link" => "studentlist1.php"],

        // Requests
        ['query' => "SELECT * FROM message WHERE status=1 AND request ='$request'", 'label' => "TOTAL APPROVED REQUEST", 'icon' => " fas fa-check-circle", "bg" => "bg-success", 'link' => "request1.php"],
        ['query' => "SELECT * FROM message WHERE status=0 AND request ='$request'", 'label' => "PENDING PAYMENT REQUEST", 'icon' => "fas fa-clock", "bg" => "bg-warning", 'link' => "request11.php"],

        // Documents
        ['query' => "SELECT * FROM document", 'label' => "UPLOADED DOCUMENT", 'icon' => "fas fa-upload", "bg" => "bg-secondary", 'link' => "document_list1.php"],
    ];
                            $styleMap = [
                            "bg-primary"   => ["bg" => "bg-blue-50", "text" => "text-blue-600"],
                            "bg-success"   => ["bg" => "bg-emerald-50", "text" => "text-emerald-600"],
                            "bg-warning"   => ["bg" => "bg-orange-50", "text" => "text-orange-600"],
                            "bg-info"      => ["bg" => "bg-cyan-50", "text" => "text-cyan-600"],
                            "bg-danger"    => ["bg" => "bg-rose-50", "text" => "text-rose-600"],
                            "bg-secondary" => ["bg" => "bg-slate-50", "text" => "text-slate-600"]
                        ];


foreach ($dashboard_items as $item) {
        $res = mysqli_query($con, $item["query"]);
        $count = mysqli_num_rows($res);
        $st = $styleMap[$item['bg']] ?? $styleMap["bg-secondary"];
    ?>
        <div class="bento-card group">
            <div class="flex justify-between items-start mb-3">
                <div class="w-10 h-10 <?php echo $st['bg']; ?> <?php echo $st['text']; ?> rounded-xl flex items-center justify-center text-base transition-transform group-hover:scale-110">
                    <i class="<?php echo $item['icon']; ?>"></i>
                </div>
                <a href="<?php echo $item['link']; ?>" class="text-[12px] font-black text-slate-400 group-hover:text-slate-900 transition-colors tracking-tighter"><i class ="fas fa-arrow-right"></i> CLICK HERE</a>
            </div>
            
            <h2 class="text-2xl font-black text-slate-800 tracking-tight"><?php echo number_format($count); ?></h2>
            <p class="text-[10px] font-bold text-slate-500 uppercase tracking-tight truncate"><?php echo $item['label']; ?></p>
            
            <div class="mt-4 w-full h-1 bg-slate-200/50 rounded-full overflow-hidden">
                <div class="h-full <?php echo str_replace('text-', 'bg-', $st['text']); ?> opacity-40 w-full transform -translate-x-1/2 group-hover:translate-x-0 transition-transform duration-1000"></div>
            </div>
        </div>
    <?php } ?>
</div>

                    <div class="mt-12">
                        <hr>
                         <h5 class="text-lg font-black text-slate-600 mb-6 flex items-center">
                            <span class="w-8 h-8 bg-white shadow-sm rounded-lg flex items-center justify-center mr-3 text-sm">📅</span>
                            School Calendar
                         </h5>
                         <hr>
                         <div id="calendar" class="bg-white p-6 rounded-3xl border border-slate-100 shadow-sm mb-8"></div>
                    </div>

                </div>
            </section>
        </div>



    

        <!-- Footer -->
        <?php @include("includes/footer.php"); ?>
        <aside class="control-sidebar control-sidebar-dark"></aside>
    </div>

    <!-- Scripts -->
    <?php @include("includes/foot.php"); ?>
</body>
</html>
