<?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 = "Item Query ";
$action = "itemquery_fr.php";
$usertype = $_SESSION["usertype_" . $dbname];
$zroot = $_SESSION["zdir"];

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

include("includes/linkshead.php");
?>

<style>
    .search {
        border: 1px solid;
        margin-left: 15px;
        height: 28px;
        width: 30px;
        font-size: 17px;
        text-align: center;
        cursor: pointer;
    }
</style>

<script>
    $(document).ready(function () {
        $("body").on("click", ".search", itemModalShow);
        $("body").on("blur change", ".barcode", GetDetails);
        $(".iTbody").on("click", ".itemTr", addTrVal);
        $(".itemModal").on("keyup", ".isearch", getItemData);
        $(".itemModal").on("change", ".isortBy, iorderAS", getItemData);
        $(".inoRecord").hide();



        function GetDetails() {
            var barcode = $('.barcode').val();


                $.ajax({
                    type: "POST",
                    datatype: "json",
                    url: "search_ibarcode.php",
                    data: {barcode: barcode},
                    success: function (response) {

                        var m = response;
                        $(".barcode").val(m.barcode);
                        $(".prodname").val(m.prodname);
                        $(".hsncode").val(m.hsncode);
                        $(".brand").val(m.brand);
                        $(".size").val(m.size);
                        $(".quality").val(m.quality);

                    },
                    error: function (request, textStatus, errorThrown) {

                    }
                });
        }



        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");
                $(".prodname").removeClass("dataProdname");
                $(".hsncode").removeClass("dataHsncode");
                $(".brand").removeClass("dataBrand");
                $(".size").removeClass("dataSize");
                $(".quality").removeClass("dataQuality");
                $(".godown").removeClass("dataGodown");

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

                $(".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;
                        $(".barcode").val(m.barcode);
                        $(".prodname").val(m.prodname);
                        $(".hsncode").val(m.hsncode);
                        $(".brand").val(m.brand);
                        $(".size").val(m.size);
                        $(".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='iprodname' value='" + this.prodname + "' readonly style='width:100%' ></td>" +
                                "<td align='left'><input type='text' class='ihsncode' value='" + this.hsncode + "' 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='isize' value='" + this.size + "' 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(),
                    ihsncode = tr.find(".ihsncode").val(),
                    ibrand = tr.find(".ibrand").val(),
                    isize = tr.find(".isize").val(),
                    iquality = tr.find(".iquality").val();

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


            $(".dataBarcode").removeClass('dataBarcode');
            $(".dataProdname").removeClass('dataProdname');
            $(".dataHsncode").removeClass("dataHsncode");
            $(".dataBrand").removeClass("dataBrand");
            $(".dataSize").removeClass("dataSize");
            $(".dataQuality").removeClass("dataQuality");
            $(".itemModal").modal("hide");
//        $(".dataQty").focus();
            $(".qty").focus();
            $(".qty").select();

        }


        $(".qty").blur(function () {
            $(".dataQty").removeClass("dataQty");
        });
    });


</script>

</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 ">Barcode</label>
                        </div>
                        <div class="col-lg-5 col-md-5 col-sm-8 col-xs-8 pad">
                            <select class='form-control select2 barcode'  name='barcode'   required style="height:33px;">
                                <option value=""></option>
                                <?php
                                $getcode = mysqli_query($conn, "SELECT `code` FROM `item` ORDER BY `code`") or die(mysqli_error($conn));
                                while ($ibarcode = mysqli_fetch_array($getcode)) {
                                    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 ">Product Name</label>
                        </div>
                        <div class="col-lg-5 col-md-5 col-sm-8 col-xs-8 pad">
                            <input type="text" id="prodname" name="prodname" class="prodname form-control" readonly value="<?php echo $prodname; ?>" />  
                        </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 ">Brand</label>
                        </div>
                        <div class="col-lg-5 col-md-5 col-sm-8 col-xs-8 pad">
                            <input type="text" id="brand" name="brand" class="brand form-control" readonly value="<?php echo $brand; ?>" />  
                        </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 ">Size</label>
                        </div>
                        <div class="col-lg-5 col-md-5 col-sm-8 col-xs-8 pad">
                            <input type="text" id="size" name="size" class="size form-control" readonly value="<?php echo $size; ?>" />  
                        </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 ">Quality</label>
                        </div>
                        <div class="col-lg-5 col-md-5 col-sm-8 col-xs-8 pad">
                            <input type="text" id="qualtiy" name="qualtiy" class="quality form-control" readonly value="<?php echo $quality; ?>" />  
                        </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 ">Godown</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 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>

                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control ">As On Date</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" required value="<?php echo $mtodate; ?>" />  
                        </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="1">HSN Code</option>
                                                <option value="3">Brand</option>
                                                <option value="2">Size</option>
                                                <option value="4">Quality</option>
                                                <option value="4">Barcode</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%">HSN Code</th>
                                                    <th width="10%">Brand</th>
                                                    <th width="10%">Size</th>
                                                    <th width="10%">Quality</th>
                                                    <th width="10%">Barcode</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>


                    <div class="panel-footer">
                        <?php include("includes/but_rep.php"); ?>

                    </div>
                </div>
            </div>
    </form>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
