<?php
require_once __DIR__ . '/header.php';

// บังคับสิทธิ์เฉพาะ Admin เท่านั้น
if (!is_admin()) {
    header('Location: index.php');
    exit;
}

// 1. คำนวณสรุปสถิติต่างๆ
$total_sales = $pdo->query("SELECT SUM(total_price) FROM orders WHERE status != 'cancelled'")->fetchColumn() ?? 0;
$total_orders = $pdo->query("SELECT COUNT(*) FROM orders")->fetchColumn() ?? 0;
$pending_orders = $pdo->query("SELECT COUNT(*) FROM orders WHERE status = 'pending'")->fetchColumn() ?? 0;
$shipping_orders = $pdo->query("SELECT COUNT(*) FROM orders WHERE status = 'shipping'")->fetchColumn() ?? 0;
$total_products = $pdo->query("SELECT COUNT(*) FROM products")->fetchColumn() ?? 0;

// 2. ดึงรายการคำสั่งซื้อล่าสุด 5 รายการ
$stmt = $pdo->prepare("
    SELECT o.*, u.fullname 
    FROM orders o 
    JOIN users u ON o.user_id = u.id 
    ORDER BY o.id DESC LIMIT 5
");
$stmt->execute();
$recent_orders = $stmt->fetchAll();

// 3. ดึงสินค้าที่สต็อกต่ำกว่า 5 ชิ้น
$low_stock_products = $pdo->query("SELECT * FROM products WHERE stock <= 5 ORDER BY stock ASC")->fetchAll();
?>

<div class="row mb-4">
    <div class="col-12 d-flex justify-content-between align-items-center flex-wrap gap-2">
        <div>
            <h3 class="fw-bold mb-1"><i class="fa-solid fa-chart-pie text-primary me-2"></i>แผงควบคุมผู้ขาย (Seller Dashboard)</h3>
            <p class="text-muted small mb-0">ยินดีต้อนรับคุณ <?= htmlspecialchars($_SESSION['fullname']) ?> - ภาพรวมการขายและคำสั่งซื้อ</p>
        </div>
        <div class="d-flex gap-2">
            <a href="admin_orders.php" class="btn btn-primary rounded-pill px-3">
                <i class="fa-solid fa-truck-fast me-1"></i> จัดการส่งสินค้า (<?= $pending_orders ?>)
            </a>
            <a href="admin_products.php" class="btn btn-outline-primary rounded-pill px-3">
                <i class="fa-solid fa-box-archive me-1"></i> จัดการสินค้า
            </a>
        </div>
    </div>
</div>

<!-- Stat Cards -->
<div class="row row-cols-1 row-cols-md-2 row-cols-lg-4 g-3 mb-4">
    <div class="col">
        <div class="card border-0 shadow-sm rounded-4 bg-primary text-white p-3 h-100">
            <div class="d-flex align-items-center justify-content-between">
                <div>
                    <span class="small opacity-75 d-block">ยอดขายรวมทั้งหมด</span>
                    <h3 class="fw-bold mb-0"><?= format_price($total_sales) ?></h3>
                </div>
                <div class="bg-white bg-opacity-20 rounded-circle p-3">
                    <i class="fa-solid fa-sack-dollar fs-3"></i>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col">
        <div class="card border-0 shadow-sm rounded-4 bg-warning text-dark p-3 h-100">
            <div class="d-flex align-items-center justify-content-between">
                <div>
                    <span class="small opacity-75 d-block">รอการจัดส่ง (Pending)</span>
                    <h3 class="fw-bold mb-0"><?= $pending_orders ?> รายการ</h3>
                </div>
                <div class="bg-dark bg-opacity-10 rounded-circle p-3">
                    <i class="fa-solid fa-box-open fs-3"></i>
                </div>
            </div>
        </div>
    </div>

    <div class="col">
        <div class="card border-0 shadow-sm rounded-4 bg-info text-white p-3 h-100">
            <div class="d-flex align-items-center justify-content-between">
                <div>
                    <span class="small opacity-75 d-block">กำลังจัดส่ง (Shipping)</span>
                    <h3 class="fw-bold mb-0"><?= $shipping_orders ?> รายการ</h3>
                </div>
                <div class="bg-white bg-opacity-20 rounded-circle p-3">
                    <i class="fa-solid fa-truck-fast fs-3"></i>
                </div>
            </div>
        </div>
    </div>

    <div class="col">
        <div class="card border-0 shadow-sm rounded-4 bg-dark text-white p-3 h-100">
            <div class="d-flex align-items-center justify-content-between">
                <div>
                    <span class="small opacity-75 d-block">จำนวนสินค้าในร้าน</span>
                    <h3 class="fw-bold mb-0"><?= $total_products ?> รายการ</h3>
                </div>
                <div class="bg-white bg-opacity-20 rounded-circle p-3">
                    <i class="fa-solid fa-boxes-stacked fs-3"></i>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row g-4">
    <!-- Recent Orders Table -->
    <div class="col-lg-8">
        <div class="card border-0 shadow-sm rounded-4 overflow-hidden h-100">
            <div class="card-header bg-white py-3 d-flex justify-content-between align-items-center">
                <h5 class="fw-bold mb-0"><i class="fa-solid fa-clock-history me-2 text-primary"></i>คำสั่งซื้อล่าสุด</h5>
                <a href="admin_orders.php" class="btn btn-sm btn-link text-decoration-none">ดูทั้งหมด <i class="fa-solid fa-chevron-right"></i></a>
            </div>
            <div class="table-responsive">
                <table class="table align-middle table-hover mb-0">
                    <thead class="table-light small">
                        <tr>
                            <th>Order ID</th>
                            <th>ผู้ซื้อ</th>
                            <th>ยอดรวม</th>
                            <th>สถานะ</th>
                            <th>การดำเนินการ</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($recent_orders)): ?>
                            <tr><td colspan="5" class="text-center text-muted py-4">ยังไม่มีคำสั่งซื้อเข้ามา</td></tr>
                        <?php else: ?>
                            <?php foreach ($recent_orders as $ord): ?>
                                <tr>
                                    <td class="fw-bold text-primary">#ORDER-<?= str_pad($ord['id'], 5, '0', STR_PAD_LEFT) ?></td>
                                    <td><?= htmlspecialchars($ord['fullname']) ?></td>
                                    <td class="fw-bold"><?= format_price($ord['total_price']) ?></td>
                                    <td><?= render_order_status_badge($ord['status']) ?></td>
                                    <td>
                                        <a href="admin_orders.php?order_id=<?= $ord['id'] ?>" class="btn btn-sm btn-outline-primary rounded-pill px-3">
                                            จัดการส่ง
                                        </a>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Low Stock Alert -->
    <div class="col-lg-4">
        <div class="card border-0 shadow-sm rounded-4 h-100">
            <div class="card-header bg-white py-3">
                <h5 class="fw-bold mb-0 text-danger"><i class="fa-solid fa-triangle-exclamation me-2"></i>สินค้าสต็อกต่ำ (<= 5)</h5>
            </div>
            <div class="card-body p-0">
                <?php if (empty($low_stock_products)): ?>
                    <div class="p-4 text-center text-muted">
                        <i class="fa-solid fa-shield-check display-6 text-success mb-2"></i>
                        <p class="mb-0">สินค้าทุกรายการมีสต็อกเพียงพอ</p>
                    </div>
                <?php else: ?>
                    <ul class="list-group list-group-flush">
                        <?php foreach ($low_stock_products as $p): ?>
                            <li class="list-group-item d-flex justify-content-between align-items-center p-3">
                                <div>
                                    <h6 class="mb-0 fw-semibold"><?= htmlspecialchars($p['name']) ?></h6>
                                    <small class="text-muted">ราคา <?= format_price($p['price']) ?></small>
                                </div>
                                <span class="badge bg-danger rounded-pill px-3 py-2">คงเหลือ <?= $p['stock'] ?> ชิ้น</span>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </div>
            <div class="card-footer bg-white text-center p-3">
                <a href="admin_products.php" class="btn btn-sm btn-outline-danger rounded-pill w-100">เติมสต็อกสินค้า</a>
            </div>
        </div>
    </div>
</div>

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