<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8" %>
<%@include file="/WEB-INF/ehr/commons/jsp/commonInclude.jsp" %>
<script>

    /* *************************** 模块初始化 ***************************** */
    var menu = {
        grid: null,
        dialog: null,
        init: function () {
            var me = this;
            $('.m-retrieve-area').show();
            $("#txtName").ligerSearch({
                onClick:function(value){
                me.reloadGrid();
            }});
            me.grid = $("#div_grid").ligerGrid({
                url: '${contextRoot}/menu/getMenuList',
                parms: {
                    name: $('#txtName').val()
                },
                columns: [
                    {display: '菜单名称', id: 'code', name: 'name', width: '10%'},
                    {display: '排序', name: 'sort', width: '6%'},
                    {
                        display: '是否有效', name: 'valid', width: '8%', render: function (row) {
                        if (row.valid == "true") {
                            return "<span>有效</span>";
                        } else {
                            return "<span style='color:red'>无效</span>";
                        }
                    }
                    },
                    {display: 'url', name: 'url', width: '57%', align: "center"},
                    {
                        display: '操作', name: 'operator', width: '17%', render: function (row,rowid) {
                        var html = '<div class="m-inline-buttons" style="width:150px;">';
                        if (row.valid == 'true') {
                            html += '<a href="javascript:void(0)" onclick="javascript:menu.activity(\'' + row.id + '\',\'false\')">失效</a>';
                        } else {
                            html += '<a href="javascript:void(0)" onclick="javascript:menu.activity(\'' + row.id + '\',\'true\')">开启</a>';
                        }
                        html += "<a class=\"m-btn-edit\" onclick=\"menu.editorDialog('"+row.id+"')\"></a>";
                        html += "<a class=\"m-btn-delete\" onclick=\"menu.delete('"+rowid+"')\"></a>";
                        html += '</div>';
                        return html;
                    }
                    }
                ],
                onDblClickRow: function (row) {
                    me.editorDialog(row.id);
                },
                alternatingRow: false,
                rownumbers: false,
                usePager: false,
                scrollToPage: false,
                tree: {
                    columnId: 'code',
                    idField: 'id',
                    parentIDField: 'pid'
                }
            });
            me.bindEvents();

        },
        bindEvents: function () {
            var me = this;

            $('#div_new_record').click(function () {
                me.editorDialog();
            });
        },
        delete:function(rowid){
            var currentRow=this.grid.getRow(rowid);
            var childrenRows=this.grid.getChildren(currentRow, "deep");
            var ids=currentRow.id;
            for(var i=0;i<childrenRows.length;i++){
                ids+=","+childrenRows[i].id;
            }
            var message = "如果该菜单有子菜单也会被删除，是否删除？";

            jQuery.ligerDialog.confirm(message, function (confirm) {
                if (confirm)
                {
                    $.ajax({ //ajax处理
                        type: "POST",
                        url : "${contextRoot}/menu/deleteMenu",
                        dataType : "json",
                        data:{ids:ids},
                        cache:false,
                        success :function(data){
                            if(data.successFlg) {
                                $.ligerDialog.success(data.message);
                                menu.grid.reload();
                            }
                            else{
                                $.ligerDialog.error(data.message);
                            }
                        },
                        error :function(data){
                            $.ligerDialog.error("Status:"+data.status +"(" +data.statusText+")");
                        }
                    });
                }
            });
        },
        //刷新列表数据
        reloadGrid: function () {
            this.grid.set({
                parms: {name: $('#txtName').val()}
            });
            this.grid.reload();
        },
        //编辑弹窗
        editorDialog: function (id) {
            var me = this;
            var title = "新增菜单信息";
            var params = null;
            if (id != undefined && id != null) {
                title = "编辑菜单信息";
                params = {menuId: id};
            }

            me.dialog = $.ligerDialog.open({
                height: 420,
                width: 600,
                title: title,
                url: '${contextRoot}/menu/editorOrganization',
                //load: true,
                urlParms: params
            });
        },
        //设置有效无效
        activity: function (id, activityFlag) {
            var me = this;
            $.ajax({ //ajax处理
                type: "POST",
                url: '${contextRoot}/menu/activityMenu',
                dataType: "json",
                data: {orgId: id, activityFlag: activityFlag},
                cache: false,
                success: function (data) {
                    if (data.successFlg) {
                        me.grid.reload();
                    }
                    else {
                        $.ligerDialog.error(data.message);
                    }
                },
                error: function (data) {
                    $.ligerDialog.error("Status:" + data.status + "(" + data.statusText + ")");
                }
            });
        },
        //弹窗返回
        dialogSuccess: function (message) {
            $.ligerDialog.success(message);
            this.reloadGrid();
            this.dialog.close();
        }
    };

    $(function () {
        menu.init();
    });
</script>