let gridManager = null;
let coRowNum=0;
//页面控制标识
let control = {
    isInitiator: false,//是否为审核人
    isDistribute: false,//是否已分发
    isConfirm:false,//是否已确认
}

$(document).ready(function () {
    initializate()
    loadRejectReasonBox();
    bindEvent();
})


function initializate() {
   // UICtrl.setDisable("#businessUsageDiv")
    UICtrl.setDisable("#totalContentDiv");
    coRowNum= $("#busiLens").val();
    $("#klart").val("023")
    $("#classify").val("Z_YCL")
    //页面控制
    UICtrl.enable($("#businessUsage"));
    UICtrl.enable($("#matklName"));
    //物品类型 控制存储的序列号参数
    $("#mtart").combox({onChange:function(data) {
         storeDisplaySer(data.value);
         getBklas(data.value);
      }
    });
    //价格控制  控制财务核算的 不参与成本核算
    $("#vprsv").combox({onChange:function(data) {
          financialDisplayNcost(data.value);
        }
    });
    //获取类型   控制财务核算的 不参与成本核算
    $("#beskz").combox({onChange:function(data) {
            financialDisplayNcost($("#vprsv").val());
        }
    });
    //特殊获取方式  价格控制  控制财务核算的 不参与成本核算
    $("#sobsl").combox({onChange:function(data) {
          financialDisplayNcost($("#vprsv").val());
       }
    });
    //仓库设施1类型 控制 仓库设施1存储数量和单位的必填
    $("#lety1").combox({onChange:function(data) {
           warehouseSet(data.value);
        }
    });
    //不参与成本核算  修改 根据价格控制决定
    $("#ncost").combox({onChange:function(data) {
            financialDisplayNcost($("#vprsv").val());
        }
    });
    switch ($("#active").val()) {
        case '0'://待填报
          //  $("#businessUsageDiv").hide();
            hideAllBusiness(false); //隐藏但不清理值
            for (var x=0;x<$("input[name='businessUsage']").length;x++) {
               if ($("input[name='businessUsage']")[x].checked) {
                  showBusiness($("input[name='businessUsage']")[x].value);
               }
            }
            if ($("#deptCode").val() === "system") {
                $("#auditForm").show();
            } else {
                $("#buttonBox").show();
                $("#showChart").remove();
            }
            if (applyType==2){ //扩展
                $("#isAll").attr("disabled",true);
            }
            break;
        case '1'://待确认
            $("#buttonBox").show();
            $("#submit").remove();
            $("#save").remove();
            $("#back").remove();
              viewOnly();
               break;
        case '2'://已办结
            // $("#auditForm").show();
            $("#buttonBox").remove();
             UICtrl.setDisable("#basicInfoDiv");
             UICtrl.setDisable("#distributeDiv");
             viewOnly();
            break;
      //  case '3'://已办结
          //  showRejectCauseBox();
       //     $("#buttonBox").remove();
       //     break;
        default://其他/详情
            $("#buttonBox").show();
            break;
    }

     if (isAll=="1"){
        $('input[name="isAll"]').attr("checked",true);
     }

    $('input[name="isAll"]').change(function () {
       // debugger
        if ($(this).is(':checked')) {
            $("#busiLens").val(dictUsage.length);
            coRowNum=dictUsage.length;
             for (var x=0;x<$("input[name='businessUsage']").length;x++) {
                $("input[name='businessUsage']")[x].checked = true;
             }
            showAllBusiness();
        } else {
            $("input[name='businessUsage']").each(function(){
                $(this).attr("checked",false);
                hideAllBusiness(true);
            })
        }
    });

    var dictUsage=dictUsages.split(";");
    $('input[name="businessUsage"]').change(function () {
       //alert($(this).is(':checked'));
        if ($(this).is(':checked')) {
             //let datas=gridManager.getData();
            // var name="",deptCode="";
            coRowNum=MathUtil.add(coRowNum,1);
            showBusiness($(this).val());

            if (coRowNum==dictUsage.length){
              $('input[name="isAll"]')[0].checked = true;
            }
        } else {
             hideBusiness($(this).val());
             coRowNum=MathUtil.sub(coRowNum,1);
             $('input[name="isAll"]').attr("checked",false);
        }
    });
}

function  showBusiness(busiCode) {
    if (busiCode=="V") {  //销售
        $("#saleDiv").show();
        saleMustInput(true);
    }
    if (busiCode=="E") { //采购
        $("#purchaseDiv").show();
        purchaseMustInput(true);
    }
    if (busiCode=="D")  { //MRP
        $("#mrpDiv").show();
        $("#dismm").combox({onChange:function(data) {
             if (data.value=='ND'){
                  mrpMustInput(false);
              } else {
                  mrpMustInput(true);
              }
            }
        });
        UICtrl.setElRequiredFlag("#dismm",true);
        UICtrl.setElRequiredFlag("#werksName",true);
        UICtrl.setElRequiredFlag("#mtvfp",true);
    }

    if (busiCode=="A")  { //生产
        $("#produceDiv").show();
        produceMustInput(true);
    }
    if (busiCode=="L")  {  //存储
        $("#storeDiv").show();
        storeMustInput(true);
    }
    if (busiCode=="S")   { // 仓库
        $("#warehouseDiv").show();
        warehouseMustInput(true);
    }
    if (busiCode=="B")  { //财务
        $("#financialDiv").show();
        financialMustInput(true);
    }
}

