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


$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];

$zyear = right($zdb, 4);
$mfrdate = getzdates($zyear, 'S');
$mtodate = getzdates($zyear, 'E');
$bseries = $_SESSION["series_" . $dbname];

$formname = "Stock";
$action = "stock_fr.php";
$usertype = $_SESSION["usertype_" . $dbname];
$zroot = $_SESSION["zdir"];

$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

include("includes/linkshead.php");
?>
<script>
    $(document).ready(function () {
        $("body").on("click", ".search", itemModalShow);
        $(".iTbody").on("click", ".itemTr", addTrVal);
        $(".itemModal").on("keyup", ".isearch", getItemData);
        $(".itemModal").on("change", ".isortBy, iorderAS", getItemData);
        $(".inoRecord").hide();

        window.addEventListener('keydown', function (e) {
            if (e.keyIdentifier == 'U+000A' || e.keyIdentifier == 'Enter' || e.keyCode == 13) {
                if (e.target.nodeName == 'INPUT' && e.target.type == 'text' && e.target.className == "iprodname") {
                    addTrVal(e);
                    event.preventDefault();
                }
            }
        }, true);

        function itemModalShow() {
            var tr = $(this).closest("tr"),
                    srno = tr.find(".srno").val(),
                    party = $(".cmbparty").val(),
                    file = $(".file").val(),
                    godown = $(".godown").val(),
                    margin = srno * 2.5,
                    barcode = $(".barcode").val(),
                    itemcode = tr.find(".itemcode").val();
            $(".modal-content").css("margin-top", margin + "%")

            if (barcode == "") {
                $(".barcode").removeClass("dataBarcode");
                $(".brand").removeClass("dataBrand");
                $(".prodname").removeClass("dataProdname");
                $(".quality").removeClass("dataQuality");
                $(".size").removeClass("dataSize");

                $(".prodname").addClass("dataProdname");
                $(".barcode").addClass("dataBarcode");
                $(".brand").addClass("dataBrand");
                $(".quality").addClass("dataQuality");
                $(".size").addClass("dataSize");

                $(".itemModal").modal("show");
                getItemData();
            } else {
                $.ajax({
                    type: "POST",
                    datatype: "json",
                    url: "search_item.php",
                    data: {barcode: barcode, itemcode: itemcode, party: party, file: file, godown: godown},
                    success: function (response) {
                        var m = response;

                        $(".prodname").val(m.prodname);
                        $(".barcode").val(m.barcode);
                        $(".brand").val(m.brand);
                        $(".quality").val(m.quality);
                    },
                    error: function (request, textStatus, errorThrown) {

                    }
                });
            }
        }

        $(".itemModal").on('shown.bs.modal', function () {
            $(".isearch").focus();
            $(".isearch").val("");
            $(".ifield").val("1");
            getItemData();
        });

        $(".itemModal").on('hidden.bs.modal', function () {
            $(".dataQty").focus();
//        $(".qty").focus();
            $(".dataQty").select();
        });

        var imrangeFrom = 0,
                imrangeTo = 40;

        function getItemData(pagination) {

            $(".iloader").show();

            if (pagination == 'prev') {
                if (imrangeFrom > 0) {
                    imrangeFrom = (imrangeFrom - 40);
                    imrangeTo = (imrangeTo - 40);
                }
            }
            if (pagination == 'next') {
                imrangeFrom = (imrangeFrom + 40);
                imrangeTo = (imrangeTo + 40);
            }

            var vouchno = $(".vouchno").val(),
                    file = $(".file").val(),
                    isearch = $(".isearch").val(),
                    ifield = $(".ifield").val(),
                    ioperator = $(".ioperator").val(),
                    sortBy = $(".isortBy").val(),
                    orderAs = $(".iorderBy").val(),
                    party = $(".cmbparty").val(),
                    godown = $(".godown").val(),
                    data = {isearch: isearch, ifield: ifield, ioperator: ioperator, sortBy: sortBy, orderAs: orderAs,
                        vouchno: vouchno, file: file, rangeFrom: imrangeFrom, rangeTo: imrangeTo, party: party, godown: godown},
                    values = "";
            $.ajax({
                type: 'GET',
                dataType: 'json',
                url: "search_item.php",
                data: data,
                success: function (response) {
                    $.each(response, function () {
                        values += "<tr class='itemTr'>" +
//                            "<td align='left'><input type='text' class='itemcode' value='" + this.itemcode + "' readonly style='width:100%'tabindex='-1'></td>" +
                                "<td align='left'><input type='text' class='iprodname' value='" + this.prodname + "' readonly style='width:100%' ></td>" +
                                "<td align='left'><input type='text' class='isize' value='" + this.size + "' readonly style='width:100%'  tabindex='-1'></td>" +
                                "<td align='left'><input type='text' class='ibrand' value='" + this.brand + "' readonly style='width:100%' tabindex='-1'></td>" +
                                "<td align='left'><input type='text' class='iquality' value='" + this.quality + "' readonly style='width:100%' tabindex='-1'></td>" +
                                "<td align='left'><input type='text' class='icode' value='" + this.code + "' readonly style='width:100%' tabindex='-1'></td>" +
                                "</tr>";
                    });
                    $(".iloader").hide();
                    $(".iTbody").empty();
                    $(".iTbody").html(values);
                    if (response) {
                        $(".search-action-bar").show();
                    }
                },
                error: function (request, textStatus, errorThrown) {

                }
            });
        }
        $(".iload").click(function () {
            getItemData();
        });
//    $(".isearch").change(function () {
//        getItemData();
//    });
        $(".iprevious").click(function () {
            getItemData('prev');
        });
        $(".inext").click(function () {
            getItemData('next');
        });

        function addTrVal(e) {
            var tr = "";
            if (e == "undefined") {
                tr = $(this).closest("tr");
            } else {
                tr = $(e.target).closest("tr");
            }

            var iprodname = tr.find(".iprodname").val(),
                    icode = tr.find(".icode").val(),
                    ibrand = tr.find(".ibrand").val(),
                    isize = tr.find(".isize").val(),
                    iquality = tr.find(".iquality").val();



            $(".dataProdname").val(iprodname).trigger('change');
            $(".dataBarcode").val(icode).trigger('change');
            $(".dataBrand").val(ibrand).trigger('change');
            $(".dataQuality").val(iquality).trigger('change');
            $(".dataSize").val(isize).trigger('change');


            $(".dataProdname").removeClass('dataProdname');
            $(".dataBarcode").removeClass('dataBarcode');
            $(".dataBrand").removeClass("dataBrand");
            $(".dataSize").removeClass("dataSize");
            $(".dataQuality").removeClass("dataQuality");
            $(".itemModal").modal("hide");
//        $(".dataQty").focus();
            $("#mfrdt").focus();
        }


        $("#printbut").click(function () {
            var prodname = $(".prodname").val(),
                    barcode = $(".barcode").val(),
                    size = $(".size").val(),
                    brand = $(".brand").val(),
                    frmdt = $(".frmdt").val(),
                    todt = $(".todt").val(),
                    quality = $(".quality").val();
            $.post('getDStk.php', {quality: quality, brand: brand, prodname: prodname, size: size, barcode: barcode, todt: todt, frmdt: frmdt}, function (data) {
                $(".tbody").html(data);
                $("#printmodal").modal('show');
            });

        });

        $(".checkAll").click(function () {
            if ($(this).is(':checked')) {
                $(".chk").prop('checked', true);
            } else {
                $(".chk").prop('checked', false);
            }
        });
    });

