<?php
$page_title = 'Product Outgoing Trend';
require_once('includes/load.php');
page_require_level(2);

// Memeriksa apakah parameter part_number ada di URL
if (isset($_GET['part_number'])) {
    $part_number = $_GET['part_number'];

    // Mendapatkan bulan berjalan dan bulan sebelumnya dalam format 'YYYY-MM'
    $current_month = date('Y-m');
    $previous_month = date('Y-m', strtotime('-1 month'));

    // Query untuk keseluruhan (semua lokasi) untuk dua bulan terakhir
    $sql_total  = "SELECT DATE(scan_in) AS date, SUM(quantity) AS total_quantity ";
    $sql_total .= "FROM awb ";
    $sql_total .= "WHERE part_number = '{$part_number}' AND (DATE_FORMAT(scan_in, '%Y-%m') = '{$current_month}' OR DATE_FORMAT(scan_in, '%Y-%m') = '{$previous_month}') ";
    $sql_total .= "GROUP BY DATE(scan_in) ";
    $sql_total .= "ORDER BY DATE(scan_in) ASC";
    $result_total = $db->query($sql_total);

    // Mengumpulkan data untuk grafik total
    $data_points_total = [];
    while ($row = $result_total->fetch_assoc()) {
        $data_points_total[] = [$row['date'], (int)$row['total_quantity']];
    }

    // Query untuk Makassar
    $sql_makassar  = "SELECT DATE(scan_in) AS date, SUM(quantity) AS total_quantity ";
    $sql_makassar .= "FROM awb ";
    $sql_makassar .= "WHERE part_number = '{$part_number}' AND (DATE_FORMAT(scan_in, '%Y-%m') = '{$current_month}' OR DATE_FORMAT(scan_in, '%Y-%m') = '{$previous_month}') ";
    $sql_makassar .= "AND loc = 'makassar' ";
    $sql_makassar .= "GROUP BY DATE(scan_in) ";
    $sql_makassar .= "ORDER BY DATE(scan_in) ASC";
    $result_makassar = $db->query($sql_makassar);

    // Mengumpulkan data untuk grafik Makassar
    $data_points_makassar = [];
    while ($row = $result_makassar->fetch_assoc()) {
        $data_points_makassar[] = [$row['date'], (int)$row['total_quantity']];
    }

    // Query untuk Jakarta
    $sql_jakarta  = "SELECT DATE(scan_in) AS date, SUM(quantity) AS total_quantity ";
    $sql_jakarta .= "FROM awb ";
    $sql_jakarta .= "WHERE part_number = '{$part_number}' AND (DATE_FORMAT(scan_in, '%Y-%m') = '{$current_month}' OR DATE_FORMAT(scan_in, '%Y-%m') = '{$previous_month}') ";
    $sql_jakarta .= "AND loc = 'jakarta' ";
    $sql_jakarta .= "GROUP BY DATE(scan_in) ";
    $sql_jakarta .= "ORDER BY DATE(scan_in) ASC";
    $result_jakarta = $db->query($sql_jakarta);

    // Mengumpulkan data untuk grafik Jakarta
    $data_points_jakarta = [];
    while ($row = $result_jakarta->fetch_assoc()) {
        $data_points_jakarta[] = [$row['date'], (int)$row['total_quantity']];
    }

    // Query untuk Surabaya (NULL)
    $sql_surabaya  = "SELECT DATE(scan_in) AS date, SUM(quantity) AS total_quantity ";
    $sql_surabaya .= "FROM awb ";
    $sql_surabaya .= "WHERE part_number = '{$part_number}' AND (DATE_FORMAT(scan_in, '%Y-%m') = '{$current_month}' OR DATE_FORMAT(scan_in, '%Y-%m') = '{$previous_month}') ";
    $sql_surabaya .= "AND loc IS NULL ";
    $sql_surabaya .= "GROUP BY DATE(scan_in) ";
    $sql_surabaya .= "ORDER BY DATE(scan_in) ASC";
    $result_surabaya = $db->query($sql_surabaya);

    // Mengumpulkan data untuk grafik Surabaya
    $data_points_surabaya = [];
    while ($row = $result_surabaya->fetch_assoc()) {
        $data_points_surabaya[] = [$row['date'], (int)$row['total_quantity']];
    }

    // Query untuk Bali
    $sql_bali  = "SELECT DATE(scan_in) AS date, SUM(quantity) AS total_quantity ";
    $sql_bali .= "FROM awb ";
    $sql_bali .= "WHERE part_number = '{$part_number}' AND (DATE_FORMAT(scan_in, '%Y-%m') = '{$current_month}' OR DATE_FORMAT(scan_in, '%Y-%m') = '{$previous_month}') ";
    $sql_bali .= "AND loc = 'bali' ";
    $sql_bali .= "GROUP BY DATE(scan_in) ";
    $sql_bali .= "ORDER BY DATE(scan_in) ASC";
    $result_bali = $db->query($sql_bali);

    // Mengumpulkan data untuk grafik Bali
    $data_points_bali = [];
    while ($row = $result_bali->fetch_assoc()) {
        $data_points_bali[] = [$row['date'], (int)$row['total_quantity']];
    }

    // Query untuk Bandung
    $sql_bandung  = "SELECT DATE(scan_in) AS date, SUM(quantity) AS total_quantity ";
    $sql_bandung .= "FROM awb ";
    $sql_bandung .= "WHERE part_number = '{$part_number}' AND (DATE_FORMAT(scan_in, '%Y-%m') = '{$current_month}' OR DATE_FORMAT(scan_in, '%Y-%m') = '{$previous_month}') ";
    $sql_bandung .= "AND loc = 'bandung' ";
    $sql_bandung .= "GROUP BY DATE(scan_in) ";
    $sql_bandung .= "ORDER BY DATE(scan_in) ASC";
    $result_bandung = $db->query($sql_bandung);

    // Mengumpulkan data untuk grafik Bandung
    $data_points_bandung = [];
    while ($row = $result_bandung->fetch_assoc()) {
        $data_points_bandung[] = [$row['date'], (int)$row['total_quantity']];
    }

} else {
    // Jika part_number tidak ada di URL, alihkan kembali ke halaman produk utama atau tampilkan pesan error
    header('Location: products.php');
    exit();
}

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

