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

    /* *************************** 模块初始化 ***************************** */
    var role = {
        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}/role/getRoleList',
                parms: {
                    name: $('#txtName').val()
                },
                columns: [
                    {display: '角色名称',id: 'name', name: 'name', width: '10%'},
                   // {display: '所属机构', name: 'name', width: '10%', align: "center"},
                    {display: '是否有效', name: 'valid', width: '8%', render: function (row) {
                        if (row.valid == "true") {
                            return "有效";
                        } else {
                            return "无效";
                        }
                    }},
                    {display: '备注', name: 'remark', width: '60%'},
                    {
                        display: '操作', name: 'operator', width: '22%', render: function (row) {
                        var html = '<div class="m-inline-buttons" style="width:150px;">';
                        if (row.valid == 'true') {
                            html += '<a href="javascript:void(0)" onclick="javascript:role.activity(\'' + row.id + '\',\'false\')">失效</a> ';
                        } else {
                            html += '<a href="javascript:void(0)" onclick="javascript:role.activity(\'' + row.id + '\',\'true\')">开启</a> ';
                        }
                        html += " <a class=\"m-btn-edit\" onclick=\"role.editorDialog('"+row.id+"')\"></a>";
                        html += "<a class=\"m-btn-delete\" onclick=\"role.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}/role/deleteRole",
                        dataType : "json",
                        data:{id:id},
                        cache:false,
                        success :function(data){
                            if(data.successFlg) {
                                $.ligerDialog.success(data.message);
                                role.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 = {roleId: id};
            }

            me.dialog = $.ligerDialog.open({
                height:400,
                width: 600,
                title: title,
                url: '${contextRoot}/role/editorRole',
                //load: true,
                urlParms: params
            });
        },
        anthorize:function(id){

        },
        //设置有效无效
        activity: function (id, activityFlag) {
            var me = this;
            $.ajax({ //ajax处理
                type: "POST",
                url: '${contextRoot}/role/activityRole',
                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 () {
        role.init();
    });
</script>