<style>
    .nav-tabs-fillup>li>a:after {
        background: none repeat scroll 0 0 #ed9421 !important;
        border: 1px solid #ed9421 !important;
    }
    .AddRow{
        background: #e5e5e5;
        padding: 5px !important;
        border-radius: 6px;
        margin-bottom: 10px;
    }
    .wrong{
        border: 1px solid rgba(255, 0, 0, 0.2) !important;
        background: rgba(255, 0, 0, 0.05) !important;
        /*        width:100%;*/
    }
    .tag{
        background-color: #4c0870 !important;
        color:white;
    }
    .prod_width{
        width:600px !important;
    }
</style>
<div class="modal fade stick-up " id="add_production_template"  data-backdrop="static" tabindex="-1" role="dialog" aria-hidden="true">
    <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-20"></i>
                </button>
                <h5 class="add-label h-labels bold hidden"><?php echo CommonLanguage::getMessage('add_production_template'); ?></h5>
                <h5 class="edit-label h-labels bold hidden"><?php echo CommonLanguage::getMessage('edit_production_template'); ?></h5>
                <div id="TemplateValidationAlert"></div>
            </div>
            <div class="modal-body">
                <form id="AddProductPostForm" method="POST" onsubmit="return false">
                    <div class="panel panel-transparent ">
                        <ul class="nav nav-tabs nav-tabs-fillup">
                            <li class="active">
                                <a data-toggle="tab" href="#slide1"><span><?php print CommonLanguage::getMessage('production_template'); ?></span></a>
                            </li>
                            <li>
                                <a data-toggle="tab" href="#slide2"><span><?php print CommonLanguage::getMessage('template_expenses'); ?></span></a>
                            </li>
                        </ul>
                        <div class="tab-content">
                            <div class="tab-pane slide-left active form-group" id="slide1">
                                <div class="row">
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('Name'); ?></label><span class="red-star">*</span>
                                        <input type="text" placeholder="<?php print CommonLanguage::getMessage('Name'); ?>" id="template_name" name="template_name" class="form-control textinput"  required>
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('currency'); ?></label><span class="red-star">*</span>
                                        <div id="CurrencyIDContainer"  >
                                            <div class="form-group form-material floating ">
                                                <div class="input-group-btn DropDownForForms" id="CurrencyDropDown" data-symbol="<?php echo smc_session()['local_currency_symbol']; ?>">
                                                    <button style="color:white !important;" type="button" class="btn bg-success-dark text-white btn-block dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                                                        <span><strong><?php echo smc_session()['local_currency_symbol'] ?></strong></span>
                                                        <input type="hidden"  value="<?php echo smc_session()['local_currency'] ?>" class="form-control ignore" onkeyup="return false" onkeydown="return false" name="Currency_ID"/>
                                                    </button>
                                                    <ul class="dropdown-menu">
                                                        <?php echo $CurList ?>
                                                        <li class="divider"></li>
                                                        <li><a class="LoadMore">More..</a></li>
                                                    </ul>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php echo CommonLanguage::getMessage('Category'); ?><span class="fa fa-plus-circle m-l-5 openFrame" data-src="Inventory/GeneralSetting?is_pop=1&ID=15" style="color:#ed9421;cursor:pointer"></span></label>
                                        <input type="hidden" name="production_category_id" id="production_category_id" class="full-width"/>
                                    </div>

                                    <div class="col-md-3 m-t-20">
                                        <input class="" id="is_line_required"   type="checkbox"/>
                                        <label for="is_line_required"><?php print CommonLanguage::getMessage('is_line_required'); ?>
                                        </label>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-3">
                                        <label><?php echo CommonLanguage::getMessage('production_emp_type'); ?></label>
                                        <input type="hidden" name="production_emp_type_ids" id="production_emp_type_ids" class="full-width"/>
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('out_products_template'); ?></label>
                                        <input type="hidden"  id="out_product_ids" name="out_product_ids" class="full-width"  >
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('in_products_template'); ?></label>
                                        <input type="hidden"  id="in_product_ids" name="in_product_ids" class="full-width"  >
                                    </div>
                                    <div class="col-md-3">
                                        <label><?php print CommonLanguage::getMessage('groups'); ?></label>

                                        <input size="1" type="hidden" class="groups_tag" id="groups_tag">
                                    </div>

                                </div>
                                <h3><?php echo CommonLanguage::getMessage('production_of') ?></h3>
                                <div class="row">
                                    <div class="col-md-12"  id="out_items">
                                        <div class="col-md-12 AddRow m-b-10 form-group"  >
                                            <div class="col-md-11">
                                                <div class="col-xs-3 ">
                                                    <label><?php echo CommonLanguage::getMessage('Item') ?></label>
                                                    <input type="hidden" class=" full-width" name="out_product"/>
                                                </div>
                                                <div class="col-xs-1">
                                                    <label><?php echo CommonLanguage::getMessage('UOM') ?></label>
                                                    <input class="full-width" name="out_uom" type="hidden" />
                                                </div>
                                                <div class="col-xs-2">
                                                    <label><?php echo CommonLanguage::getMessage('Depot') ?></label>
                                                    <input class="full-width" name="out_depot" type="hidden" value="<?php echo smc_session()['default_depot_id'] ?>" data-select-name="<?php echo smc_session()['default_depot_name'] ?>" />
                                                </div>
                                                <div class="col-xs-1 <?php echo $is_adv ? '' : 'hidden' ?>">
                                                    <label><?php echo CommonLanguage::getMessage('value_type') ?></label>
                                                    <select class="out_value_type full-width" data-init-plugin="select2">
                                                        <option value="1"><?php echo CommonLanguage::getMessage('Value') ?></option>
                                                        <option value="2"><?php echo CommonLanguage::getMessage('formula') ?></option>
                                                    </select>
                                                </div>
                                                <div class="col-xs-1">
                                                    <label class="out_qty_label"><?php echo CommonLanguage::getMessage('qty') ?></label>
                                                    <input type="text" class="out_qty form-control"/>
                                                </div>
                                                <div class="col-xs-1  <?php echo $is_adv ? '' : 'hidden' ?>">
                                                    <label><?php echo CommonLanguage::getMessage('identifier') ?></label>
                                                    <input type="text" class="out_identifier form-control"/>
                                                </div>
                                                <div class="col-xs-1">
                                                    <label><?php echo CommonLanguage::getMessage('optional') ?></label>
                                                    <input type="checkbox" class="out_optional"/>
                                                </div>
                                                <div class="col-xs-2">
                                                    <label><?php echo CommonLanguage::getMessage('group') ?></label>
                                                    <select class="out_group full-width" data-init-plugin="select2">
                                                        <option value="" ><?php echo CommonLanguage::getMessage('Select') ?></option>
                                                    </select>
                                                </div>

                                            </div>
                                            <div class="col-md-1 AddButtons">
                                                <span class="fa fa-plus-circle" style="color:#ed9421;margin-top:15px;margin-right:25px"></span>
                                                <span class="fa fa-minus-circle" style="color:#ed9421;margin-top:15px;"></span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="row">
                                    <h3><?php echo CommonLanguage::getMessage('needed_items') ?></h3>
                                    <div class="col-md-12"  id="in_items">
                                        <div class="col-md-12 AddRow m-b-10 form-group"  >
                                            <div class="col-md-11">
                                                <div class="col-xs-3 ">
                                                    <label><?php echo CommonLanguage::getMessage('Item') ?></label>
                                                    <input type="hidden" class=" full-width" name="in_product"/>
                                                </div>
                                                <div class="col-xs-1">
                                                    <label><?php echo CommonLanguage::getMessage('UOM') ?></label>
                                                    <input class="full-width" name="in_uom" type="hidden" />
                                                </div>
                                                <div class="col-xs-2">
                                                    <label><?php echo CommonLanguage::getMessage('Depot') ?></label>
                                                    <input class="full-width" name="in_depot" type="hidden" value="<?php echo smc_session()['default_depot_id'] ?>" data-select-name="<?php echo smc_session()['default_depot_name'] ?>" />
                                                </div>
                                                <div class="col-xs-1 <?php echo $is_adv ? '' : 'hidden' ?>">
                                                    <label><?php echo CommonLanguage::getMessage('value_type') ?></label>
                                                    <select class="in_value_type full-width" data-init-plugin="select2">
                                                        <option value="1"><?php echo CommonLanguage::getMessage('Value') ?></option>
                                                        <option value="2"><?php echo CommonLanguage::getMessage('formula') ?></option>
                                                    </select>
                                                </div>
                                                <div class="col-xs-1">
                                                    <label class="in_qty_label"><?php echo CommonLanguage::getMessage('qty') ?></label>
                                                    <input type="text" class="in_qty form-control"/>
                                                </div>
                                                <div class="col-xs-1 <?php echo $is_adv ? '' : 'hidden' ?>">
                                                    <label><?php echo CommonLanguage::getMessage('identifier') ?></label>
                                                    <input type="text" class="in_identifier form-control"/>
                                                </div>
                                                <div class="col-xs-1">
                                                    <label><?php echo CommonLanguage::getMessage('optional') ?></label>
                                                    <input type="checkbox" class="in_optional"/>
                                                </div>
                                                <div class="col-xs-2">
                                                    <label><?php echo CommonLanguage::getMessage('group') ?></label>
                                                    <select class="in_group full-width" data-init-plugin="select2">
                                                        <option value="" ><?php echo CommonLanguage::getMessage('Select') ?></option>

                                                    </select>
                                                </div>
                                            </div>
                                            <div class="col-md-1 AddButtons">
                                                <span class="fa fa-plus-circle" style="color:#ed9421;margin-top:15px;margin-right:25px"></span>
                                                <span class="fa fa-minus-circle" style="color:#ed9421;margin-top:15px;"></span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="tab-pane slide-left " id="slide2">
                                <div class="col-md-12"  id="template_expenses">
                                    <div class="col-md-12 AddRow m-b-10 form-group"  >
                                        <div class="col-md-11">
                                            <div class="col-xs-5 ">
                                                <label><?php echo CommonLanguage::getMessage('account_name') ?></label>
                                                <input type="hidden" class=" full-width" name="account_id"/>
                                            </div>
                                            <div class="col-xs-3">
                                                <label><?php echo CommonLanguage::getMessage('cost') ?></label>
                                                <input type="text" class="expense_cost form-control"/>
                                            </div>
                                            <div class="col-xs-3">
                                                <div class="checkbox check-primary  ">
                                                    <input type="checkbox" class="is_total" id="is_total1" />
                                                    <label class="total_label" for="is_total1"><?php echo CommonLanguage::getMessage('total_items') ?></label>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col-md-1 AddButtons">
                                            <span class="fa fa-plus-circle" style="color:#ed9421;margin-top:15px;margin-right:25px"></span>
                                            <span class="fa fa-minus-circle" style="color:#ed9421;margin-top:15px;"></span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal"   >  <?php print CommonLanguage::getMessage('Close'); ?></button>
                <button style="color:white !important;" class="btn bg-success-dark text-white" id="submit_pro"  >   <?php print CommonLanguage::getMessage('Save'); ?> </button>
            </div>
            <?php echo Forms::ModalLoader('TemplateIsBusy') ?>
        </div>
    </div>