<div class="row">
    <div class="col-md-12">
        <h3>Outgoing Trend for Part Number: <?php echo htmlspecialchars($part_number); ?></h3>

        <!-- Grafik Total -->
        <div id="chart_total" style="width: 100%; height: 600px;"></div>
        <h4>Total Outgoing Trend</h4>

        <!-- Grafik Makassar -->
        <div id="chart_makassar" style="width: 100%; height: 600px;"></div>
        <h4>Outgoing Trend for Makassar</h4>

        <!-- Grafik Jakarta -->
        <div id="chart_jakarta" style="width: 100%; height: 600px;"></div>
        <h4>Outgoing Trend for Jakarta</h4>

        <!-- Grafik Surabaya -->
        <div id="chart_surabaya" style="width: 100%; height: 600px;"></div>
        <h4>Outgoing Trend for Surabaya</h4>

        <!-- Grafik Bali -->
        <div id="chart_bali" style="width: 100%; height: 600px;"></div>
        <h4>Outgoing Trend for Bali</h4>

        <!-- Grafik Bandung -->
        <div id="chart_bandung" style="width: 100%; height: 600px;"></div>
        <h4>Outgoing Trend for Bandung</h4>
    </div>
</div>

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

<!-- Load the Google Charts library -->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
    google.charts.load('current', {packages: ['corechart', 'line']});
    google.charts.setOnLoadCallback(drawCharts);

    function drawCharts() {
        // Data untuk grafik total
        var data_total = new google.visualization.DataTable();
        data_total.addColumn('string', 'Date');
        data_total.addColumn('number', 'Total Quantity');
        data_total.addRows(<?php echo json_encode($data_points_total); ?>);
        var options_total = {
            title: 'Total Outgoing Trend',
            hAxis: {title: 'Date'},
            vAxis: {title: 'Quantity'},
            colors: ['#007bff'],
            curveType: 'function',
            legend: {position: 'bottom'},
            pointSize: 5,
            chartArea: {width: '85%', height: '80%'},
        };
        var chart_total = new google.visualization.LineChart(document.getElementById('chart_total'));
        chart_total.draw(data_total, options_total);

        // Data untuk grafik Makassar
        var data_makassar = new google.visualization.DataTable();
        data_makassar.addColumn('string', 'Date');
        data_makassar.addColumn('number', 'Total Quantity');
        data_makassar.addRows(<?php echo json_encode($data_points_makassar); ?>);
        var options_makassar = {
            title: 'Outgoing Trend for Makassar',
            hAxis: {title: 'Date'},
            vAxis: {title: 'Quantity'},
            colors: ['#ff0000'],
            curveType: 'function',
            legend: {position: 'bottom'},
            pointSize: 5,
            chartArea: {width: '85%', height: '80%'},
        };
        var chart_makassar = new google.visualization.LineChart(document.getElementById('chart_makassar'));
        chart_makassar.draw(data_makassar, options_makassar);

        // Data untuk grafik Jakarta
        var data_jakarta = new google.visualization.DataTable();
        data_jakarta.addColumn('string', 'Date');
        data_jakarta.addColumn('number', 'Total Quantity');
        data_jakarta.addRows(<?php echo json_encode($data_points_jakarta); ?>);
        var options_jakarta = {
            title: 'Outgoing Trend for Jakarta',
            hAxis: {title: 'Date'},
            vAxis: {title: 'Quantity'},
            colors: ['#28a745'],
            curveType: 'function',
            legend: {position: 'bottom'},
            pointSize: 5,
            chartArea: {width: '85%', height: '80%'},
        };
        var chart_jakarta = new google.visualization.LineChart(document.getElementById('chart_jakarta'));
        chart_jakarta.draw(data_jakarta, options_jakarta);

        // Data untuk grafik Surabaya
        var data_surabaya = new google.visualization.DataTable();
        data_surabaya.addColumn('string', 'Date');
        data_surabaya.addColumn('number', 'Total Quantity');
        data_surabaya.addRows(<?php echo json_encode($data_points_surabaya); ?>);
        var options_surabaya = {
            title: 'Outgoing Trend for Surabaya',
            hAxis: {title: 'Date'},
            vAxis: {title: 'Quantity'},
            colors: ['#ffc107'],
            curveType: 'function',
            legend: {position: 'bottom'},
            pointSize: 5,
            chartArea: {width: '85%', height: '80%'},
        };
        var chart_surabaya = new google.visualization.LineChart(document.getElementById('chart_surabaya'));
        chart_surabaya.draw(data_surabaya, options_surabaya);

        // Data untuk grafik Bali
        var data_bali = new google.visualization.DataTable();
        data_bali.addColumn('string', 'Date');
        data_bali.addColumn('number', 'Total Quantity');
        data_bali.addRows(<?php echo json_encode($data_points_bali); ?>);
        var options_bali = {
            title: 'Outgoing Trend for Bali',
            hAxis: {title: 'Date'},
            vAxis: {title: 'Quantity'},
            colors: ['#17a2b8'],
            curveType: 'function',
            legend: {position: 'bottom'},
            pointSize: 5,
            chartArea: {width: '85%', height: '80%'},
        };
        var chart_bali = new google.visualization.LineChart(document.getElementById('chart_bali'));
        chart_bali.draw(data_bali, options_bali);

        // Data untuk grafik Bandung
        var data_bandung = new google.visualization.DataTable();
        data_bandung.addColumn('string', 'Date');
        data_bandung.addColumn('number', 'Total Quantity');
        data_bandung.addRows(<?php echo json_encode($data_points_bandung); ?>);
        var options_bandung = {
            title: 'Outgoing Trend for Bandung',
            hAxis: {title: 'Date'},
            vAxis: {title: 'Quantity'},
            colors: ['#6f42c1'], // Warna ungu untuk Bandung
            curveType: 'function',
            legend: {position: 'bottom'},
            pointSize: 5,
            chartArea: {width: '85%', height: '80%'},
        };
        var chart_bandung = new google.visualization.LineChart(document.getElementById('chart_bandung'));
        chart_bandung.draw(data_bandung, options_bandung);
    }
</script>