<?php
$page_title = 'Export Filter AWB Data';
require_once('includes/load.php');
page_require_level(3);

require 'vendor/autoload.php'; // Pastikan jalur ini sesuai dengan lokasi autoload PHPSpreadsheet

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

// Menangani request form untuk filter bulan
$selected_month = isset($_GET['month']) ? $_GET['month'] : date('m');
$selected_year = isset($_GET['year']) ? $_GET['year'] : date('Y');

if (isset($_GET['export'])) {
    // Query untuk mengambil data berdasarkan bulan dan tahun yang dipilih
    $query = "SELECT * FROM awb WHERE MONTH(date_scan) = ? AND YEAR(date_scan) = ?";
    $stmt = $db->prepare($query);
    $stmt->bind_param("ii", $selected_month, $selected_year);
    $stmt->execute();
    $result = $stmt->get_result();

    // Membuat Spreadsheet baru
    $spreadsheet = new Spreadsheet();
    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setTitle("AWB Data");

    // Menulis header kolom
    $headers = ["ID", "Tanggal Pesenan Dibuat", "Invoice", "Resi", "Nama Toko", "Kurir", "Part Number", "Product Name", "Date Scan", "Quantity", "Scan In", "Scan Status", "Scan Retur", "Packer No", "Batas Pengiriman", "Uniq Code", "Unit Price", "Total Price", "Loc"];
    $sheet->fromArray($headers, NULL, 'A1');

    // Menulis data ke dalam sheet
    $rowNum = 2;
    while ($row = $result->fetch_assoc()) {
        $sheet->fromArray(array_values($row), NULL, 'A' . $rowNum);
        $rowNum++;
    }
    $stmt->close();

    // Menyiapkan file untuk diunduh
    $filename = "awb_data_{$selected_year}_{$selected_month}.xlsx";
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    header('Cache-Control: max-age=0');

    // Membuat writer dan mengunduh file
    $writer = new Xlsx($spreadsheet);
    $writer->save('php://output');
    exit;
}

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

<!-- Form Filter Bulan dan Tahun -->
<div class="row">
    <div class="col-md-12">
        <form method="GET" action="data_upload_export.php" class="form-inline">
            <div class="form-group">
                <label for="month">Bulan:</label>
                <select name="month" id="month" class="form-control">
                    <?php
                    for ($m = 1; $m <= 12; $m++) {
                        $month_value = str_pad($m, 2, '0', STR_PAD_LEFT);
                        echo "<option value=\"$month_value\" " . ($month_value == $selected_month ? 'selected' : '') . ">$month_value</option>";
                    }
                    ?>
                </select>
            </div>
            <div class="form-group">
                <label for="year">Tahun:</label>
                <select name="year" id="year" class="form-control">
                    <?php
                    for ($y = date('Y') - 5; $y <= date('Y'); $y++) {
                        echo "<option value=\"$y\" " . ($y == $selected_year ? 'selected' : '') . ">$y</option>";
                    }
                    ?>
                </select>
            </div>
            <input type="hidden" name="export" value="1">
            <button type="submit" class="btn btn-primary">Export Data</button>
        </form>
    </div>
</div>

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