<?php

require_once __DIR__ . '/vendor/autoload.php';


require_once('includes/load.php');
// $jsonData = '[{"order_id":"241111VG7B96UM"},{"order_id":"241111VXC79URK"},{"order_id":"24111106JB9KNY"},{"order_id":"241111075TYDN5"},{"order_id":"24111107D7F711"},{"order_id":"24111108XVA4QU"},{"order_id":"24111109S5EHF0"},{"order_id":"24111109WQAUJD"},{"order_id":"2411110AB1KDVX"},{"order_id":"2411110ABFY8VF"},{"order_id":"2411110AFT8FS2"},{"order_id":"2411110AG5PWUM"},{"order_id":"2411110ASG5UJ1"},{"order_id":"2411110AU9BV54"},{"order_id":"2411110AUXBAE3"},{"order_id":"2411110AUXBAE3"},{"order_id":"2411110BVAYTDT"},{"order_id":"2411110BY8G09P"},{"order_id":"2411110BY8G09P"},{"order_id":"2411110BY8G09P"},{"order_id":"2411110CHE8TP5"},{"order_id":"2411110D5H000F"},{"order_id":"2411110D8GD0CT"},{"order_id":"2411110D8GD0CT"},{"order_id":"2411110E2B7TVX"},{"order_id":"2411110F8PKE90"},{"order_id":"2411110FQEND5K"},{"order_id":"2411110GCVC9FW"},{"order_id":"2411110GSQBX9Q"},{"order_id":"2411110H4Q906B"},{"order_id":"2411110H9FW3Y3"},{"order_id":"2411110HBKKBS9"},{"order_id":"2411110HF6XQN9"},{"order_id":"2411110HWMHS5E"},{"order_id":"2411110HY6658C"},{"order_id":"2411110JFRB8HM"},{"order_id":"2411110JJ9E468"},{"order_id":"2411110JQYM2NC"},{"order_id":"2411110M14KBVR"},{"order_id":"2411110M1G3EDX"},{"order_id":"2411110M29TR06"},{"order_id":"2411110M29TR06"},{"order_id":"2411110M67PTUQ"},{"order_id":"2411110MM8WW4C"},{"order_id":"2411110N5K0QE7"},{"order_id":"2411110N7TGT2C"},{"order_id":"2411110ND568B1"},{"order_id":"2411110NFN9Q8V"},{"order_id":"2411110NUSED57"},{"order_id":"2411110NYNDJKQ"},{"order_id":"2411110P2C7EU3"},{"order_id":"2411110PR946ET"},{"order_id":"2411110Q8X8EQM"},{"order_id":"2411110R9W09S2"},{"order_id":"2411110RJBW4XC"},{"order_id":"2411110RP08CN5"},{"order_id":"2411110RP08CN5"},{"order_id":"2411110RVPRY7B"},{"order_id":"2411110S1VPS5D"},{"order_id":"2411110S5QRB5T"},{"order_id":"2411110S6GHAWG"},{"order_id":"2411110SU5EB44"},{"order_id":"2411110TGYHGGD"},{"order_id":"2411110U358K0E"},{"order_id":"2411110UA79U0K"},{"order_id":"2411110UJWR9TX"},{"order_id":"2411110UQX0EX6"},{"order_id":"2411110WSKV57E"},{"order_id":"2411110X6Y6AYX"},{"order_id":"2411110XAHMFB3"},{"order_id":"2411110XAHMFB3"},{"order_id":"2411110XAHMFB3"},{"order_id":"2411110XD6EBTX"},{"order_id":"2411110XM4MRJ9"},{"order_id":"24111110P44DQA"},{"order_id":"24111110TPY8G6"},{"order_id":"24111110TPY8G6"},{"order_id":"24111110VBDP0N"},{"order_id":"24111111123GCV"},{"order_id":"24111111A236Q3"},{"order_id":"24111111SRNUSR"},{"order_id":"241111127SU5K4"},{"order_id":"241111128871NA"},{"order_id":"241111129CEM6K"},{"order_id":"24111112DD4DPE"},{"order_id":"24111112RQHE0F"},{"order_id":"2411111335BMJK"},{"order_id":"241111139N97SJ"},{"order_id":"24111113XUBU53"},{"order_id":"24111113YK3Y2X"},{"order_id":"24111114NACP76"},{"order_id":"24111215H9VGYP"},{"order_id":"24111215NSW3EF"},{"order_id":"241112161GQPUM"},{"order_id":"241112162XDVXP"},{"order_id":"2411121631ANK9"},{"order_id":"24111216GXPXHB"},{"order_id":"2411121767MHJW"},{"order_id":"241112178PT3JQ"},{"order_id":"24111217C6A1V3"},{"order_id":"24111217HQS2NF"},{"order_id":"2411121APQRHC5"},{"order_id":"2411121T5X1141"},{"order_id":"2411121XJMA1YH"},{"order_id":"2411121YHXM0S7"},{"order_id":"2411121YNH1VVB"},{"order_id":"241112245AHEV6"},{"order_id":"24111225EBR31M"},{"order_id":"241112268J165V"},{"order_id":"24111226A4GC47"},{"order_id":"24111226AGX49J"},{"order_id":"24111226BYPFTW"},{"order_id":"24111227MRBC5B"},{"order_id":"24111227MRBC5K"},{"order_id":"241112283BKVY4"}]';