function  hideBusiness(busiCode) {
    if (busiCode=="E") {
        $("#purchaseDiv").hide();
        purchaseMustInput(false);
        clearForm_purchase();
    }
    if (busiCode=="V") {
        $("#saleDiv").hide();
        saleMustInput(false);
        clearForm_sale();
    }
    if (busiCode=="D"){
        $("#mrpDiv").hide();
        mrpMustInput(false);
        UICtrl.setElRequiredFlag("#dismm",false);
        clearForm_mrp();
    }
    if (busiCode=="A") {
        $("#produceDiv").hide();
        produceMustInput(false);
        clearForm_produce();
    }
    if (busiCode=="L") {
        $("#storeDiv").hide();
        storeMustInput(false);
        clearForm_store();
    }
    if (busiCode=="S") {
        $("#warehouseDiv").hide();
        warehouseMustInput(false);
        clearForm_warehouse();
    }
    if (busiCode=="B") {
        $("#financialDiv").hide();
        financialMustInput(false);
        clearForm_financial();
    }
}
function  showAllBusiness() {
    $("#saleDiv").show();
    $("#purchaseDiv").show();
    $("#mrpDiv").show();
    $("#produceDiv").show();
    $("#storeDiv").show();
    $("#warehouseDiv").show();
    $("#financialDiv").show();
    //设置必输项
    saleMustInput(true);
    purchaseMustInput(true);
    produceMustInput(true);
    UICtrl.setElRequiredFlag("#dismm",true); //MRP类型
    storeMustInput(true);
    warehouseMustInput(true);
    financialMustInput(true);
    werksMustInput();
    lgortMustInput();
    mtvfpMustInput();
}

function  hideAllBusiness(flag) {
    $("#saleDiv").hide();
    $("#purchaseDiv").hide();
    $("#mrpDiv").hide();
    $("#produceDiv").hide();
    $("#storeDiv").hide();
    $("#warehouseDiv").hide();
    $("#financialDiv").hide();
    werksMustInput(); //可以不需要的？
    lgortMustInput();
    mtvfpMustInput();

    saleMustInput(false);
    purchaseMustInput(false);
    produceMustInput(false);
    UICtrl.setElRequiredFlag("#dismm",false); //MRP类型
    storeMustInput(false);
    warehouseMustInput(false);
    financialMustInput(false);

    if (flag) {
        clearForm_sale();
        clearForm_produce();
        clearForm_purchase();
        clearForm_mrp();
        clearForm_store();
        clearForm_warehouse();
        clearForm_financial();
    }
}

function  showBusinessReadOnly(busiCode) {
  if (busiCode=="V") {
      $("#saleDiv").show();
      UICtrl.setDisable("#saleDiv");
   }
  if (busiCode=="E") {
      $("#purchaseDiv").show();
      UICtrl.setDisable("#purchaseDiv");
  }
  if (busiCode=="D")  {
     $("#mrpDiv").show();
      UICtrl.setDisable("#mrpDiv");
  }
  if (busiCode=="A")  {
     $("#produceDiv").show();
      UICtrl.setDisable("#produceDiv");
 }
  if (busiCode=="L")  {
     $("#storeDiv").show();
      UICtrl.setDisable("#storeDiv");
  }
  if (busiCode=="S")   {
     $("#warehouseDiv").show();
      UICtrl.setDisable("#warehouseDiv");
  }
  if (busiCode=="B")  {
     $("#financialDiv").show();
     UICtrl.setDisable("#financialDiv");
  }
}
function viewOnly(){ //只能查看
      hideAllBusiness();
    for (var x=0;x<$("input[name='businessUsage']").length;x++) {
        if ($("input[name='businessUsage']")[x].checked) {
          showBusinessReadOnly($("input[name='businessUsage']")[x].value);
        }
    }
    UICtrl.setDisable("#basicInfoDiv");
    UICtrl.setDisable("#distributeDiv");
    $("#maktxRemZh").attr("readonly",true);
    $("#maktxRemEn").attr("readonly",true);
    //UICtrl.setDisable($("#businessUsageV"));
    // UICtrl.setDisable($("#isAll"));
    $("#businessUsageV").attr("disabled",true);
    $("#businessUsageE").attr("disabled",true);
    $("#businessUsageD").attr("disabled",true);
    $("#businessUsageA").attr("disabled",true);
    $("#businessUsageL").attr("disabled",true);
    $("#businessUsageS").attr("disabled",true);
    $("#businessUsageB").attr("disabled",true);
    $("#isAll").attr("disabled",true);
}

function  saleMustInput(flag) { //销售必输项
    UICtrl.setElRequiredFlag("#alandName",flag);
    UICtrl.setElRequiredFlag("#aland",flag);
    UICtrl.setElRequiredFlag("#taxkm",flag);
    UICtrl.setElRequiredFlag("#ktgrm",flag);
    UICtrl.setElRequiredFlag("#vkorgName",flag);
    UICtrl.setElRequiredFlag("#vtwegName",flag);
    if (!flag) {
        UICtrl.setElRequiredFlag("#umrenSale", flag);
        UICtrl.setElRequiredFlag("#umrezSale", flag);
    }
    mtvfpMustInput();
}
function purchaseMustInput(flag) { //采购必输项
    UICtrl.setElRequiredFlag("#ekgrpName",flag);
    UICtrl.setElRequiredFlag("#ekgrp",flag);
    if (!flag) {
        UICtrl.setElRequiredFlag("#umrenPurch", flag);
        UICtrl.setElRequiredFlag("#umrezPurch", flag);
    }
    werksMustInput();
}
function werksMustInput(){ //工厂必输
    var isMustInput="0";
    for (var x=0;x<$("input[name='businessUsage']").length;x++) {
       // alert($("input[name='businessUsage']")[x].value);
        if ($("input[name='businessUsage']")[x].checked) {
            // E 采购  L 存储 D MRP  A 生产
            if ($("input[name='businessUsage']")[x].value == "L" || $("input[name='businessUsage']")[x].value == "D"
                || $("input[name='businessUsage']")[x].value=="A"|| $("input[name='businessUsage']")[x].value=="E"){
                isMustInput="1";
                break;
            }
        }
    }
    if  (isMustInput=="1"){
        UICtrl.setElRequiredFlag("#werksName",true);
    } else {
        UICtrl.setElRequiredFlag("#werksName",false);
    }
}

