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

// التحقق من تسجيل الدخول
if (!isset($_SESSION['user_id']) && basename($_SERVER['PHP_SELF']) !== 'login.php') {
    header('Location: login.php');
    exit();
}

$db = new Database();
$pdo = $db->getConnection();

// إحصائيات المبيعات اليوم
$stmt = $pdo->query("
    SELECT COALESCE(SUM(price * quantity), 0) as total 
    FROM sales 
    WHERE date(sale_date) = date('now')
");
$total_sales_today = $stmt->fetchColumn();

// إحصائيات المبيعات هذا الشهر
$stmt = $pdo->query("
    SELECT COALESCE(SUM(price * quantity), 0) as total 
    FROM sales 
    WHERE strftime('%Y-%m', sale_date) = strftime('%Y-%m', 'now')
");
$total_sales_month = $stmt->fetchColumn();

// إجمالي الديون المستحقة
$stmt = $pdo->query("
    SELECT COALESCE(SUM(amount - paid), 0) as total 
    FROM debts
");
$total_debts = $stmt->fetchColumn();

// عدد المنتجات
$stmt = $pdo->query("SELECT COUNT(*) FROM products");
$products_count = $stmt->fetchColumn();

// عدد الزبائن
$stmt = $pdo->query("SELECT COUNT(*) FROM customers");
$customers_count = $stmt->fetchColumn();
?>
<!DOCTYPE html>
<html lang="ar" 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.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="assets/css/style.css" rel="stylesheet">
</head>
<body>
 
    <?php include 'includes\header.php'; ?>

    <div class="container mt-4">
        <div class="row">
            <div class="col-md-4">
                <div class="card bg-primary text-white">
                    <div class="card-body">
                        <h5 class="card-title">إجمالي المبيعات اليوم</h5>
                        <p class="card-text display-6"><?php echo number_format($total_sales_today, 2); ?> DH</p>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card bg-success text-white">
                    <div class="card-body">
                        <h5 class="card-title">إجمالي المبيعات هذا الشهر</h5>
                        <p class="card-text display-6"><?php echo number_format($total_sales_month, 2); ?> DH</p>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card bg-info text-white">
                    <div class="card-body">
                        <h5 class="card-title">إجمالي الديون المستحقة</h5>
                        <p class="card-text display-6"><?php echo number_format($total_debts, 2); ?> DH</p>
                    </div>
                </div>
            </div>
        </div>

        <div class="row mt-4">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-body">
                        <h5 class="card-title">إحصائيات عامة</h5>
                        <ul class="list-group list-group-flush">
                            <li class="list-group-item d-flex justify-content-between align-items-center">
                                عدد المنتجات
                                <span class="badge bg-primary rounded-pill"><?php echo $products_count; ?></span>
                            </li>
                            <li class="list-group-item d-flex justify-content-between align-items-center">
                                عدد الزبائن
                                <span class="badge bg-primary rounded-pill"><?php echo $customers_count; ?></span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
            
            <div class="col-md-6">
                <div class="card">
                    <div class="card-body">
                        <h5 class="card-title">المنتجات منخفضة المخزون</h5>
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>المنتج</th>
                                        <th>الكمية المتبقية</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $stmt = $pdo->query("
                                        SELECT name, quantity 
                                        FROM products 
                                        WHERE quantity < 10 
                                        ORDER BY quantity ASC 
                                        LIMIT 5
                                    ");
                                    while ($product = $stmt->fetch()): ?>
                                        <tr>
                                            <td><?php echo htmlspecialchars($product['name']); ?></td>
                                            <td>
                                                <span class="text-danger">
                                                    <?php echo $product['quantity']; ?> وحدة
                                                </span>
                                            </td>
                                        </tr>
                                    <?php endwhile; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="assets/js/script.js"></script>
</body>
</html>
