<?php
require_once("../includes/config.php");

$docid = $_REQUEST['doc_id'];
$stock = $_REQUEST['stock_type'];
$location = $_REQUEST['loc'];
$bin_id = $_REQUEST['bin_id'];
//// bin allocation details
$sql_bindet = "SELECT * FROM bin_allocation where partcode='" . $docid . "' and stock_type='" . $stock . "' and location_code='" . $location . "' and bin_id = '".$bin_id."'";
$res_bindet = mysqli_query($link1, $sql_bindet);
$row_bindet = mysqli_fetch_assoc($res_bindet);
$res = mysqli_query($link1, "select brandid,productid,psubcatid,itemtypeid from partcode_master where partcode='" . $docid . "'");
$row = mysqli_fetch_assoc($res);
?> 
 <div class="panel-group">
    <div class="panel panel-success table-responsive">
        <div class="panel-heading"><i class="fa fa-newspaper-o fa-lg"></i>&nbsp;&nbsp;Current Bin Details</div>
        <div class="panel-body">
            <table class="table table-bordered" width="100%">
                <tbody>
                    <tr>
                        <td width="25%"><label class="control-label">Product</label></td>
                        <td width="25%">
                            <?= getAnyDetails($row_bindet["partcode"], "part_name", "partcode", "partcode_master", $link1) . "(" . $row_bindet["partcode"] . ")"; ?>
                            <input type="hidden" name="partcode" id="partcode" value="<?= $row_bindet["partcode"] ?>">
                            <input type="hidden" name="location_code" id="location_code" value="<?= $row_bindet["location_code"] ?>">
                            <input type="hidden" name="stock_type" id="stock_type" value="<?= $row_bindet["stock_type"] ?>">
                        </td>
                        <td width="25%"><label class="control-label">Brand</label></td>
                        <td width="25%">
                            <?= getAnyDetails($row["brandid"], "brand", "brandid", "brand_master", $link1); ?>
                    </tr>
                    <tr>
                        <td width="25%"><label class="control-label">Product Category</label></td>
                        <td width="25%"><?= getAnyDetails($row["productid"], "product_name", "productid", "product_master", $link1); ?>
                        </td>
                        <td width="25%"><label class="control-label">Product Sub Category</label></td>
                        <td width="25%"><?= getAnyDetails($row["psubcatid"], "prod_sub_cat", "psubcatid", "product_sub_category", $link1); ?>
                        </td>
                    </tr>
    				<tr class="bg-warning">
                        <td width="25%"><label class="control-label">Current Bin No.</label></td>
                        <td width="25%"><?php echo $bin_no = getAnyDetails($row_bindet["bin_id"], "bin_no", "bin_id", "bin_master", $link1); ?>  
            <input type="hidden" name="changebin_id" id="changebin_id" value="<?=$row_bindet["bin_id"]?>"/>
            <input type="hidden" name="ref_no" id="ref_no" value="<?=$bin_no?>"/>
                        </td>
                        <td width="25%"><label class="control-label">Current Bin QTY</label></td>
                        <td width="25%"><?= $row_bindet["qty"] ?><input type="hidden" name="bin_qty" id="bin_qty" value="<?= $row_bindet["qty"] ?>">             
                        </td>
                    </tr>
                </tbody>
            </table>
        </div><!--close panel body-->
    </div><!--close panel-->
