<?php
require_once '../includes/config.php';
require_once '../includes/db.php';
require_once '../includes/functions.php';
require_once 'includes/admin-functions.php';

// Check if user is logged in and is admin
if (!is_admin()) {
    header('Location: /admin/login.php');
    exit;
}

// Get statistics
$total_products = get_row("SELECT COUNT(*) as count FROM products")['count'];
$total_orders = get_row("SELECT COUNT(*) as count FROM orders")['count'];
$total_sales = get_row("SELECT SUM(total) as total FROM orders WHERE status IN ('delivered', 'shipped', 'completed')")['total'] ?: 0;
$total_customers = get_row("SELECT COUNT(*) as count FROM users WHERE role = 'customer'")['count'];

// Get recent orders
$recent_orders = get_rows("
    SELECT o.*, COUNT(oi.id) as item_count 
    FROM orders o 
    LEFT JOIN order_items oi ON o.id = oi.order_id 
    GROUP BY o.id 
    ORDER BY o.created_at DESC 
    LIMIT 5
");

// Get top selling products
$top_products = get_rows("
    SELECT p.id, p.name, p.slug, SUM(oi.quantity) as sold_count 
    FROM products p 
    JOIN order_items oi ON p.id = oi.product_id 
    GROUP BY p.id 
    ORDER BY sold_count DESC 
    LIMIT 5
");

// Get low stock products
$low_stock_products = get_rows("
    SELECT id, name, slug, stock 
    FROM products 
    WHERE stock <= 5 AND stock > 0 
    ORDER BY stock ASC 
    LIMIT 5
");

// Get page title
$page_title = 'Dashboard';

// Include header
require_once 'includes/header.php';
?>

<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1 class="m-0">Dashboard</h1>
            </div>
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item active">Dashboard</li>
                </ol>
            </div>
        </div>
    </div>
</div>

<section class="content">
    <div class="container-fluid">
        <!-- Statistics -->
        <div class="row">
            <div class="col-lg-3 col-6">
                <div class="small-box bg-info">
                    <div class="inner">
                        <h3><?php echo number_format($total_products); ?></h3>
                        <p>Total Products</p>
                    </div>
                    <div class="icon">
                        <i class="fas fa-box"></i>
                    </div>
                    <a href="/admin/products/" class="small-box-footer">
                        More info <i class="fas fa-arrow-circle-right"></i>
                    </a>
                </div>
            </div>
            
            <div class="col-lg-3 col-6">
                <div class="small-box bg-success">
                    <div class="inner">
                        <h3><?php echo number_format($total_orders); ?></h3>
                        <p>Total Orders</p>
                    </div>
                    <div class="icon">
                        <i class="fas fa-shopping-cart"></i>
                    </div>
                    <a href="/admin/orders/" class="small-box-footer">
                        More info <i class="fas fa-arrow-circle-right"></i>
                    </a>
                </div>
            </div>
            
            <div class="col-lg-3 col-6">
                <div class="small-box bg-warning">
                    <div class="inner">
                        <h3><?php echo format_price($total_sales); ?></h3>
                        <p>Total Sales</p>
                    </div>
                    <div class="icon">
                        <i class="fas fa-chart-line"></i>
                    </div>
                    <a href="/admin/reports/" class="small-box-footer">
                        More info <i class="fas fa-arrow-circle-right"></i>
                    </a>
                </div>
            </div>
            
            <div class="col-lg-3 col-6">
                <div class="small-box bg-danger">
                    <div class="inner">
                        <h3><?php echo number_format($total_customers); ?></h3>
                        <p>Customers</p>
                    </div>
                    <div class="icon">
                        <i class="fas fa-users"></i>
                    </div>
                    <a href="/admin/users/" class="small-box-footer">
                        More info <i class="fas fa-arrow-circle-right"></i>
                    </a>
                </div>
            </div>
        </div>
        
        <!-- Recent Orders -->
        <div class="row">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title">Recent Orders</h3>
                        <div class="card-tools">
                            <a href="/admin/orders/" class="btn btn-tool">
                                <i class="fas fa-list"></i> View All
                            </a>
                        </div>
                    </div>
                    <div class="card-body p-0">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>Order #</th>
                                    <th>Customer</th>
                                    <th>Items</th>
                                    <th>Total</th>
                                    <th>Status</th>
                                    <th>Date</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if (empty($recent_orders)): ?>
                                <tr>
                                    <td colspan="6" class="text-center">No orders found</td>
                                </tr>
                                <?php else: ?>
                                    <?php foreach ($recent_orders as $order): ?>
                                    <tr>
                                        <td>
                                            <a href="/admin/orders/view.php?id=<?php echo $order['id']; ?>">
                                                <?php echo $order['order_number']; ?>
                                            </a>
                                        </td>
                                        <td><?php echo $order['name']; ?></td>
                                        <td><?php echo $order['item_count']; ?></td>
                                        <td><?php echo format_price($order['total']); ?></td>
                                        <td>
                                            <span class="badge badge-<?php echo get_order_status_class($order['status']); ?>">
                                                <?php echo ucfirst($order['status']); ?>
                                            </span>
                                        </td>
                                        <td><?php echo date('d.m.Y', strtotime($order['created_at'])); ?></td>
                                    </tr>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            
            <div class="col-md-4">
                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title">Top Selling Products</h3>
                    </div>
                    <div class="card-body p-0">
                        <ul class="products-list product-list-in-card pl-2 pr-2">
                            <?php if (empty($top_products)): ?>
                            <li class="item">
                                <div class="product-info">
                                    <div class="product-title">No products found</div>
                                </div>
                            </li>
                            <?php else: ?>
                                <?php foreach ($top_products as $product): ?>
                                <li class="item">
                                    <div class="product-info">
                                        <a href="/<?php echo $product['slug']; ?>" class="product-title">
                                            <?php echo $product['name']; ?>
                                            <span class="badge badge-info float-right"><?php echo $product['sold_count']; ?> sold</span>
                                        </a>
                                    </div>
                                </li>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </ul>
                    </div>
                </div>
                
                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title">Low Stock Products</h3>
                    </div>
                    <div class="card-body p-0">
                        <ul class="products-list product-list-in-card pl-2 pr-2">
                            <?php if (empty($low_stock_products)): ?>
                            <li class="item">
                                <div class="product-info">
                                    <div class="product-title">No low stock products</div>
                                </div>
                            </li>
                            <?php else: ?>
                                <?php foreach ($low_stock_products as $product): ?>
                                <li class="item">
                                    <div class="product-info">
                                        <a href="/<?php echo $product['slug']; ?>" class="product-title">
                                            <?php echo $product['name']; ?>
                                            <span class="badge badge-danger float-right"><?php echo $product['stock']; ?> in stock</span>
                                        </a>
                                    </div>
                                </li>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

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