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

$formname = 'Barcode Label';
$font_size = "2px";
$barcode_width = "155px";
$barcode_height = "20px";

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

$zroot = $_SESSION["zdbroot"];

$count = $_REQUEST["number"];
$num_qty = $count;

$barcode = $_REQUEST['barcode'];
//browse($barcode);
//browse($itemname);
$itemname = $_REQUEST['name'];
$mrp = $_REQUEST['mrp'];
$msp1 = $_REQUEST['msp'];
$msp = round($msp1, 0);
//browse($msp);
$costcode = '';
//browse($costcode);
?>

<html>
    <head>
        <title><?= $formname ?></title>
        <style type="text/css">
            table { page-break-inside:auto }
            tr    { page-break-inside:avoid; page-break-after:auto }
            thead { display:table-header-group }
            tfoot { display:table-footer-group }
            table {
                table-layout:fixed;
                width:100%;
                margin-left: 1px;
                margin-bottom: 1px;
                margin-top: 3px;
            }
            td.one{
                width:20%;
                border:0px solid black;                
            }

            .page {

                width: 210mm;
                min-height: 290mm;
                padding: 1mm;
                margin:2mm auto;
                /*border: 1px #D3D3D3 solid;*/
                /*border-radius: 5px;*/
                background: white;

            }
        </style>

    </head>
    <body >
<!--        <table width='100%' class="page" border='0' cellspacing='0' cellpadding='0' style=''>
        -->
<!--<tbody>-->
        <?php
//                browse(fmod(3,5));
        $ctr = 0;
        $breaker = 1;
        $location = '';

        $convmsp = (string) $msp;
        $clen = strlen($convmsp);
//                browse($clen);
//   
        $dcode = "";
        for ($i = 0; $i < $clen; $i++) {
            $c1 = substr($convmsp, $i, 1);
            $getmsp = mysqli_query($conn, "Select * from $zroot.msp where no = '$c1'") or die(mysqli_error($conn));
            $fetmsp = mysqli_fetch_array($getmsp);
            $codmsp = $fetmsp[code];
            $dcode = $dcode . $codmsp;
//                    browse($dcode);
        }

        if ($num_qty < 5) {
            $bal5 = 5 - $num_qty;
        } else {
            $bal5 = fmod($num_qty, 5) + 1;
        }
        for ($i = 1; $i <= $num_qty; $i++) {


            $ctr++;
            $lblctr++;
            if ($ctr == 1) {
//                browse($ctr);
                echo "<table border=\"0\" align=\"left\" style =\"width: 45mm;height:36mm\">";

                echo "\n <tr> \n ";
                echo "\n <td style=\"width: 40mm; max-width: 40mm; height:35mm; padding-left:2mm;padding-top:1mm;\" valign=\"top\"> \n";
            } else {
//                browse($ctr . '  2');
                echo "\n <td style=\"width: 40mm; max-width: 40mm; height:35mm; padding-left:1mm;\" valign=\"top\"> \n";
            }

            echo "<span>Barcode $barcode ";
            echo "<br /> <img alt='$itemcode' src='barcode.php?text=$barcode' style=''/>";
            echo '<br />' . strtoupper($itemname);
            echo '<br />  Qty. : 1 PCS.';
            echo '<br />' . "<b>MRP : " . $mrp . "</b>";
            echo '<br />' . $dcode;

            echo "</span> ";
            echo "\n";
            echo "</td>";
            if ($ctr == 5) {
                echo "\n </tr> \n ";
                $ctr = 0;
                echo "</table>";
            }
            if ($lblctr >= 40) {
                $lblctr = 0;
            }
        }
        $bstart = $num_qty + 1;
        if ($bal5 > 0) {
//                    browse('gya');
//                    browse($bal5);
            for ($i = 1; $i <= $bal5; $i++) {

//                        browse('aaya');
                $ctr++;
                $lblctr++;
                if ($ctr == 1) {
//                    browse($ctr . ' 3');
                    echo "<table border=\"0\" align=\"left\" style =\"width: 45mm;height:36mm\">";
                    echo "\n <tr> \n ";
                    echo "\n <td style=\"width: 40mm; max-width: 40mm; height:35mm; padding-left:1mm;\" valign=\"top\"> \n";
                } else {
//                    browse($ctr . '  4');
                    echo "\n <td style=\"width: 40mm; max-width: 40mm; height:35mm; padding-left:1mm;\" valign=\"top\"> \n";
                }

                echo "<span><b>&nbsp;&nbsp;</b>";
                echo "<br />&nbsp;&nbsp;";

                echo '<br />&nbsp;&nbsp;';
                echo '<br />&nbsp;&nbsp;';
                echo '<br />&nbsp;&nbsp;';
                echo '<br />&nbsp;&nbsp;';

                echo "</span> ";
                echo "\n";
                echo "</td>";
                if ($ctr == 5) {
                    echo "\n </tr> \n ";
                    $ctr = 0;
                    echo "</table>";
                }
            }
        }
###################################
//                    echo "<td width='20%' class=''>";
//                    echo "<table width='100%'   style=''>";
//
//                    echo "<tr>";
//                    echo "<td align='' class=''>";
//                    echo "<font size='1px'>Barcode : " . $barcode . "</font>";
//                    echo "</td>";
//                    echo "</tr>";
//
//                    echo "<tr>";
//                    echo "<td align='' class=''>";
//                    echo "<img alt='$itemcode' src='barcode.php?text=$barcode' style=''/>";
//                    echo "</td>";
//                    echo "</tr>";
//
//
//                    echo "<tr>";
//                    echo "<td align='' class=''>";
//                    echo "<font size='1px'>" . strtoupper($itemname) . "</font>";
//                    echo "</td>";
//
//                    echo "<tr>";
//
//                    echo "<td align='' class=''>";
//                    echo "<font size='$font_size'><b> Qty. : 1 PCS.</b></font>";
////                    echo "<font size='$font_size'><b>1 PCS.</b></font>";
//                    echo "</td>";
//                    echo "</tr>";
//                    echo "<tr>";
//                    echo "<td align='' class=''>";
//                    echo "<font size='$font_size'><b> MRP : " . $mrp . "</b></font>";
//
////                    echo "<font size='$font_size'><b>" . $mrp . "</b></font>";
//                    echo "</td>";
//                    echo "</tr>";
//
//                    echo "<tr>";
//                    echo "<td align='' class=''>";
////                    echo "<font size='$font_size'><b</b></font>";
//                    echo "<font size='$font_size'><b>" . $dcode . "</b></font>";
//                    echo "</td>";
//                    echo "</tr>";
//
//
//                    echo "<tr style='width:15px;'>";
//                    echo "</tr>";
//                    echo "</table>";
//                    echo "</td>";
//
//                    if ($ctr == 5) {
//
//                        if ($breaker == 40) {
//                            
//                        } else {
//                            echo "<tr>";
//                        }
//                        $ctr = 1;
//                    } else {
//                        $ctr++;
//                    }
//
//                    if ($breaker == 40) {
//                        echo "</tbody></table>";
//                        echo "<div  style='page-break-before:always;'>";
//                        //echo "<br/>";
//                        echo "<table width='230px' border='0' cellspacing='0' cellpadding='0' style='height:20px;'>";
//                        echo "</br>";
//                        echo "</br>";
//                        $breaker = 1;
//                    } else {
//                        $breaker++;
//                    }
//                }
//                if ($breaker < 40) {
//                    for ($j = $breaker; $j <= 40; $j++) {
//                        $breaker++;
//                    }
        ?>
        <!--</tbody>-->
        <!--</table>-->
    </body>
</html>