<?php
session_start();

error_reporting(E_ALL);
ini_set('display_errors', 1);

include('con.php');

/* ================= LOGIN CHECK ================= */

if (!isset($_SESSION['phone'])) {

    header("Location: login.php");
    exit;
}

$username = $_SESSION['phone'];

/* ================= USER DATA ================= */

$getUser = mysqli_query(
    $ahk_conn,
    "SELECT * FROM users WHERE phone='$username' LIMIT 1"
);

$user = mysqli_fetch_assoc($getUser);

/* ================= ADMIN CHECK ================= */

if ($user['type'] != 'admin') {

    die("
    <div style='
        font-family:Arial;
        height:100vh;
        display:flex;
        justify-content:center;
        align-items:center;
        background:#f5f5f5;
    '>

        <div style='
            background:#fff;
            padding:40px;
            border-radius:15px;
            box-shadow:0 10px 30px rgba(0,0,0,.1);
            text-align:center;
        '>

            <h1 style='color:red;'>Access Denied</h1>

            <p>You are not authorized to access admin panel.</p>

            <a href='dashboard.php'
               style='
               display:inline-block;
               margin-top:15px;
               background:#0d6efd;
               color:#fff;
               padding:10px 20px;
               border-radius:8px;
               text-decoration:none;
               '>
               Back Dashboard
            </a>

        </div>

    </div>
    ");
}

/* ================= TOTAL COUNTS ================= */

$totalUsers = mysqli_fetch_assoc(
    mysqli_query($ahk_conn,
        "SELECT COUNT(*) AS total FROM users"
    )
)['total'];

$totalBalance = mysqli_fetch_assoc(
    mysqli_query($ahk_conn,
        "SELECT SUM(balance) AS total FROM users"
    )
)['total'];

$totalApi = mysqli_fetch_assoc(
    mysqli_query($ahk_conn,
        "SELECT COUNT(*) AS total FROM price"
    )
)['total'];

$totalHistory = mysqli_fetch_assoc(
    mysqli_query($ahk_conn,
        "SELECT COUNT(*) AS total FROM wallethistory"
    )
)['total'];

/* ================= RECENT USERS ================= */

$recentUsers = mysqli_query(
    $ahk_conn,
    "SELECT * FROM users ORDER BY id DESC LIMIT 10"
);

?>

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Admin Panel</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css"/>

<style>

*{
    margin:0;
    padding:0;
    box-sizing:border-box;
}

body{
    background:#f4f7fb;
    font-family:'Segoe UI',sans-serif;
}

/* ================= SIDEBAR ================= */

.sidebar{
    width:270px;
    height:100vh;
    position:fixed;
    left:0;
    top:0;
    background:linear-gradient(180deg,#0f172a,#1e293b);
    padding:25px 20px;
    overflow-y:auto;
}

.logo{
    color:#fff;
    font-size:28px;
    font-weight:800;
    text-align:center;
    margin-bottom:35px;
}

.logo span{
    color:#38bdf8;
}

.menu-title{
    color:#94a3b8;
    font-size:12px;
    margin-bottom:12px;
    text-transform:uppercase;
    letter-spacing:1px;
}

.menu-link{
    display:flex;
    align-items:center;
    gap:14px;
    padding:14px 18px;
    border-radius:14px;
    text-decoration:none;
    color:#e2e8f0;
    margin-bottom:10px;
    transition:.3s;
    font-size:15px;
    font-weight:600;
}

.menu-link i{
    width:22px;
    text-align:center;
}

.menu-link:hover,
.menu-link.active{
    background:linear-gradient(135deg,#0ea5e9,#2563eb);
    color:#fff;
    transform:translateX(4px);
}

/* ================= MAIN ================= */

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

/* ================= TOPBAR ================= */

.topbar{
    background:#fff;
    border-radius:20px;
    padding:20px 25px;
    box-shadow:0 5px 20px rgba(0,0,0,.05);
    margin-bottom:25px;
}

.welcome{
    font-size:28px;
    font-weight:800;
    color:#0f172a;
}

.sub-text{
    color:#64748b;
    margin-top:5px;
}

/* ================= CARDS ================= */

.stats-card{
    background:#fff;
    border-radius:22px;
    padding:25px;
    position:relative;
    overflow:hidden;
    box-shadow:0 10px 25px rgba(0,0,0,.05);
    transition:.3s;
    height:100%;
}

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

.stats-card::before{
    content:'';
    position:absolute;
    width:120px;
    height:120px;
    background:rgba(59,130,246,.08);
    border-radius:50%;
    top:-40px;
    right:-40px;
}

.icon-box{
    width:65px;
    height:65px;
    border-radius:18px;
    display:flex;
    align-items:center;
    justify-content:center;
    font-size:28px;
    margin-bottom:18px;
    color:#fff;
}

.bg1{
    background:linear-gradient(135deg,#0ea5e9,#2563eb);
}

.bg2{
    background:linear-gradient(135deg,#10b981,#059669);
}

.bg3{
    background:linear-gradient(135deg,#f59e0b,#ea580c);
}

.bg4{
    background:linear-gradient(135deg,#8b5cf6,#7c3aed);
}

.card-title{
    color:#64748b;
    font-size:14px;
    margin-bottom:8px;
}

.card-value{
    font-size:32px;
    font-weight:800;
    color:#0f172a;
}

/* ================= TABLE ================= */

.table-box{
    background:#fff;
    border-radius:22px;
    padding:25px;
    margin-top:25px;
    box-shadow:0 10px 25px rgba(0,0,0,.05);
}

.table-title{
    font-size:22px;
    font-weight:700;
    margin-bottom:20px;
}

.table thead{
    background:#0f172a;
    color:#fff;
}

.table th{
    border:none;
    padding:14px;
}

.table td{
    padding:14px;
    vertical-align:middle;
}

/* ================= BADGE ================= */

.badge-status{
    padding:7px 14px;
    border-radius:30px;
    font-size:12px;
    font-weight:700;
}

.active-badge{
    background:#dcfce7;
    color:#166534;
}

.inactive-badge{
    background:#fee2e2;
    color:#991b1b;
}

/* ================= MOBILE ================= */

@media(max-width:991px){

    .sidebar{
        width:100%;
        height:auto;
        position:relative;
    }

    .main{
        margin-left:0;
    }
}

</style>

</head>

<body>

<!-- ================= SIDEBAR ================= -->

<div class="sidebar">

<div class="logo">
Admin<span>Panel</span>
</div>

<div class="menu-title">
Main Menu
</div>

<a href="#" class="menu-link active">
<i class="fa-solid fa-gauge"></i>
Dashboard
</a>
<a href="adminhis.php" class="menu-link ">
<i class="fa-solid fa-gauge"></i>
Today History
</a>

<a href="adminshow.php" class="menu-link">
<i class="fa-solid fa-users"></i>
All Users
</a>

<a href="AddRetailer.php" class="menu-link">
<i class="fa-solid fa-user-plus"></i>
Add Retailer
</a>
<a href="AddDistributor.php" class="menu-link">
<i class="fa-solid fa-user-plus"></i>
Add Distributor
</a>



<!------<a href="api_settings.php" class="menu-link">
<i class="fa-solid fa-gears"></i>
API Settings
</a>---->

<a href="service.php" class="menu-link">
<i class="fa-solid fa-layer-group"></i>
Services
</a>

<a href="logout.php" class="menu-link">
<i class="fa-solid fa-right-from-bracket"></i>
Logout
</a>

</div>

<!-- ================= MAIN CONTENT ================= -->

<div class="main">

<!-- ================= TOPBAR ================= -->

<div class="topbar">

<div class="d-flex justify-content-between align-items-center flex-wrap gap-3">

<div>

<div class="welcome">
Welcome Admin 
</div>

<div class="sub-text">
Manage users, services, APIs and wallet system
</div>

</div>

<div>

<span class="badge bg-primary p-3 fs-6">
<?= htmlspecialchars($user['name']) ?>
</span>

</div>

</div>

</div>

<!-- ================= STATS ================= -->

<div class="row g-4">

<div class="col-lg-3 col-md-6">

<div class="stats-card">

<div class="icon-box bg1">
<i class="fa-solid fa-users"></i>
</div>

<div class="card-title">
Total Users
</div>

<div class="card-value">
<?= $totalUsers ?>
</div>

</div>

</div>

<div class="col-lg-3 col-md-6">

<div class="stats-card">

<div class="icon-box bg2">
<i class="fa-solid fa-wallet"></i>
</div>

<div class="card-title">
Total Wallet Balance
</div>

<div class="card-value">
₹<?= number_format($totalBalance,2) ?>
</div>

</div>

</div>

<div class="col-lg-3 col-md-6">

<div class="stats-card">

<div class="icon-box bg3">
<i class="fa-solid fa-gears"></i>
</div>

<div class="card-title">
Total APIs
</div>

<div class="card-value">
<?= $totalApi ?>
</div>

</div>

</div>

<div class="col-lg-3 col-md-6">

<div class="stats-card">

<div class="icon-box bg4">
<i class="fa-solid fa-clock-rotate-left"></i>
</div>

<div class="card-title">
Wallet History
</div>

<div class="card-value">
<?= $totalHistory ?>
</div>

</div>

</div>

</div>

<!-- ================= RECENT USERS ================= -->

<div class="table-box">

<div class="table-title">
Recent Users
</div>

<div class="table-responsive">

<table class="table align-middle">

<thead>

<tr>
<th>ID</th>
<th>Name</th>
<th>Phone</th>
<th>Type</th>
<th>Balance</th>
<th>Status</th>
</tr>

</thead>

<tbody>

<?php while($u = mysqli_fetch_assoc($recentUsers)){ ?>

<tr>

<td>
#<?= $u['id'] ?>
</td>

<td>
<?= htmlspecialchars($u['name']) ?>
</td>

<td>
<?= htmlspecialchars($u['phone']) ?>
</td>

<td>

<span class="badge bg-primary">

<?= htmlspecialchars($u['type']) ?>

</span>

</td>

<td>
₹<?= number_format($u['balance'],2) ?>
</td>

<td>

<?php if($u['status'] == 1){ ?>

<span class="badge-status active-badge">
Active
</span>

<?php } else { ?>

<span class="badge-status inactive-badge">
Inactive
</span>

<?php } ?>

</td>

</tr>

<?php } ?>

</tbody>

</table>

</div>

</div>

</div>

</body>
</html>