<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>WireLabel</title>
    @php

    $pageHeightMm = 100;
    $pageWidthMm = 85;
    $paddingMm = 1.3;


    $headerRows = [
        'PRODUCT' => $product,
        'MFG. MONTH & YEAR' => $monthYear,
        'CUSTOMER PO' => $customerCode,
        'CUSTOMER NAME' => $customerName,
        'MASTER BOX NO.' => $masterBox,
    ];

    $titleHeight = 10;
    $headerRowHeight = 5;
    $itemHeaderHeight = 5;

    // FOOTER SECTION
    $grossWeightHeight = 5;
    $netWeightHeight = 5;
    $licenseHeight = 5;
    $companyInfoHeight = 6.9; //6.9
    $logoHeight = $titleHeight * 0.8;
    $logoMaxWidth = 20;
    $isilogoHeight = $titleHeight * 0.9;
    $isilogoMaxWidth = 11;

    $availableHeight = $pageHeightMm - (2 * $paddingMm); // 97.4mm

    $numItems = count($items) ?: 1;

    // Total fixed space
    $fixedSpace = $titleHeight +
                 (5 * $headerRowHeight) +
                 $itemHeaderHeight +
                 $grossWeightHeight +
                 $netWeightHeight +
                 $licenseHeight +
                 $companyInfoHeight;

    $spaceForItemsOnly = $availableHeight - $fixedSpace; //97.4 - 68 = 29.4mm

    // $itemRowHeight = $spaceForItemsOnly / $numItems; // 29.4 / 2 = 14.7

    $itemRowHeight = floor(($spaceForItemsOnly / $numItems) * 10) / 10;

    $itemRowHeight -= 0.5;
    if ($numItems == 1) {
        $itemRowHeight -= 2.0;
    }
    if ($numItems == 2) {
        $itemRowHeight -= 2.0;
    }

    if ($itemRowHeight < 3) {
        $itemFontSize = '5.5px';
        $itemPadding = '0.1mm 0.3mm';
    } elseif ($itemRowHeight < 3.5) {
        $itemFontSize = '6px';
        $itemPadding = '0.1mm 0.4mm';
    } elseif ($itemRowHeight < 4) {
        $itemFontSize = '6.5px';
        $itemPadding = '0.1mm 0.5mm';
    } else {
        $itemFontSize = '7px';
        $itemPadding = '0.2mm 0.5mm';
    }

    // Compensate for borders (0.3mm top + 0.3mm bottom = 0.6mm)
    $compensatedTitleHeight = $titleHeight - 0.6;
    $compensatedHeaderHeight = $headerRowHeight - 0.6;
    $compensatedItemHeaderHeight = $itemHeaderHeight - 0.6;
    $compensatedItemHeight = $itemRowHeight - 0.6;
    $compensatedGrossHeight = $grossWeightHeight - 0.6;
    $compensatedNetHeight = $netWeightHeight - 0.6;
    $compensatedLicenseHeight = $licenseHeight - 0.6;
    $compensatedCompanyHeight = $companyInfoHeight - 0.6;

        $qrBase64 = 'data:image/png;base64,' . base64_encode(
        QrCode::format('png')
            ->size(120)      // 12mm ~ 120px
            ->margin(0)
            ->generate($pallet)
        // $qrBase64 = 'data:image/png;base64,' . base64_encode(
        //  \SimpleSoftwareIO\QrCode\Facades\QrCode::format('png')
        // ->size(120)
        // ->margin(0)
        // ->errorCorrection('H')
        // ->generate($pallet)
     );
    @endphp