// Decode JSON menjadi array PHP
// $orderData = json_decode($jsonData, true);

// Ambil semua order_id dan buat string untuk klausa WHERE IN
// $orderIds = array_column($orderData, 'order_id');
$string = "2411135CPPD2TP,2411135BGGR69E,24111356U12GAV,2411135747QEHM,241113524XBD2D";

// Ubah string menjadi array
$orderIds = explode(",", $string);
$orderIdsString = "'" . implode("', '", $orderIds) . "'"; // Menghasilkan string yang sesuai untuk SQL
$sql = "
SELECT 
    i.item_sku,
    i.model_sku,
    SUM(i.model_quantity_purchased) AS quantity,
    GROUP_CONCAT(opq.resi SEPARATOR ', ') AS resi
FROM
    order_detail i
JOIN 
    orders o ON i.order_id = o.id
JOIN 
    (SELECT * FROM orders_packing_queue WHERE processed = 0 LIMIT 50) opq 
    ON opq.order_sn = o.order_sn
GROUP BY 
    i.item_sku, i.model_sku;
";


$result = $db->query($sql);


// Create a new PDF document with A6 page size
$pdf = new TCPDF('P', 'mm', 'A6', true, 'UTF-8', false);

// Set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Your Name');
$pdf->SetTitle('Order Summary');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// Remove default header and footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

// Set margins
$pdf->SetMargins(0, 0);
$pdf->SetAutoPageBreak(TRUE, 0);

// Add a page
$pdf->AddPage();

// Set title
$pdf->SetFont('helvetica', 'B', 18);

// Set font for the table headers
$pdf->SetFont('helvetica', 'B', 12);
$pdf->Cell(50, 10, 'SKU', 1, 0, 'L'); // Left aligned SKU header
$pdf->Cell(10, 10, 'Qty', 1, 1, 'C'); // Right aligned Quantity header
$pdf->Cell(30, 10, 'Resi', 1, 1, 'C'); // Right aligned Quantity header

// Set font for the table content
$pdf->SetFont('helvetica', '', 12);

// Loop through the data
$no = 0;
while ($row = $result->fetch_assoc()) {
    $no++;
    if ($no == 14) {
        $no = 0;
        // Set font for the table headers
        $pdf->SetFont('helvetica', 'B', 12);
        $pdf->Cell(50, 10, 'SKU', 1, 0, 'L'); // Left aligned SKU header
        $pdf->Cell(10, 10, 'Quantity', 1, 1, 'C'); // Right aligned Quantity header
    }

    $pdf->SetFont('helvetica', '', 12);
    $pdf->Cell(50, 10, $row["model_sku"] ?? $row["item_sku"], 1, 0, 'L'); // Left aligned SKU
    $pdf->Cell(10, 10, $row["quantity"], 1, 1, 'C'); // Right aligned Quantity
    $pdf->Cell(30, 10, $row["resi"], 1, 1, 'C'); // Right aligned Quantity
}

// Close and output PDF document
$pdf->Output('packing_list.pdf', 'I');
