<?php
// admin/index.php
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/functions.php';

// Check admin session
if (!isset($_SESSION['admin_id']) || $_SESSION['admin_role'] !== 'admin') {
    header('Location: login.php');
    exit;
}

// Get stats
$stats = [];

// Total products
$stmt = $pdo->query("SELECT COUNT(*) as total FROM products WHERE status = 1");
$stats['products'] = $stmt->fetch()['total'];

// Total orders
$stmt = $pdo->query("SELECT COUNT(*) as total FROM orders");
$stats['orders'] = $stmt->fetch()['total'];

// Total users
$stmt = $pdo->query("SELECT COUNT(*) as total FROM users WHERE role = 'user'");
$stats['users'] = $stmt->fetch()['total'];

// Pending orders
$stmt = $pdo->query("SELECT COUNT(*) as total FROM orders WHERE order_status = 'pending'");
$stats['pending_orders'] = $stmt->fetch()['total'];

// Total revenue
$stmt = $pdo->query("SELECT SUM(total) as total FROM orders WHERE order_status != 'cancelled'");
$stats['revenue'] = $stmt->fetch()['total'] ?: 0;

// Recent orders
$stmt = $pdo->query("SELECT * FROM orders ORDER BY created_at DESC LIMIT 10");
$recent_orders = $stmt->fetchAll();

// Recent products
$stmt = $pdo->query("SELECT * FROM products ORDER BY created_at DESC LIMIT 5");
$recent_products = $stmt->fetchAll();

include 'header.php';
?>

<div class="admin-content">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>Dashboard</h2>
        <span class="text-muted">Welcome back, <?php echo $_SESSION['admin_name'] ?? 'Admin'; ?>!</span>
    </div>
    
    <!-- Stats Cards -->
    <div class="row g-4">
        <div class="col-lg-3 col-md-6">
            <div class="stat-card bg-primary text-white">
                <div class="stat-icon"><i class="fa-solid fa-box"></i></div>
                <div class="stat-info">
                    <h3><?php echo $stats['products']; ?></h3>
                    <p>Total Products</p>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6">
            <div class="stat-card bg-success text-white">
                <div class="stat-icon"><i class="fa-solid fa-shopping-cart"></i></div>
                <div class="stat-info">
                    <h3><?php echo $stats['orders']; ?></h3>
                    <p>Total Orders</p>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6">
            <div class="stat-card bg-warning text-white">
                <div class="stat-icon"><i class="fa-solid fa-users"></i></div>
                <div class="stat-info">
                    <h3><?php echo $stats['users']; ?></h3>
                    <p>Total Users</p>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6">
            <div class="stat-card bg-danger text-white">
                <div class="stat-icon"><i class="fa-solid fa-clock"></i></div>
                <div class="stat-info">
                    <h3><?php echo $stats['pending_orders']; ?></h3>
                    <p>Pending Orders</p>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6">
            <div class="stat-card bg-info text-white">
                <div class="stat-icon"><i class="fa-solid fa-dollar-sign"></i></div>
                <div class="stat-info">
                    <h3><?php echo number_format($stats['revenue']); ?> TK</h3>
                    <p>Total Revenue</p>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Recent Orders -->
    <div class="row mt-4">
        <div class="col-lg-8">
            <div class="admin-card">
                <h5 class="card-title">Recent Orders</h5>
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>Invoice</th>
                                <th>Customer</th>
                                <th>Total</th>
                                <th>Status</th>
                                <th>Date</th>
                                <th>Action</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><strong><?php echo $order['invoice_no']; ?></strong></td>
                                <td><?php echo $order['name']; ?></td>
                                <td><?php echo number_format($order['total']); ?> TK</td>
                                <td>
                                    <span class="badge bg-<?php echo $order['order_status'] == 'delivered' ? 'success' : ($order['order_status'] == 'cancelled' ? 'danger' : 'warning'); ?>">
                                        <?php echo ucfirst($order['order_status']); ?>
                                    </span>
                                </td>
                                <td><?php echo date('d M Y', strtotime($order['created_at'])); ?></td>
                                <td>
                                    <a href="order-details.php?id=<?php echo $order['id']; ?>" class="btn btn-sm btn-primary">
                                        <i class="fa-solid fa-eye"></i>
                                    </a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <div class="col-lg-4">
            <div class="admin-card">
                <h5 class="card-title">Recent Products</h5>
                <?php if (empty($recent_products)): ?>
                <p class="text-muted">No products found</p>
                <?php else: ?>
                <?php foreach ($recent_products as $product): ?>
                <div class="recent-item">
                    <img src="../assets/images/products/<?php echo $product['image'] ?: 'default.jpg'; ?>" alt="<?php echo $product['name']; ?>">
                    <div>
                        <a href="edit-product.php?id=<?php echo $product['id']; ?>"><?php echo $product['name']; ?></a>
                        <p><?php echo number_format($product['sale_price'] ?: $product['price']); ?> TK</p>
                    </div>
                </div>
                <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<style>
.stat-card {
    padding: 20px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    gap: 15px;
    transition: transform 0.3s ease;
}

.stat-card:hover {
    transform: translateY(-5px);
}

.stat-icon {
    font-size: 32px;
    opacity: 0.8;
}

.stat-info h3 {
    margin: 0;
    font-size: 28px;
    font-weight: 700;
}

.stat-info p {
    margin: 0;
    opacity: 0.8;
    font-size: 14px;
}

.admin-card {
    background: #fff;
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.recent-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 0;
    border-bottom: 1px solid #f1f5f9;
}

.recent-item:last-child {
    border-bottom: none;
}

.recent-item img {
    width: 50px;
    height: 50px;
    object-fit: cover;
    border-radius: 8px;
}

.recent-item a {
    font-weight: 500;
    transition: color 0.3s ease;
}

.recent-item a:hover {
    color: #047dc3;
}

.recent-item p {
    margin: 0;
    font-size: 14px;
    color: #047dc3;
    font-weight: 600;
}
</style>

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