<style>
    @page {
        size: 85mm 100mm;
        margin: 0;
    }

    * {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
    }

    body {
        margin: 0;
        padding: <?php echo $paddingMm; ?>mm;
        font-family: DejaVu Sans, sans-serif;
        font-size: 7px;
        color: #000;
        width: <?php echo $pageWidthMm - (2 * $paddingMm); ?>mm;
        height: <?php echo $availableHeight; ?>mm;
        line-height: 1;
        overflow: hidden;
    }

    table {
        width: 100%;
        border-collapse: collapse;
        /* table-layout: fixed; */
        /* height: <?php echo $availableHeight; ?>mm !important; */
    }

    td, th {
        border: 0.3px solid #000 !important;
        vertical-align: middle;
        line-height: 1 !important;
        overflow: hidden;
    }

    /* Title row - FIXED 12mm */
    .title-row td {
        height: <?php echo $compensatedTitleHeight; ?>mm !important;
        text-align: center;
        font-weight: bold;
        position: relative;
        padding: 0 !important;
        font-size: 8.5px;
    }

    .logo {
        position: absolute;
        left: 2mm;
        top: 50%;
        transform: translateY(-50%);
        height: <?php echo min(8, $compensatedTitleHeight * 0.6); ?>mm;
        width: auto;
    }

    .vertical-line {
        position: absolute;
        top: 0;
        bottom: 0;
        border-left: 0.3px solid #000;
    }

    .vertical-line.left { left: 12mm; }
    .vertical-line.right { right: 12mm; }

    /* Header rows - FIXED 5mm each */
    .header-row td {
        height: <?php echo $compensatedHeaderHeight; ?>mm !important;
        padding: 0.2mm 0.5mm !important;
    }

    /* Items header - FIXED 5mm */
    .items-header-row td {
        height: <?php echo $compensatedItemHeaderHeight; ?>mm !important;
        font-weight: bold;
        text-align: center;
        font-size: 6.5px;
        padding: 0.2mm 0.5mm !important;
    }

    /* ITEM ROWS - ONLY THESE ADJUST DYNAMICALLY */
    .item-row td {
        height: <?php echo $compensatedItemHeight; ?>mm !important;
        font-size: <?php echo $itemFontSize; ?> !important;
        padding: <?php echo $itemPadding; ?> !important;
        line-height: 1 !important;
    }

    /* Gross Weight row - FIXED 5mm */
    .gross-weight-row td {
        height: <?php echo $compensatedGrossHeight; ?>mm !important;
        text-align: center;
        font-size: 6.5px;
        padding: 0.2mm 0.5mm !important;
    }

    /* Net Weight row - FIXED 5mm */
    .net-weight-row td {
        height: <?php echo $compensatedNetHeight; ?>mm !important;
        text-align: center;
        font-size: 6.5px;
        padding: 0.2mm 0.5mm !important;
    }

    /* License row - FIXED 5mm */
    .license-row td {
        height: <?php echo $compensatedLicenseHeight; ?>mm !important;
        text-align: center;
        font-size: 6.5px;
        padding: 0.2mm 0.5mm !important;
    }

    /* Company info row - FIXED 8mm */
    .company-info-row td {
        height: <?php echo $compensatedCompanyHeight; ?>mm !important;
        font-size: 5.5px;
        line-height: 0.9 !important;
        padding: 0.1mm 0.5mm !important;
    }

    .label {
        font-weight: bold;
        white-space: nowrap;
    }

    .header-row .label {
        width: 40%;  /* All header label cells get same width */
        font-weight: bold;
    }

    .center {
        text-align: center;
    }

    .right {
        text-align: right;
    }

    /* Column widths */
    .col-1 { width: 22%; }
    .col-2 { width: 45%; }
    .col-3 { width: 11%; }
    .col-4 { width: 29%; }

    /* .col-1 { width: 20%; }
    .col-2 { width: 48%; }
    .col-3 { width: 15%; }
    .col-4 { width: 17%; } */

    /* Force exact heights for rows - ALL FIXED EXCEPT ITEM ROWS */
    .title-row {
        height: <?php echo $titleHeight; ?>mm !important;
    }

    .header-row {
        height: <?php echo $headerRowHeight; ?>mm !important;
    }

    .items-header-row {
        height: <?php echo $itemHeaderHeight; ?>mm !important;
    }

    /* ONLY ITEM ROWS HAVE DYNAMIC HEIGHT */
    .item-row {
        height: <?php echo $itemRowHeight; ?>mm !important;
    }

    .gross-weight-row {
        height: <?php echo $grossWeightHeight; ?>mm !important;
    }

    .net-weight-row {
        height: <?php echo $netWeightHeight; ?>mm !important;
    }

    .license-row {
        height: <?php echo $licenseHeight; ?>mm !important;
    }

    .company-info-row {
        height: <?php echo $companyInfoHeight; ?>mm !important;
    }

</style>

