var gridManager = null;
var  canEdit=true;

$(document).ready(function () {
    initialize();
    bindDetailEvent();
});
function initialize(){
    UICtrl.layout("#layout", {leftWidth: 3});
   // let isDetailPage = ($("#isDetailPage").val() === "true");
 //   if (isDetailPage) {
   //     $("#jobPageTop").hide();
 //   }
   // alert($("#isReadOnly").val());
    if ($("#isReadOnly").val()=="true") {
         disableForm("infoRecord","disable");
    } else {
         var readwriteList = UICtrl.getUIElementPermissions(UIPO.READWRITE, UIPK.DETAIL);
        // alert("dsd=="+readwriteList.length); alert(TaskKind.isReplenishTask(taskKindId)); //打回的
        if (($("#statusId").val() == 1 && readwriteList.length > 0&&TaskKind.isReplenishTask(taskKindId)!=true)) {
             disableForm("infoRecord", "disable");
            $.each(readwriteList, function (i, o) {
                $("#" + o).removeAttr('disabled');
            });
        }
    }
    if ($('#matnr').val()!=""){ //物料编码不为空 禁用物料组
        UICtrl.disable($("#matklName"));
        UICtrl.enable($("#esokz"));
    }else{
        //没有物料号的，只能是标准，不可修改
        $("#esokz").val("0");
        $("#esokz_text").val("0-标准采购信息记录");
        UICtrl.disable($("#esokz"));
    }
    $('#webre').val("X");

}


function disableForm(formId,isDisabled) {

    var attr="disable";
    if(!isDisabled){
        attr="enable";
    }
    $("form[id='"+formId+"'] :text").attr("disabled",isDisabled);
    $("form[id='"+formId+"'] textarea").attr("disabled",isDisabled);
    $("form[id='"+formId+"'] select").attr("disabled",isDisabled);
    $("form[id='"+formId+"'] :radio").attr("disabled",isDisabled);
    $("form[id='"+formId+"'] :checkbox").attr("disabled",isDisabled);

    //禁用jquery easyui中的下拉选（使用input生成的combox）

    $("#" + formId + " input[class='combobox-f combo-f']").each(function () {
        if (this.id) {alert("input"+this.id);
            $("#" + this.id).combobox(attr);
        }
    });

    //禁用jquery easyui中的下拉选（使用select生成的combox）
    $("#" + formId + " select[class='combobox-f combo-f']").each(function () {
        if (this.id) {
            alert(this.id);
            $("#" + this.id).combobox(attr);
        }
    });

    //禁用jquery easyui中的日期组件dataBox
    $("#" + formId + " input[class='datebox-f combo-f']").each(function () {
        if (this.id) {
            alert(this.id)
            $("#" + this.id).datebox(attr);
        }
    });
}

