<?php // echo"<pre>";die(print_r(smc_session())); ?>
<style>
    .inputIcon{
        width:30px;}
    </style>
    <div class="modal fade stick-up " id="assignProductLocation" tabindex="-1" role="dialog" aria-hidden="true"style="width:80%;margin:0 auto">
    <div class="modal-dialog ">
        <div class="modal-content">
            <div class="modal-header clearfix text-left">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-14"></i></button>
                <h5><span class="semi-bold"><?php print CommonLanguage::getMessage('Assign_Product_Location'); ?></span></h5>
            </div>
            <div class="modal-body" id="AddForm">
                <div id="LocationValidationAlert" class="WizardValidationAlert alert"></div>
                <form id="AssignProductLocationPostForm" method="POST" onsubmit="return false">
                    <div class="row">
                        <div class="col-md-12" style="margin:15px;">
                            <div class="col-md-10">
                                <div><label><?php print CommonLanguage::getMessage('Product'); ?></label>
                                    <span class="fa fa-plus-circle m-l-5 openFrame" data-src="Inventory/listproduct?is_pop=1" style="color:#8a7dbe;cursor:pointer"></span>
                                </div>

                                <div class="input-group m-b Product_Code"><span class="input-group-btn">
                                        <button type="button" class="btn btn-bg-complete-darker"><i class="fa fa-certificate"></i> </button> </span>
                                    <input class=" full-width Product_Code" id="Product_Code" name="Product_Code" type="hidden" />
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12" style="margin:10px;">
                            <div class="col-md-10">
                                <div><label><?php print CommonLanguage::getMessage('Depot'); ?></label>
                                    <span class="fa fa-plus-circle m-l-5 openFrame" data-src="Stock/ListDepot?is_pop=1" style="color:#8a7dbe;cursor:pointer"></span>
                                </div>
                                <div class="input-group m-b Product_Depot_Loc"><span class="input-group-btn">
                                        <button type="button" class="btn btn-bg-complete-darker"><i class="fa fa-home"></i> </button> </span>

                                    <input class=" full-width Product_Depot_Loc" id="Product_Depot_Loc" name="Product_Depot_Loc" type="hidden" value="<?php echo smc_session()['default_depot_id'] ?>" data-select-name='<?= json_encode(smc_session()['default_depot_name']) ?>'/>

                                </div>

                            </div>
                        </div>

                        <div class="col-md-12">
                            <div class="col-md-3"><label><?php print CommonLanguage::getMessage('Quantity'); ?></label></div>
                            <div class="col-md-3"><label><?php print CommonLanguage::getMessage('UOM'); ?></label></div>
                            <div class="col-md-3"><label><?php print CommonLanguage::getMessage('Cost'); ?></label></div>
                            <div class="col-md-3"><label><?php print CommonLanguage::getMessage('Currency'); ?></label></div>
                            <div><label></label></div>
                        </div>
                        <div class="col-md-12 AddRow" style="margin:10px;" >
                            <div class="col-md-2">
                                <input type="text" class="form-control Product_Quantity_Val digits" >
                            </div>
                            <div class="col-md-3">
                                <select required name="UOM_ID"  class="full-width UOM_ID " data-init-plugin="select2">
                                    <?php foreach ($all_uom as $uom): ?>
                                        <option value="<?php echo $uom['UOM_ID']; ?>"><?php echo $uom['UOM_Name']; ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-2">
                                <input type="text"  class="form-control Product_Product_Cost digits" >
                            </div>

                            <div class="col-md-3">
                                <?php echo Forms::CurrencyDropDown(1, 'bg-complete-darker'); ?>

                            </div>
                            <div class="col-md-1 ExpiryDate">
                                <span class="fa fa-plus-circle" style="color:#4c0870;margin-top:15px;margin-right:25px"></span>
                                <span class="fa fa-minus-circle" style="color:#ed9421;margin-top:15px;display:none"></span>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <div class="pull-right">
                            <button class="btn btn-default" id="CloseProductLocation" data-dismiss="modal"  > <?php print CommonLanguage::getMessage('Close'); ?> </button>
                            <button class="btn btn-bg-complete-darker " id="SubmitProductLocation"  > <?php print CommonLanguage::getMessage('Save'); ?>   </button>
                        </div>
                    </div>
                </form>
            </div>

        </div>
    </div>
</div>

