<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserList.aspx.cs" Inherits="Medical.MSystem.SysUser.UserList" %>

<%@ Register Src="../../Controls/Pager.ascx" TagName="Pager" TagPrefix="ucPager" %>
<!DOCTYPE html>
<html>
<head runat="server">
    <title></title>
    <link href="../../css/style.css" rel="stylesheet" type="text/css" />
    <script src="../../Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script src="../../js/myLib/jsHelper_Form.js" type="text/javascript"></script>
    <script src="../../js/myLib/Pager.js" type="text/javascript"></script>
    <script src="../../js/myLib/Grid.js" type="text/javascript"></script>
    <style>
        #Role div{ padding-left:25px; width:200px}
        .roleTitle{ font-size:14px; font-weight:bold;padding-left:15px}
    </style>
</head>
<body>
    <table>
        <tr>
            <td style="width:250px; ">
                <div class="pt-panel" style="height:512px">
                    <strong>角色管理</strong>
                    <div class="pt-padding" style="text-align:center">
                        <input type="button" class="pt-button active" id="btnAddRole" value="新增角色" />   
                        <input type="button" class="pt-button active" id="btnAuthorization" value="分配权限" /> 
                    </div>
                    <div class="pt-padding" style="text-align:center">
                        <input type="button" class="pt-button active" id="btnEditRole" value="编辑" />   
                        <input type="button" class="pt-button active" id="btnDeleteRole" value="删除" />
                    </div>
                    <div class="pt-tree" id="Role">
                        <div class="roleTitle">角色名称</div>
                        <%=GetRole() %>
                    </div>
                </div>
            </td>
            <td>
                <div style="margin-left:3px">
                    <div class="pt-panel">
                        <strong>用户管理</strong>
                        <form id="fCondition" name="fCondition" class="pt-padding">
                            <div class="pt-padding">
