<?php
session_start();
require_once '../config/database.php';

if (!isset($_SESSION['admin_id'])) {
    header('Location: login.php');
    exit;
}

$stmt = $pdo->query("SELECT COUNT(*) as total_users FROM users");
$totalUsers = $stmt->fetchColumn();

$stmt = $pdo->query("SELECT COUNT(*) as total_products FROM products WHERE is_active = 1");
$totalProducts = $stmt->fetchColumn();

$stmt = $pdo->query("SELECT COUNT(*) as total_orders FROM orders");
$totalOrders = $stmt->fetchColumn();

$stmt = $pdo->query("SELECT SUM(total_coins) as total_revenue FROM orders WHERE status = 'completed'");
$totalRevenue = $stmt->fetchColumn() ?: 0;

$stmt = $pdo->query("SELECT id, username, email, full_name, coins, created_at FROM users ORDER BY created_at DESC LIMIT 5");
$recentUsers = $stmt->fetchAll(PDO::FETCH_ASSOC);

$stmt = $pdo->query("
    SELECT o.id, o.quantity, o.total_coins, o.status, o.created_at,
           u.username, p.name as product_name
    FROM orders o
    JOIN users u ON o.user_id = u.id
    JOIN products p ON o.product_id = p.id
    ORDER BY o.created_at DESC
    LIMIT 5
");
$recentOrders = $stmt->fetchAll(PDO::FETCH_ASSOC);

$welcomeName = $_SESSION['admin_username'] ?? 'مدیر';
?>

<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>پنل مدیریت - داشبورد</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.rtl.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Vazirmatn:wght@400;600;700&display=swap" rel="stylesheet">
    <link href="assets/css/admin.css" rel="stylesheet">
</head>
<body class="admin-app">
    <?php include 'includes/menu.php'; ?>
    <?php include 'includes/header_bar.php'; ?>

    <main class="main">
        <div class="main-content">
            <?php
            $admin_page_title = 'داشبورد';
            $admin_page_icon = 'fas fa-tachometer-alt';
            include 'includes/topbar.php';
            ?>

            <div class="card welcome-card mb-4">
                <div class="card-body d-flex flex-wrap align-items-center justify-content-between gap-3">
                    <div class="welcome-content">
                        <div class="welcome-greeting">روز بخیر،</div>
                        <div class="welcome-name"><?= htmlspecialchars($welcomeName) ?>!</div>
                        <div class="welcome-date">
                            <i class="far fa-calendar-alt" aria-hidden="true"></i>
                            <span><?= date('Y/m/d H:i:s') ?></span>
                        </div>
                    </div>
                    <div class="welcome-illustration" aria-hidden="true">
                        <i class="fas fa-chart-line"></i>
                    </div>
                </div>
            </div>

            <div class="row g-3 mb-4">
                <div class="col-lg-3 col-md-6">
                    <div class="card h-100">
                        <div class="card-body">
                            <div class="stat-card-header">
                                <div class="stat-card-label">کاربران</div>
                                <div class="stat-card-icon primary"><i class="fas fa-users"></i></div>
                            </div>
                            <div class="stat-card-value"><?= number_format($totalUsers) ?></div>
                            <div class="stat-card-change"><span>کل کاربران ثبت‌شده</span></div>
                        </div>
                    </div>
                </div>
                <div class="col-lg-3 col-md-6">
                    <div class="card h-100">
                        <div class="card-body">
                            <div class="stat-card-header">
                                <div class="stat-card-label">محصولات</div>
                                <div class="stat-card-icon success"><i class="fas fa-box"></i></div>
                            </div>
                            <div class="stat-card-value"><?= number_format($totalProducts) ?></div>
                            <div class="stat-card-change"><span>محصول فعال</span></div>
                        </div>
                    </div>
                </div>
                <div class="col-lg-3 col-md-6">
                    <div class="card h-100">
                        <div class="card-body">
                            <div class="stat-card-header">
                                <div class="stat-card-label">سفارشات</div>
                                <div class="stat-card-icon info"><i class="fas fa-shopping-cart"></i></div>
                            </div>
                            <div class="stat-card-value"><?= number_format($totalOrders) ?></div>
                            <div class="stat-card-change"><span>همه سفارش‌ها</span></div>
                        </div>
                    </div>
                </div>
                <div class="col-lg-3 col-md-6">
                    <div class="card h-100">
                        <div class="card-body">
                            <div class="stat-card-header">
                                <div class="stat-card-label">درآمد سکه</div>
                                <div class="stat-card-icon warning"><i class="fas fa-coins"></i></div>
                            </div>
                            <div class="stat-card-value"><?= number_format($totalRevenue) ?></div>
                            <div class="stat-card-change"><span>سفارش‌های تکمیل‌شده</span></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="row g-3">
                <div class="col-lg-6">
                    <div class="card h-100">
                        <div class="card-header py-3">
                            <h6 class="m-0 fw-semibold"><i class="fas fa-users ms-2 text-accent"></i>کاربران اخیر</h6>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-hover align-middle mb-0">
                                    <thead>
                                        <tr>
                                            <th>نام کاربری</th>
                                            <th>ایمیل</th>
                                            <th>سکه</th>
                                            <th>تاریخ</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($recentUsers as $user): ?>
                                        <tr>
                                            <td><?= htmlspecialchars($user['username']) ?></td>
                                            <td><?= htmlspecialchars($user['email']) ?></td>
                                            <td><?= number_format($user['coins']) ?></td>
                                            <td><?= date('Y/m/d', strtotime($user['created_at'])) ?></td>
                                        </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-lg-6">
                    <div class="card h-100">
                        <div class="card-header py-3">
                            <h6 class="m-0 fw-semibold"><i class="fas fa-shopping-cart ms-2 text-accent"></i>سفارشات اخیر</h6>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-hover align-middle mb-0">
                                    <thead>
                                        <tr>
                                            <th>کاربر</th>
                                            <th>محصول</th>
                                            <th>مبلغ</th>
                                            <th>وضعیت</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($recentOrders as $order): ?>
                                        <tr>
                                            <td><?= htmlspecialchars($order['username']) ?></td>
                                            <td><?= htmlspecialchars($order['product_name']) ?></td>
                                            <td><?= number_format($order['total_coins']) ?> سکه</td>
                                            <td>
                                                <span class="badge bg-<?= $order['status'] === 'completed' ? 'success' : ($order['status'] === 'pending' ? 'warning' : 'danger') ?>">
                                                    <?= $order['status'] === 'completed' ? 'تکمیل' : ($order['status'] === 'pending' ? 'در انتظار' : 'لغو') ?>
                                                </span>
                                            </td>
                                        </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <?php include 'includes/admin_shell_scripts.php'; ?>
</body>
</html>
