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

<!DOCTYPE html>
<html>
<head>
    <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="../../Scripts/json2.js" type="text/javascript"></script>
    <script src="../../js/myLib/jsHelper_Form.js" type="text/javascript"></script>
    <script src="../../js/myLib/helper.js" type="text/javascript"></script>
    <style>
        #Role div,#UserRole div{ padding-left:25px; width:320px}
    </style>
</head>
<body>
    <form id="billForm" name="billForm">
        <input type="hidden" id="uID" name="uID" runat="server" />
        <fieldset>
            <legend>用户信息</legend>
            <table class="pt-edit">
                <tr>
                    <th>帐号</th>
                    <td><input type="text" id="uAccount" name="uAccount" class="pt-readonly" runat="server" error="帐号" checkexpression="NotNull" /></td>
                    <th>密码</th>
                    <td><input type="text" id="uPwd" name="uPwd" runat="server" error="密码" checkexpression="NotNull" /></td>
                </tr>
                <tr>
                    <th>用户名</th>
                    <td><input type="text" id="uName" name="uName" class="pt-readonly" runat="server" error="用户名" checkexpression="NotNull" /></td>
                    <th>备注</th>
                    <td><input type="text" id="uRemark" name="uRemark" runat="server" /></td>
                </tr>
            </table>
        </fieldset>
    </form>
    <fieldset>
        <legend>角色编辑</legend>
        <table>
            <tr>
                <td style="width:50%">系统角色</td>
                <td style="padding-left:3px">用户所属角色</td>
            </tr>
            <tr>
                <td>
                    <div id="Role" class="pt-tree" style="border:1px solid rgb(107,172,255); height:330px">
                        
                    </div>
                </td>
                <td style="padding-left:3px">
                    <div id="UserRole" class="pt-tree" style="border:1px solid rgb(107,172,255);height:330px">
                    
                    </div>
                </td>
            </tr>
            <tr>
                <td class="pt-tip">*双击加入【用户所属角色】</td>
                <td class="pt-tip">*双击移除【用户所属角色】</td>
            </tr>
        </table>
    </fieldset>
    <div class="pt-padding" style="text-align:center">
        <input type="button" id="btnSave" class="pt-button active" value="保存" />
        <input type="button" class="pt-button active" onclick="top.OpenClose('User_Form')" value="关闭" />
    </div>
    <script>
        (function() {
            var dataSplit = 'Θ ☺ ☻'.split(' '),
            url = "UserForm.aspx",
            key = "<%=key %>";

            function init() {
                bindRole();
                readonly();
                sysRoleClick();
                userRoleClick();
                bindEvents();
            }
            function readonly() {
                $(".pt-readonly").focus(function() {
                    this.blur();
                });
            }
            function bindRole() {
                $.get(url, { action: "bindRole", key: key }, function(data) {
                    data = data.split(dataSplit[0]);
                    $("#Role").html(data[0]);
                    $("#UserRole").html(data[1]);
                });
            }
            function sysRoleClick() {
                $("#Role div").live({
                    click: function() {
                        removeHighlight();
                        $(this).addClass("pt-highlight");
                        return false;
                    },
                    dblclick: function() {
                        $("#UserRole").append($(this).clone());
                        $(this).remove();
                        return false;
                    }
                });
            }
            function userRoleClick() {
                $("#UserRole div").live({
                    click: function() {
                        removeHighlight();
                        $(this).addClass("pt-highlight");
                        return false;
                    },
                    dblclick: function() {
                        $("#Role").append($(this).clone());
                        $(this).remove();
                        return false;
                    }
                });
            }
            function bindEvents() {
                $("#btnSave").click(function() {
                    save();
                });
            }
            function save() {
                if (!JudgeValidate1()) return;
                var result = false;
                $.ajax({
                    url: url,
                    data: $.parseJSON(form2JsonString(billForm, { action: "save", roles: getUserRoles() })),
                    async: false,
                    success: function(data) {
                        result = parseInt(data, 10);
                    }
                });
                if (result == 100) {
                    top.showTipsMsg("帐号已经存在!", 1, 4000);
                    return;
                } 
                if (result > 0) {
                    top.main.location.reload();
                    top.showTipsMsg('操作成功!', 0, 3000);
                    top.OpenClose('User_Form'); //如果写在ajax的success里面报错
                } else {
                    top.showTipsMsg("操作失败，请稍后重试", 1, 4000);
                }
            }
            function getUserRoles() {
                var role = [];
                $("#UserRole div").each(function() {
                    role.push($(this).attr("data"));
                });
                return role.join(dataSplit[0]);
            }
            function removeHighlight() {
                $(".pt-highlight").removeClass("pt-highlight");
            }
            init();
        })();
    </script>
</body>
</html>
