<%--
  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="java.sql.Connection" %>
<%@ page import="java.util.*" %>
<%@ page import="com.censoft.common.db.ConnectionFactory" %>
<%@ page import="com.censoft.common.base.Pub" %>
<%@ page import="com.censoft.db.Logger" %>
<%
    ConnectionFactory cf = new ConnectionFactory();
    Pub pub = new Pub();
    Connection conn = null;
    Vector v_list = null;
    Vector v_list_day = null;
    String user_names ="";
    String login_id = (String) session.getAttribute("session_login_id");
    if ("".equals(login_id)) {
        System.out.println("当前没有用户信息");
    }
    String page_number = pub.trimNull(request.getParameter("page_number"));
    String role_id = pub.trimNull(request.getParameter("role_id"));
    if ("".equals(page_number)) page_number = "1";
    String page_size = pub.trimNull(request.getParameter("page_size"));
    if ("".equals(page_size)) page_size = "100";
    StringBuffer sql = new StringBuffer();
    List list = new ArrayList();
    sql.append("select * from zb_bgs_user where id not in( ");
    sql.append("select user_id from zb_role_user where role_id!=?)");
    sql.append(" order by id asc");
    list.add(role_id);
    try {
        conn = com.censoft.db.DBUtil.getConnection();
        v_list = cf.doQuery(conn, sql.toString(),  null, list.toArray());
        if (v_list == null) {
            out.println("查询用户出错");
            return;
        }
        v_list_day = cf.doQuery(conn, "select t1.user_name from zb_bgs_user t1 join zb_role_user t2 on t1.id=t2.user_id where t2.role_id=? order by t1.id desc", null, new Object[]{role_id});
        if (v_list_day != null && v_list_day.size()>0) {
            for(int i=0;i<v_list_day.size();i++){
                Hashtable ht1= (Hashtable) v_list_day.get(i);
                user_names += pub.trimNull((String) ht1.get("user_name"))+",";
            }
        }
        Hashtable ht_row = (Hashtable) v_list.get(0);
        String rowCnt = pub.trimNull((String) ht_row.get("ROWCOUNT"));
%>
<div style="margin-top: 10px;">
    <a class="btn btn-primary radius" onclick="saveGroupForm('<%=role_id%>');" >
        确定
    </a>
</div>
<form action="selectUser-t.jsp" method="post" id="groupUser" name="groupUser">
    <table class="table table-border table-bordered table-hover table-bg table-sort">
        <thead>
        <tr class="text-c">
            <th width="30">序号</th>
            <th width="30">
                <input id="checkboxAll" type="checkbox" onclick="checkAll(this);"></input>
            </th>
            <th width="100">人员姓名</th>
            <th width="100">联系电话</th>
            <th width="100">工号</th>
        </tr>
        </thead>
        <tbody id="groupUserInfo">
        <%
            Hashtable ht_1 =null;
            if (v_list != null && v_list.size() > 0) {
                for (int i = 0; i < v_list.size(); i++) {
                    Hashtable ht = (Hashtable) v_list.get(i);
                    String user_id = pub.trimNull((String) ht.get("id"));
                    String user_name = pub.trimNull((String) ht.get("user_name"));
                    String tel = pub.trimNull((String) ht.get("tel"));
                    String gh = pub.trimNull((String) ht.get("gh"));
                    String order_num = pub.trimNull((String) ht.get("order_num"));
        %>
        <tr class="text-c">
            <td><%=order_num%>
            </td>
            <td >
                <input type="checkbox" class="info" id="userids" name="userids" value="<%=user_id%>"
                       <%if(user_names.indexOf(user_name)>=0){%>checked="checked"<%}%>/>
            </td>
            <td><%=user_name%>
            </td>
            <td><%=tel%>
            </td>
            <td><%=gh%>
            </td>
        </tr>
        <%
                }
            }
        %>
        </tbody>
    </table>
</form>
<script type="text/javascript">
    var checkAll = function (checkbox) {
        $(".info").prop("checked", checkbox.checked);
    }
    function saveGroupForm(role_id){
        var isChecked=false;
        var us = document.getElementsByName("userids");
        for(var i=0;i<us.length;i++){
            if(us[i].checked){
                isChecked = true;
            }
        }
        if(!isChecked){
            layer.alert("请勾选用户",{icon:5,title:"提示"});
            return false;
        }
        $.ajax({
            type: "POST",
            url: "./action.jsp?action=addUser&role_id="+role_id,
            data: $('#groupUser').serialize(),
            success: function (msg) {
                if (msg.indexOf("success") >= 0) {
                    layer.msg('操作成功!', {icon: 1, time: 1000},
                        function () {
                            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>
<%
    } catch (Exception e) {
        out.println("查询用户信息异常" + e);
        return;
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
            }
        }
    }
%>