</script>
<style>
    .search {
        /*border: 1px solid;*/
        padding: 1px; 
        margin: 6px;
        width: 30px;
        height:  15px;
        font-size: 17px;
        text-align: center;
        cursor: pointer;
    }
    
</style>
</head>
<body>
    <form method='POST' action="<?= $action; ?>">
        <!-- Default box -->

        <div class="col-lg-5 col-md-12 col-sm-12 col-xs-12  pad">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><?php Echo $formname; ?></h3>
                    <div class="actions pull-right">
                        <i class="fa fa-chevron-down"></i>
                        <i class="fa fa-times"></i>
                    </div>
                </div>
                <div class="panel-body">

                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Barcode &nbsp;</label>
                        </div>
                        <div class="col-lg-5 col-md-5 col-sm-8 col-xs-8 pad">
                            <select class='form-control  barcode' name='barcode' style="height:33px;">
                                <option value=""></option>
                                <?php
                                $getbarcode = mysqli_query($conn, "SELECT `code` FROM `item` ORDER BY `code`") or die(mysqli_error($conn));
                                while ($ibarcode = mysqli_fetch_array($getbarcode)) {
                                    echo "<option value='$ibarcode[code]'>$ibarcode[code]</option>";
                                }
                                ?>
                            </select>
                        </div>
                        <label class="label-control search"><i class="fa fa-search"></i></label>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Product Name &nbsp;</label>
                        </div>
                        <div class="col-lg-5 col-md-5 col-sm-8 col-xs-8 pad">
                            <select class='form-control prodname' name='prodname' class="prodname form-control"  style="height:33px;">
                                <option value=""></option>
                                <?php
                                $getprodname = mysqli_query($conn, "SELECT `prodname` FROM `item` GROUP BY `prodname` ORDER BY `prodname`") or die(mysqli_error($conn));
                                while ($iprodname = mysqli_fetch_array($getprodname)) {
                                    echo "<option value='$iprodname[prodname]'>$iprodname[prodname]</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Brand &nbsp;</label>
                        </div>
                        <div class="col-lg-5 col-md-5 col-sm-8 col-xs-8 pad">
                            <select class='form-control brand' name='brand' class="brand form-control" style="height:33px;">
                                <option value=""></option>
                                <?php
                                $getbrand = mysqli_query($conn, "SELECT `brand` FROM `item` GROUP BY brand ORDER BY `brand`") or die(mysqli_error($conn));
                                while ($ibrand = mysqli_fetch_array($getbrand)) {
                                    echo "<option value='$ibrand[brand]'>$ibrand[brand]</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Quality &nbsp;</label>
                        </div>
                        <div class="col-lg-5 col-md-5 col-sm-8 col-xs-8 pad">
                            <select class='form-control quality' name='quality' class="quality form-control" style="height:33px;">
                                <option value=""></option>
                                <?php
                                $getquality = mysqli_query($conn, "SELECT `quality` FROM `item` GROUP BY quality ORDER BY `quality`") or die(mysqli_error($conn));
                                while ($iquality = mysqli_fetch_array($getquality)) {
                                    echo "<option value='$iquality[quality]'>$iquality[quality]</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Size &nbsp;</label>
                        </div>
                        <div class="col-lg-5 col-md-5 col-sm-8 col-xs-8 pad">
                            <select class='form-control size' name='size' class="size form-control" style="height:33px;">
                                <option value=""></option>
                                <?php
                                $getsize = mysqli_query($conn, "SELECT `size` FROM `item` GROUP BY size ORDER BY `size`") or die(mysqli_error($conn));
                                while ($isize = mysqli_fetch_array($getsize)) {
                                    echo "<option value='$isize[size]'>$isize[size]</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <br>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Godown &nbsp;</label>
                        </div>
                        <div class="col-lg-5 col-md-5 col-sm-8 col-xs-8 pad">
                            <select class='form-control godown' name='godown' class="godown" style="height:33px;">
                                <option value=""></option>
                                <?php
                                $getgodn = mysqli_query($conn, "SELECT `code`,name FROM `godown` ORDER BY `name`") or die(mysqli_error($conn));
                                while ($igodn = mysqli_fetch_array($getgodn)) {
                                    echo "<option value='$igodn[code]'>$igodn[name]</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <br>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control ">From Date &nbsp;</label>
                        </div>
                        <div class="col-lg-5 col-md-5 col-sm-8 col-xs-8 pad">
                            <input type="date" id="mfrdt" name="frmdt" class="form-control frmdt" value="<?php echo $mfrdate; ?>" />  
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control ">To Date &nbsp;</label>
                        </div>
                        <div class="col-lg-5 col-md-5 col-sm-8 col-xs-8 pad">
                            <input type="date" id="mtodt" name="todt" class="form-control todt" value="<?php echo $mtodate; ?>" />  
                        </div>
                    </div>
                    <br>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Print Zero Stock &nbsp;</label>
                        </div>
                        <div class="col-lg-2 col-md-5 col-sm-8 col-xs-8 pad">
                            <select name="prnzero" class="form-control select2" style="height:33px;">
                                <option value="Y">Yes</option>
                                <option value="N">No</option>
                            </select>
                        </div>
                    </div>
                    <br>
                    <!--                    <div class="row">
                                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                                <label class="label-control">Print Value</label>
                                            </div>
                                            <div class="col-lg-2 col-md-5 col-sm-8 col-xs-8 pad">
                                                <select name="prnval" class="form-control select2" style="height:33px;">
                                                    <option value="1">No</option>
                                                    <option value="2">Yes</option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                                <label class="label-control" style="color:red">Report Type</label>
                                            </div>
                                            <div class="col-lg-2 col-md-5 col-sm-8 col-xs-8 pad">
                                                <select name="reptype" class="form-control select2 reptype" style="height:33px;">
                                                    <option value="1">Summary</option>
                                                    <option value="2">Detail</option>
                                                </select>
                                            </div>
                                        </div>-->

                    <div class="panel-footer">
                        <div class="text-center pad">
                            <input type="button" id="printbut" class="btn btn-pad btn-primary" style="color: #fff;background-color: #05B2EA;" value="Print">
                            <button id='cancel' type="reset" class="btn btn-pad btn-primary " style="background-color:red;"  onclick="window.location.href = '<?= 'default.php' ?>'">
                                <i class="fa fa-power-off" style="color:#ffffff"></i>
                            </button>
                        </div>
                    </div>
                    <!--                    <div class="panel-footer">
                    <?php // include("includes/but_rep.php"); ?>
                                        </div>-->
                </div>
            </div>
            <div class="modal fade" tabindex="-1" role="dialog" id="printmodal" aria-labelledby="myLargeModalLabel">
                <div class="modal-dialog modal-lg" style="margin-top: 10%;width: 50%">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button class="close" data-dismiss="modal"><span>&times;</span></button>
                            <h4 class="modal-title" id="myLargeModalLabel"></h4>
                        </div>
                        <div class="modal-body" id="">
                            <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                                <table id="" class="table table-striped table-condensed table-bordered sizeTable" width="50%">
                                    <thead>
                                        <tr class="mySearchTr">

                                            <th style="width:10%" class="text-center"><input type="checkbox" name="checkAll" class="checkAll" checked></th>
                                            <th width="20%">Product Name</th>
                                            <th width="10%">Size</th>
                                            <th width="10%">Brand</th>
                                            <th width="10%">Quality</th>
                                            <th width="10%">Barcode</th>
                                        </tr>
                                    </thead>

                                    <tbody class="tbody">

                                    </tbody>
                                </table>     
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button id="view" name='view' formtarget='_blank'  class="btn btn-pad btn-square " style="background-color:#05B2EA; ">
                                <i class="fa fa-television" style="color:#ffffff"></i>
                            </button>
        <!--                    <a id='print' class="btn btn-pad btn-square " style="background-color:#5BC0DE;" href='<?= $printfile ?>?vouchno=<?= $qvouch; ?>&buycopy' + document.invoice.buycopy.value + '' target="_blank">
                                <i class="fa fa-print" style="color:#ffffff"></i>
                            </a>-->
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal fade itemModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
                <div class="modal-dialog modal-lg">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button class="close" data-dismiss="modal"><span>&times;</span></button>
                            <h4 class="modal-title" id="myLargeModalLabel">Search <? Echo $formname; ?></h4>
                        </div>
                        <div class="modal-body">
                            <input type="hidden" value="<?php // Echo $fseries                                                                                                             ?>" class="form-control series">
                            <input type="hidden" value="<?php Echo $get['vouchno']; ?>" class="form-control vouchno">
                            <input type="hidden" value="<?php Echo $file ?>" class="form-control file">
                            <!--<input type="hidden" value="<?php Echo $para ?>" class="form-control para">-->
                            <div class="row">
                                <div class="col-lg-3">
                                    <select class="form-control ifield">
                                        <option value="1">Product Name</option>
                                        <option value="2">Size</option>
                                        <option value="3">Brand</option>
                                        <option value="4">Quality</option>
                                        <option value="5">Code</option>
                                        <!--                            <option value="4">Gram</option>
                                                                    <option value="5">Unit</option>-->
                                    </select>
                                </div>
                                <div class="col-lg-2">
                                    <select class="form-control ioperator">
                                        <option value=""></option>
                                        <option value="%">Like</option>
                                        <option value="=">Exactly</option>
                                    </select>
                                </div>
                                <div class="col-lg-4">
                                    <input type="text" value="" class="form-control isearch">
                                </div>
                                <div class="col-lg-3">
                                    <input type="button" class="btn btn-primary iload" value="Search">
                                </div>
                            </div>

                            <div class="col-lg-12 iloader text-center">
                                <img src="images/search.gif" alt="loading">       
                            </div>
                            <div class="col-lg-12 inoRecord text-center">
                                <h3>NO RECORDS FOUND</h3>
                            </div>
                            <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                                <table id="" class="table table-striped table-condensed " width="100%">
                                    <thead>
                                        <tr class="mySearchTr">
                                            <th width="20%">Product Name</th>
                                            <th width="10%">Size</th>
                                            <th width="10%">Brand</th>
                                            <th width="10%">Quality</th>
                                            <th width="10%">Barcode</th>
<!--                                            <th width="10%">Stk.</th>
                                            <th width="10%">Prod Type</th>-->

                                        </tr>
                                    </thead>
                                    <tbody class="iTbody">
                                    </tbody>
                                </table>
                            </div>
                            <div class="row">
                                <div class="col-xs-12 text-right search-action-bar">
                                    <input type="button"  class="btn btn-primary iprevious" value="Previous">
                                    <input type="button"  class="btn btn-primary inext" value="Next">
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer"></div>
                    </div>
                </div>
            </div>
    </form>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
