/**
 * 菜单管理 detail
 * @Author NZhang
 * ResourceController
 * system/resources/appointment.ftl
 */

var el = {
    parentId: '#parentId',
    shortcutMenu: '#shortcutMenu',
    id: '#id',
    form: '#form'
};

$(function () {
    initCombo();

    var parentId = $(el.parentId).combobox('getValue');
    if (parentId === '0') {
        $(el.parentId).combobox('clear')
    }

});

//初始化下拉框
function initCombo() {
    $.get(g.ctx + 'pc/sys/resource/listAll?menuType='+menuType, {}, function (data) {
        var option = {id: 0, name: '--- 请选择 ---'};
        data.unshift(option);
        // 查询条件-基础数据表
        $(el.parentId).combobox({
            valueField: 'id',
            textField: 'name',
            panelHeight: 200,
            data: data,
            prompt: '-- 请选择 --',
            editable: true,
            panelWidth: 300
        });
    });


    $(el.shortcutMenu).combobox({
        valueField: 'value',
        textField: 'name',
        panelHeight: 200,
        data: [{
            value: 0,
            name: "否"
        }, {
            value: 1,
            name: "是"
        }],
        prompt: '-- 请选择 --',
        editable: false,
    });

}

//提交
function saveOrUpdate(call) {
    var parentId = $('#parentId').val();
    var id = $('#id').val();
    var formData = $(form).serializeJson();
    if (id === formData["parentId"]) {
        parent.layer.msg('不能选择自己作为上级菜单', {time: 2000});
        return;
    }

    if (!checkData()) {
        parent.layer.msg("请完善必填信息", {icon: 2});
        return;
    }
    var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
    $.ajax({
        url: g.ctx + 'pc/sys/resource/saveOrUpdate',
        data: formData,
        method: 'post',
        success: function (data) {
            call();
        },
        complete: function(data) {
            top.layer.close(loading);
        },
        error: function (data) {
            if (data.responseText) {
                var dataObj = JSON.parse(data.responseText);
                parent.layer.msg(dataObj.message, {time: 2000});
            } else {
                parent.layer.msg('保存菜单资源失败', {time: 2000});
            }
        }
    })
}

function checkData() {
    return $(el.form).form('validate');
}