function lgortMustInput(){ //库存地点必输
    var isMustInput="0";
    for (var x=0;x<$("input[name='businessUsage']").length;x++) {
        // alert($("input[name='businessUsage']")[x].value);
        if ($("input[name='businessUsage']")[x].checked) {
            //  L 存储 S 仓库
            if ($("input[name='businessUsage']")[x].value == "L" || $("input[name='businessUsage']")[x].value == "S"
              ){
               isMustInput="1";
              break;
            }
        }
    }
    if  (isMustInput=="1"){
        UICtrl.setElRequiredFlag("#lgortName",true);
    } else {
        UICtrl.setElRequiredFlag("#lgortName",false);
    }
}

function mtvfpMustInput(){ //可用性检查必输
    var isMustInput="0";
    for (var x=0;x<$("input[name='businessUsage']").length;x++) {
        // alert($("input[name='businessUsage']")[x].value);
        if ($("input[name='businessUsage']")[x].checked) {
            //  V  销售  D MRP
            if ($("input[name='businessUsage']")[x].value == "V" || $("input[name='businessUsage']")[x].value == "D"
            ){
                isMustInput="1";
                break;
            }
        }
    }
    if  (isMustInput=="1"){
        UICtrl.setElRequiredFlag("#mtvfp",true);
    } else {
        UICtrl.setElRequiredFlag("#mtvfp",false);
    }
}

function  mrpMustInput(flag) { //MRP必输项
    UICtrl.setElRequiredFlag("#dispo",flag);
    UICtrl.setElRequiredFlag("#disls",flag);
    UICtrl.setElRequiredFlag("#beskz",flag);
    UICtrl.setElRequiredFlag("#mtvfp",flag);

    UICtrl.setElRequiredFlag("#rgekz",flag);
    UICtrl.setElRequiredFlag("#disgr",flag);
    UICtrl.setElRequiredFlag("#strgr",flag);
    UICtrl.setElRequiredFlag("#sbdkz",flag);
    UICtrl.setElRequiredFlag("#vrmod",flag);
    UICtrl.setElRequiredFlag("#vint1",flag);
    UICtrl.setElRequiredFlag("#vint2",flag);
    werksMustInput();
    mtvfpMustInput();
}
function produceMustInput(flag){
    UICtrl.setElRequiredFlag("#sfcpf",flag);
    UICtrl.setElRequiredFlag("#fevor",flag);
    if (!flag) {
        UICtrl.setElRequiredFlag("#umrenProd", flag);
        UICtrl.setElRequiredFlag("#umrezProd", flag);
    }
    werksMustInput();
}

function  storeMustInput(flag) {
    UICtrl.setElRequiredFlag("#prctr",flag);
    UICtrl.setElRequiredFlag("#insmk",flag);
    storeDisplaySer($('#mtart').val());
    werksMustInput();
    lgortMustInput();
}
function storeDisplaySer(mtart){
    if (mtart=='ZGMB'){
        $('#sernp').val('Z005');
        $('#sernp_text').val('刀工模版采购序列号参数');
        $('#serlv').val('1');
        $('#serlv_text').val('保持设备号和序列号同步');
        $("#sernp_text").attr("disabled", false);
        $("#serlv_text").attr("disabled", false);
        $("#sernp").attr("disabled", false);
        $("#serlv").attr("disabled", false);

        $("#sernp_text").next().find("button").attr("disabled",false);
        $("#serlv_text").next().find("button").attr("disabled",false);
    } else {
        $('#sernp').val('');
        $('#sernp_text').val('');
        $('#serlv').val('');
        $('#serlv_text').val('');
        $("#sernp").attr("disabled", true);
        $("#sernp_text").attr("disabled", true);
        $("#serlv").attr("disabled", true);
        $("#serlv_text").attr("disabled", true);

        $("#sernp_text").next().find("button").attr("disabled",true);
        $("#serlv_text").next().find("button").attr("disabled",true);
    }
}
function  warehouseMustInput(flag) {
    // if (flag){
    //     $('#lhme1').val($('#meins').val());  //仓库的仓库设施1存储单位 默认基本计量单位
    //     $('#lhme1Name').val( $('#meinsName').val());
    // }   24.08.28取消
    UICtrl.setElRequiredFlag("#ltkzaName",flag);
    UICtrl.setElRequiredFlag("#ltkzeName",flag);
    UICtrl.setElRequiredFlag("#lgortProfitName",flag);
   // UICtrl.setElRequiredFlag("#lhmg1",flag);
   // UICtrl.setElRequiredFlag("#lhme1Name",flag);
    UICtrl.setElRequiredFlag("#lgnumName",flag);
    UICtrl.setElRequiredFlag("#lgortName",flag);
    lgortMustInput();
    if (!flag){
        $('#lety1').val("");
        $('#lety1_text').val("");
    }
    warehouseSet($('#lety1').val());
}

