<?php
session_start();
if (!isset($_SESSION['user_id'])) {
    header("Location: index.php");
    exit();
}
require_once 'db_connect.php';

// جلب إحصائيات المبيعات (افتراضيًا من جدول sales)
$stmt_sales = $conn->prepare("SELECT DATE(sale_date) as date, SUM(total) as total_sales FROM sales GROUP BY DATE(sale_date) ORDER BY sale_date ASC LIMIT 7");
$stmt_sales->execute();
$sales_data = $stmt_sales->fetchAll(PDO::FETCH_ASSOC);

// جلب إحصائيات المشتريات (افتراضيًا من جدول purchases)
$stmt_purchases = $conn->prepare("SELECT DATE(purchase_date) as date, SUM(total) as total_purchases FROM purchases GROUP BY DATE(purchase_date) ORDER BY purchase_date ASC LIMIT 7");
$stmt_purchases->execute();
$purchases_data = $stmt_purchases->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="ar">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Astra - الإحصائيات</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body { direction: rtl; font-family: 'Segoe UI', Tahoma, sans-serif; background: #f4f7fa; padding: 20px; }
        .chart-container { background: #fff; padding: 20px; border-radius: 10px; box-shadow: 0 0 10px rgba(0,0,0,0.1); margin-bottom: 20px; }
    </style>
</head>
<body>
    <div class="container">
        <h1 class="text-center my-4">إحصائيات المبيعات والمشتريات</h1>
        <div class="chart-container">
            <canvas id="salesChart"></canvas>
        </div>
        <div class="chart-container">
            <canvas id="purchasesChart"></canvas>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <script>
        // بيانات المبيعات
        const salesLabels = [<?php foreach($sales_data as $data) echo "'{$data['date']}',"; ?>];
        const salesData = [<?php foreach($sales_data as $data) echo "{$data['total_sales']},"; ?>];

        // بيانات المشتريات
        const purchasesLabels = [<?php foreach($purchases_data as $data) echo "'{$data['date']}',"; ?>];
        const purchasesData = [<?php foreach($purchases_data as $data) echo "{$data['total_purchases']},"; ?>];

        // رسم بياني للمبيعات
        new Chart(document.getElementById('salesChart'), {
            type: 'line',
            data: {
                labels: salesLabels,
                datasets: [{
                    label: 'إجمالي المبيعات (آخر 7 أيام)',
                    data: salesData,
                    borderColor: '#4169E1',
                    fill: false
                }]
            },
            options: { scales: { y: { beginAtZero: true } } }
        });

        // رسم بياني للمشتريات
        new Chart(document.getElementById('purchasesChart'), {
            type: 'line',
            data: {
                labels: purchasesLabels,
                datasets: [{
                    label: 'إجمالي المشتريات (آخر 7 أيام)',
                    data: purchasesData,
                    borderColor: '#2ECC71',
                    fill: false
                }]
            },
            options: { scales: { y: { beginAtZero: true } } }
        });
    </script>
</body>
</html>