/**
 * 菜单管理 菜单分配页面
 * @Author NZhang
 * ResourceController
 * system/resources/buttons.ftl
 */

var el = {
    id: '#id',
    form: '#form',
    buttons: '#buttons',
    add: '#add',
    edit: '#edit',
    look: '#look',
    remove: '#remove',
    import: '#import',
    export: '#export',
    examine: '#examine',
    discard: '#discard',
    tb: '#tb'
};

$(function () {

    loadGrid();
    // getNodesSelected();

});

//加载按钮权限表格
function loadGrid() {
    var id = $(el.id).val();
    $(el.tb).datagrid({
        iconCls: 'icon-list',
        height: gridheight,
        pagination: false,
        url: g.ctx+'pc/sys/resource/button/listAll',
        loadMsg: '数据远程载入中,请等待...',
        singleSelect: true,
        idField: 'id',
        fitColumns: true,
        striped: true,
        rownumbers: true,
        queryParams: {
            resourceId: id
        },
        onLoadSuccess: function () {
        },
        columns: [[
            {
                field: 'ck',
                hidden: true
            },
            {
                field: 'name',
                title: '名称',
                width: '140',
                align: 'left'
            },
            {
                field: 'button',
                title: '按钮编码',
                width: '140',
                align: 'left'
            }
        ]],
        toolbar: [
            {
                text: '新增',
                iconCls: 'icon-add',
                hidden: true,
                handler: function () {
                    createOrEdit(0);
                }
            }, "-",
            {
                text: '编辑',
                iconCls: 'icon-add',
                hidden: true,
                handler: function () {
                    createOrEdit(1);
                }
            }, "-",
            {
                text: '删除',
                iconCls: 'icon-remove',
                handler: function () {
                    remove();
                }
            }, "-"
        ]
    });
}

/**
 * 新增、编辑
 * @param state 0.新增 1.编辑
 */
function createOrEdit(state) {
    var self = this;
    var title;
    var id = '';
    switch (state) {
        case 0:
            title = '新增';
            break;
        case 1:
            title = '编辑';
            var row = $(el.tb).datagrid('getSelected');
            if (!row) {
                layer.msg("请选中一条记录");
                return;
            }
            id = row.id;
            break;
    }
    layer.open({
        title: title,
        type: 2,
        scrollbar: false,
        area: ['500px', '260px'],
        content: g.ctx + 'pc/sys/resource/button/detail?id=' + id,
        success: function (layero, index) {
            var body = layer.getChildFrame('body', index);
            var iframe = window[layero.find('iframe')[0]['name']];
            var resourceId = $(el.id).val();
            body.find('#resourceId').val(resourceId);
        },
        btn: ['保存', '关闭'],
        yes: function (index, layero) {
            var body = layer.getChildFrame('body', index);
            var iframeWin = parent.window[layero.find('iframe')[0]['name']];//得到iframe页的窗口对象，执行iframe页的方法：
            var form = body.find('#form');
            var formData = $(form).serializeJson();

            $.ajax({
                url: g.ctx + 'pc/sys/resource/button/saveOrUpdate',
                data: formData,
                method: 'post',
                success: function (data) {
                    layer.msg('保存成功', {time: 2000});
                    layer.close(index);
                    $(el.tb).datagrid('reload');
                    $(el.tb).datagrid('clearSelections');
                },
                error: function (data) {
                    if (data.responseText) {
                        var dataObj = JSON.parse(data.responseText);
                        layer.msg(dataObj.message, {time: 2000});
                    } else {
                        layer.msg('保存菜单资源失败', {time: 2000});
                    }
                }
            })
        }
    });
}

/**
 * 菜单按钮删除
 */
function remove() {
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        layer.msg("请选中一条记录");
        return;
    }
    var id = row.id;

    layer.confirm('确定删除？', function (index) {
        var url = g.ctx + 'resources/button/remove';
        $.post(g.ctx + 'pc/sys/resource/button/remove', {id: id}, function (data) {
            layer.msg('删除成功', {time: 2000});
            layer.close(index);
            $(el.tb).datagrid('reload');
            $(el.tb).datagrid('clearSelections');

        }).fail(function (data) {
            var dataObj = JSON.parse(data.responseText);
            layer.msg(dataObj.message, {time: 2000});
        });
    });
}

//查找已经分配过的按钮,并在页面初始化的时候选中
function getNodesSelected() {
    var button = $(el.buttons).val();

    if (button) {
        var arr = button.replace('[', '').replace(']', '').split(',');
        $.each(arr, function (index, item) {
            var id = item.trim();
            $('input[name="' + id + '"]').prop("checked", true);
        })
    }
}