var gridManager = null;
var  canEdit=true;

$(document).ready(function () {
    initialize();
    bindEvent();
});
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"));
    }
    $('#webre').val("X");

}

/**
 * 流程引擎回调设置打样的id
 * @param value 打样申请的id
 */
function setId(id) {
   // $("#id").val(id);
}

/**
 * 流程引擎获取打样申请id的回掉方法。
 * @return 打样申请的id
 */
function getId() {
    var id=$("#id").val() || "";
    return id;
}



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 bindEvent(){
    $('#werksName').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "WERKS"}
        },
        onChange: function (value, data) {
            $('#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) {
            $('#lifnr').val(data.partner);
            $('#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"))
            } else {
                UICtrl.disable($("#matklName"));
                UICtrl.disable($("#maktx"))
             }
         }
    });
    //物料组
    $('#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);
        }
    });
}

function reloadGrid() {
}