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

if (!isset($_GET['ids'])) {
    die("No serials selected");
}

$ids = array_map('intval', explode(',', $_GET['ids']));
$id_list = implode(',', $ids);

$result = $conn->query(
    "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)"
);

if ($result->num_rows == 0) {
    die("No records found");
}

/* PDF SETTINGS */
$pdf = new TCPDF('L', 'mm', [34, 25]);
$pdf->SetMargins(1.2, 1.2, 1.2);
$pdf->SetAutoPageBreak(false);

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

while ($row = $result->fetch_assoc()) {

    $pdf->AddPage();

    /* Border */
    $pdf->Rect(0.8, 0.8, 32.4, 23.4);

    /* Model (BIGGER) */
    $pdf->SetFont('helvetica', 'B', 7);
    $pdf->SetTextColor(0, 0, 0);
    $pdf->MultiCell(
        0,
        5,
        strtoupper($row['brand'].' '.$row['model']),
        0,
        'C'
    );

    /* Barcode (SHORTER) */
    $pdf->write1DBarcode(
        $row['serial_number'],
        'C128',
        1.5,
        9.5,
        31,
        6,     // 🔽 reduced height
        0.30,
        $style
    );

    /* Serial number (BIGGER) */
    $pdf->SetFont('helvetica', 'B', 7);
    $pdf->SetY(17);
    $pdf->Cell(0, 5, $row['serial_number'], 0, 0, 'C');
}

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


$pdf->Output('bulk_labels_34x25.pdf', 'I');
