<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

$page_title = 'SKU Sales Chart';
require_once('includes/load.php');
page_require_level(3); // Hanya level 3 yang diizinkan

// Include Chart.js
echo '<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>';

// Get the part number from the URL
$part_number = isset($_GET['part_number']) ? $_GET['part_number'] : '';

if (empty($part_number)) {
    die('Part number is required.');
}

// Fetch sales data for the last 5 months
$query_last_5_months = "
    SELECT 
        DATE_FORMAT(sales_data.date, '%Y-%m') AS month,
        SUM(sales_data.qty) AS total_quantity
    FROM (
        SELECT date, qty FROM sales WHERE part_number = '$part_number' AND categorie_id IN (7, 8) -- Surabaya
        UNION ALL
        SELECT date, qty FROM sales_jakarta WHERE part_number = '$part_number' AND categorie_id IN (7, 8) -- Jakarta
        UNION ALL
        SELECT date, qty FROM sales_makassar WHERE part_number = '$part_number' AND categorie_id IN (7, 8) -- Makassar
        UNION ALL
        SELECT date, qty FROM sales_bali WHERE part_number = '$part_number' AND categorie_id IN (7, 8) -- Bali
        UNION ALL
        SELECT date, qty FROM sales_semarang WHERE part_number = '$part_number' AND categorie_id IN (7, 8) -- Semarang
    ) AS sales_data
    WHERE sales_data.date >= DATE_SUB(CURDATE(), INTERVAL 5 MONTH)
    GROUP BY month
    ORDER BY month
";

// Execute query for sales data
$result_last_5_months = $db->query($query_last_5_months);

// Prepare data for chart
$months = [];
$sales_data = [];

while ($row = $result_last_5_months->fetch_assoc()) {
    $months[] = $row['month'];
    $sales_data[] = (int)$row['total_quantity'];
}

include_once('layouts/header.php');
?>

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title text-center">Sales Chart for Part Number: <?php echo htmlspecialchars($part_number); ?></h3>
            </div>
            <div class="panel-body">
                <canvas id="salesChart" width="400" height="200"></canvas>
                <script>
                    var ctx = document.getElementById('salesChart').getContext('2d');
                    var salesChart = new Chart(ctx, {
                        type: 'line',
                        data: {
                            labels: <?php echo json_encode($months); ?>,
                            datasets: [{
                                label: 'Sales Quantity',
                                data: <?php echo json_encode($sales_data); ?>,
                                backgroundColor: 'rgba(75, 192, 192, 0.2)',
                                borderColor: 'rgba(75, 192, 192, 1)',
                                borderWidth: 1
                            }]
                        },
                        options: {
                            scales: {
                                y: {
                                    beginAtZero: true
                                }
                            }
                        }
                    });
                </script>
            </div>
        </div>
    </div>
</div>

<?php include_once('layouts/footer.php'); ?>
