<?php
session_start();
require_once 'includes/auth.php';
requireAdmin();
require_once 'config/db.php';

// ---- Stats ----
$total_products = (int) $pdo->query("SELECT COUNT(*) FROM products")->fetchColumn();
$total_stock    = (int)($pdo->query("SELECT COALESCE(SUM(current_stock), 0) FROM products")->fetchColumn());
$out_of_stock   = (int) $pdo->query("SELECT COUNT(*) FROM products WHERE current_stock = 0")->fetchColumn();
$low_stock_count = (int) $pdo->query("SELECT COUNT(*) FROM products WHERE current_stock > 0 AND current_stock < 10")->fetchColumn();

// ---- Stock summary (all products, lowest stock first) ----
$products = $pdo->query(
    "SELECT name, unit, current_stock FROM products ORDER BY current_stock ASC"
)->fetchAll();

// ---- Recent 6 transactions ----
$recent = $pdo->query("
    SELECT st.quantity, st.type, st.created_at, p.name AS product_name
    FROM   stock_transactions st
    JOIN   products p ON p.id = st.product_id
    ORDER  BY st.created_at DESC
    LIMIT  6
")->fetchAll();

include 'includes/header.php';
?>

<!-- Page heading -->
<div class="row mb-4">
    <div class="col">
        <h4 class="page-title"><i class="bi bi-speedometer2 me-2 text-primary"></i>Dashboard</h4>
        <p class="text-muted small mb-0">Welcome back, <strong><?= htmlspecialchars($_SESSION['username']) ?></strong></p>
    </div>
    <div class="col-auto">
        <span class="text-muted small"><?= date('l, d M Y') ?></span>
    </div>
</div>

<!-- ===== Stat Cards ===== -->
<div class="row g-3 mb-4">

    <div class="col-6 col-md-3">
        <div class="card stat-card border-primary h-100">
            <div class="card-body d-flex justify-content-between align-items-center">
                <div>
                    <p class="text-muted small mb-1">Total Products</p>
                    <h2 class="fw-bold text-primary mb-0"><?= $total_products ?></h2>
                </div>
                <i class="bi bi-box stat-icon text-primary"></i>
            </div>
        </div>
    </div>

    <div class="col-6 col-md-3">
        <div class="card stat-card border-success h-100">
            <div class="card-body d-flex justify-content-between align-items-center">
                <div>
                    <p class="text-muted small mb-1">Total Stock</p>
                    <h2 class="fw-bold text-success mb-0"><?= number_format($total_stock) ?></h2>
                </div>
                <i class="bi bi-stack stat-icon text-success"></i>
            </div>
        </div>
    </div>

    <div class="col-6 col-md-3">
        <div class="card stat-card <?= $low_stock_count > 0 ? 'border-warning' : 'border-success' ?> h-100">
            <div class="card-body d-flex justify-content-between align-items-center">
                <div>
                    <p class="text-muted small mb-1">Low Stock</p>
                    <h2 class="fw-bold <?= $low_stock_count > 0 ? 'text-warning' : 'text-success' ?> mb-0">
                        <?= $low_stock_count ?>
                    </h2>
                </div>
                <i class="bi bi-exclamation-triangle stat-icon <?= $low_stock_count > 0 ? 'text-warning' : 'text-success' ?>"></i>
            </div>
        </div>
    </div>

    <div class="col-6 col-md-3">
        <div class="card stat-card <?= $out_of_stock > 0 ? 'border-danger' : 'border-success' ?> h-100">
            <div class="card-body d-flex justify-content-between align-items-center">
                <div>
                    <p class="text-muted small mb-1">Out of Stock</p>
                    <h2 class="fw-bold <?= $out_of_stock > 0 ? 'text-danger' : 'text-success' ?> mb-0">
                        <?= $out_of_stock ?>
                    </h2>
                </div>
                <i class="bi bi-x-circle stat-icon <?= $out_of_stock > 0 ? 'text-danger' : 'text-success' ?>"></i>
            </div>
        </div>
    </div>

</div>
<!-- ===== End Stat Cards ===== -->

<div class="row g-4">

    <!-- ===== Stock Summary Table ===== -->
    <div class="col-lg-7">
        <div class="card h-100">
            <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
                <span><i class="bi bi-bar-chart-line me-1"></i>Current Stock Summary</span>
                <a href="product_list.php" class="btn btn-sm btn-light text-primary fw-semibold">View All</a>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Product</th>
                                <th>Unit</th>
                                <th class="text-end">Stock</th>
                                <th class="text-center">Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (empty($products)): ?>
                            <tr>
                                <td colspan="5" class="text-center text-muted py-4">
                                    No products yet.
                                    <a href="add_product.php">Add your first product</a>
                                </td>
                            </tr>
                            <?php else: ?>
                            <?php foreach ($products as $i => $p): ?>
                            <tr class="<?= $p['current_stock'] < 10 ? 'low-stock' : '' ?>">
                                <td class="text-muted small"><?= $i + 1 ?></td>
                                <td class="fw-semibold"><?= htmlspecialchars($p['name']) ?></td>
                                <td class="text-muted"><?= htmlspecialchars($p['unit']) ?></td>
                                <td class="text-end fw-bold">
                                    <span class="<?= $p['current_stock'] === 0 ? 'text-danger' : ($p['current_stock'] < 10 ? 'text-warning' : 'text-success') ?>">
                                        <?= number_format($p['current_stock']) ?>
                                    </span>
                                </td>
                                <td class="text-center">
                                    <?php if ($p['current_stock'] === 0): ?>
                                        <span class="badge bg-danger">Out of Stock</span>
                                    <?php elseif ($p['current_stock'] < 10): ?>
                                        <span class="badge bg-warning text-dark">Low Stock</span>
                                    <?php else: ?>
                                        <span class="badge bg-success">In Stock</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- ===== Recent Transactions ===== -->
    <div class="col-lg-5">
        <div class="card h-100">
            <div class="card-header bg-secondary text-white d-flex justify-content-between align-items-center">
                <span><i class="bi bi-clock-history me-1"></i>Recent Transactions</span>
                <a href="stock_history.php" class="btn btn-sm btn-light text-secondary fw-semibold">View All</a>
            </div>
            <div class="card-body p-0">
                <?php if (empty($recent)): ?>
                <div class="text-center text-muted py-4">
                    No transactions yet.
                </div>
                <?php else: ?>
                <ul class="list-group list-group-flush">
                    <?php foreach ($recent as $r): ?>
                    <li class="list-group-item d-flex justify-content-between align-items-center px-3 py-2">
                        <div>
                            <div class="fw-semibold small"><?= htmlspecialchars($r['product_name']) ?></div>
                            <div class="text-muted" style="font-size:0.78rem;">
                                <?= date('d M Y', strtotime($r['created_at'])) ?>
                            </div>
                        </div>
                        <div class="text-end">
                            <?php if ($r['type'] === 'added'): ?>
                                <span class="badge bg-success fs-6">+<?= $r['quantity'] ?></span>
                            <?php else: ?>
                                <span class="badge bg-danger fs-6">-<?= $r['quantity'] ?></span>
                            <?php endif; ?>
                        </div>
                    </li>
                    <?php endforeach; ?>
                </ul>
                <?php endif; ?>
            </div>
        </div>
    </div>

</div>

<!-- ===== Quick Action Buttons ===== -->
<div class="row mt-4">
    <div class="col-12">
        <div class="card">
            <div class="card-body">
                <h6 class="text-muted mb-3 fw-semibold">QUICK ACTIONS</h6>
                <div class="d-flex flex-wrap gap-2">
                    <a href="add_product.php"  class="btn btn-primary">
                        <i class="bi bi-plus-circle me-1"></i> Add Product
                    </a>
                    <a href="stock_add.php"    class="btn btn-success">
                        <i class="bi bi-arrow-up-circle me-1"></i> Add Stock
                    </a>
                    <a href="stock_deduct.php" class="btn btn-warning text-white">
                        <i class="bi bi-arrow-down-circle me-1"></i> Deduct Stock
                    </a>
                    <a href="stock_history.php" class="btn btn-outline-secondary">
                        <i class="bi bi-clock-history me-1"></i> View History
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

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