<%@ 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 year = pub.trimNull(request.getParameter("year"));
    String zb_type = pub.trimNull(request.getParameter("zb_type"));
    zb_type=java.net.URLDecoder.decode(zb_type,"UTF-8");
    String action = pub.trimNull(request.getParameter("action"));
    StringBuffer sql = new StringBuffer();
    List list = new ArrayList();
    sql.append("select t2.id,t2.user_name,t2.tel,t2.gh,t2.order_num from zb_role_user t1 join zb_bgs_user t2 on " +
            "t1.user_id=t2.id join zb_role t3 on t1.role_id=t3.id where t3.role_name=? order by t2.id desc ");
    list.add(zb_type);
    try {
        conn = com.censoft.db.DBUtil.getConnection();
        conn.setAutoCommit(false);
        if("update".equals(action)){
            String[] userids = request.getParameterValues("userids");
            String get_user_names="";
            if(userids!=null && userids.length>0) {
                for (int i = 0; i < userids.length; i++) {
                    get_user_names+=userids[i]+",";
                }
            }
            if(!"".equals(get_user_names)){
                get_user_names=get_user_names.substring(0,get_user_names.length()-1);
            }
            //System.out.println("get_user_names="+get_user_names+year+zb_type);
            boolean is_oper = cf.executeUpdate(conn, "update zb_user_gl_bgs set zb_user=? where year=? and zb_type=?",
                    new Object[]{get_user_names,year,zb_type});
            if (!is_oper) {
                conn.rollback();
                System.out.println("修改zb_user_gl表出错了" + cf.getMsg());
                return;
            }
            conn.commit();
        }
        v_list = cf.doQuery(conn, sql.toString(),  null, list.toArray());
        if (v_list == null) {
            out.println("查询用户出错");
            return;
        }
        v_list_day = cf.doQuery(conn, "select zb_user from zb_user_gl_bgs where year=? and zb_type=?", null, new Object[]{year,zb_type});
        if (v_list_day != null && v_list_day.size()>0) {
            Hashtable ht1= (Hashtable) v_list_day.get(0);
            user_names = pub.trimNull((String) ht1.get("zb_user"));
        }
%>
<div style="margin-top: 10px;">
    <a class="btn btn-primary radius" onclick="saveGroupForm();" >
        确定
    </a>
</div>
<div style="width:100%;height:90%;overflow-y: auto">
    <form action="selectUser-t.jsp" method="post" id="groupUser" name="groupUser">
        <input type="hidden" name="action" id="action" value="update">
        <input type="hidden" name="year" id="year" value="<%=year%>">
        <input type="hidden" name="zb_type" id="zb_type" value="<%=zb_type%>">
        <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_name = pub.trimNull((String) ht.get("user_name"));
                        String tel = pub.trimNull((String) ht.get("tel"));
                        String gh = pub.trimNull((String) ht.get("gh"));
            %>
            <tr class="text-c">
                <td><%=i+1%>
                </td>
                <td >
                    <input type="checkbox" class="info" id="userids" name="userids" value="<%=user_name%>"
                           <%if(user_names.indexOf(user_name)>=0){%>checked="checked"<%}%>/>
                </td>
                <td><%=user_name%>
                </td>
                <td><%=tel%>
                </td>
                <td><%=gh%>
                </td>
            </tr>
            <%
                    }
                }
            %>
            </tbody>
        </table>
    </form>
</div>
<script type="text/javascript">
    var checkAll = function (checkbox) {
        $(".info").prop("checked", checkbox.checked);
    }
    function saveGroupForm(){
        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: "./selectUser-t.jsp",
            data: $('#groupUser').serialize(),
            success: function (msg) {
                if (msg.indexOf("success") >= 0) {
                    layer.msg('操作成功!', {icon: 1, time: 1000},
                        function () {
                            //window.parent.location.reload();
                            window.parent.parent.location.reload();
                            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) {
            }
        }
    }
%>