<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/9/11
  Time: 12:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.censoft.common.base.Pub" %>
<%@ page import="com.censoft.common.db.ConnectionFactory" %>
<%@ page import="com.censoft.db.DBUtil" %>
<%@ page import="com.censoft.db.Logger" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.util.Hashtable" %>
<%@ page import="java.util.Vector" %>
<%
    ConnectionFactory cf = new ConnectionFactory();
    Pub pub = new Pub();
    Connection conn = null;
    Vector v_list = null;
    Vector v_roles = null;
    String action = pub.trimNull(request.getParameter("action"));
    String login_name = pub.trimNull(request.getParameter("login_name"));
    String login_id = pub.trimNull(request.getParameter("login_id"));
    String bz = pub.trimNull(request.getParameter("bz"));
    String status = pub.trimNull(request.getParameter("status"));
    String passwd = pub.trimNull(request.getParameter("passwd"));
    String user_level = pub.trimNull(request.getParameter("user_level"));
    String[] role_ids = request.getParameterValues("role_ids");
    String id = pub.trimNull(request.getParameter("id"));
    String dept_id = pub.trimNull(request.getParameter("dept_id"));
    Hashtable ht = new Hashtable();
    String have_roles = "";
    try {

        conn = com.censoft.db.DBUtil.getConnection();
        if ("add".equals(action)) {
            passwd = DBUtil.stringToMD5(passwd);
            id = DBUtil.getUUID();
            String add_time = pub.getCurrentServerTime();
            String add_user = (String) session.getAttribute("session_login_id");
            boolean is_up = cf.executeUpdate(conn, "insert into t_book_admin(id,login_name,login_id,passwd,bz,status,add_time,add_user,user_level,dept_id) values (?,?,?,?,?,?,?,?,?,?)", new Object[]{id, login_name, login_id, passwd, bz, status, add_time, add_user,user_level,dept_id});
            Logger.log(conn,"用户管理", "添加", id, login_name, request);
            is_up = cf.executeUpdate(conn, "delete from  t_book_user_role_rel  where login_id=?", new Object[]{login_id});
            if(role_ids!=null&&role_ids.length>0){
                for(int i=0;i<role_ids.length;i++) {
                    String role_id = pub.trimNull((String)role_ids[i]);
                    if(!"".equals(role_id)) {
                        is_up = cf.executeUpdate(conn, "insert into  t_book_user_role_rel(login_id,role_id) values (?,?)", new Object[]{login_id, role_id});
                    }
                }
            }
            if (!is_up) {
                out.println("error" + cf.getMsg());
                return;
            } else {
                out.println("success");
            }
            return;
        } else if ("update".equals(action)) {
            String update_time = pub.getCurrentServerTime();
            String update_user = (String) session.getAttribute("session_login_id");
            Vector v_user = cf.doQuery(conn,"select * from t_book_admin where id=?",null,new Object[]{id});
            if(v_user!=null&&v_user.size()<=0){
                out.println("用户不存在");
                return;
            }else{
                Hashtable ht_u = (Hashtable)v_user.get(0);
                login_id = pub.trimNull((String)ht_u.get("login_id"));
            }
            boolean is_up = cf.executeUpdate(conn, "update t_book_admin set  user_level=?,login_name=?,bz=?,status=?,update_time=?,update_user=? where id=?", new Object[]{user_level,login_name, bz, status, update_time, update_user, id});
            Logger.log(conn,"用户管理", "修改", id, login_name, request);
            is_up = cf.executeUpdate(conn, "delete from  t_book_user_role_rel  where login_id=?", new Object[]{login_id});
            if(role_ids!=null&&role_ids.length>0) {
                for (int i = 0; i < role_ids.length; i++) {
                    String role_id = pub.trimNull((String) role_ids[i]);
                    if (!"".equals(role_id)) {
                        is_up = cf.executeUpdate(conn, "insert into  t_book_user_role_rel(login_id,role_id) values (?,?)", new Object[]{login_id, role_id});
                    }
                }
            }
            if (!is_up) {
                out.println("error" + cf.getMsg());
                return;
            } else {
                out.println("success");
            }
            return;
        } else if ("delete".equals(action)) {
            boolean is_up = cf.executeUpdate(conn, "delete from  t_book_admin where id=?", new Object[]{id});
            Logger.log(conn,"用户管理", "删除", id, login_name, request);
            is_up = cf.executeUpdate(conn, "delete from  t_book_user_role_rel  where login_id=(select login_id from t_book_admin where id=?)", new Object[]{id});
            if (!is_up) {
                out.println("error" + cf.getMsg());
                return;
            } else {
                out.println("success");
            }
            return;
        }
        if (!"".equals(id)) {
            v_list = cf.doQuery(conn, "select * from t_book_admin where id=?", null, new Object[]{id});
            ht = (Hashtable) v_list.get(0);
            dept_id = pub.trimNull((String)ht.get("dept_id"));
            action = "update";
            v_roles = cf.doQuery(conn, "select * from t_book_user_role_rel where login_id=?", null, new Object[]{""+ht.get("login_id")});
            if(v_roles!=null&& v_roles.size()>0){
                have_roles = v_roles.toString();
            }
        } else {
            action = "add";
        }

        v_roles = cf.doQuery(conn, "select * from t_book_role order by add_time asc", null, new Object[]{});
    } catch (Exception e) {
        out.println("查询用户信息异常" + e);
        return;
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
            }
        }
    }