<%--                                <input type="button" class="pt-button active" id="btnAdd" value="新增" /> --%>  
                                <input type="button" class="pt-button active" id="btnQuery" value="查询" /> 
                            </div>
                            <dl>
                                <dt>帐号</dt>
                                <dd><input type="text" name="account" /></dd>
                            </dl>
                            <dl>
                                <dt>用户名称</dt>
                                <dd><input type="text" name="name" /></dd>
                            </dl>
                        </form>
                    </div>
                    <div class="pt-panel pt-below">
                        <strong>查询一览</strong>
                        <div class="pt-grid_frame">
                            <table class="pt-grid" id="grid">
                                <thead>
                                    <tr>
                                        <th style="width:130px">操作</th>
                                        <th>帐号</th>
                                        <th>密码</th>
                                        <th>用户名</th>
                                        <th>科室</th>
                                    </tr>
                                </thead>
                                <tbody></tbody>
                            </table>
                        </div>
                        <ucPager:Pager ID="gridPager" runat="server" />
                    </div>
                </div>
            </td>
        </tr>
    </table>
    <script>
        (function() {
            var url = "UserList.aspx",
            roleDialogUrl = "/MSystem/SysUser/RoleForm.aspx",
            roleDialogFormName = "Role_Form",
            authorizationDialogUrl = "/MSystem/SysUser/AuthorizationForm.aspx",
            authorizationDialogFormName = "Authorization_Form",
            userDialogUrl = "/MSystem/SysUser/UserForm.aspx",
            userDialogFormName = "User_Form",

            grid = new Grid({ id: "grid", url: url, pager: new Pager("gridPager"), bindFun: bindData }),
            currentRoleID = "";
            currentRoleName = "";

            grid.getCondition = function() {
                var parms = { action: "query", searchFlag: grid.searchFlag };
                if (grid.pager) {
                    parms = { action: "query", searchFlag: grid.searchFlag, pageSize: grid.pager.pageSize, pageIndex: grid.pager.pageIndex };
                }
                if (currentRoleID != "") {
                    if (grid.pager) {
                        parms = { action: "query", searchFlag: grid.searchFlag, pageSize: grid.pager.pageSize, pageIndex: grid.pager.pageIndex, roleID: currentRoleID };
                    } else {
                        parms = { action: "query", searchFlag: grid.searchFlag, roleID: currentRoleID };
                    }
                }
                if ($("[name='fCondition']").length > 0) {
                    return $.parseJSON(form2JsonString(fCondition, parms));
                } else {
                    return parms;
                }
            };
            function init() {
                grid.search();
                bindEvents();
                roleEvents();
            }
            function roleEvents() {
                $("#Role div").click(function() {
                    currentRoleID = $(this).attr("data");
                    currentRoleName = $(this).html();
                    if ($(this).html() == "角色名称") {
                        currentRoleID = "";
                    }
                    removeHighlight();
                    $(this).addClass("pt-highlight");
                    grid.search(true);
                });
                $("#btnAddRole").click(function() {
                    top.openDialog(roleDialogUrl, roleDialogFormName, '角色 - 添加', 700, 100, 50, 50);
                });
                $("#btnEditRole").click(function() {
                    if (currentRoleID == "") {
                        top.showTipsMsg('请选择要编辑的角色!', 1, 5000);
                        return;
                    }
                    top.openDialog(roleDialogUrl + "?key=" + currentRoleID, roleDialogFormName, '角色 - 编辑', 700, 100, 50, 50);
                });
                $("#btnAuthorization").click(function() {
                    if (currentRoleID == "") {
                        top.showTipsMsg('请选择要授权的角色!', 1, 5000);
                        return;
                    }
                    top.openDialog(authorizationDialogUrl + "?key=" + currentRoleID, authorizationDialogFormName, '分配权限 - 授权', 700, 500, 50, 50);
                });

                $("#btnDeleteRole").click(function() {
                    if (currentRoleID == "") {
                        top.showTipsMsg('请选择要删除的角色!', 1, 5000);
                        return;
                    }
                    top.showConfirmMsg("要删除角色名称为[" + currentRoleName + "]的角色信息吗?", function(r) {
                    if (r) {
                            $.get(url, { action: "removeRole", id: currentRoleID }, function(data) {
                                if (data == "200") {
                                    top.showTipsMsg('删除成功!', 0, 5000);
                                    location.reload();
                                } else if (data == "100") {
                                    top.showTipsMsg('操作失败:您要删除的角色信息存在用户!', 1, 5000);
                                } else {
                                    top.showTipsMsg('删除失败!', 1, 5000);
                                }
                            });
                        }
                    });
                });
            }

            function removeHighlight() {
                $("[class*='pt-highlight']").removeClass("pt-highlight");
            }
            function bindEvents() {
                $("#btnAdd").click(function() {
                    top.openDialog(userDialogUrl, userDialogFormName, '用户 - 添加', 800, 550, 50, 50);
                });
                $("#btnQuery").click(function() {
                    grid.search(true);
                });
                grid.control.click(function(e) {
                    var target = $(e.target);
                    if (target.is("input")) {
                        var data = target.parent().parent().data("data");
                        switch (target.val()) {
                            case "修改":
                                top.openDialog(userDialogUrl + "?key=" + data.uID, userDialogFormName, '用户 - 编辑', 800, 550, 50, 50);
                                break;
                            case "删除":
                                remove(data);
                                break;
                        }
                    }
                });
            }
            function remove(data) {
                top.showConfirmMsg("确定要删除[用户名称]为[" + data.uName + "]的用户信息吗?", function(r) {
                    if (r) {
                        $.get(url, { action: "remove", id: data.uID }, function(data) {
                            if (data == "success") {
                                top.showTipsMsg('删除成功!', 0, 5000);
                                grid.search();
                            } else {
                                top.showTipsMsg('删除失败!', 1, 5000);
                            }
                        });
                    }
                });
            }
            function bindData(tr, data) {
                tr.append($("<td>").html("<input type='button' class='pt-button active' value='修改' /><input type='button' class='pt-button active' value='删除' />"))
                .append($("<td>").html(data.uAccount))
                .append($("<td>").html(data.uPwd))
                .append($("<td>").html(data.uName))
                .append($("<td>").html(data.uAttribute));
            }
            init();
        })();
    </script>
</body>
</html>
