$(function () {
    loadGrid();
});

function loadGrid() {
    $('#tb').treegrid({
        url: g.ctx + 'pc/sys/org/treegrid',
        singleSelect: true,
        idField: 'id',  //关键字段来标识树节点，不能重复
        treeField: 'name',  //树节点字段，也就是树节点的名称
        fitColumns: true,
        height: document.documentElement.clientHeight,
        width: gridwidth,
        rownumbers: true,//显示一个行号列
        collapsible: true,//收起表格的内容
        queryParams: {parentId: -1},
        loadMsg: '数据加载中...',
        animate: true,//在节点展开或折叠的时候是否显示动画效果
        onLoadSuccess: function (data) {
            delete $(this).treegrid('options').queryParams['id'];
        },
        onContextMenu: function (e, row) {
            e.preventDefault();                    //屏蔽浏览器的菜单
            $(this).treegrid('unselectAll');    //清除所有选中项
        },
        onContextMenu: function (e, row) {
            e.preventDefault();                    //屏蔽浏览器的菜单
            $(this).treegrid('unselectAll');    //清除所有选中项
            if ($("#menu > div").size() > 1) {
                $('#menu').menu('show', {
                    left: e.pageX - 10,
                    top: e.pageY - 5
                });
            }
        },
        onBeforeExpand: function (row) {
            //动态设置展开查询的url
            var self = $(this);
            var url = g.ctx + 'pc/sys/org/treegrid?parentId=' + row.id;
            var opts = self.treegrid('options');//获取属性表格的属性对象
            if (row.state == "closed" && row.children == undefined) {
                $.post(url, function (data) {
                    self.datagrid("loading", "玩命加载。。。");
                    if (data) {
                        self.treegrid('append', {
                            parent: row[opts.idField],//父节点id
                            data: data.rows
                        });
                    } else {
                        layer.alert("error", {icon: 5, time: 1000});
                    }
                    self.datagrid("loaded");
                }, "JSON");
            }
        },
        columns: [[
            {
                title: 'id',
                field: 'id',
                checkbox: true,
                align: 'center',
                width: 60
            },
            {field: 'name', title: '组织名称', width: 100, align: 'left'},
            {field: 'code', title: '组织编码', width: 100, align: 'left'},
            {
                field: 'status', title: '状态', width: 50, align: 'left',
                formatter: function (value, row, index) {

                    if (value == 1) {
                        return '<span style="color: green">有效</span>';
                    } else {
                        return '<span style="color: red">无效</span>';
                    }
                }
            }
        ]],

        toolbar: [
            "-", {
                text: '新增',
                iconCls: 'icon-add',
                handler: function () {
                    add();
                }
            }, "-", {
                text: '修改',
                iconCls: 'icon-edit',
                handler: function () {
                    update();
                }
            }, "-", {
                text: '删除',
                iconCls: 'icon-remove',
                handler: function () {
                    del();
                }
            }
        ]
    })
}

function del() {
    var cbox = $('#tb').datagrid('getSelected');
    if (!cbox) {
        layer.msg(commonMsg.msg1);
        return;
    }
    if (cbox.status == "") {
        layer.msg("请选中一条记录");
        return;
    }
    topWin.layer.confirm("确认删除此条记录", {icon: 3}, function (idx) {
        $.ajax({
            url: g.ctx + 'pc/sys/org/remove',
            method: "POST",
            data: {id: cbox.id},
            success: function (data) {
                topWin.layer.msg("删除成功", {icon: 1});
                topWin.layer.close(idx);
                $("#tb").treegrid('reload');
            },
            error: function (data) {
                topWin.layer.close(idx);
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });

    })
}

function add() {
    var cbox = $('#tb').datagrid('getSelected');
    if (cbox == null || cbox == '') {
        top.layer.alert("未选择父级菜单,无法添加", {icon: 3});
    }

    addOrEdit(0, cbox.id);

}

function addOrEdit(id, parentId) {
    var orgAddOrEditDialog = openDialog('orgAddOrEditDialog', g.ctx + 'pc/sys/org/addOrEdit?parentId=' + parentId + "&id=" + id, {
        title: (id === 0 ? '新增' : '编辑') + "组织机构",
        width: '420px',
        height: '330px',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            orgAddOrEditDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.save,
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['orgAddOrEditDialog'].saveOrUpdate(function () {
                        $("#tb").treegrid('reload');
                        orgAddOrEditDialog.dialog('close');
                    });
                }
            },
            {
                text: itablei8n.close,
                iconCls: 'icon-cancel',
                handler: function () {
                    orgAddOrEditDialog.dialog('close');
                }
            }
        ]
    });
}

function update() {
    var cbox = $('#tb').datagrid('getSelected');
    addOrEdit(cbox.id, cbox.parentCode)
}