function warehouseSet(val){
    if (val=="TP"){
        UICtrl.setElRequiredFlag("#lhme1Name",true);
        UICtrl.setElRequiredFlag("#lhmg1",true);
     } else {
        UICtrl.setElRequiredFlag("#lhme1Name",false);
        UICtrl.setElRequiredFlag("#lhmg1",false);
        $('#lhmg1').val("");
    }

}
function  financialMustInput(flag) {
    UICtrl.setElRequiredFlag("#bklas",flag);
    UICtrl.setElRequiredFlag("#vprsv",flag);
    UICtrl.setElRequiredFlag("#mlast",flag);
    UICtrl.setElRequiredFlag("#peinh",flag);
    UICtrl.setElRequiredFlag("#ncost",flag);
    UICtrl.setElRequiredFlag("#hkmat",flag);
    UICtrl.setElRequiredFlag("#ekalr",flag);
    var peinh = $("#peinh").val(); //定价基数
    if (Public.isBlank(peinh)){
        $("#peinh").val("1000");
    }
    financialDisplayNcost($("#vprsv").val());
    getBklas($('#mtart').val());
}

function financialDisplayNcost(vprsv) { // alert("-="+vprsv);
    if (vprsv == 'S') { //标准价格
        $('#ncost').val('0');
        $('#ncost_text').val('0-空');
        $('#mlast').val('3');
        $('#mlast_text').val('单/多级');
    } else  if (vprsv == 'V') { //移动平均价格
        if ($('#beskz').val() == 'F' && ($('#sobsl').val() == '40' || $('#sobsl').val() == '70')) { //外部采购，库存转储，从1010工厂领料
            $('#ncost').val('0');
            $('#ncost_text').val('0-空');
        } else {
            $('#ncost').val('X');
            $('#ncost_text').val('X-是');
        }
        $('#mlast').val('2');
        $('#mlast_text').val('基于交易的');
    }
}

function getBklas(mtart){ //获取评估分类
    if (mtart=='ZBCP'){
       $('#bklas').val("7900");
    } else
     if (mtart=='ZBPJ'||mtart=='ZBZP'||mtart=='ZCOM'||mtart=='ZECC'||mtart=='ZFBL'||mtart=='ZGMB'
      ||mtart=='ZXHP'||mtart=='ZXNJ') {
         $('#bklas').val("3001");
     } else  if (mtart=='ZKGL') {
         $('#bklas').val("3000");
     } else  if (mtart=='ZMYP') {
         $('#bklas').val("7930");
     } else  if (mtart==' ZSHB'|| mtart=='ZZCP') {
         $('#bklas').val("7920");
     } else  if (mtart=='ZSRV') {
         $('#bklas').val("7940");
     } else if  (mtart=='ZYCL') {
         $('#bklas').val("3000");
     }

}
/**
 * 表单提交
 * @returns {boolean}
 */
function saveForm(status) {
   // alert($("#businessCode").val());
   // let extendedData = getExtendedData();
   // debugger
   // if (!extendedData) return false;
    if (status==1&&coRowNum<1){
        Public.errorTip("请选择业务用途！");
        return false;
    }
    var _self = this;
    $("#active").val(status);
    $('#submitForm').ajaxSubmit({
        url: web_app.name + '/sapNonProdApplyNext/saveSapNonProdApplyDept.ajax',
        //param: extendedData,
        success: function (data) {
            $("#applyDeptId").val(data.applyDeptId);
             if (data.result=="")
             {
               Public.successTip("保存成功！");
             } else {
               Public.tip(data.result);
             }
             if (status==1) {
               $("#buttonBox").remove();
              }
           // _self.close();
            UICtrl.reloadTabById('homepage');
        }
    });
}

//撤销
function  clearForm() {
    // $("#saleForm").formClean();
   // $("#matnr").val("");
    $("#mtart").val("");
    $("#matkl").val("");
    $("#matklName").val("");

    $("#groes").val("");
    $("#lvorm").val("");
    $("#spart").val("");
    $("#spartName").val("");

    $("#mtposMara").val("");
    $("#maktx").val("");
    $("#xchpf").val("");


    $("#magrv").val("");
    $("#maktxEn").val("");
    $("#meins").val("");
    $("#meinsName").val("");

    $("#raube").val("");
    $("#brgew").val("");
    $("#gewei").val("");
    $("#volum").val("");
    $("#voleh").val("");

    $("#iprkz").val("");
    $("#mhdhb").val("");
    $("#mhdrz").val("");
    $("#normt").val("");

    $("#werks").val("");
    $("#werksName").val("");
    $("#lgort").val("");
    $("#vkorg").val("");
    $("#vtweg").val("");
    $("#lgnum").val("");
    $("#lgtyp").val("");
    $("#lgtypName").val("");

    $("#maktxRemZh").val("");
    $("#maktxRemEn").val("");

    clearForm_sale();
    clearForm_produce();
    clearForm_purchase();
    clearForm_mrp();
    clearForm_store();
    clearForm_warehouse();
    clearForm_financial();
}

