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

    /* *************************** 模块初始化 ***************************** */
    var user = {
        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}/user/getUserList',
                parms: {
                    name: $('#txtName').val()
                },
                columns: [
                    {display: '用户名',id: 'loginCode', name: 'loginCode', width: '20%'},
                    {display: '姓名', name: 'userName', width: '20%'},
                    {display: '角色名', name: 'roleName', width: '30%'},
                    {
                        display: '操作', name: 'operator', width: '30%', render: function (row) {
                        var html = '<div class="m-inline-buttons" style="width:250px;">';
                        if (row.activated == 'usable') {
                            html += ' <a href="javascript:void(0)" onclick="javascript:user.activity(\'' + row.id + '\',\'forbidden\')">禁用</a> ';
                        } else {
                            html += ' <a href="javascript:void(0)" onclick="javascript:user.activity(\'' + row.id + '\',\'usable\')">激活</a> ';
                        }
                        html += "<a class=\"m-btn-edit\" onclick=\"user.editorDialog('"+row.id+"')\"></a>";
                        html += "<a class=\"m-btn-delete\" onclick=\"user.delete('"+row.id+"')\"></a>";
                        html+= '<a href="javascript:void(0)" style="width:70px" onclick="javascript:user.restP(\'' + row.id + '\')">重置密码</a> ';
                        html+= '<a href="javascript:void(0)" style="width:70px" onclick="javascript:user.roleDistribute(\'' + row.id + '\')">角色分配</a> ';
                        html += '</div>';
                        return html;
                    }
                    }
                ],
                onDblClickRow: function (row) {
                    me.editorDialog(row.id);
                },
                alternatingRow: false
            });
            me.bindEvents();

        },
        roleDistribute:function(id){
            var me = this;
            var title = "角色分配";
            var params = {userId:id};

            me.dialog = $.ligerDialog.open({
                height:460,
                width: 600,
                title: title,
                url: '${contextRoot}/user/roleDistribute',
                //load: true,
                urlParms: params
            });
        },
        restP:function(id){


            var message = "是否重置改用户的密码？";

            jQuery.ligerDialog.confirm(message, function (confirm) {
                if (confirm)
                {
                    $.ajax({ //ajax处理
                        type: "POST",
                        url : "${contextRoot}/user/resetPassword",
                        dataType : "json",
                        data:{userid:id},
                        cache:false,
                        success :function(data){
                            if(data.successFlg) {
                                $.ligerDialog.success(data.message);
                                user.grid.reload();
                            }
                            else{
                                $.ligerDialog.error(data.message);
                            }
                        },
                        error :function(data){
                            $.ligerDialog.error("Status:"+data.status +"(" +data.statusText+")");
                        }
                    });
                }
            });
        },
        delete:function(id){

            var message = "是否删除用户？";

            jQuery.ligerDialog.confirm(message, function (confirm) {
                if (confirm)
                {
                    $.ajax({ //ajax处理
                        type: "POST",
                        url : "${contextRoot}/user/deleteUser",
                        dataType : "json",
                        data:{userid:id},
                        cache:false,
                        success :function(data){
                            if(data.successFlg) {
                                $.ligerDialog.success(data.message);
                                user.grid.reload();
                            }
                            else{
                                $.ligerDialog.error(data.message);
                            }
                        },
                        error :function(data){
                            $.ligerDialog.error("Status:"+data.status +"(" +data.statusText+")");
                        }
                    });
                }
            });
        },
        activity:function(id,flag){
            var me = this;
            $.ajax({ //ajax处理
                type: "POST",
                url: '${contextRoot}/user/activityUser',
                dataType: "json",
                data: {useid: id, flag: flag},
                cache: false,
                success: function (data) {
                    if (data.successFlg) {
                        user.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;
            var i="new";
            if (id != undefined && id != null) {
                title = "编辑用户";
                i="edit";
                params = {userId: id};
            }

            me.dialog = $.ligerDialog.open({
                height:i=="new"?550:460,
                width: 600,
                title: title,
                url: '${contextRoot}/user/editorUser',
                //load: true,
                urlParms: params
            });
        },
        //设置有效无效
        activity: function (id, activityFlag) {
            var me = this;
            $.ajax({ //ajax处理
                type: "POST",
                url: '${contextRoot}/user/activityUser',
                dataType: "json",
                data: {userid: id, flag: 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 () {
        user.init();
    });
</script>