%>
<article class="page-container">
    <form class="form form-horizontal" id="form-admin-add">
        <input type="hidden" name="id" value="<%=id%>"/>
        <input type="hidden" name="dept_id" value="<%=dept_id%>"/>
        <input type="hidden" name="action" value="<%=action%>"/>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>姓名：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="<%=pub.trimNull((String)ht.get("login_name"))%>"
                       placeholder="" id="login_name" name="login_name">
            </div>
        </div>
        <%if ("add".equals(action)) {%>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>帐号：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="<%=pub.trimNull((String)ht.get("login_name"))%>"
                       placeholder="" id="login_id" name="login_id">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>初始密码：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="password" class="input-text" autocomplete="off"
                       value="<%=pub.trimNull((String)ht.get("passwd"))%>" placeholder="密码" id="passwd" name="passwd">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>确认密码：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="password" class="input-text" autocomplete="off"
                       value="<%=pub.trimNull((String)ht.get("passwd"))%>" placeholder="确认新密码" id="password"
                       name="password" onBlur="checkPwd()">
            </div>
        </div>
        <%}%>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">角色：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <%
                    if (v_roles != null && v_roles.size() > 0) {
                        for (int i = 0; i < v_roles.size(); i++) {
                            Hashtable ht_role = (Hashtable) v_roles.get(i);
                            String role_id = pub.trimNull((String) ht_role.get("id"));
                            String role_name = pub.trimNull((String) ht_role.get("role_name"));
                %>
				<input type="checkbox" name="role_ids" value="<%=role_id%>"
                        <%if(have_roles.indexOf(role_id)>0){%>checked<%}%>  /><%=role_name%>
                <%
                        }
                    }
                %>
			</div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>状态：</label>
            <div class="formControls col-xs-8 col-sm-9 skin-minimal">
                <div class="radio-box">
                    <input name="status" type="radio" id="sex-1" value="00"
                           <%if("00".equals(pub.trimNull((String)ht.get("status")))||"".equals(status)){%>checked<%}%>>
                    <label for="sex-1">启用</label>
                </div>
                <div class="radio-box">
                    <input name="status" type="radio" id="sex-2" name="sex" value="01"
                           <%if("01".equals(pub.trimNull((String)ht.get("status")))){%>checked<%}%>>
                    <label for="sex-2">禁用</label>
                </div>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>等级：</label>
            <div class="formControls col-xs-8 col-sm-9 skin-minimal">
                <div class="radio-box">
                    <input name="user_level" type="radio" id="user_level-1" value="普通"
                           <%if("普通".equals(pub.trimNull((String)ht.get("user_level")))||"".equals(status)){%>checked<%}%>>
                    <label for="sex-1">普通</label>
                </div>
                <div class="radio-box">
                    <input name="user_level" type="radio" id="user_level-2"  value="一般"
                           <%if("一般".equals(pub.trimNull((String)ht.get("user_level")))){%>checked<%}%>>
                    <label for="sex-2">一般</label>
                </div>
                <div class="radio-box">
                    <input name="user_level" type="radio" id="user_level-3"  value="重要"
                           <%if("重要".equals(pub.trimNull((String)ht.get("user_level")))){%>checked<%}%>>
                    <label for="sex-2">重要</label>
                </div>
                <div class="radio-box">
                    <input name="user_level" type="radio" id="user_level-4"  value="核心"
                           <%if("核心".equals(pub.trimNull((String)ht.get("user_level")))){%>checked<%}%>>
                    <label for="sex-2">核心</label>
                </div>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">备注：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <textarea name="bz" cols="" rows="2" class="textarea" placeholder="说点什么...100个字符以内"
                          dragonfly="true"><%=pub.trimNull((String) ht.get("bz"))%></textarea>
            </div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <input class="btn btn-primary radius" type="button" onclick="javascript:saveForm();"
                       value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
            </div>
        </div>
    </form>
</article>
<script>
    function checkPwd() {
        if($("#password").val()!=$("#passwd").val()){
            layer.msg('两次密码不一致!', {icon: 5, time: 2000},
                function () {
                });
            return false;
        }
    }
    function saveForm() {
        if($("#password").val()!=$("#passwd").val()){
            layer.msg('两次密码不一致!', {icon: 5, time: 2000},
                function () {
                });
            return false;
        }
        $.ajax({
            type: "POST",
            url: "./member_add-t.jsp",
            data: $('#form-admin-add').serialize(),
            success: function (msg) {
                if (msg.indexOf("success") >= 0) {
                    layer.msg('操作成功!', {icon: 1, time: 1000},
                        function () {
                            parent.searchModel();
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        });
                } else {
                    layer.msg('操作失败', {
                        icon: 5,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function () {
                        // console.log("********************8")
                    });
                }
            }
        });
    }
</script>