//撤销采购
function  clearForm_purchase(){
    // $("#saleForm").formClean();
    $("#bstme").val("");
    $("#bstmeName").val("");
    $("#umrenPurch").val("");
    $("#umrezPurch").val("");
    $("#ekgrp").val("");
    $("#ekgrpName").val("");
}

//撤销销售
function  clearForm_sale(){

    $("#vrkme").val("");
    $("#vrkmeName").val("");

    $("#umrenSale").val("");
    $("#umrezSale").val("");

    $("#dwerkName").val("");
    $("#dwerk").val("");
    $("#aland").val("");
    $("#alandName").val("");

    $("#taxkm").val("");
    $("#versg").val("");
    $("#kondm").val("");
    $("#ktgrm").val("");
    $("#prodh").val("");
    $("#mvgr1").val("");
    $("#mvgr2").val("");
    $("#mvgr3").val("");

    $("#mvgr4").val("");
    $("#mvgr5").val("");
    $("#mtpos").val("");
    $("#xchpf").val("");
    $("#tragr").val("");
    $("#ladgr").val("");
    $("#ladgrName").val("");
}
// /撤销MRP
function  clearForm_mrp() {
    // $("#saleForm").formClean();
    $("#disgr").val("");
    $("#dismm").val("");
    $("#minbe").val("");
    $("#dispo").val("");
    $("#disls").val("");
    $("#bstfe").val("");
    $("#bstma").val("");
    $("#bstmi").val("");
    $("#mabst").val("");
    $("#bstrf").val("");

    $("#beskz").val("");
    $("#sobsl").val("");
    $("#lgpro").val("");
    $("#rgekz").val("");
    $("#lgfsb").val("");
    $("#webaz").val("");
    $("#dzeit").val("");
    $("#plifz").val("");
    $("#fhori").val("");
    $("#eisbe").val("");
    $("#eislo").val("");
    $("#strgr").val("");
    $("#mtvfp").val("");
    $("#wzeit").val("");
    $("#sbdkz").val("");
    $("#diskz").val("");
}
//撤销生产
function  clearForm_produce() {
    // $("#saleForm").formClean();
    $("#ausmeName").val("");
    $("#ausme").val("");
    $("#umrenProd").val("");
    $("#umrezProd").val("");
    $("#sfcpf").val("");
    $("#fevor").val("");
   // $("#fevorName").val("");
    $("#insmk").val("");
}

//撤销储存
function  clearForm_store() {
    // $("#saleForm").formClean();
    $("#sernp").val("");
    $("#serlv").val("");
    $("#prctr").val("");
}

//撤销仓库
function  clearForm_warehouse(){
    // $("#saleForm").formClean();
    $("#ltkza").val("");
    $("#ltkze").val("");
    $("#lgbkz").val("");
    $("#lhmg1").val("");
    $("#lhme1").val("");
    $("#lety1").val("");
}
//撤销财务核算
function  clearForm_financial(){
    // $("#saleForm").formClean();
    $("#bklas").val("");
    $("#mlast").val("");
    $("#vprsv").val("");
    //$("#peinh").val("");
    $("#verpr").val("");
    $("#stprs").val("");

}

/**
 *领导确认/回退
 */
function  confirmForm(val){
    $("#status").val(val);
    if (control.isConfirm) {
        Public.successTip("已确认！");
        return false;
    }
  //  let extendedData = getExtendedData();
  //  if (!extendedData) return false;
    $('#submitForm').ajaxSubmit({
        url: web_app.name + '/sapNonProdApplyNext/confirmSapNonProdApplyDept.ajax?confirm='+val,
       // param: extendedData,
        success: function (data) {
          //  alert(data.result);
            if (data.result=='')  {
                Public.successTip("操作成功！");
            } else {
                Public.errorTip(data.result);
            }
            //parent.reloadSpecialTaskGrid();
           // parent.homepage.trackTaskContent
            UICtrl.reloadTabById('homepage');
            $("#buttonBox").remove();
            control.isConfirm = true;
           // _self.close();
        }
    });

}
/**
 * 分发表单到各个部门
 */
/*function distributeForm() {
    $("#status").val(2);
    if (control.isDistribute) {
        Public.successTip("已分发");
        return false;
    }
    let deptStaffResult = gridManager.getData();
    if (deptStaffResult.length<1){
        Public.tip('未选择业务用途！');
        return false
    }
    let hasSaleView = false;
    for (var i = 0; i < deptStaffResult.length; i++) {
        if (deptStaffResult[i].deptCode == "V") { //销售视图
            hasSaleView = true;
            break;
        }
    }
    let hasWarehouseView = false;
    for (var i = 0; i < deptStaffResult.length; i++) {
        if (deptStaffResult[i].deptCode == "S") { //仓库视图
            hasWarehouseView = true;
            break;
        }
    }
    let hasPurchaseView = false;
    for (var i = 0; i < deptStaffResult.length; i++) {
        if (deptStaffResult[i].deptCode == "E") { //采购视图
            hasPurchaseView = true;
            break;
        }
    }
    let hasMRPView = false;
    for (var i = 0; i < deptStaffResult.length; i++) {
        if (deptStaffResult[i].deptCode == "D") { //MRP视图
            hasMRPView = true;
            break;
        }
    }
    let hasProduceView = false;
    for (var i = 0; i < deptStaffResult.length; i++) {
        if (deptStaffResult[i].deptCode == "A") { //生产视图
            hasProduceView = true;
            break;
        }
    }
    let hasStoreView = false;
    for (var i = 0; i < deptStaffResult.length; i++) {
        if (deptStaffResult[i].deptCode == "L") { //存储视图
            hasStoreView = true;
            break;
        }
    }
    if (hasPurchaseView||hasStoreView||hasMRPView||hasProduceView) {
        if ($("#werks").val() == "") {
            Public.tip("工厂必输！");
            return false;
        }
    }
    if (hasPurchaseView||hasWarehouseView||hasStoreView || hasMRPView) {
        if ($("#lgort").val() == "") {
            Public.tip("库存地点必输！");
            return false;
        }
    }
    if (hasWarehouseView){
        if ($("#lgnum").val() == "") {
            Public.tip("仓库号必输！");
            return false;
        }
    }
    if (hasSaleView) {

     if ($("#vkorg").val() == "") {
        Public.tip("销售组织必输！");
        return false;
     }
      if ($("#vtweg").val() == "") {
        Public.tip("分销渠道必输！");
        return false;
      }
    }

    let extendedData = getExtendedData();
    if (!extendedData) return false;
    $('#submitForm').ajaxSubmit({
        url: web_app.name + '/sapNonProdApplyNext/distributeSapNonProdApplyDept.ajax?distribute=2',
        param: extendedData,
        success: function (data) {
            Public.successTip("分发成功！");
            $("#buttonBox").remove();
            control.isDistribute = true;
            _self.close();
        }
    });
}   */

