<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8" %>
<%@include file="/WEB-INF/ehr/commons/jsp/commonInclude.jsp" %>
<script>
    var roleDistribute = {
        roleId:'',
        roleGrid:'',
        userId:"${userId}",
        roleId:"${roleId}",
        roleDistributeGrid:'',
        init:function(){
            var me = this;

            $("#btnSave").click(function () {
                var roleId = "",roleData = me.roleDistributeGrid.getData();

                if(roleData && roleData.length > 0)
                {
                    for(var i =0;i < roleData.length;i++)
                    {
                        roleId += (roleId == ""?"":",") + roleData[i].roleId;
                    }
                }

                $.ajax({ //ajax处理
                    type: "POST",
                    url: "/user/userRoleDistribute",
                    dataType: "json",
                    data: {userId:me.userId,roleId:roleId},
                    cache: false,
                    success: function (data) {
                        if (data.successFlg) {
                            parent.user.dialogSuccess(data.message);
                        }
                        else {
                            $.ligerDialog.error(data.message);
                        }
                    },
                    error: function (data) {
                        $.ligerDialog.error("Status:" + data.status + "(" + data.statusText + ")");
                    }
                });
            });

            $("#btnCancel").click(function () {
                parent.user.dialog.close();
            });

            $("#btnReset").click(function () {
                me.roleDistributeGrid.set("data",[])
            });

            $("#layoutPanel").ligerLayout({
                leftWidth:350
                ,centerWidth:230
            })

            me.roleDistributeGrid = $("#roleDistributeGrid").ligerGrid({
                columns:[
                    {display:"已分配角色",name:"roleName",align:"center"}
                ]
                ,url:""
                ,width:225
                ,height:300
                ,usePager:false
                ,onDblClickRow :function(data,rowid,rowdata){
                    me.roleDistributeGrid.deleteRow(rowid);
                }
            })

            me.roleGrid = $("#roleGrid").ligerGrid({
                columns:[
                    {display:"角色",name:"name",align:"center",width:120},
                    {display:"说明",name:"remark",align:"center",width:175}
                ]
                ,url:"/role/getRoleList"
                ,height:300
                ,usePager:false
                ,onDblClickRow :function(data,rowid,rowdata){
                    var roleDistriData = me.roleDistributeGrid.getData();
                    var isExist  = false;

                    for(var i = 0;i < roleDistriData.length;i++)
                    {
                        if( roleDistriData[i].roleId == data.id)
                        {
                            isExist = true;
                            break;;
                        }
                    }

                    if(isExist)
                        return;

                    me.roleDistributeGrid.addRow({roleId:data.id,roleName:data.name});
                }
                ,onSuccess:function(data,grid)
                {
                    if(me.roleId && data)
                    {
                        var roleArray = me.roleId.split(",");
                        var roleData = data.detailModelList, roleDisData = [];
                        for(var i =0;i < roleArray.length;i++)
                        {
                            for(var j =0; j < roleData.length;j++)
                            {
                                if(roleData[j].id == roleArray[i])
                                {
                                    roleDisData.push({roleId:roleData[j].id,roleName:roleData[j].name});
                                }
                            }
                        }

                        me.roleDistributeGrid.addRows(roleDisData);
                    }

                    return true;
                }
            })
        }
    }

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