<?php include 'header.php'; ?>

<style>
/* ========= PROFESSIONAL ADMIN DASHBOARD ========= */

.msp-card{
    background:#fff;
    border:1px solid #e3e3e3;
    border-radius:6px;
    overflow:hidden;
    height:100%;
    box-shadow:0 1px 2px rgba(0,0,0,0.08);
    transition:0.2s;
}
.msp-card:hover{ box-shadow:0 4px 12px rgba(0,0,0,0.12); }

.msp-head{
    background:#f7f7f7;
    border-bottom:1px solid #e3e3e3;
    padding:10px 12px;
    font-size:14px;
    font-weight:600;
    display:flex;
    align-items:center;
    gap:8px;
}

.msp-body{ padding:22px 10px; text-align:center; }
.msp-body h1{ margin:0;font-size:40px;font-weight:800;color:#000; }

.msp-foot{
    background:#fafafa;
    border-top:1px solid #e3e3e3;
    padding:9px;
    text-align:center;
    font-size:13px;
    font-weight:600;
}

.quick-box{
    background:#fff;
    border:1px solid #e3e3e3;
    border-radius:6px;
    padding:14px;
    box-shadow:0 1px 2px rgba(0,0,0,0.08);
}

.table-box{
    background:#fff;
    border:1px solid #e3e3e3;
    border-radius:6px;
    padding:15px;
    box-shadow:0 1px 2px rgba(0,0,0,0.08);
}

.quick-btn{
    display:block;
    padding:10px;
    border:1px solid #ddd;
    border-radius:4px;
    margin-bottom:8px;
    font-weight:600;
    text-decoration:none;
    color:#000;
    background:#fafafa;
}
.quick-btn:hover{ background:#f1f1f1; }

</style>

<div class="container-fluid">

<?php if ($admin["two_factor"] == 0) : ?>
<div class="alert alert-danger d-flex justify-content-between align-items-center">
<div><i class="fa fa-exclamation-circle"></i> 2FACTOR AUTHENTICATION IS DISABLED! TURN ON FOR EXTRA SECURITY.</div>
<a class="btn btn-danger btn-sm" href="<?php echo site_url('admin/activate-google-2fa') ?>">Enable Now</a>
</div>
<?php endif; ?>

<div class="row g-3">

<?php
$today_start = date('Y-m-d 00:00:00');
$today_end = date('Y-m-d 23:59:59');

$q = $conn->prepare("SELECT SUM(payment_amount) total FROM payments WHERE payment_status=3 AND payment_create_date BETWEEN :s AND :e");
$q->execute(["s"=>$today_start,"e"=>$today_end]);
$todayIncome = $q->fetch(PDO::FETCH_ASSOC)['total'] ?? 0;

$q2 = $conn->prepare("SELECT COUNT(*) total FROM orders WHERE order_create BETWEEN :s AND :e");
$q2->execute(["s"=>$today_start,"e"=>$today_end]);
$todayOrders = $q2->fetch(PDO::FETCH_ASSOC)['total'];
?>

<!-- STAT CARDS -->
<?php
$cards = [
["Registered Users","users","clients","/admin/clients"],
["Total Orders","shopping-cart","orders","/admin/orders"],
["Orders Failed","times-circle",$failCount,"/admin/orders/1/fail",true],
["Pending Support","life-ring","tickets","/admin/tickets?search=unread"],
["Total Payments","credit-card","payments","/admin/payments"],
["Admin Notification","bell","0","/admin/tickets"]
];

foreach($cards as $c):
?>
<div class="col-md-3 col-sm-6 mb-3">
<div class="msp-card">
<a href="<?=$c[3]?>">
<div class="msp-head"><i class="fa fa-<?=$c[1]?>"></i> <?=$c[0]?></div>
<div class="msp-body">
<h1>
<?php
if(isset($c[4])) echo $c[2];
else if(is_numeric($c[2])) echo $c[2];
else echo countRow(["table"=>$c[2]]);
?>
</h1>
</div>
<div class="msp-foot">See in Detail →</div>
</a>
</div>
</div>
<?php endforeach; ?>

</div>

<!-- TODAY & QUICK -->
<div class="row mt-3">

<div class="col-md-3 mb-3">
<div class="quick-box">
<h5><b>📊 Today Report</b></h5>
<hr>
<p><b>Today Orders:</b> <?=$todayOrders?></p>
<p><b>Today Income:</b> <?=$settings["site_currency"].$todayIncome?></p>
</div>

<div class="quick-box mt-3">
<h5><b>⚡ Quick Actions</b></h5>
<hr>
<a class="quick-btn" href="/admin/orders">➜ View Orders</a>
<a class="quick-btn" href="/admin/clients">➜ View Users</a>
<a class="quick-btn" href="/admin/services">➜ Services</a>
<a class="quick-btn" href="/admin/payments">➜ Payments</a>
<a class="quick-btn" href="/admin/tickets">➜ Support Tickets</a>
</div>
</div>

<!-- RECENT ORDERS -->
<div class="col-md-9 mb-3">
<div class="table-box">
<h5><b>🧾 Recent Orders</b></h5>
<hr>
<div class="table-responsive">
<table class="table table-bordered table-sm">
<tr>
<th>ID</th><th>User</th><th>Service</th><th>Price</th><th>Status</th><th>Date</th>
</tr>
<?php
$orders = $conn->prepare("SELECT * FROM orders ORDER BY order_id DESC LIMIT 10");
$orders->execute();
foreach($orders as $o):
?>
<tr>
<td>#<?=$o['order_id']?></td>
<td><?=$o['client_id']?></td>
<td><?=$o['service_id']?></td>
<td><?=$o['order_charge']?></td>
<td><?=$o['order_status']?></td>
<td><?=$o['order_create']?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
</div>
</div>

</div>
</div>

<?php include 'footer.php'; ?>