function bindEvent() {
    //物料组
    $('#matklName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "MATKL"}
        },
        onChange: function (value, data) {
            $('#matkl').val(data.MATKL);
            $('#matklName').val(data.WGBEZ);
        }
    });
    //产品组
    $('#spartName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "SPART"}
        },
        onChange: function (value, data) {
            $('#spart').val(data.SPART);
            $('#spartName').val(data.VTEXT);
        }
    });
    //基本计量单位
    $('#meinsName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "MEINS"}
        },
        onChange: function (value, data) {
            $('#meins').val(data.MSEH3);
            $('#meinsName').val(data.MSEHL);
            $('#lhme1').val(data.MSEH3);  // 仓库的仓库设施1存储单位
            $('#lhme1Name').val(data.MSEHL);

            if ($('#ausme').val()==data.MSEH3) {//投料计量单位
                $('#ausme').val("");
                $('#ausmeName').val("");
                UICtrl.setElRequiredFlag("#umrenProd", false);
                UICtrl.setElRequiredFlag("#umrezProd", false);
            }
            if ($('#vrkme').val()==data.MSEH3) { //销售计量单位
                $('#vrkme').val("");
                $('#vrkmeName').val("");
                UICtrl.setElRequiredFlag("#umrenSale", false);
                UICtrl.setElRequiredFlag("#umrezSale", false);
            }
            if ($('#bstme').val()==data.MSEH3) { //采购计量单位
                $('#bstme').val("");
                $('#bstmeName').val("");
                UICtrl.setElRequiredFlag("#umrenPurch", false);
                UICtrl.setElRequiredFlag("#umrezPurch", false);
            }
        }
    });

  //工厂
    $('#werksName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "WERKS"}
        },
        onChange: function (value, data) {
            $('#werks').val(data.WERKS);
            $('#werksName').val(data.NAME1);
        }
    });
    //库存地点
    $('#lgortName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "LGORT", filterValue: $('#werks').val()}
        },
        onChange: function (value, data) {
            $('#lgort').val(data.LGORT);
            $('#lgortName').val(data.LGOBE);
        }
    });
   //仓库号
    $('#lgnumName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "LGNUM"}
        },
        onChange: function (value, data) {
            $('#lgnum').val(data.LGNUM);
            $('#lgnumName').val(data.LNUMT);
        }
    });
    //存储类型
    $('#lgtypName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {
                fieldName: "LGTYP",
                filterValue:$('#lgnum').val()
            }
        },
        onChange: function (value, data) {
            $('#lgtyp').val(data.LGTYP);
            $('#lgtypName').val(data.LTYPT);
        }
    });
     //默认采购组 --采购视图
    $('#ekgrpName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "EKGRP"}
        },
        onChange: function (value, data) {
            $('#ekgrp').val(data.EKGRP);
            $('#ekgrpName').val(data.EKNAM);
        }
    });
    //采购计量单位
    $('#bstmeName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "MEINS"}
        },
        onChange: function (value, data) {
           if ($('#meins').val()==data.MSEH3) {
               Public.errorTip("不能与基本计量单位相同！");
               $('#bstme').val("");
               $('#bstmeName').val("");
               UICtrl.setElRequiredFlag("#umrenPurch",false);
               UICtrl.setElRequiredFlag("#umrezPurch",false);
           } else {
             $('#bstme').val(data.MSEH3);
             $('#bstmeName').val(data.MSEHL);
               UICtrl.setElRequiredFlag("#umrenPurch",true);
               UICtrl.setElRequiredFlag("#umrezPurch",true);
          }
        }
    });
    bindEvent_sale(); //销售视图
    bindEvent_mrp();
    //库存地点--存储管理
    $('#lgortQmName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {
                fieldName: "LGORT",
                filterValue:$("#werks").val()
            }
        },
        onChange: function (value, data) {
            $('#lgortQm').val(data.LGORT);
            $('#lgortQmName').val(data.LGOBE);
        }
    });
    $('#fevor').searchbox({  //生产主管
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "T024F", filterValue: $('#werks').val()}
        },
        onChange: function (value, data) {
            $('#fevor').val(data.FEVOR);
          //  $('#fevorName').val(data.LGOBE);
        }
    });
    //投料计量单位
    $('#ausmeName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "MEINS"}
        },
        onChange: function (value, data) {
            if ($('#meins').val()==data.MSEH3) {
                Public.errorTip("不能与基本计量单位相同！");
                $('#ausme').val("");
                $('#ausmeName').val("");
                UICtrl.setElRequiredFlag("#umrenProd",false);
                UICtrl.setElRequiredFlag("#umrezProd",false);
            }else {
                $('#ausme').val(data.MSEH3);
                $('#ausmeName').val(data.MSEHL);
                UICtrl.setElRequiredFlag("#umrenProd",true);
                UICtrl.setElRequiredFlag("#umrezProd",true);
            }
        }
    });
    bindEvent_warehouse();
}

