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

$formname = 'METRIC';
$maindb = 'diamast';

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


$shape = $_POST['shape'];


$shapecond="";

if (!empty($shape)) {
    $shapecond = " AND `shape`='$shape' ";
}

$mainqry = mysqli_query($conn,"SELECT * FROM $zroot.`$maindb`  WHERE `name` !=''  GROUP by `shape` ORDER BY shape,grp,name") or die(mysqli_error($conn));

//$atype = $get['active'];
include("includes/linkshead.php");
?>
<style>
    body {
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
        font: 12pt "Tahoma";
    }
    * {
        box-sizing: border-box;
        -moz-box-sizing: border-box;
    }
    .page {
        width: 210mm;
        min-height: 148mm;
        padding: 2mm;
        margin: 6mm auto;
        /*border: 1px #D3D3D3 solid;*/
        border-radius: 2px;
        background: white;

    }
    .subpage {
        padding: 0cm;
        /*        border: 1px solid;*/
        height: 150mm;
        /*outline: 2cm #ffffff solid;*/
    }
    
    td {
      
        font-weight: bold;
    }

    @page {
        size: A5;
        margin: 0;
    }
    @media print {
        html, body {
            width: 210mm;
            height: 148mm;        
        }
        .page {
            margin: 0;
            border: initial;
            border-radius: initial;
            width: 150%;
            min-height: initial;
            box-shadow: initial;
            background: initial;
            page-break-after: always;
            /*margin-left: 10%;*/ 
        }
    }
    .border{
        border: 1px solid;
    }
</style>
</head>
<body id="main_container">
    <div class="book">
        <?php
        $ctr = 0;
        while ($get = mysqli_fetch_array($mainqry)) {
            ?>
            <div class="page">

                <div class="row">
                    <div class="col-md-12 text-center">
                        <?php
                        require("includes/dbconnect.php");
                        $comp = mysqli_query($conn,"SELECT * FROM `company`") or die(mysqli_errno());
                        $zcomp = mysqli_fetch_assoc($comp);
                        ?>
                        <b><?php echo $zcomp[name]; ?></b><br>
                        <?php echo $zcomp[add1] . ' ' . $zcomp[add2] . ' ' . $zcomp[add3]; ?><br>
                        <b><?php echo $get['shape'] ?></b>

                    </div>

                </div>
                <br>
                <table class="" align='center' style="height:35%;width:90%;border:1px solid black; font-weight: 150em;">
                    <tbody>
                        <?php
                        $ctr = 0;
                        echo "<tr style='font-weight: bold;'>";
                        $subqry = mysqli_query($conn,"SELECT CAST(name as UNSIGNED) as mname, name FROM $zroot.`$maindb` WHERE `shape`='$get[shape]'  ORDER BY name ASC")or die(mysqli_error($conn));
                        while ($sub = mysqli_fetch_array($subqry)) {
                            
                            if ($ctr > 0) {
                                if ((int) $prevname != (int) $sub['mname']) {
                                    echo "<tr>";
                                }
                            }
                            echo "<td align='left' style='border:1px solid black;'>$sub[name]</td>";
                            if ($ctr > 0) {
                                if ((int) $prevname != (int) $sub['mname']) {
                                    echo "</tr>";
                                }
                            }
                            $prevname = (int) $sub[mname];
                            $ctr++;
                        }
                        echo "</tr>";
                        ?>
                    </tbody>
                </table>
                <br><br><br><br>
            </div>  
        </div>
    <?php } ?>
    <?php
    include("includes/linksfoot.php");
    ?>
    <script>
        $(function () {
            formGeneral();
        });
    </script>
</body>
</html>
