<?php
require_once 'auth.php';
?>
<?php
$conn = new mysqli(
    "localhost",
    "root",
    "",
    "tradelicense"
);

if($conn->connect_error){
    die("Database Error");
}

/* Total License */
$totalLicense = $conn->query(
"SELECT COUNT(*) AS total FROM details"
)->fetch_assoc()['total'];

/* Active License */
$activeLicense = $conn->query(
"SELECT COUNT(*) AS total
 FROM details
 WHERE expiry_date >= CURDATE()"
)->fetch_assoc()['total'];

/* Expired License */
$expiredLicense = $conn->query(
"SELECT COUNT(*) AS total
 FROM details
 WHERE expiry_date < CURDATE()"
)->fetch_assoc()['total'];

/* Expiring Within 30 Days */
$expiringSoon = $conn->query(
"SELECT COUNT(*) AS total
 FROM details
 WHERE expiry_date BETWEEN CURDATE()
 AND DATE_ADD(CURDATE(), INTERVAL 30 DAY)"
)->fetch_assoc()['total'];

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Dashboard</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css" rel="stylesheet">

<style>
body{
    background:#f4f7fc;
}

.sidebar{
    position:fixed;
    left:0;
    top:0;
    width:260px;
    height:100vh;
    background:linear-gradient(135deg,#0f172a,#1e293b);
    color:#fff;
    overflow-y:auto;
}

.logo{
    padding:20px;
    text-align:center;
    border-bottom:1px solid rgba(255,255,255,.1);
}

.logo h4{
    margin:0;
    font-weight:700;
}

.sidebar a{
    display:block;
    color:#cbd5e1;
    text-decoration:none;
    padding:14px 20px;
    transition:.3s;
}

.sidebar a:hover{
    background:#2563eb;
    color:#fff;
}

.sidebar a i{
    width:25px;
}

.main-content{
    margin-left:260px;
    padding:25px;
}

.topbar{
    background:#fff;
    border-radius:15px;
    padding:15px 20px;
    box-shadow:0 2px 10px rgba(0,0,0,.08);
}

.card-box{
    border:none;
    border-radius:18px;
    color:#fff;
    overflow:hidden;
}

.bg-blue{
    background:linear-gradient(135deg,#2563eb,#1d4ed8);
}

.bg-green{
    background:linear-gradient(135deg,#16a34a,#15803d);
}

.bg-orange{
    background:linear-gradient(135deg,#f59e0b,#d97706);
}

.bg-red{
    background:linear-gradient(135deg,#dc2626,#b91c1c);
}

.card-box .card-body{
    padding:25px;
}

.card-box h2{
    font-weight:bold;
}

.quick-links a{
    text-decoration:none;
}

.quick-card{
    background:#fff;
    border-radius:15px;
    padding:20px;
    text-align:center;
    transition:.3s;
    box-shadow:0 2px 8px rgba(0,0,0,.08);
}

.quick-card:hover{
    transform:translateY(-5px);
}

.quick-card i{
    font-size:35px;
    margin-bottom:10px;
    color:#2563eb;
}
</style>
</head>
<body>

<div class="sidebar">

    <div class="logo">
        <h4>LICENSE PANEL</h4>
        <small>Admin Dashboard</small>
    </div>

    <a href="dashboard.php">
        <i class="fas fa-home"></i> Dashboard
    </a>

    <a href="up.php">
        <i class="fas fa-plus-circle"></i> Add License
    </a>

    <a href="search.php">
        <i class="fas fa-search"></i> Search License
    </a>

    <a href="license_list.php">
        <i class="fas fa-list"></i> All Licenses
    </a>

    <a href="VerifyLicense.php" target="_blank">
        <i class="fas fa-check-circle"></i> Verify Page
    </a>

    <a href="reports.php">
        <i class="fas fa-chart-bar"></i> Reports
    </a>

    <a href="settings.php">
        <i class="fas fa-cog"></i> Settings
    </a>

    <a href="logout.php">
        <i class="fas fa-sign-out-alt"></i> Logout
    </a>

</div>

<div class="main-content">

    <div class="topbar d-flex justify-content-between align-items-center mb-4">
        <div>
            <h3 class="mb-0">Admin Dashboard</h3>
            <small>License Management System</small>
        </div>

        <div>
            <span class="fw-bold">Welcome, Admin</span>
        </div>
    </div>

    <div class="row">

        <div class="col-md-3 mb-3">
            <div class="card card-box bg-blue">
                <div class="card-body">
                    <h6>Total Licenses</h6>
                    <h2><?= number_format($totalLicense) ?></h2>
                </div>
            </div>
        </div>

        <div class="col-md-3 mb-3">
            <div class="card card-box bg-green">
                <div class="card-body">
                    <h6>Active</h6>
                   <h2><?= number_format($activeLicense) ?></h2>
                </div>
            </div>
        </div>

        <div class="col-md-3 mb-3">
            <div class="card card-box bg-orange">
                <div class="card-body">
                    <h6>Expired</h6>
                   <h2><?= number_format($expiredLicense) ?></h2>
                </div>
            </div>
        </div>

        <div class="col-md-3 mb-3">
            <div class="card card-box bg-red">
                <div class="card-body">
                    <h6>Today Verify</h6>
                    <h2><?= number_format($expiringSoon) ?></h2>
                </div>
            </div>
        </div>

    </div>

    <div class="row mt-4 quick-links">

        <div class="col-md-3 mb-3">
            <a href="up.php">
                <div class="quick-card">
                    <i class="fas fa-plus-circle"></i>
                    <h5>Add License</h5>
                </div>
            </a>
        </div>

        <div class="col-md-3 mb-3">
            <a href="search.php">
                <div class="quick-card">
                    <i class="fas fa-search"></i>
                    <h5>Search License</h5>
                </div>
            </a>
        </div>

        <div class="col-md-3 mb-3">
            <a href="license_list.php">
                <div class="quick-card">
                    <i class="fas fa-list"></i>
                    <h5>All Licenses</h5>
                </div>
            </a>
        </div>

        <div class="col-md-3 mb-3">
            <a href="VerifyLicense.php" target="_blank">
                <div class="quick-card">
                    <i class="fas fa-check-circle"></i>
                    <h5>Verify Page</h5>
                </div>
            </a>
        </div>

    </div>

</div>

</body>
</html>