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

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

if (isset($_REQUEST["type"])) {
    $mdate = Date("Y-m-d");
    $brand = $_REQUEST['brand'];
    $quality = $_REQUEST['quality'];
    $shade = $_REQUEST['shade'];
    $yarn = $_REQUEST['yarn'];
    $type = $_REQUEST['type'];

    $brandcond = "";
    $qualitycond = "";
    $shadecond = "";
    $yarncond = "";
    $typecond = "";

    if (!empty($brand)) {
        $brandcond = " AND b.`brand`='$brand' ";
    }
    if (!empty($quality)) {
//        $qualitycond = " AND `quality` LIKE '$quality' ";
        $qualitycond = " AND b.`quality` = '$quality' ";
    }
    if (!empty($shade)) {
//        $shadecond = " AND `shade` LIKE '$shade' ";
        $shadecond = " AND b.`shade` = '$shade' ";
    }
    if (!empty($yarn)) {
//        $yarncond = " AND `yarn` LIKE '$yarn' ";
        $yarncond = " AND b.`yarn` = '$yarn' ";
    }
    if (!empty($type)) {
        $typecond = " AND b.`type`='$type' ";
    }
    
     $getitem = mysqli_query($conn, "SELECT * from item where code in (SELECT itemcode FROM `transacn` a, `item` b 
                           WHERE a.`itemcode` =  b.`code` $brandcond $qualitycond $yarncond $typecond  $shadecond
                           GROUP BY a.itemcode, a.shade, a.actbatch having sum(a.totqty) > 1) 
                           group by code order by name") or die(mysqli_error($conn));

//    $getitem = mysqli_query($conn, "SELECT * FROM `item` WHERE `name`!='' $brandcond $qualitycond $yarncond $typecond  $shadecond GROUP BY name ORDER BY `name`")or die(mysqli_error($conn));
        
    if (mysqli_num_rows($getitem) != 0) {
        ?>

        <div class="col-lg-12 pad">
            <div class="panel panel-default">
                <div class="panel-body">
                    <div class="col-lg-12 pad" style="overflow-y: scroll; height: 800px;">
                        <table class="table table-condensed table-bordered">
                            <tr style="background-color: #1ABC9C;">
                                <th style="width:20%;">ITEMNAME</th>
                                <th style="width:80%;">SHADE/QTY.</th>
                            </tr>
                            <?php
                            while ($item = mysqli_fetch_array($getitem)) {
                                ?>
                                <tr>
                                    <td><label class="label-control"><?php Echo $item['name']; ?></label></td>
                                    <td>
                                        <?php
                                        $getstock = mysqli_query($conn, "SELECT SUM(totqty) as tactqty, SUM(flag) as tflag, shade FROM 
                                                `transacn` WHERE `itemcode`='$item[code]' GROUP BY shade, actbatch 
                                                 having sum(totqty) > 1 ORDER BY shade, actbatch")or die(mysqli_error($conn));
                                        while ($stock = mysqli_fetch_array($getstock)) {
                                            $actcol = "";
                                            if ($stock['tactqty'] < 0) {
                                                $actcol = "style='color : red;'";
                                            }
                                            if ($stock['tactqty'] > 1) {
//                                                browse($stock[tactqty]);
                                            ?>
                                            <label class="label-control" style="color: #1ABC9C;">
                                                <?php Echo $stock['shade'] ?>
                                            </label><label class="label-control">
                                                &nbsp;/&nbsp;
                                            </label>
                                            <label class="label-control" <?php Echo $actcol; ?>> 
                                                <?php 
                                                if ($stock['tflag'] > 0) {
                                                    Echo $stock['tactqty'] . '<span class="text-primary"> ('. $stock['tflag'] . 'F)</span>'; 
                                                } else {
                                                    Echo $stock['tactqty']; 
                                                }    
                                                ?>
                                            </label>
                                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                            <?php
                                            }
                                        }
                                        ?>
                                    </td>
                                    <?php
                                }
                                ?>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <?php } else { ?>
        <div class="col-lg-12 pad">
            <div class="panel panel-default">
                <div class="panel-body">
                    <div class="col-lg-12 pad text-center">
                        <h1>No Record Found !!!!</h1>
                        <br>
                        <img src="assets/img/norecord.jpg" style="width: 250px; height: 200px" alt="No Record">
                    </div>
                </div>
            </div>
        </div>
        <?php } ?>
<?php } ?>
