<?php
include 'config.php';
checkLogin();
require_once 'tcpdf/tcpdf.php';

$ids = [];
if (isset($_GET['ids'])) {
    if (is_array($_GET['ids'])) {
        foreach ($_GET['ids'] as $rid) {
            $id = (int)$rid;
            if ($id > 0) $ids[] = $id;
        }
    } else {
        $raw_ids = explode(',', $_GET['ids']);
        foreach ($raw_ids as $rid) {
            $id = (int)$rid;
            if ($id > 0) $ids[] = $id;
        }
    }
}

$ids = array_values(array_unique($ids));
if (empty($ids)) die("No valid serial IDs");

$id_list = implode(',', $ids);

$sql = "
    SELECT s.id, s.serial_number, m.brand, m.model
    FROM laptop_serials s
    JOIN laptop_models m ON s.model_id = m.id
    WHERE s.id IN ($id_list)
    ORDER BY s.id ASC
";

$result = $conn->query($sql);
if (!$result || $result->num_rows == 0) die("No serials found");

$rows = [];
while ($row = $result->fetch_assoc()) $rows[] = $row;

$pdf = new TCPDF('L', 'mm', [75, 25], true, 'UTF-8', false);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetMargins(1, 1, 1);
$pdf->SetAutoPageBreak(false, 0);

$style = [
    'align'    => 'C',
    'stretch'  => false,
    'fitwidth' => true,
    'border'   => false,
];

$labelWidth = 36.5;
$gap = 3;
$count      = count($rows);

for ($i = 0; $i < $count; $i += 2) {

    $left  = $rows[$i];
    $right = ($i + 1 < $count) ? $rows[$i + 1] : null;

    $pdf->AddPage();

    /* ---------- LEFT LABEL ---------- */
    $leftX = 0;
    $pdf->Rect($leftX + 0.5, 0.5, $labelWidth, 24);

    // Model name — 2 rows, fixed area Y=1 to Y=9
    $pdf->SetFont('helvetica', 'B', 6);
    $pdf->SetTextColor(0, 0, 0);
    $pdf->SetXY($leftX + 1, 1);
    $pdf->MultiCell($labelWidth - 2, 4, strtoupper($left['brand'] . ' ' . $left['model']), 0, 'C', false, 1);

    // Barcode — always fixed at Y=10
    $pdf->write1DBarcode($left['serial_number'], 'C128', $leftX + 2, 10, 33, 6, 0.27, $style, 'N');

    // Serial number — always fixed at Y=17
    $pdf->SetFont('helvetica', 'B', 7);
    $pdf->SetXY($leftX, 17);
    $pdf->Cell($labelWidth, 5, $left['serial_number'], 0, 0, 'C');

    /* ---------- RIGHT LABEL ---------- */
    if ($right) {
        $rightX = $labelWidth + $gap;
        $pdf->Rect($rightX + 0.5, 0.5, $labelWidth, 24);

        // Model name — 2 rows, fixed area Y=1 to Y=9
        $pdf->SetFont('helvetica', 'B', 6);
        $pdf->SetTextColor(0, 0, 0);
        $pdf->SetXY($rightX + 1, 1);
        $pdf->MultiCell($labelWidth - 2, 4, strtoupper($right['brand'] . ' ' . $right['model']), 0, 'C', false, 1);

        // Barcode — always fixed at Y=10
        $pdf->write1DBarcode($right['serial_number'], 'C128', $rightX + 2, 10, 33, 6, 0.27, $style, 'N');

        // Serial number — always fixed at Y=17
        $pdf->SetFont('helvetica', 'B', 7);
        $pdf->SetXY($rightX, 17);
        $pdf->Cell($labelWidth, 5, $right['serial_number'], 0, 0, 'C');
    }
}

$conn->query("UPDATE laptop_serials SET printed = 1, printed_at = NOW() WHERE id IN ($id_list)");

if (ob_get_length()) ob_end_clean();
$pdf->Output('bulk_labels_2up_37_5x25.pdf', 'I');
exit;