</div>
    <div class="form-group">
        <div class="col-md-12">
            <table class="table table-bordered" width="100%" id="itemsTable1">
                <thead>
                    <tr class="<?= $tableheadcolor ?>">
                        <th width="20%" style="text-align: center;">Change Bin No.</th>
                        <th width="20%" style="text-align: center;">Change Bin QTY</th>                                               
                    </tr>
                </thead>
                <tbody>
                    <tr id="addr1_0">
                        <td width="20%">
                            <select name="bin_no[0]" id="bin_no[0]" class="form-control required selectpicker show-tick" required onchange="checkDuplicatePart(this.value, 0)">
                                <option value="">--Please Select--</option>
                                <?php
                                $res_pro = mysqli_query($link1, "select bin_id,bin_no from bin_master where status='1' order by bin_no")or die(mysqli_error($link1));
                                while ($row_pro = mysqli_fetch_assoc($res_pro)) {
                                    if($bin_no !=$row_pro['bin_no']){
                                    ?>
                                    <option value="<?= $row_pro['bin_no'] ?>"><?= $row_pro['bin_no']; ?></option>
                                <?php } } ?>
                            </select>
                        </td>
                        <td width="10%">
                            <input type="text" name="qty[0]" class="form-control number required" id="qty[0]" required value="" onkeyup="getBinqty(this.value)" align="left" style="width: 30%;
                                   text-align: right;
                                   margin-left: 160px;
                                   "/><p id="bin_msg[0]" style="color:red;text-align: center;"></p>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-4" style="display:inline-block; float:left">
            <button class='btn<?= $btncolor ?>' id="add_row" type="button" name="add" value="Add"><i class="fa fa-plus-circle fa-lg"></i>&nbsp;&nbsp;Add More Row</button>
            <input type="hidden" name="rowno" id="rowno" value="0"/>
        </div>
        <div class="col-md-8" style="display:inline-block; float:right" align="left">

        </div>
    </div>
    <script>
        $(document).ready(function () {
            var spinner = $('#loader');
            $("#frm1").validate({
                submitHandler: function (form) {
                    if (!this.wasSent) {
                        this.wasSent = true;
                        $(':submit', form).val('Please wait...')
                                .attr('disabled', 'disabled')
                                .addClass('disabled');
                        spinner.show();
                        form.submit();
                    } else {
                        return false;
                    }
                }
            });
            $('.selectpicker').selectpicker({
                liveSearch: true
            });
            $('.number').on('input', function () {
                match = (/(\d{0,6})[^.]*((?:\.\d{0,6})?)/g).exec(this.value.replace(/[^\d.]/g, ''));
                this.value = match[1] + match[2];
            });
        });

        /////// add new row 
        $(document).ready(function () {
            $("#add_row").click(function () {

                var numi = document.getElementById('rowno');
                var itm = "bin_no[" + numi.value + "]";
                var partval = document.getElementById(itm).value;
                var preno = document.getElementById('rowno').value;
                var num = (document.getElementById("rowno").value - 1) + 2;

                numi.value = num;
                if ((partval != "") || ($("#addr1_" + numi.value + ":visible").length == 0)) {

                    var r = '<tr id="addr1_' + num + '"><td width="26%"><select name="bin_no[' + num + ']" id="bin_no[' + num + ']" class="form-control required selectpicker show-tick" required onchange="checkDuplicatePart(this.value, ' + num + ')" style="display:block !important"><option value="">--Select Part--</option><?php
                                $res_pro = mysqli_query($link1, "select bin_id,bin_no from bin_master where status='1' order by bin_no")or die(mysqli_error($link1));
                                while ($row_pro = mysqli_fetch_assoc($res_pro)) {
									if($bin_no !=$row_pro['bin_no']){
                                    ?><option value="<?= $row_pro['bin_no'] ?>"><?= $row_pro['bin_no']; ?></option><?php }} ?></select><div style="display:inline-block;float:left;"><i class="fa fa-close fa-lg" onClick="fun_remove(' + num + ');"></i></div></td><td width="10%"><input type="text" name="qty[' + num + ']" class="form-control number required" id="qty[' + num + ']" required onkeyup="getBinqty(this.value)" style="width: 30%;text-align: right;margin-left: 160px;/><p id="bin_msg[' + num + ']" style="color:red;text-align: center;"></p></td></td></tr>';
                    $('#itemsTable1').append(r);
                    makedropdown();
                }
            });
        });
        function fun_remove(con) {
            var c = document.getElementById('addr1_' + con);
            c.parentNode.removeChild(c);
            con--;
            document.getElementById('rowno').value = con;
        }
        function makedropdown() {
            $('.selectpicker').selectpicker({
                liveSearch: true
            });
        }
        ///// function for checking duplicate partcode
        function checkDuplicatePart(val, fldIndx1) {
            if (val != '') {
                var check2 = "bin_no[" + fldIndx1 + "]";
                var flag = 1;
                for (var i = 0; i <= fldIndx1; i++) {
                    var check1 = "bin_no[" + i + "]";
                    if (fldIndx1 != i && document.getElementById(check2).value != '' && document.getElementById(check1).value != '') {
                        if ((document.getElementById(check2).value == document.getElementById(check1).value)) {
                            document.getElementById("error_msg").innerHTML = "Duplicate bin selection";
                            document.getElementById(check2).value = "";
                            flag *= 0;
                        } else {
                            document.getElementById("error_msg").innerHTML = "";
                            flag *= 1;
                            ///do nothing
                        }
                    }
                }//// close for loop
                if (flag == 0) {
                    return false;
                } else {
                    return true;
                }
            }
        }
        ////////////////// Calculate Tax/////////////////////
        function checkAllRows() {
            var rowno1 = document.getElementById("rowno").value;
            var flag = 1;
            if (rowno1 > 0) {
                for (var j = 0; j < rowno1; j++) {
                    var check2 = "bin_no[" + j + "]";
                    for (var i = 0; i <= rowno1; i++) {
                        if (j != i) {
                            var check1 = "bin_no[" + i + "]";
                            var rqty = "qty[" + i + "]";
                            if (document.getElementById(check2).value == document.getElementById(check1).value) {
                                document.getElementById("error_msg").innerHTML = "Duplicate bin selection";
                                document.getElementById(check1).value = "";
                                document.getElementById(rqty).value = '';
                                flag *= 0;
                            } else {

                                flag *= 1;
                            }
                        }
                    }
                }
                if (flag == 0) {
                    return false;
                } else {
                    return true;
                }
            }
        }
        //// get bin qty
        function getBinqty(val) {
            var rowno1 = document.getElementById("rowno").value;
            var bin_qty = document.getElementById("bin_qty").value;
            var sum = 0;
            for (var i = 0; i <= rowno1; i++) {
                var bqty = "qty[" + i + "]";
                var bin_msg1 = "bin_msg[" + i + "]";
                var qty1 = document.getElementById(bqty).value;
                sum = parseFloat(sum) + parseFloat(qty1);
                if (sum > bin_qty) {
                    document.getElementById(bin_msg1).innerHTML = "QTY less than bin qty";
                    $("#save").attr('disabled',true);
                } else {
                    document.getElementById(bin_msg1).innerHTML = "";
                     $("#save").attr('disabled',false);
                }
            }

        }
    </script>