<?php
/**
 * Reports & Statistics Page
 * CMTC Shopping
 */
require_once __DIR__ . '/../includes/header.php';
require_once __DIR__ . '/../includes/navbar.php';

require_login();
require_permission($db, 'view_reports');

// Sales summary
$today_sales = $db->query("SELECT COALESCE(SUM(net_amount), 0) FROM orders WHERE DATE(created_at) = CURDATE() AND status IN ('approved', 'preparing', 'ready', 'received')")->fetchColumn();
$month_sales = $db->query("SELECT COALESCE(SUM(net_amount), 0) FROM orders WHERE MONTH(created_at) = MONTH(CURDATE()) AND YEAR(created_at) = YEAR(CURDATE()) AND status IN ('approved', 'preparing', 'ready', 'received')")->fetchColumn();
$total_sales = $db->query("SELECT COALESCE(SUM(net_amount), 0) FROM orders WHERE status IN ('approved', 'preparing', 'ready', 'received')")->fetchColumn();

// Top selling products
$top_products = $db->query("
    SELECT p.name, p.sku, SUM(oi.quantity) as total_qty, SUM(oi.total) as total_revenue
    FROM order_items oi
    JOIN products p ON oi.product_id = p.id
    JOIN orders o ON oi.order_id = o.id
    WHERE o.status IN ('approved', 'preparing', 'ready', 'received')
    GROUP BY p.id
    ORDER BY total_qty DESC
    LIMIT 10
")->fetchAll();
?>

<div class="d-flex">
    <?php require_once __DIR__ . '/../includes/sidebar.php'; ?>

    <main class="flex-grow-1 p-4" style="background-color: var(--bs-body-bg);">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <div>
                <h3 class="fw-bold mb-0"><i class="bi bi-graph-up text-primary me-2"></i>รายงานสถิติและยอดขาย</h3>
                <p class="text-muted small mb-0">รายงานภาพรวมรายได้ และสินค้ายอดนิยม</p>
            </div>
        </div>

        <div class="row g-3 mb-4">
            <div class="col-md-4">
                <div class="card border-0 shadow-sm glass-card p-4 text-center border-top border-4 border-primary">
                    <span class="text-muted small fw-semibold">ยอดขายวันนี้</span>
                    <h3 class="fw-bold text-primary mt-2 mb-0"><?php echo format_price($today_sales); ?></h3>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card border-0 shadow-sm glass-card p-4 text-center border-top border-4 border-success">
                    <span class="text-muted small fw-semibold">ยอดขายเดือนนี้</span>
                    <h3 class="fw-bold text-success mt-2 mb-0"><?php echo format_price($month_sales); ?></h3>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card border-0 shadow-sm glass-card p-4 text-center border-top border-4 border-info">
                    <span class="text-muted small fw-semibold">ยอดขายรวมทั้งหมด</span>
                    <h3 class="fw-bold text-info mt-2 mb-0"><?php echo format_price($total_sales); ?></h3>
                </div>
            </div>
        </div>

        <div class="card border-0 shadow-sm glass-card p-4">
            <h5 class="fw-bold mb-3"><i class="bi bi-trophy-fill text-warning me-2"></i>10 อันดับสินค้าขายดี</h5>
            <div class="table-responsive">
                <table class="table table-hover align-middle">
                    <thead>
                        <tr>
                            <th>อันดับ</th>
                            <th>รหัสสินค้า (SKU)</th>
                            <th>ชื่อสินค้า</th>
                            <th class="text-center">จำนวนที่ขายได้</th>
                            <th class="text-end">ยอดขายรวม</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (count($top_products) > 0): ?>
                            <?php foreach ($top_products as $idx => $tp): ?>
                                <tr>
                                    <td><span class="badge bg-primary rounded-circle px-2 py-1"><?php echo $idx + 1; ?></span></td>
                                    <td><code><?php echo sanitize($tp['sku']); ?></code></td>
                                    <td class="fw-semibold"><?php echo sanitize($tp['name']); ?></td>
                                    <td class="text-center fw-bold"><?php echo number_format($tp['total_qty']); ?> ชิ้น</td>
                                    <td class="text-end fw-bold text-success"><?php echo format_price($tp['total_revenue']); ?></td>
                                </tr>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="5" class="text-center text-muted py-4">ยังไม่มีข้อมูลการขาย</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </main>
</div>

<?php require_once __DIR__ . '/../includes/footer.php'; ?>