</div>
<script type="text/javascript">
    var Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>,
            Required_Fields = <?php echo json_encode(CommonLanguage::getMessage('Required_Fields')); ?>,
            default_depot_id = <?php echo json_encode(smc_session()['default_depot_id']); ?>,
            default_depot_name = <?php echo json_encode(smc_session()['default_depot_name']); ?>,
            Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>;
    $(function () {
        var row_index = 1;
        build_select2($("input[name=in_product],input[name=out_product]"), ("xhr/ProductSearch?Status_ID=1"), 0, 'prod_width')
        build_select2($("input[name=out_uom]"), "xhr/productuomSearch?pid=" + $("input[name=out_product]").val())
        build_select2($("input[name=in_uom]"), "xhr/productuomSearch?pid=" + $("input[name=in_product]").val())
        build_select2($("input[name=out_depot],input[name=in_depot]"), "xhr/depotSearch")
        build_select2($("input[name=account_id]"), "xhr/allAccountsSearch")
        build_select2($("input[name=production_emp_type_ids]"), "xhr/commonSearch?col_id=type_id&col_name=type_name&table=tbl_production_emp_types", 1)
        build_select2($("input[name=production_category_id]"), "xhr/commonSearch?col_id=category_id&col_name=category_name&table=tbl_production_category")
        build_select2($("#out_product_ids,#in_product_ids"), "xhr/ProductSearch", 1)
        $("#groups_tag").tagsinput({
            tagClass: 'tag-color'
        });

        $('#groups_tag').on('itemAdded', function (event) {
            $("select.out_group,select.in_group").each(function () {
                $('<option value=' + event.item + '>' + event.item + '</option>').appendTo($(this))
            })
        });
        $('#groups_tag').on('itemRemoved', function (event) {
            $("select.out_group,select.in_group").each(function () {
                if ($(this).val() == event.item) {
                    console.log($(this))
                    $(this).val("").select2()
                }
            })
            $("select.out_group option[value=" + event.item + "],select.in_group option[value=" + event.item + "]").remove()

        });
        new Switchery($("#create_trans").get(0), {color: $.Pages.getColor("primary")})
        $("#out_items").on("click", ".AddButtons > .fa-plus-circle", function (e) {
            $(".AddButtons > .fa-minus-circle").css("display", "")
            var Parent = $(this).parents(".AddRow"), NewItem = Parent.clone().insertAfter(Parent);
            NewItem.find("input").val('');
            NewItem.find(".out_optional").prop("checked", false)
            $(".select2-container", NewItem).empty().remove()
            NewItem.find("input[name=out_uom],input[name=out_product]").val('').removeAttr("data-select-name");
            build_select2(NewItem.find("input[name=out_product]"), ("xhr/ProductSearch?Status_ID=1"), 0, 'prod_width')
            NewItem.find("input[name=out_depot]").val(default_depot_id).attr("data-select-name", default_depot_name)
            build_select2(NewItem.find("input[name=out_depot]"), "xhr/depotSearch")
            build_select2(NewItem.find("input[name=out_uom]"), "xhr/productuomSearch?pid=" + NewItem.find("input[name=out_product]").val())
            $("select.out_value_type", NewItem).val(1).select2()
            $("select.out_group", NewItem).val("").select2()
            $(".out_qty_label", NewItem).text('<?php echo CommonLanguage::getMessage('qty') ?>')
            $(".select2-container", NewItem).show()
        })
        $("#in_items").on("change", ".in_value_type", function () {
            if ($(this).val() == 1)
                $(this).parents('.AddRow').find('.in_qty_label').text('<?php echo CommonLanguage::getMessage('qty') ?>')
            else if ($(this).val() == 2)
                $(this).parents('.AddRow').find('.in_qty_label').text('<?php echo CommonLanguage::getMessage('formula') ?>')
        })
        $("#out_items").on("change", ".out_value_type", function () {
            if ($(this).val() == 1)
                $(this).parents('.AddRow').find('.out_qty_label').text('<?php echo CommonLanguage::getMessage('qty') ?>')
            else if ($(this).val() == 2)
                $(this).parents('.AddRow').find('.out_qty_label').text('<?php echo CommonLanguage::getMessage('formula') ?>')
        })

        $("#out_items").on("change", "input[name=out_product]", function (e) {
            //            var out_uom = $(this).parents(".AddRow").find("input[name=out_uom]");
            //            out_uom.val(0);
            //            out_uom.attr("data-select-name", "");
            //            build_select2(out_uom, "xhr/productuomSearch?pid=" + $(this).val())

            var out_uom = $(this).parents(".AddRow").find("input[name=out_uom]"),
                    pid = $(this).val();
            $.ajax({
                url: "InventoryAjax/get_default_uom",
                dataType: 'json',
                data: {pid: pid, type: 'pur'},
                complete: function (v1, v2) {
                    if (v2 == 'success') {
                        out_uom.val(v1.responseJSON['UOM_ID']);
                        out_uom.attr("data-select-name", v1.responseJSON['UOM_Name']);
                        build_select2(out_uom, "xhr/productuomSearch?pid=" + pid)
                    }
                }
            })
        })
        $("#out_items").on("click", ".AddButtons > .fa-minus-circle", function (e) {
            if ($("#out_items .AddRow").length == 1) {
                $("#out_items .AddRow input").val("")
                $("input[name=out_uom],input[name=out_product]").val('').removeAttr("data-select-name", "");
                build_select2($("input[name=out_product]"), ("xhr/ProductSearch?Status_ID=1"), 0, 'prod_width')
                build_select2($("input[name=out_uom]"), "xhr/productuomSearch?pid=" + $("input[name=out_product]").val())
            } else {
                $(this).closest("#out_items .AddRow").empty().remove();
            }
        })
        $("#in_items").on("click", ".AddButtons > .fa-plus-circle", function (e) {
            $(".AddButtons > .fa-minus-circle").css("display", "")
            var Parent = $(this).parents(".AddRow"), NewItem = Parent.clone().insertAfter(Parent);
            NewItem.find("input").val('');
            $(".select2-container", NewItem).empty().remove()
            NewItem.find(".in_optional").prop("checked", false)
            NewItem.find("input[name=in_uom],input[name=in_product]").val('').removeAttr("data-select-name");
            build_select2(NewItem.find("input[name=in_product]"), ("xhr/ProductSearch?Status_ID=1"), 0, 'prod_width')
            NewItem.find("input[name=in_depot]").val(default_depot_id).attr("data-select-name", default_depot_name)
            build_select2(NewItem.find("input[name=in_depot]"), "xhr/depotSearch")
            build_select2(NewItem.find("input[name=in_uom]"), "xhr/productuomSearch?pid=" + NewItem.find("input[name=in_product]").val())
            $("select.in_value_type", NewItem).val(1).select2()
            $("select.in_group", NewItem).val("").select2()

            $(".in_qty_label", NewItem).text('<?php echo CommonLanguage::getMessage('qty') ?>')
            $(".select2-container", NewItem).show()
        })
        $("#in_items").on("change", "input[name=in_product]", function (e) {
            var in_uom = $(this).parents(".AddRow").find("input[name=in_uom]"),
                    pid = $(this).val();
            $.ajax({
                url: "InventoryAjax/get_default_uom",
                dataType: 'json',
                data: {pid: pid, type: 'inv'},
                complete: function (v1, v2) {
                    if (v2 == 'success') {
                        in_uom.val(v1.responseJSON['UOM_ID']);
                        in_uom.attr("data-select-name", v1.responseJSON['UOM_Name']);
                        build_select2(in_uom, "xhr/productuomSearch?pid=" + pid)
                    }
                }
            })
            //get_default_uom

        })
        $("#in_items").on("click", ".AddButtons > .fa-minus-circle", function (e) {
            if ($("#in_items .AddRow").length == 1) {
                $("#in_items .AddRow input").val("")
                $("input[name=in_uom],input[name=in_product]").val('').removeAttr("data-select-name", "");
                build_select2($("input[name=in_product]"), ("xhr/ProductSearch?Status_ID=1"), 0, 'prod_width')
                build_select2($("input[name=in_uom]"), "xhr/productuomSearch?pid=" + $("input[name=in_product]").val())
            } else {
                $(this).closest("#in_items .AddRow").empty().remove();
            }
        })
        $("#template_expenses").on("click", ".AddButtons > .fa-plus-circle", function (e) {
            $(".AddButtons > .fa-minus-circle").css("display", "")
            var Parent = $(this).parents(".AddRow"), NewItem = Parent.clone().insertAfter(Parent);
            NewItem.find("input").val('');
            row_index = row_index + 1;
            NewItem.find(".is_total").attr("id", "is_total" + row_index);
            NewItem.find(".total_label").attr("for", "is_total" + row_index);
            $(".select2-container", NewItem).empty().remove()
            NewItem.find("input[name=account_id]").val('').removeAttr("data-select-name");
            build_select2(NewItem.find("input[name=account_id]"), ("xhr/allAccountsSearch"))
            $(".select2-container", NewItem).show()
        })
        $("#template_expenses").on("click", ".AddButtons > .fa-minus-circle", function (e) {
            if ($("#template_expenses .AddRow").length == 1) {
                $("#template_expenses .AddRow input").val("")
                $("input[name=account_id]").val('').removeAttr("data-select-name", "");
                build_select2($("input[name=account_id]"), ("xhr/allAccountsSearch"))
            } else {
                $(this).closest("#template_expenses .AddRow").empty().remove();
            }
        })
        $("#submit_pro").on("click", function () {
            $(".in_qty,.out_qty,.out_identifier,.in_identifier").removeClass("wrong")
            if ($("#template_name").val() == '') {
                $("#TemplateValidationAlert").attr("class", "alert alert-danger").show().html(Required_Fields)
                $("#add_production_template").scrollTop(0)
                return false;
            }
            $("#TemplateIsBusy").removeClass('hidden');
            var Data = {
                template_name: $("#template_name").val(),
                Currency_ID: $("#CurrencyDropDown button input").val(),
                create_trans: $("#create_trans").is(":checked") ? 1 : 0,
                out_product_ids: $("#out_product_ids").val(),
                in_product_ids: $("#in_product_ids").val(),
                production_emp_type_ids: $("#production_emp_type_ids").val(),
                production_category_id: $("#production_category_id").val(),
                template_type: is_adv,
                expense_arr: [],
                items: []
            }
            var identifier_validate = true,
                    pattern = /^[a-zA-Z]$/,
                    number_pattern = /^\d+(\.\d+)?$/;
            $("#out_items .AddRow").each(function () {
                var identifier = $(this).find("input.out_identifier").val(),
                        type = $(this).find("select.out_value_type").val(),
                        qty = $(this).find(".out_qty").val();
                if (qty != '' && (type == 2 || number_pattern.test(qty))
                        && (identifier == '' || pattern.test(identifier))) {
                    Data.items.push({
                        item_id: $(this).find("input[name=out_product]").val(),
                        item_uom: $(this).find("input[name=out_uom]").val(),
                        item_qty: qty,
                        item_depot: $(this).find("input[name=out_depot]").val(),
                        group: $(this).find("select.out_group").val(),
                        value_type: type,
                        optional: $(this).find("input.out_optional").is(":checked") ? 1 : 0,
                        identifier: identifier,
                        operation: '+'
                    })
                } else {
                    if ($(this).find(".out_qty").val() == '' || (type == 1 && !number_pattern.test(qty)))
                        $(this).find("input.out_qty").addClass("wrong")
                    else
                        $(this).find("input.out_identifier").addClass("wrong")
                    identifier_validate = false;

                }
            })


            $("#in_items .AddRow").each(function () {

                var identifier = $(this).find("input.in_identifier").val(),
                        type = $(this).find("select.in_value_type").val(),
                        qty = $(this).find(".in_qty").val();
                if (qty != '' && (type == 2 || number_pattern.test(qty))
                        && (identifier == '' || pattern.test(identifier))) {
                    Data.items.push({
                        item_id: $(this).find("input[name=in_product]").val(),
                        item_uom: $(this).find("input[name=in_uom]").val(),
                        item_qty: qty,
                        item_cost: $(this).find(".in_cost").val(),
                        item_depot: $(this).find("input[name=in_depot]").val(),
                        group: $(this).find("select.in_group").val(),
                        value_type: type,
                        optional: $(this).find("input.in_optional").is(":checked") ? 1 : 0,
                        identifier: identifier,
                        operation: '-'
                    })
                } else {
                    if ($(this).find(".in_qty").val() == '' || (type == 1 && !number_pattern.test(qty)))
                        $(this).find(".in_qty").addClass("wrong")
                    else
                        $(this).find("input.in_identifier").addClass("wrong")
                    identifier_validate = false;
                }
            })
            $("#template_expenses .AddRow").each(function () {
                if ($(this).find(".expense_cost").val() != '') {
                    Data.expense_arr.push({
                        account_id: $(this).find("input[name=account_id]").val(),
                        expense_cost: $(this).find(".expense_cost").val(),
                        is_total: $(this).find(".is_total").is(":checked") ? 1 : 0
                    })
                }
            })
            if (IsEdit) {
                Data.template_id = IDToEdit;
            }
            if (!identifier_validate) {
                $("#TemplateIsBusy").addClass('hidden');
                $("#TemplateValidationAlert").attr("class", "alert alert-danger").empty().html(Required_Fields);
                $("#add_production_template").scrollTop(0)
                return;
            }

//            console.log(identifier_validate)
//            return
            Data.is_line_required = $('#is_line_required').is(":checked") ? 1 : 0;
            $.ajax({
                url: IsEdit ? "InventoryAjax/update_production_template" : "InventoryAjax/addProductionTemplate",
                method: "POST",
                data: Data,
                complete: function (v1, v2) {
                    if (v2 == 'success' && v1.responseText == 'OK') {
                        //                        $("#TemplateIsBusy").remove();
                        $("#TemplateValidationAlert").attr("class", "alert alert-success").empty().html(Submitted_Successfully);
                        setTimeout(function () {
                            $("#add_production_template").modal('hide')
                            $("#TemplateValidationAlert").addClass('hidden')
                            $("#in_items .AddRow:gt(0),#out_items .AddRow:gt(0),#template_expenses .AddRow:gt(0)").remove()
                            $("input.in_qty,input.out_qty,input.expense_cost").val('')
                            $("input[name=in_product],input[name=out_product],input[name=in_uom],input[name=out_uom],input[name=account_id]").val('').attr("data-select-name", "");
                            build_select2($("input[name=in_product],input[name=out_product]"), ("xhr/ProductSearch?Status_ID=1"), 0, 'prod_width')
                            build_select2($("input[name=out_uom]"), "xhr/productuomSearch?pid=" + $("input[name=out_product]").val())
                            build_select2($("input[name=in_uom]"), "xhr/productuomSearch?pid=" + $("input[name=in_product]").val())
                            build_select2($("input[name=account_id]"), "xhr/allAccountsSearch")
                            $("#slide2").removeClass("active")
                            $("#slide1").addClass("active")
                        }, 1500)
                        RebuildDataTable(title, [0], "InventoryAjax/production_template_list", {is_adv: is_adv})
                    } else {
                        $("#TemplateValidationAlert").attr("class", "alert alert-danger").empty().html(v1.responseText);
                    }
                    $("#add_production_template").scrollTop(0)
                    $("#TemplateIsBusy").addClass('hidden');
                }
            })
            //  }
            //                                              });
            //            $("#AddProductPostForm").validate().resetForm();
            $("#NewProductUploadImagePreview").attr("src", "img/profiles/product.png");
        })

        $("#add_production_template").on("hidden.bs.modal", function () {
            $("#groups_tag").tagsinput('removeAll')
        })
    })
</script>
