<?php

declare(strict_types=1);

require_once '../../private/db.php';
require_once '../../private/auth.php';
require_once '../../private/functions.php';

require_login();

$productCount = 0;
$orderCount = 0;
$totalSales = 0;
$lowStockProducts = [];
$recentOrders = [];

$stmt = $pdo->query("SELECT COUNT(*) AS total FROM products");
$row = $stmt->fetch();
$productCount = (int)$row['total'];

$stmt = $pdo->query("SELECT COUNT(*) AS total FROM orders");
$row = $stmt->fetch();
$orderCount = (int)$row['total'];

$stmt = $pdo->query("SELECT SUM(total) AS total_sales FROM orders WHERE status = 'paid'");
$row = $stmt->fetch();
$totalSales = $row['total_sales'] ?? 0;

$stmt = $pdo->query("SELECT * FROM products WHERE quantity <= low_stock_threshold ORDER BY quantity ASC LIMIT 10");
$lowStockProducts = $stmt->fetchAll();

$stmt = $pdo->query("SELECT * FROM orders ORDER BY id DESC LIMIT 10");
$recentOrders = $stmt->fetchAll();

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

<style>

body{
margin:0;
background:#111827;
color:#fff;
font-family:Arial,sans-serif;
}

.topbar{
background:#000;
padding:18px 25px;
display:flex;
justify-content:space-between;
align-items:center;
}

.topbar h1{
margin:0;
font-size:24px;
}

.nav-links a{
color:#fff;
text-decoration:none;
margin-left:20px;
font-size:15px;
}

.container{
padding:25px;
}

.stats-grid{
display:grid;
grid-template-columns:repeat(auto-fit,minmax(250px,1fr));
gap:20px;
margin-bottom:35px;
}

.card{
background:#1f2937;
border-radius:14px;
padding:25px;
}

table{
width:100%;
border-collapse:collapse;
background:#1f2937;
}

table th{
background:#374151;
text-align:left;
padding:14px;
}

table td{
padding:14px;
border-top:1px solid #374151;
}

.view-btn{
display:inline-block;
background:#2563eb;
color:#fff;
padding:8px 14px;
border-radius:6px;
text-decoration:none;
}

</style>

</head>
<body>

<div class='topbar'>

<h1>EpiPos Dashboard</h1>

<div class='nav-links'>
<a href='dashboard.php'>Dashboard</a>
<a href='pos.php'>POS</a>
<a href='inventory.php'>Inventory</a>
<a href='customers.php'>Customers</a>
<a href='reports.php'>Reports</a>
<a href='settings.php'>Settings</a>
<a href='logout.php'>Logout</a>
</div>

</div>

<div class='container'>

<div class='stats-grid'>

<div class='card'>
<h3>Total Products</h3>
<div><?= number_format($productCount) ?></div>
</div>

<div class='card'>
<h3>Total Orders</h3>
<div><?= number_format($orderCount) ?></div>
</div>

<div class='card'>
<h3>Total Sales</h3>
<div>$<?= number_format((float)$totalSales,2) ?></div>
</div>

</div>

<h2>Low Stock Products</h2>

<table>

<tr>
<th>SKU</th>
<th>Product</th>
<th>Qty</th>
</tr>

<?php foreach($lowStockProducts as $product): ?>

<tr>

<td><?= htmlspecialchars($product['sku']) ?></td>

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

<td><?= (int)$product['quantity'] ?></td>

</tr>

<?php endforeach; ?>

</table>

<h2>Recent Orders</h2>

<table>

<tr>
<th>Order #</th>
<th>Payment</th>
<th>Total</th>
<th>Status</th>
<th>Receipt</th>
</tr>

<?php foreach($recentOrders as $order): ?>

<tr>

<td><?= htmlspecialchars($order['order_number']) ?></td>

<td><?= htmlspecialchars($order['payment_method']) ?></td>

<td>$<?= number_format((float)$order['total'],2) ?></td>

<td><?= htmlspecialchars($order['status']) ?></td>

<td>

<a
class="view-btn"
href="view_receipt.php?id=<?= (int)$order['id'] ?>"
    target="_blank">
View
</a>

</td>

</tr>

<?php endforeach; ?>

</table>

</div>

</body>
</html>