function bindEvent_sale() {
    $('#vkorgName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "VKORG"}
        },
        onChange: function (value, data) {
            $('#vkorg').val(data.VKORG);
            $('#vkorgName').val(data.VTEXT);
        }
    });
    $('#vtwegName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "VTWEG"}
        },
        onChange: function (value, data) {
            $('#vtweg').val(data.VTWEG);
            $('#vtwegName').val(data.VKORGTEXT);
        }
    });


    $('#ladgrName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "LADGR"}
        },
        onChange: function (value, data) {
            $('#ladgr').val(data.LADGR);
            $('#ladgrName').val(data.LADGRTEXT);
        }
    });


    $('#vrkmeName').searchbox({ //销售记量单位
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {
                fieldName:"MEINS"
            }
        },
        onChange: function (value, data) {
            if ($('#meins').val()==data.MSEH3) {
                Public.errorTip("不能与基本计量单位相同！");
                $('#vrkme').val("");
                $('#vrkmeName').val("");
                UICtrl.setElRequiredFlag("#umrenSale",false);
                UICtrl.setElRequiredFlag("#umrezSale",false);
            }else {
                $('#vrkme').val(data.MSEH3);
                $('#vrkmeName').val(data.MSEHL);
                UICtrl.setElRequiredFlag("#umrenSale",true);
                UICtrl.setElRequiredFlag("#umrezSale",true);
            }

        }
    });

    //交货工厂
    $('#dwerkName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "WERKS"}
        },
        onChange: function (value, data) {
            $('#dwerk').val(data.WERKS);
            $('#dwerkName').val(data.NAME1);
        }
    });

    $('#alandName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "LAND1"}
        },
        onChange: function (value, data) {
            $('#aland').val(data.LAND1);
            $('#alandName').val(data.LANDX);
        }
    });
}

function bindEvent_mrp(){
    if ($('#vint1').val()==''){
        $('#vint1').val("999");
    }
    if ($('#vint2').val()==''){
        $('#vint2').val("999");
    }
    if ($('#vrmod').val()==''){ //消耗模式
        $('#vrmod').val("2");
    }
    $('#lgproName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "LGORT"}
        },
        onChange: function (value, data) {
            $('#lgpro').val(data.LGORT);
            $('#lgproName').val(data.LGOBE);
        }
    });
    $('#lgfsbName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "LGORT"}
        },
        onChange: function (value, data) {
            $('#lgfsb').val(data.LGORT);
            $('#lgfsbName').val(data.LGOBE);
        }
    });


    /*$('#minbeName').searchbox({  重订货点
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {
                fieldName: "MINBE",
                //filterValue:$('#lgnum').val()
            }
        },
        onChange: function (value, data) {
            $('#minbe').val(data.MINBE);
            $('#minbeName').val(data.NAME);
        }
    }); */

    $('#dispoName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {
                fieldName: "DISPO",
                //filterValue:$('#lgnum').val()
            }
        },
        onChange: function (value, data) {
            $('#dispo').val(data.DISPO);
            $('#dispoName').val(data.NAME);
        }
    });
}

function bindEvent_warehouse(){
   // $("#lety1").val("TP");
    //库存地点
    $('#lgortProfitName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
           return {fieldName: "LGORT", filterValue: $('#werks').val()}
        },
        onChange: function (value, data) {
            $('#lgortProfit').val(data.LGORT);
            $('#lgortProfitName').val(data.LGOBE);
            //  $('#ltkza').val("001");
            //   $('#ltkzaName').val("高架仓库");
        }
    });
    //存储类型
    $('#lgtypName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {
                fieldName: "LGTYP",
                filterValue:$('#lgnum').val(),
            }
        },
        onChange: function (value, data) {
            $('#lgtyp').val(data.LGTYP);
            $('#lgtypName').val(data.LTYPT);
        }
    });
    //存货下架存储类型
    $('#ltkzaName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {
                fieldName: "LGTYP",
                filterValue:$('#lgnum').val()
            }
        },
        onChange: function (value, data) {
            $('#ltkza').val(data.LGTYP);
            $('#ltkzaName').val(data.LTYPT);
        }
    });
    //存货上架存储类型
    $('#ltkzeName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {
                fieldName: "LGTYP",
                filterValue:$('#lgnum').val()
            }
        },
        onChange: function (value, data) {
            $('#ltkze').val(data.LGTYP);
            $('#ltkzeName').val(data.LTYPT);
        }
    });
    //存储分区标识
    // $('#lgbkzName').searchbox({
    //     type: "system", name: "dictionary",
    //     getParam: function (item) {
    //         return {
    //             fieldName: "LGTYP",
    //             filterValue:$('#lgnum').val()
    //         }
    //     },
    //     onChange: function (value, data) {
    //         $('#lgbkz').val(data.LGTYP);
    //         $('#lgbkzName').val(data.LTYPT);
    //     }
    // });

    $('#lhme1Name').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "MEINS"}
        },
        onChange: function (value, data) {
            $('#lhme1').val(data.MSEH3);
            $('#lhme1Name').val(data.MSEHL);
        }
    });
}