</head>
<body>

    <table>
        <tr class="title-row">
            <td colspan="4">
                <div class="vertical-line left"></div>
                {{-- <img src="<?php echo public_path('images/crilogo1.png'); ?>" class="logo" alt="CRI Logo"> --}}
                <img src="<?php echo public_path('images/crilogo1.png'); ?>"
             class="logo"
             alt="CRI Logo"
             style="height: <?php echo $logoHeight; ?>mm;
                    max-width: <?php echo $logoMaxWidth; ?>mm;
                    width: auto;">
                C.R.I. POLY WRAPPED WINDING WIRE
            <div class="vertical-line right"></div>
                {{-- <img src="<?php echo public_path('images/isi_8783.png'); ?>"
                class="logo"
                alt="CRI Logo"
                style="height: <?php echo $isilogoHeight; ?>mm;
                        max-width: <?php echo $isilogoMaxWidth; ?>mm;
                        left: 71mm;"> --}}
                         <img src="{{ $qrBase64 }}"
                        style="position: absolute; bottom: 0.8mm; right: 2mm; width: 8mm; height: 7.8mm;">
            </td>
        </tr>

        <!-- Header Information Rows - FIXED 5mm each -->
        <?php foreach ($headerRows as $label => $value): ?>
        {{-- <tr class="header-row">
            <td class="label" colspan="2"><?php echo $label; ?> :</td>
            <td colspan="2"><?php echo $value; ?></td>
        </tr> --}}
        <tr class="header-row">
            <td class="label"><?php echo $label; ?></td>
            <td colspan="3"><?php echo $value; ?></td>
        </tr>

        <?php endforeach; ?>

        <!-- Items Header - FIXED 5mm -->
        <tr class="items-header-row">
            <td class="col-1 center">MATERIAL CODE</td>
            <td class="col-2 center">DESCRIPTION</td>
            <td class="col-3 center">QTY in kg</td>
            <td class="col-4 center">NO. OF BOXES</td>
        </tr>

        <!-- Item Rows - ONLY THESE ADJUST DYNAMICALLY -->
        <?php if(count($items) > 0): ?>
            <?php foreach ($items as $item): ?>
            <tr class="item-row">
                <td class="col-1 center"><?php echo $item->code; ?></td>
                <td class="col-2" style="white-space: nowrap"><?php echo $item->description; ?></td>
                <td class="col-3 right"><?php echo number_format($item->weight, 3); ?></td>
                <td class="col-4 center"><?php echo $item->box_count; ?></td>
            </tr>
            <?php endforeach; ?>
        <?php else: ?>
            <tr class="item-row">
                <td colspan="4" class="center">No items available</td>
            </tr>
        <?php endif; ?>

        <!-- Gross Weight - FIXED 5mm -->
        <tr class="gross-weight-row">
            <td colspan="2" class="label center">GROSS WEIGHT(kg)</td>
            <td colspan="2" class="center"><?php echo number_format($grossWeight, 3); ?></td>
        </tr>

        <!-- Net Weight - FIXED 5mm -->
        <tr class="net-weight-row">
            <td colspan="2" class="label center">NET WEIGHT(kg)</td>
            <td colspan="2" class="center"><?php echo number_format($netWeight, 3); ?></td>
        </tr>

        <!-- License - FIXED 5mm -->
        <tr class="license-row">
            <td colspan="4" class="center">
                {{-- MANUFACTURERS MADE IN INDIA *UNDER LICENSE --}}
                MANUFACTURERS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                MADE IN INDIA&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                *Under License
            </td>
        </tr>

        <!-- Company Info - FIXED 8mm -->
        <tr class="company-info-row">
            <td colspan="4" class="center">
                C.R.I. PUMPS PRIVATE LIMITED<br>
                (Unit of {{ $plantName }})<br>
                {{ $plantAddress }}<br>
                India Regd.Office : 7/46-1, Keeranatham Road, Saravanampatti, Coimbatore- 641 035<br>
                For Feedback/Complaint: C.R.I. Customer care cell Toll-Free: 1800 121 1243
                 {{-- <img src="{{ $qrBase64 }}"
             style="position: absolute; bottom: 2.8mm; right: 2mm; width: 8mm; height: 6.8mm;"> --}}
            </td>
        </tr>
    </table>
</body>
</html>
