<%@ 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" %>
<%!
    public static String getRoleName(ConnectionFactory cf,Connection conn,String userId) {
        String role_name="";
        Vector userList = cf.doQuery(conn,"select t2.role_name from zb_role_user t1 join zb_role t2 on t1.role_id=t2.id where t1.user_id=? ",null,new Object[]{userId});
        if(userList!=null && userList.size()>0){
            Hashtable ht1 = (Hashtable) userList.get(0);
            role_name = (String) ht1.get("role_name");
        }
        return role_name;
    }
%>
<%
    ConnectionFactory cf = new ConnectionFactory();
    Pub pub = new Pub();
    Connection conn = null;
    Vector v_list = null;
    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"));
    if ("".equals(page_number)) page_number = "1";
    String page_size = pub.trimNull(request.getParameter("page_size"));
    if ("".equals(page_size)) page_size = "10";
    StringBuffer sql = new StringBuffer();
    List list = new ArrayList();
    //sql.append("select * from zb_bgs_user order by order_num asc ");
    sql.append("select t2.id,t2.user_name,t2.tel,t2.gh,t2.order_num from zb_bgs_user t2 left join zb_role_user t1 on " +
            "t1.user_id=t2.id  order by t1.role_id asc,t2.role_num asc ");
    try {
        conn = com.censoft.db.DBUtil.getConnection();
        v_list = cf.doPageQueryWithCount(conn, sql.toString(), page_number, page_size, null, list.toArray());
        if (v_list == null) {
            out.println("查询用户出错"+cf.getMsg());
            return;
        }

    Hashtable ht_row = (Hashtable) v_list.get(0);
    String rowCnt = pub.trimNull((String) ht_row.get("ROWCOUNT"));
    String pageCnt = pub.trimNull((String) ht_row.get("PAGECOUNT"));
%>
<div >
    <a class="btn btn-primary " onclick="addUser();">
        新建</a>
</div>
<table class="table table-border table-bordered table-hover table-bg table-sort">
    <thead>
    <tr class="text-c">
        <th width="30">序号</th>
        <th width="100">姓名</th>
        <th width="100">值班角色</th>
        <th width="50">电话号码</th>
        <th width="120">工号</th>
        <th width="140">操作</th>
    </tr>
    </thead>
    <tbody>
    <%
        if (v_list != null && v_list.size() > 0) {
            for (int i = 1; i < v_list.size(); i++) {
                Hashtable ht = (Hashtable) v_list.get(i);
                String user_id = pub.trimNull((String) ht.get("id"));
                String role_name=getRoleName(cf,conn,user_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"));
                String edit_url="./editUser-t.jsp?action=update&user_id="+user_id;
    %>
    <tr <%if(i%2==0){%>id="tr_doub"<%}else{%>id="tr_single"<%}%> class="text-c">
        <td><%=order_num%>
        </td>
        <td><%=user_name%>
        </td>
        <td><%=role_name%>
        </td>
        <td><%=tel%>
        </td>
        <td><%=gh%>
        </td>
        <td>
            <a  onclick="viewDeptOrUserInfo('人员信息','<%=edit_url%>','1000','500');" style="padding:4px 0px;font-size:12px;">
                编辑</a>
            <a onclick="deleteUser('<%=user_id%>');" style="padding:4px 0px;font-size:12px;">
                删除</a>
        </td>
    </tr>
    <%
            }
        }
    %>
    </tbody>
</table>
<jsp:include page="/common/page_ajax.jsp" >
    <jsp:param name="page_number" value="<%=page_number%>" />
    <jsp:param name="rowCnt" value="<%=rowCnt%>" />
    <jsp:param name="page_size" value="<%=page_size%>" />
</jsp:include>
<script type="text/javascript">
    function deleteUser(user_id){
      layer.confirm('确认要删除吗？', function (index) {
			$.ajax({
				type: "POST",
				url: "./action.jsp?user_id="+user_id+"&action=delete",
				success: function (msg) {
					if (msg.indexOf("success") >= 0) {
						layer.msg('操作成功!', {icon: 1, time: 1000},
							function () {
								searchModel();
							});
					} else {
						layer.msg('操作失败', {
							icon: 5,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						}, function () {
							// console.log("********************8")
						});
					}
				}
			});
		})
    }
    function optOrder(opt,user_id){
        $.ajax({
            type: "post",
            url: "opt_order.jsp",
            data:{"user_id":user_id,"opt":opt},
            success: function (data) {
                if (data.indexOf("success") >= 0) {
                    layer.msg('操作成功!', {icon: 1, time: 1000},
                        function () {
                            searchModel();
                        });
                } else {
                    layer.msg('操作失败，'+data, {
                        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) {
            }
        }
    }
%>