/**
 * 菜单资源 index
 * system/resources/list.ftl
 * ResourcesController
 */

var el = {
    tb: '#tb',
    treeCode: '#treeCode',
    orgTree: '#orgTree'
};

var $table;
var zTree;
var selfData = {
    treeNodes: {},
    parentNodes: []
};
$(function () {

    initResourceTree('');

    $(el.treeCode).textbox({
        prompt: '输入资源名称搜索',
        icons: [
            {
                iconCls: 'icon-search',
                handler: function (e) {
                    var name = $(e.data.target).textbox('getValue');
                    initResourceTree(name);
                }
            }
        ]
    });
});

function searchInfo() {
    var queryParams = $("#searchForm").serializeJson();
    var row = this.zTree.tree('getSelected');
    if (row) {
        queryParams = {parentId: row.id};
    }
    $(el.tb).treegrid('options').queryParams = queryParams;
    $(el.tb).treegrid('load');
}

function loadGrid() {
    $(el.tb).treegrid({
        iconCls: 'icon-list',
        height: gridheight,
        method: 'get',
        width: gridwidth,
        url: g.ctx + 'pc/sys/resource/listAll?menuType='+menuType,
        loadMsg: '数据远程载入中,请等待...',
        singleSelect: true,
        idField: 'id',
        nowrap: true,
        remoteSort: false,
        fitColumns: true,
        striped: true,
        rownumbers: true,
        onLoadSuccess: function () {
        },
        columns: [[
            {
                field: 'ck',
                hidden: true
            },
            {
                field: 'name',
                title: '名称',
                width: '140',
                align: 'left'
            },
            {
                field: 'enName',
                title: '英文名称',
                width: '140',
                align: 'left'
            },
            {
                field: 'description',
                title: '描述',
                width: '150',
                align: 'left'

            },
            {
                field: 'indicatorCode',
                title: '指标编码',
                width: '140',
                align: 'left'
            },
            {
                field: "resKey",
                title: "唯一KEY",
                width: '150',
                align: 'left'
            }, {
                field: "resUrl",
                title: "URL地址",
                width: '200',
                align: 'left'
            }, {
                field: "shortcutMenu",
                title: "快捷菜单",
                width: '80',
                align: 'left',
                formatter: function (value) {
                    if (value == 1) {
                        return "是";
                    }

                    return "否";
                }
            }, {
                field: "sort",
                title: "排序",
                width: '80',
                align: 'left'
            },
            {
                field: "description",
                width: '140',
                title: "描述",
                align: 'left'
            },
            {
                field: 'filePath',
                title: '操作',
                hidden:menuType == 2,
                width: '100',
                align: 'left',
                formatter: function (value, row) {
                    var id = row.id;
                    var tree = selfData.treeNodes[id];
                    if (menuType == 1) {
                        if (tree.children.length === 0) {
                            return '<a onclick="setButton(' + row.id + ')">按钮分配</a>';
                        }
                    }
                }
            }
        ]],
        toolbar: [
            {
                text: '新增',
                iconCls: 'icon-add',
                hidden: true,
                handler: function () {
                    addFun(0);
                }
            },
            "-", {
                text: '编辑',
                iconCls: 'icon-edit',
                handler: function () {
                    addFun(1);
                }
            }, "-", {
                text: '删除',
                iconCls: 'icon-remove',
                handler: function () {
                    remove();
                }
            }, "-"

        ]

    });
}

/**
 * 删除
 */
function remove() {
    var self = this;
    var row = $(el.tb).treegrid('getSelected');
    if (!row) {
        layer.msg("请选中一条记录");
        return;
    }
    var id = row.id;
    parent.layer.confirm("确定要删除菜单资源?", {title: '信息', btn: ['确定', '取消']}, function (index) {
             var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
            $.ajax({
                url: g.ctx + 'pc/sys/resource/updateHideFlag',
                method: "post",
                data: {
                    id: id
                },
                complete: function(data) {
                    top.layer.close(loading);
                },
                success: function (data) {
                    parent.layer.close(index);
                    layer.msg("操作成功！");
                    $(el.tb).treegrid('reload');
                    $(el.tb).treegrid('clearSelections');
                    self.zTree.tree('reload')
                },
                error: function () {
                    layer.msg("删除失败！");
                }
            })
        }
    );
}

function addFun(status) {
    var self = this;
    var parentId = 0;
    var title;
    var id = '';
    switch (status) {
        case 0:
            title = '新增';
            var row = this.zTree.tree('getSelected');
            if (row) {
                parentId = row.parentId;
            }
            break;
        case 1:
            title = '编辑';
            var cbox = $(el.tb).treegrid('getSelected');
            if (cbox == null) {
                layer.msg("请选中一条记录");
                return;
            }
            id = cbox.id;
            break;
    }
    var resourceDialog = openDialog('resource', g.ctx + 'pc/sys/resource/detail?id=' + id + '&parentId=' + parentId+'&menuType='+menuType, {
        title: title,
        width: '80%',
        height: '70%',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            resourceDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.save,
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['resource'].saveOrUpdate(function () {
                        topWin.layer.msg('数据保存成功', {icon: 1});
                        resourceDialog.dialog('close');
                        $(el.tb).treegrid('reload');
                        $(el.tb).treegrid('clearSelections');
                        self.zTree.tree('reload');
                    });
                }
            },
            {
                text: itablei8n.close,
                iconCls: 'icon-cancel',
                handler: function () {
                    resourceDialog.dialog('close');
                }
            }
        ]
    });
}

// 初始化树
function initResourceTree(likeName) {
    if (!likeName) {
        likeName = '';
    }
    this.zTree = $(el.orgTree).tree({
        animate: true,
        method: 'get',
        height: gridheight,
        url: g.ctx + 'pc/sys/resource/treeList?menuType=' + menuType + '&name=' + likeName,
        onClick: function (node) {
            searchByDeptId(node.id);
        },
        onLoadSuccess: function (node, data) {
            getTreeNodeList(data, selfData.treeNodes);
            $(el.orgTree).tree('collapseAll');
            loadGrid();
        }
    });
}

// 树点击事件
function searchByDeptId(parentId) {
    var queryParam = {};
    queryParam = {'treeId': parentId};
    $(el.tb).treegrid('options').queryParams = queryParam;
    $(el.tb).treegrid('clearSelections');
    $(el.tb).treegrid('reload');
}

//树搜索框搜索事件
function searchTree() {
    var name = $(el.treeCode).val();
    initResourceTree(name);
}

function setButton(id) {
    var buttonDialog = openDialog('resource', g.ctx + 'pc/sys/resource/buttons?id=' + id, {
        title: '按钮分配',
        width: '700px',
        height: '520px',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            buttonDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.close,
                iconCls: 'icon-cancel',
                handler: function () {
                    buttonDialog.dialog('close');
                }
            }
        ]
    });
}

function getTreeNodeList(data, obj) {
    $.each(data, function (index, item) {
        obj[item.id] = item;
        if (item.children) {
            getTreeNodeList(item.children, obj)
        }
    });
}

//父页面关闭子页面窗口
function closeDetailWin() {
    parent.layer.closeAll();
}