<script type="text/javascript">
    var Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>,
            Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>;
    //	var sum = 0,TotalQuantity=0;
    var TotalQty = 0;
    var default_depot_id = <?php echo json_encode(smc_session()['default_depot_id']);?>,
        default_depot_name = <?php echo json_encode(smc_session()['default_depot_name']);?>;
    $(function () {
        build_select2($("#Product_Code"), ("xhr/ProductSearch"))
        build_select2($("#Product_Depot_Loc"), ("xhr/depotSearch"))
        $("#AddForm").on("click", ".ExpiryDate > .fa-plus-circle", function (e) {
            $(".ExpiryDate > .fa-minus-circle").css("display", "")
            var Parent = $(this).parents(".AddRow"), NewItem = Parent.clone().insertAfter(Parent);
            NewItem.find(".Product_Quantity_Val,.Product_Product_Cost").val('');
            $(".select2-container", NewItem).empty().remove()
            $(".UOM_ID", NewItem).show().select2()
            //            NewItem.find(".Product_Expiry_Date").datepicker({autoclose:true});
        })
        $("#AddForm").on("click", ".ExpiryDate > .fa-minus-circle", function (e) {
            $(this).closest(".AddRow").empty().remove();
            $("#AddForm").each(function () {
                $(".ExpiryDate > .fa-minus-circle", $(this).parent()).css({"display": $(".ExpiryDate", $(this).parent()).length <= 1 ? "none" : "inline-block"})
            })
        })

        $("#Product_Code").on("change", function () {
            var Type = $(this).data("TypeListTarget"),
                    Catolder = $("#s2id_" + $(this).attr("id"));
            $("#s2id_Product_Code").append("<div class='LoadingCities' style='position:absolute;left:0;top:0;width:100%;height:100%;background:rgba(180,180,180,0.5);text-align:center'><img style='height:25px;margin-top:5px;' src='img/demo/progress.svg' /></div>")
            Catolder.append("<div class='LoadingCities' style='position:absolute;left:0;top:0;width:100%;height:100%;background:rgba(180,180,180,0.5);text-align:center'></div>")
            $.ajax({
                url: "StockAjax/getProductUOM",
                dataType: "json",
                data: {pid: $(this).val()},
                complete: function (v, vv) {
                    if (vv == "success") {
                        //console.log($(".AddRow"))
                        var ToBeAdded = "";
                        for (var x = 0; x < v.responseJSON.length; x++) {
                            ToBeAdded += "<option value='" + v.responseJSON[x]["uom_from"] + "'>" + v.responseJSON[x]["from_name"] + "</option>"
                        }
                        $(".AddRow").each(function () {
                            $(this).find("select[name=UOM_ID]").select2("destroy").empty()
                            $(this).find("select[name=UOM_ID]").html(ToBeAdded).select2();

                        })
                        $(">.LoadingCities", Catolder).remove()
                    }
                }
            })
        }).data("MyModuleListTarget", "#Module_ID").trigger("change")

        //        $(".Product_Expiry_Date").datepicker({autoclose:true})
        $("#SubmitProductLocation").on("click", function () {
            $("#AssignProductLocationPostForm").validate()
            if ($("#AssignProductLocationPostForm").valid()) {

                $("#LocationIsBusy").remove();
                $("#assignProductLocation").append('<div id="LocationIsBusy" style="position:absolute;left:0;right:0;top:0px;height:100%;background:rgba(255,255,255,0.9);z-index:9999;opacity:0"><img class="image-responsive-height demo-mw-50" src="img/demo/progress.svg" alt="Progress" style="position: absolute;left: 50%;top: 50%;margin: -25px 0 0 -25px;" /></div>');
                $("#LocationIsBusy").animate({opacity: 1});
                var Data = {
                    Product_Code: $("#Product_Code").val(),
                    Product_Depot_Loc: $("#Product_Depot_Loc").val(),
                    Product_ID: $("#Product_Code").val(),
                    Category_ID: 0,
                    Depot_ID: $("#Product_Depot_Loc").val(),
                    Transaction_Type_ID: 10,
                    Products: []
                }
                $(".AddRow").each(function () {
                    var me = $(this);
                    Data.Products.push({
                        Item_Quantity: me.find('.Product_Quantity_Val').val(),
                        Item_Cost: me.find(".Product_Product_Cost").val(),
                        UOM_ID: me.find("select.UOM_ID").val(),
                        Currency_ID: me.find(".DropDownForForms button input").val()
                    })
                })
                $.ajax({
                    url: "StockAjax/assignProductLocationAjax",
                    method: "POST",
                    data: Data,
                    complete: function (v1, v2) {
                        if (v2 == 'success') {
                            $("#LocationIsBusy").remove();
                            RebuildDataTable(title, [0, 1, 2], 'StockAjax/ProductLocationListRequest', 1);

                            $("#LocationValidationAlert").attr("class", "alert alert-success").empty().html("<span>" + Submitted_Successfully + "</span>");

                            setTimeout(function () {
                                $("#assignProductLocation").modal('hide')
                                $("#LocationValidationAlert").hide()
                            }, 1500)
                        } else {
                            $("#LocationValidationAlert").attr("class", "alert alert-danger").empty().html("<span>" + Failed_Submit + "</span>");
                        }
                        $("#LocationIsBusy").remove();
                    }
                })
            } else {
                $("#LocationValidationAlert").attr("class", "alert alert-danger").empty().html("<span>" + Failed_Submit + "</span>");
            }
        });
        $("#assignProductLocation").on("shown.bs.modal", function () {
//        alert()
            $("#assignProductLocation input:not([name=Currency_ID])").val('');
//            $("#assignProductLocation").modal("hide");
            $("#Product_Depot_Loc").val(default_depot_id)
            $("#Product_Depot_Loc").attr("data-select-name",default_depot_name)
            build_select2($("#Product_Depot_Loc"), ("xhr/depotSearch"))
        })
//        $("#assignProductLocation input").val('');
    });
</script>
