$(document).ready(function () {
    initializateUI();
    // loadProcessListGrid();
    bindQueryEvent();
    // bindEvent();
});

function initializateUI() {
    UICtrl.layout("#layout", {leftWidth: 3});
    var anlkl = $('#anlkl').val();
    if (anlkl.indexOf("Z7")>=0){
        disable();
    }else {
        enable();
    }
}

function bindQueryEvent() {
    $('#kostl').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {
                fieldName: "KOSTL"
            }
        },
        onChange: function (value, data) {
            $('#kostl').val(data.KOSTL);
            $('#kostlv').val(data.LTEXT);
        }
    });
    $('#kostlv').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {
                fieldName: "KOSTL"
            }
        },
        onChange: function (value, data) {
            $('#kostlv').val(data.LTEXT);
        }
    });
    $('#anlkl').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {
                fieldName: "ANLKL",
                filterValue: "Z"
            }
        },
        onChange: function (value, data) {
            $('#anlkl').val(data.ANLKL);
            //当资产类别为Z701-Z703时，成本中心字段设置为灰色不允许填写，当资产类别为其他时成本中心字段设置为必输项。
            if (data.ANLKL.indexOf("Z7")>=0){
                disable();
            }else {
                enable();
            }
        }
    });
    $('#bukrs').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "BUKRS"}
        },
        onChange: function (value, data) {
            $('#bukrs').val(data.BUKRS);
        }
    });
    $('#meins').searchbox({
        type: "system", name: "dictionary",
        getParam: function (item) {
            return {fieldName: "MEINS"}
        },
        onChange: function (value, data) {
            $('#meins').val(data.MSEH3);
        }
    });
}

function disable(){
    UICtrl.setElRequiredFlag("#kostl",false);
    UICtrl.setElRequiredFlag("#kostlv",false);
    UICtrl.disable("#kostl")
    UICtrl.disable("#kostlv")
    $("#kostl").val("")
    $("#kostlv").val("")
}

function enable(){
    UICtrl.setElRequiredFlag("#kostl",true);
    UICtrl.setElRequiredFlag("#kostlv",true);
    UICtrl.enable("#kostl")
    UICtrl.enable("#kostlv")
}

function getFixedAssetsDetail(fixedAssetsAction) {
    //采购订单保存时数据不进行必填验证，提交时数据才进行必填验证
    var op = {};
    var check = true;
    if (fixedAssetsAction == "save") {
        check = false;
    }
    op = {check: check};
    var fixedAssets = $('#submitForm').formToJSON(op);
    if (!fixedAssets) {
        return false;
    }
    return fixedAssets;
}

function saveData(action) {
    debugger
    var fixedAssetsDetail = getFixedAssetsDetail("submit");
    if (!fixedAssetsDetail) {
        return;
    }
    Public.ajax(web_app.name + '/sapFixedAssets/saveFixedAssets.ajax?code=' + action,fixedAssetsDetail,
        function (data) {
            setId(data.id);
            Public.successTip("数据保存成功");
            renderingDictionaries(data);
        }
    );
}

function closeDispatchTabItem() {
    UICtrl.closeCurrentTab();
}

function setId(value) {
    $('#id').val(value);
}

function getId() {
    return $("#id").val();
}

/**
 * 渲染带数据字典的select
 * @param obj
 */
function renderingDictionaries(obj) {
    console.log(obj)
    //获取所有select的id名与对象匹配
    $("select").each(function () {
        var id = $(this).attr("id");
        if (obj.hasOwnProperty(id)) {

            var keyValuePairs = new Map();

            $('#' + id + ' option').each(function () {
                var value = $(this).val();
                var text = $(this).text();
                keyValuePairs.set(value, text);
            });

            // 获取 对应的文本值
            var selectedValue = obj[id];
            var selectedText = keyValuePairs.get(selectedValue);

            // 将文本值赋值给目标元素
            $('#' + id + '_text').val(selectedText);
            $('#' + id).val(selectedValue);

            console.log("select:" + id+"("+selectedText+":"+selectedValue+")")
        }
    });
}