
<?php
// 1. Include your database connection
include 'con.php'; 

// Check if connection exists
if (!$ahk_conn) {
    die("Connection variable \$ahk_conn not found. Check your con.php file.");
}

/**
 * Robust Function to fetch counts
 */
function fetchCount($conn, $query) {
    $res = $conn->query($query);
    if (!$res) {
        // This will print the exact database error if the query fails
        return "Error: " . $conn->error;
    }
    $row = $res->fetch_assoc();
    return $row['total'];
}

// 2. Fetching with Error Catching
$puc_count  = fetchCount($ahk_conn, "SELECT COUNT(*) as total FROM puchistory WHERE mstatus = '0' OR mstatus = '' OR mstatus IS NULL");
$eid_count  = fetchCount($ahk_conn, "SELECT COUNT(*) as total FROM eid_history WHERE status = '0'");
$fmp_count  = fetchCount($ahk_conn, "SELECT COUNT(*) as total FROM mphistory WHERE status = '0'");
$dl_count   = fetchCount($ahk_conn, "SELECT COUNT(*) as total FROM dlhistory WHERE status = '0'");
$mvcc_count = fetchCount($ahk_conn, "SELECT COUNT(*) as total FROM mvcc WHERE status = '0'");
$farmer_count = fetchCount($ahk_conn, "SELECT COUNT(*) as total FROM aadhaar_history WHERE status = '0'");

$ll_count = fetchCount($ahk_conn, "SELECT COUNT(*) as total FROM ll_exam_history WHERE status = '0'");


$cpu_count = fetchCount($ahk_conn, "SELECT COUNT(*) as total FROM cpuchistory WHERE status = '0'");

// 3. Debug: If you want to see errors on screen, uncomment the line below:
// echo "PUC: $puc_count | EID: $eid_count | DL: $dl_count";
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Admin Dashboard</title>
    <link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined" rel="stylesheet" />
    <style>
        * { box-sizing: border-box; }
        body { font-family: 'Segoe UI', sans-serif; background: #f4f7f6; padding: 20px; }
        
        .container {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
            gap: 20px;
            max-width: 1100px;
            margin: auto;
        }

        .box {
            position: relative;
            background: white;
            border-radius: 15px;
            padding: 30px 10px;
            text-align: center;
            box-shadow: 0 4px 10px rgba(0,0,0,0.05);
            transition: 0.3s;
        }

        /* The Badge */
        .badge {
            position: absolute;
            top: 10px;
            right: 15px;
            background: #ff4757;
            color: white;
            padding: 3px 8px;
            border-radius: 10px;
            font-size: 12px;
            font-weight: bold;
            min-width: 22px;
            box-shadow: 0 3px 6px rgba(255, 71, 87, 0.4);
        }

        .box:hover { transform: translateY(-5px); background: #007bff; color: white; }
        .box .material-symbols-outlined { font-size: 40px; margin-bottom: 10px; }
        a { text-decoration: none; color: #333; }
    </style>
</head>
<body>

<h1 style="text-align:center;">Admin Dashboard</h1>

<div class="container">

    <a href="adminpuc.php">
        <div class="box">
            <?php if(is_numeric($puc_count) && $puc_count > 0): ?>
                <span class="badge"><?php echo $puc_count; ?></span>
            <?php elseif(!is_numeric($puc_count)): ?>
                <small style="color:red; font-size:10px;"><?php echo $puc_count; ?></small>
            <?php endif; ?>
            <span class="material-symbols-outlined">description</span>
            <div>PUCC</div>
        </div>
    </a>

    <a href="admineid.php">
        <div class="box">
            <?php if(is_numeric($eid_count) && $eid_count > 0): ?>
                <span class="badge"><?php echo $eid_count; ?></span>
            <?php endif; ?>
            <span class="material-symbols-outlined">child_care</span>
            <div>EID</div>
        </div>
    </a>

    <a href="adminfmp.php">
        <div class="box">
            <?php if(is_numeric($fmp_count) && $fmp_count > 0): ?>
                <span class="badge"><?php echo $fmp_count; ?></span>
            <?php endif; ?>
            <span class="material-symbols-outlined">account_balance</span>
            <div>FMP</div>
        </div>
    </a>

    <a href="admindl.php">
        <div class="box">
            <?php if(is_numeric($dl_count) && $dl_count > 0): ?>
                <span class="badge"><?php echo $dl_count; ?></span>
            <?php endif; ?>
            <span class="material-symbols-outlined">badge</span>
            <div>DL</div>
        </div>
    </a>

    <a href="adminmvcc.php">
        <div class="box">
            <?php if(is_numeric($mvcc_count) && $mvcc_count > 0): ?>
                <span class="badge"><?php echo $mvcc_count; ?></span>
            <?php endif; ?>
            <span class="material-symbols-outlined">folder</span>
            <div>Mvcc</div>
        </div>
    </a>
 <a href="adminreg.php">
        <div class="box">
            <?php if(is_numeric($farmer_count) && $farmer_count > 0): ?>
                <span class="badge"><?php echo $farmer_count; ?></span>
            <?php endif; ?>
            <span class="material-symbols-outlined">folder</span>
            <div>Farmer</div>
        </div>
    </a>

  
        <a href="adminll.php">
        <div class="box">
            <?php if(is_numeric($ll_count) && $ll_count > 0): ?>
                <span class="badge"><?php echo $ll_count; ?></span>
            <?php endif; ?>
            <span class="material-symbols-outlined">folder</span>
            <div>LL Exam</div>
        </div>
    </a>
    <a href="adminration.php">
        <div class="box">
            <?php if(is_numeric($rt_count) && $rt_count > 0): ?>
                <span class="badge"><?php echo $rt_count; ?></span>
            <?php endif; ?>
            <span class="material-symbols-outlined">folder</span>
            <div>Ration 2 A</div>
        </div>
    </a>
        <a href="adminhis.php">
        <div class="box">
            <?php if(is_numeric($cpu_count) && $cpu_count > 0): ?>
                <span class="badge"><?php echo $cpu_count; ?></span>
            <?php endif; ?>
            <span class="material-symbols-outlined">folder</span>
            <div>his</div>
        </div>
    </a>
</div>

</body>
</html>