function getExtendedData() {
    let msg = "[";
    let isUndefined = false;
    let extendedData = {};
  /*  let deptStaffResult = DataUtil.getGridData({
        gridManager: gridManager,
        isAllData: true,
        onCheck: function (data) {
            if (typeof data.userCode === 'undefined') {
                msg += `${data.deptName}`;
                isUndefined = true;
            }
            return true;
        }
    }); */
        let deptStaffResult = gridManager.getData();
        for (var i = 0; i < deptStaffResult.length; i++) {
            //  alert(deptStaffResult[i].deptName); alert(deptStaffResult[i].userCode);
            if (deptStaffResult[i].userCode == undefined) {
                //if (isUndefined) {
                isUndefined = true;
                break;
            }
        }
        if (isUndefined) {
            Public.tip(deptStaffResult[i].deptName + '未选择对应分发人员');
            return false
        }
        extendedData.deptStaff = Public.encodeJSONURI(deptStaffResult);
      return extendedData;
}


function rejectForm() {
    $('#auditForm').ajaxSubmit({
        url: web_app.name + '/sapNonProdApplyNext/rejectNonProdApplyDept.ajax',
        param: {meId:getMeId()},
        success: function (data) {
            Public.successTip("已驳回！");
            _self.close();
        }
    });

}

function checkThrough(){
    $('#exPass').ajaxSubmit({
        url: web_app.name + '/sapNonProdApplyNext/rejectNonProdApplyDept.ajax',
        param: {meId:getMeId()},
        success: function (data) {
            Public.successTip("检查通过！");
            _self.close();
        }
    });
}


/**
 * 根据选择的驳回的部门生成输入框
 */
function loadRejectReasonBox() {
    $('input[name="auditDept"]').change(function () {
            let val = $(this).val()
            let textData = $(this).parent().text().trim()
            if (this.checked) {
                if ($(`#id_${val}`).length) {
                    // 元素存在
                    $(`#id_${val}`).show();
                } else {
                    // 元素不存在
                    let box = `<div id="id_${val}">
                            <div class="col-xs-4 col-sm-2" style="height:60px;">
                                <label class="hg-form-label" name="${val}" title="[${textData}]驳回原因">[${textData}]驳回原因&nbsp;:</label>
                             </div>
                             <div class="col-xs-8 col-sm-10 col-warp col-white-bg" style="height:60px;">
                                <textarea name="${val}"  label="[${textData}]驳回原因" id="text_${val}"></textarea>
                             </div>
                        </div>`;
                    $("#rejectReasonBox").append(box)
                }
                $(window).scrollTop($(document).height());
            } else {
                $(`#id_${val}`).hide();
            }
        }
    );
}

/**
 *根据驳回信息生成驳回信息展示栏
 */
/*function showRejectCauseBox() {
    Public.ajax(web_app.name + '/sapNonProdApplyNext/slicedRejectCauseList.ajax', {meId: getMeId()}, function (data) {
        const generateCauseHTML = (item, newCause) => {
            const { rectifyDate, rejectMsg, bsnExecuteId, rejectDate } = item;
            return `
    <div class="hg-form-row">
      <div class="col-xs-4 col-sm-1" style="height:56px;">
        <label class="hg-form-label" title="驳回原因">驳回原因${newCause}&nbsp;:</label>
      </div>
      <div class="col-xs-8 col-sm-11 col-warp col-gray-bg" style="height:56px;">
        <textarea rows="2" label="驳回原因" readonly="readonly">${rejectMsg}</textarea>
      </div>
    </div>
    <div class="hg-form-row">
      <div class="col-xs-4 col-sm-1">
        <label class="hg-form-label" title="驳回人">驳回人&nbsp;:</label>
      </div>
      <div class="col-xs-8 col-sm-3 col-warp col-gray-bg">
        <input type="text" value="${bsnExecuteId}" readonly="readonly" label="驳回人">
      </div>
      <div class="col-xs-4 col-sm-1">
        <label class="hg-form-label" title="驳回时间">驳回时间&nbsp;:</label>
      </div>
      <div class="col-xs-8 col-sm-3 col-warp col-gray-bg">
        <input type="text" value="${rejectDate}" readonly="readonly" label="驳回时间">
      </div>
      <div class="col-xs-4 col-sm-1">
        <label class="hg-form-label" title="处理时间">处理时间&nbsp;:</label>
      </div>
      <div class="col-xs-8 col-sm-3 col-warp col-gray-bg">
        <input type="text" value="${rectifyDate}" readonly="readonly" label="处理时间">
      </div>
    </div>
  `;
        };
        let newCause = `<span style="color:red;">new</span>`;
        const ahtml = data
            .map((item) => {
                const cause = item.rectifyDate === '' ? `[${newCause}]` : '';
                return generateCauseHTML(item, cause);
            })
            .join('');

        $("#cause-details-box").html(ahtml);
        $("#causeBox").show();
    })

} */

function getMeId() {
    return $("#meId").val() || '';
}