<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

/* GET ITEMS */
$items = json_decode($_POST['label_items'], true);

$labels = [];

/* EXPAND QTY */
foreach ($items as $item) {

    $name = $item['name'];
    $mrp = $item['mrp'];
    $qty = $item['qty'];
    $size = isset($item['size']) ? $item['size'] : "";

    for ($i = 0; $i < $qty; $i++) {
        $labels[] = [
            'name' => $name,
            'mrp' => $mrp,
            'size' => $size
        ];
    }
}

$total = count($labels);
$per_page = 16;
$total_pages = ceil($total / $per_page);

$index = 0;
?>

<!DOCTYPE html>
<html>

<head>
    <style>
        @page {
            size: A4;
            margin: 0;
        }

        body {
            margin: 0;
            padding: 0;
            font-family: Arial, Helvetica, sans-serif;
        }

        /* FULL A4 SHEET */
        .sheet {
            width: 210mm;
            height: 297mm;

            display: grid;
            grid-template-columns: 99mm 99mm;
            grid-template-rows: repeat(8, 36mm);
            grid-template-columns: 1fr 1fr;
            column-gap: 0mm;
            row-gap: 0mm;

            /* tuned margins for TechNova MPL16L */
            padding-top: 12.6mm;
            padding-left: 6mm;

            box-sizing: border-box;
            page-break-after: always;

        }


        .label {
            width: 99mm;
            height: 36mm;
            /* border: 1px solid #000; */
            box-sizing: border-box;

            display: flex;
            justify-content: center;
            align-items: center;

            padding: 2mm;
        }

        /* LEFT COLUMN */
        .label:nth-child(odd) {
            justify-self: end;
            margin-left:-2mm;            
        }

        /* RIGHT COLUMN */
        .label:nth-child(even) {
            justify-self: start;
            /* margin-left: 1mm; */
        }

        /* SMALL TABLE INSIDE */
        .tbl {
            width: 97%;
            height: 82%;
            border-collapse: collapse;
            table-layout: fixed;
        }

        .tbl td {
            border: 1px solid #000;
            padding: 1mm 2mm;
            vertical-align: middle;
        }

        .head {
            font-size: 9px;
            font-weight: bold;
            text-align: center;
        }

        .item {
            font-size: 17px;
            font-weight: bold;
        }

        .size {
            font-size: 25px;
            font-weight: bold;
            text-align: center;
        }

        .mrp {
            font-size: 16px;
            font-weight: bold;
            text-align: right;
        }
    </style>
</head>

<body>

    <?php
    for ($p = 0; $p < $total_pages; $p++) {

        echo '<div class="sheet">';

        for ($i = 0; $i < $per_page; $i++) {

            if (!isset($labels[$index]))
                break;

            $row = $labels[$index];

            echo '
        <div class="label">
        <table class="tbl">
<tr>
    <td class="head" style="width:58%;">Item Name</td>
    <td class="head" style="width:18%;">Size</td>
    <td class="head" style="width:24%;">M.R.P.</td>
</tr>

<tr>
    <td class="item">' . $row['name'] . '</td>
    <td class="size">' . $row['size'] . '</td>
    <td class="mrp">' . number_format($row['mrp']) . '</td>
</tr>
</table>
        </div>
        ';

            $index++;
        }

        echo '</div>';
    }
    ?>

    <script>
        window.print();
    </script>

</body>

</html>