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

    /* *************************** 模块初始化 ***************************** */
    var menuAction = {
        grid: null,
        dialog: null,
        init: function () {
            this.menuLeftInit();
            var me = this;
            $('.m-retrieve-area').show();
            $("#txtName").ligerSearch({
                onClick:function(value){
                    me.reloadGrid();
                }});
            $("#div_wrapper_left_inp_search").ligerSearch({
                width:180,
                onClick:function(value){
                    $.ligerui.get('menu_tree').queryByText(value);
                }});
            this.grid = $("#div_grid").ligerGrid({
                url: '${contextRoot}/menu/menuAction/getMenuActionList',
                columns: [
                    {display: '菜单按钮名称', id: 'name', name: 'name', width: '10%'},
                    {display: '菜单按钮url', name: 'url', width: '60%'},
                    {
                        display: '是否有效', name: 'flag', width: '8%', render: function (row) {
                        if (row.flag == "true") {
                            return "有效";
                        } else {
                            return "无效";
                        }
                    }
                    },
                    {
                        display: '操作', name: 'operator', width: '17%', render: function (row) {
                        var html = '<div class="m-inline-buttons" style="width:150px;">';
                       if (row.flag == 'true') {
                            html += '<a href="javascript:void(0)" onclick="javascript:menuAction.activity(\'' + row.id + '\',\'false\')">失效</a> ';
                        } else {
                            html += '<a href="javascript:void(0)" onclick="javascript:menuAction.activity(\'' + row.id + '\',\'true\')">开启</a> ';
                        }

                        html += "<a class=\"m-btn-edit\" onclick=\"menuAction.editorDialog('"+row.id+"')\"></a>";
                        html += "<a class=\"m-btn-delete\" onclick=\"menuAction.delete('"+row.id+"')\"></a>";
                        html += '</div>';
                        return html;
                    }
                    }
                ],
                onDblClickRow: function (row) {
                    me.editorDialog(row.id);
                }
            });
            me.bindEvents();
        },
        delete:function(id){
            var message = "是否删除菜单按钮？";

            jQuery.ligerDialog.confirm(message, function (confirm) {
                if (confirm)
                {
                    $.ajax({ //ajax处理
                        type: "POST",
                        url : "${contextRoot}/menu/menuAction/deleteMenuAction",
                        dataType : "json",
                        data:{id:id},
                        cache:false,
                        success :function(data){
                            if(data.successFlg) {
                                $.ligerDialog.success(data.message);
                                menuAction.grid.reload();
                            }
                            else{
                                $.ligerDialog.error(data.message);
                            }
                        },
                        error :function(data){
                            $.ligerDialog.error("Status:"+data.status +"(" +data.statusText+")");
                        }
                    });
                }
            });
        },
        bindEvents: function () {
            var me = this;

            $('#div_new_record').click(function () {
                me.editorDialog();
            });
        },
        //刷新列表数据
        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: 300,
                width: 600,
                title: title,
                url: '${contextRoot}/menu/menuAction/editorMenuAction',
                //load: true,
                urlParms: params
            });
        },
        //设置有效无效
        activity: function (id, activityFlag) {
            var me = this;
            $.ajax({ //ajax处理
                type: "POST",
                url: '${contextRoot}/menu/menuAction/activityMenuAction',
                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();
        },
        menuLeftInit: function menuLeftInit() {
            $menulayoutRight = $("#div_wrapper").ligerLayout({
                height: "99%",
                leftWidth: 200,
                isLeftCollapse: false,//左边区域初始化不可以隐藏
                allowLeftCollapse: false//左边区域不可以隐藏
            });
            $("#div_wrapper .l-layout-left").css({"border": "0px"});
            $("#div_wrapper .l-layout-right").css({"border": "0px"});
            $("#div_wrapper .l-layout-center").css({"border": "0px"});
            $("#div_wrapper .l-layout-top").css({"border": "0px"});
            $("#div_wrapper .l-layout-left .l-layout-header").hide();



            $.ajax({
                type: "POST",
                url: "${contextRoot}/menu/getMenuList",
                success: function (msg) {
                    $('#menu_tree').ligerTree({
                        data: msg.detailModelList,
                        checkbox: false,
                        nodeWidth: 200,
                        idFieldName: 'id',
                        textFieldName: 'name',
                        parentIDFieldName: 'pid',
                        isExpand: false,
                        onClick: function (obj) {
                            menuAction.grid.loadServerData({
                                "menuId": obj.data.id,
                                "page": 1,
                                "rows": 10
                            });
                        },
                        onSuccess: function (data) {

                        }
                    });
                    //修改样式
                   // $("#menu_tree span").css({"line-height": "2", "font-weight": "normal"});
                }
            });
        }
    };

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