<?php
require_once __DIR__ . '/../config.php';
requireRole(['admin']);
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../lib/logic.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Fill;

$filters = [
    'start_date' => $_GET['start_date'] ?? null,
    'end_date' => $_GET['end_date'] ?? null,
    'item_code' => $_GET['item_code'] ?? null,
    'source_type' => $_GET['source_type'] ?? null,
    'trans_type' => $_GET['trans_type'] ?? null
];

$transactions = exportInventoryTransactions($filters);

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('Inventory Transactions');

$headers = [
    'ID', 'Source Type', 'Item Code', 'Item Description', 'Other Description',
    'Item Group', 'Group Category', 'Transaction Date', 'Reference', 'Transaction Type',
    'Division', 'Allocation', 'Warehouse', 'GL Account', 'Quantity In', 'Quantity Out',
    'Transfers', 'Batch Number', 'Expiry Date', 'Medicines Code', 'DH Code', 'Doc Code',
    'Reg Serial Number', 'Odo HR Reading', 'Meter Reading Before', 'Meter Reading After',
    'Meter Quantity', 'Receiver', 'Issuer', 'Details', 'Unit Cost', 'Action Type'
];

$col = 'A';
foreach ($headers as $header) {
    $sheet->setCellValue($col . '1', $header);
    $col++;
}

$headerStyle = [
    'font' => ['bold' => true],
    'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['rgb' => 'E0E0E0']],
    'borders' => ['allBorders' => ['borderStyle' => Border::BORDER_THIN]]
];
$sheet->getStyle('A1:' . $col . '1')->applyFromArray($headerStyle);

$rowNum = 2;
foreach ($transactions as $t) {
    $col = 'A';
    foreach ($headers as $header) {
        $key = strtolower(str_replace(' ', '_', $header));
        if ($key === 'id') $key = 'id';
        elseif ($key === 'source_type') $key = 'source_type';
        elseif ($key === 'item_code') $key = 'item_code';
        elseif ($key === 'item_description') $key = 'item_description';
        elseif ($key === 'other_description') $key = 'other_description';
        elseif ($key === 'item_group') $key = 'item_group';
        elseif ($key === 'group_category') $key = 'group_category';
        elseif ($key === 'transaction_date') $key = 'transaction_date';
        elseif ($key === 'reference') $key = 'reference';
        elseif ($key === 'transaction_type') $key = 'trans_type';
        elseif ($key === 'division') $key = 'division';
        elseif ($key === 'allocation') $key = 'allocation';
        elseif ($key === 'warehouse') $key = 'warehouse';
        elseif ($key === 'gl_account') $key = 'gl_account';
        elseif ($key === 'quantity_in') $key = 'quantity_in';
        elseif ($key === 'quantity_out') $key = 'quantity_out';
        elseif ($key === 'transfers') $key = 'transfers';
        elseif ($key === 'batch_number') $key = 'batch_number';
        elseif ($key === 'expiry_date') $key = 'expiry_date';
        elseif ($key === 'medicines_code') $key = 'medicines_code';
        elseif ($key === 'dh_code') $key = 'dh_code';
        elseif ($key === 'doc_code') $key = 'doc_code';
        elseif ($key === 'reg_serial_number') $key = 'reg_serial_number';
        elseif ($key === 'odo_hr_reading') $key = 'odo_hr_reading';
        elseif ($key === 'meter_reading_before') $key = 'meter_reading_before';
        elseif ($key === 'meter_reading_after') $key = 'meter_reading_after';
        elseif ($key === 'meter_quantity') $key = 'meter_quantity';
        elseif ($key === 'receiver') $key = 'receiver';
        elseif ($key === 'issuer') $key = 'issuer';
        elseif ($key === 'details') $key = 'details';
        elseif ($key === 'unit_cost') $key = 'unit_cost';
        elseif ($key === 'action_type') $key = 'action_type';
        
        $sheet->setCellValue($col . $rowNum, $t[$key] ?? '');
        $col++;
    }
    $rowNum++;
}

foreach (range('A', $col) as $column) {
    $sheet->getColumnDimension($column)->setAutoSize(true);
}

$filename = 'inventory_transactions_' . date('Ymd_His') . '.xlsx';

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $filename . '"');
header('Cache-Control: max-age=0');

$writer = new Xlsx($spreadsheet);
$writer->save('php://output');
exit;