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

function initializateUI() {
    UICtrl.layout("#layout", {leftWidth: 3});
}

function bindQueryEvent() {
    $('#kostl').searchbox({
        type: "costCenter", name: "costCenterCode",
        onChange: function (value, data) {
            $('#kostl').val(data.kostl);
        }
    });
}

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+")")
        }
    });
}