function bindDetailEvent(){
    $('#werksName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "WERKS"}
        },
        onChange: function (value, data) {

            var werks = $("#werks").val();
            if (data.WERKS != werks) {
                //如果选择的是其他公司，清空所选物料数据
                $('#matnr').val("");
                $('#maktx').val("");
                $('#matkl').val(""); // alert(data.MATKL+"--"+data.WGBEZ);
                $('#matklName').val("");
                $('#ekgrp').val("");
                $('#ekgrpName').val("");
                $('#bprme').val("");
                $('#bprmeName').val("");

                $('#konms').val("");

                $('#peinh').val("");
                $('#meins').val("");
                $('#meinsName').val("");
            }
            $('#werks').val(data.WERKS);
            $('#werksName').val(data.NAME1);
        }
    });

    $('#ekorgName').searchbox({
        type: "system", name: "dictionary",
         getParam: function (item) {
                return {fieldName: "EKORG"}
         },
        onChange: function (value, data) {
            $('#ekorg').val(data.EKORG);
            $('#ekorgName').val(data.EKOTX);
        }
    });

    $('#lifnr').searchbox({
        type: "suppliers", name: "selectSuppliers",
        getParam: function (item) {
        },
        onChange: function (value, data) {
            var name1 = $("#name1").val();
            if (data.nameOrg1 != name1) {
                //如果选择的是其他公司，清空所选物料数据
                $('#waersName').val("");
                $('#waers').val("");
            }
            $('#lifnr').val(data.partner);
            $('#name1').val(data.nameOrg1);
        }
    });
    $('#waersName').searchbox({
        type: "system", name: "selectSupplierWaers",
        getParam: function (item) {
            return {
                partner: $('#lifnr').val(),
               // werks: $('#werks').val()
            }
        },
        onChange: function (value, data) {
            $('#waers').val(data.waers);
            $('#waersName').val(data.waersName);
            $('#ltsnr').val(data.ltsnr);
            $('#ltsbz').val(data.ltsbz);
            //$('#name1').val(data.nameOrg1);
        }
    });
    //物料编码
    $('#matnr').searchbox({
        type: "system", name: "sapMaterialSelect",
        getParam: function (item) {
            return {
                filterValue: $('#werks').val()
            }
        },
        onChange: function (value, data) {
            $('#matnr').val(data.MATNR);
            $('#maktx').val(data.MAKTX);
            $('#matkl').val(data.MATKL); // alert(data.MATKL+"--"+data.WGBEZ);
            $('#matklName').val(data.WGBEZ);
            $('#ekgrp').val(data.EKGRP);
            $('#ekgrpName').val(data.EKNAM);
            $('#bprme').val(data.MEINS);
            $('#bprmeName').val(data.MSEHL);

            $('#konms').val(data.MEINS);

            $('#peinh').val(data.PEINH);
            $('#meins').val(data.MEINS);
            $('#meinsName').val(data.MSEHL);

            if (data.MATNR == undefined) {
                UICtrl.enable($("#matklName"))

                //没有物料号的，只能是标准，不可修改
                $("#esokz").val("0");
                $("#esokz_text").val("0-标准采购信息记录");
                UICtrl.disable($("#esokz"));
            } else {
                UICtrl.disable($("#matklName"));
                UICtrl.disable($("#maktx"))
                UICtrl.enable($("#esokz"));
             }
         }
    });
    //物料组
    $('#matklName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {
               fieldName: "MATKL"
            }
        },
        onChange: function (value, data) {
            $('#matkl').val(data.MATKL);
            $('#matklName').val(data.WGBEZ);
            UICtrl.enable($("#maktx"));
            if (data.MATKL == undefined) {
                $("#maktx").val("");
                UICtrl.enable($("#matnr"));
                UICtrl.disable($("#maktx"));
                UICtrl.enable($("#sortl"));
            } else {
                UICtrl.disable($("#matnr"));
                $("#sortl").val("");
                UICtrl.disable($("#sortl"));
                UICtrl.setElRequiredFlag("#sortl",false);
            }
        }
    });

    $('#ekgrpName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "EKGRP"}
        },
        onChange: function (value, data) {
            $('#ekgrp').val(data.EKGRP);
            $('#ekgrpName').val(data.EKNAM);
        }
    });

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

    $('#urzlaName').searchbox({
        type: "system", name: "dictionarySelect",
        getParam: function (item) {
            return {fieldName: "country",displayNames:"国家",displayLens:"180"}
        },
        onChange: function (value, data) {
            $('#urzla').val(data.value);
            $('#urzlaName').val(data.name);
        }
    });

    $('#regioName').searchbox({
        type: "system", name: "getArea",
        getParam: function (item) {
            if (Public.isBlank($('#urzla').val())){
                Public.tip("请先选择原产地国!")
                return false;
            }
            return {
                LAND1: $('#urzla').val()
            }
        },
        onChange: function (value, data) {
            $('#regio').val(data.BLAND);
            $('#regioName').val(data.BEZEI);
        }
    });
    $('#kbetr').on('input', function() {
        let inputValue = $(this).val();
        if (isNaN(Number(inputValue))) {
            Public.errorTip("等级金额1必须输入数字!");
        }else {
            if (Public.isNotBlank(inputValue)) {
                UICtrl.disable($('#netpr'))
                $('#netpr').val(inputValue);
            }else {
                UICtrl.enable($('#netpr'))
            }
        }
    });
}
