<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"  %>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <!-- 引入外部CSS文件 -->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/font.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/xadmin.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.css">

    <![endif]-->

    <script type="text/javascript">
        // 定义一个函数用于改变每页显示条数
        function changePageSize() {
            // 获取下拉框的值
            var pageSize = $("#changePageSize").val();
            // 向服务器发送请求，改变每页显示条数
            location.href = "${pageContext.request.contextPath}/student/findAll?page=1&size=" + pageSize;
        }

        // 绑定搜索按钮点击事件
        $("#serarch_btn").click(function () {
            var keyword = $("#keyword").val(); // 获取输入框中的关键字
            location.href = "${pageContext.request.contextPath}/student/findAll?page=1&size=5&keyword=" + keyword; // 向服务器发送请求，根据关键字搜索
        });

        // 绑定刷新按钮点击事件
        $("#refresh").click(function () {
            $("#myform").reset(); // 重置表单
            location.href = "${pageContext.request.contextPath}/student/findAll?page=1&size=5"; // 重新加载页面
        });
    </script>
</head>
<body>
<%-- 导航栏部分被注释掉了 --%>
<%--<div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="">首页</a>
            <a href="">演示</a>
            <a>
              <cite>导航元素</cite></a>
          </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" "location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>--%>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <!-- 搜索表单 -->
                    <form id="myform" class="layui-form layui-col-space5">
                        <div class="layui-inline layui-show-xs-block">
                            <input class="layui-input" type="text" autocomplete="off" placeholder="请输入关键字" name="keyword" id="keyword" value="${param.keyword}">
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" id="serarch_btn" lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
                        </div>
                        <div class="layui-inline layui-show-xs-block x-right">
                            <a class="layui-btn layui-btn-normal" href="${pageContext.request.contextPath}/student/findAll?page=1&size=5"><i class="layui-icon">&#xe669;</i>全部</a>
                        </div>
                    </form>
                </div>
                <xblock>
                    <!-- 添加学生按钮 -->
                    <a href="${pageContext.request.contextPath}/student/addStudent" class="layui-btn layui-btn-normal"><i class="layui-icon">&#xe654;</i>添加</a>
                    <!-- 导出信息按钮（功能未实现） -->
                    <a "exportInfo()" class="layui-btn layui-btn-warm" href=";"><i class="layui-icon">&#xe67c;</i>导出</a>
                    <!-- 显示数据总数 -->
                    <span class="x-right" style="line-height:40px">共有数据：${pageInfo.total} 条</span>
                </xblock>
                <div class="layui-card-body">
                    <!-- 表格展示学生信息 -->
                    <table class="layui-table layui-form">
                        <thead>
                        <tr style="text-align: center">
                            <th style="text-align: center">ID</th>
                            <th style="text-align: center">姓名</th>
                            <th style="text-align: center">性别</th>
                            <th style="text-align: center">学号</th>
                            <th style="text-align: center">班级</th>
                            <th style="text-align: center">联系方式</th>
                            <th style="text-align: center">宿舍号</th>
                            <th style="text-align: center">育人导师</th>
                            <th style="text-align: center">状态</th>
                            <c:if test="${sessionScope.adminInfo.power > 1}">
                            <th style="text-align: center">操作</th>
                            </c:if>
                        </thead>
                        <tbody>
                        <%
                            int j = 1; // 初始化行号计数器
                        %>
                        <!-- 遍历学生列表并生成表格行 -->
                        <c:forEach items="${pageInfo.list}" var="student">
                            <tr id="light" style="text-align: center">
                                <td><%=j++%></td> <!-- 显示行号 -->
                                <td>${student.name}</td> <!-- 显示学生姓名 -->
                                <td>${student.sex}</td> <!-- 显示学生性别 -->
                                <td>${student.sno}</td> <!-- 显示学生学号 -->
                                <td>${student.stu_class}</td> <!-- 显示学生班级 -->
                                <td>${student.phone}</td> <!-- 显示学生联系方式 -->
                                <td>${student.dorm_id}</td> <!-- 显示学生宿舍号 -->
                                <td>${student.teacher}</td> <!-- 显示学生育人导师 -->
                                <c:if test="${student.status == 1}">
                                    <!-- 显示已激活状态 -->
                                    <td><button class="layui-btn layui-btn-normal layui-btn-sm">已激活</button></td>
                                </c:if>
                                <c:if test="${student.status == 0}">
                                    <!-- 显示禁用状态 -->
                                    <td><button class="layui-btn layui-btn-danger layui-btn-sm">禁用</button></td>
                                </c:if>
                                <c:if test="${sessionScope.adminInfo.power > 1}">
                                    <td class="td-manage">
                                        <!-- 编辑按钮 -->
                                        <a title="编辑" href="${pageContext.request.contextPath}/student/editStudent?sno=${student.sno}">
                                            <i class="layui-icon">&#xe642;</i>
                                        </a>
                                        <!-- 删除按钮（功能未实现） -->
                                        <a title="删除" href=";" onclick="member_del(this,${student.sno},${sessionScope.adminInfo.power})">
                                            <i class="layui-icon">&#xe640;</i>
                                        </a>
                                    </td>
                                </c:if>
                            </tr>
                        </c:forEach>
                        </tbody>

                    </table>

                </div>
                <div class="pull-left">
                    <div class="form-group form-inline">
                        共&nbsp;${pageInfo.pages}&nbsp;页&emsp;当前页：${pageInfo.pageNum}&nbsp;/&nbsp;${pageInfo.pages}&emsp; 每页
                        <select class="form-control" id="changePageSize" onchange="changePageSize()">
                            <option value="1">${pageInfo.size}</option>
                            <option value="5">5</option>
                            <option value="10">10</option>
                            <option value="15">15</option>
                            <option value="20">20</option>
                        </select> 条
                    </div>
                </div>

                // 根据总页数和当前页码，动态设置分页导航的起始和结束页码
                <c:choose>
                    <c:when test="${pageInfo.pages < 5}">
                        <c:set var="begin" value="1"/>
                        <c:set var="end" value="${pageInfo.pages}"/>
                    </c:when>
                    <c:when test="${pageInfo.pageNum <= 3}">
                        <c:set var="begin" value="1"/>
                        <c:set var="end" value="5"/>
                    </c:when>
                    <c:when test="${pageInfo.pageNum > 3 and pageInfo.pageNum <= pageInfo.pages-2}">
                        <c:set var="begin" value="${pageInfo.pageNum - 2}"/>
                        <c:set var="end" value="${pageInfo.pageNum + 2}"/>
                    </c:when>
                    <c:otherwise>
                        <c:set var="begin" value="${pageInfo.pages - 4}"/>
                        <c:set var="end" value="${pageInfo.pages}"/>
                    </c:otherwise>
                </c:choose>

                // 分页导航按钮部分
                <div class="layui-card-body x-right" style="height: min-content">
                    <div class="page">
                        <div>
                            <a class="next" href="${pageContext.request.contextPath}/student/findAll?page=1&size=${pageInfo.pageSize}&keyword=${param.keyword}">首页</a>
                            <c:if test="${pageInfo.pageNum > 1}">
                                <a class="prev" href="${pageContext.request.contextPath}/student/findAll?page=${pageInfo.pageNum-1}&size=${pageInfo.pageSize}&keyword=${param.keyword}">上一页</a>
                            </c:if>
                            <c:forEach var="i" begin="${begin}" end="${end}" step="1">
                                <c:if test="${pageInfo.pageNum == i}">
                                    <span class="current">${i}</span>
                                </c:if>
                                <c:if test="${pageInfo.pageNum != i}">
                                    <a class="num" href="${pageContext.request.contextPath}/student/findAll?page=${i}&size=${pageInfo.pageSize}&keyword=${param.keyword}">${i}</a>
                                </c:if>
                            </c:forEach>
                            <c:if test="${pageInfo.pageNum < pageInfo.pages}">
                                <a class="next" href="${pageContext.request.contextPath}/student/findAll?page=${pageInfo.pageNum+1}&size=${pageInfo.pageSize}&keyword=${param.keyword}">下一页</a>
                            </c:if>
                            <a class="next" href="${pageContext.request.contextPath}/student/findAll?page=${pageInfo.pages}&size=${pageInfo.pageSize}&keyword=${param.keyword}">尾页</a></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    //删除操作
    function member_del(obj,sno,power){
        layer.confirm('确认要删除吗？',function(index){
            if (power < 3){
                layer.msg('对不起，您没有权限！');
                return false;
            }
            //发异步删除数据
            $.get("${pageContext.request.contextPath}/student/delete",{"sno":sno},function (data) {
                if(data){
                    layer.msg('删除成功!',{icon:1,time:2000});
                    setTimeout(function () {window.location.href='${pageContext.request.contextPath}/student/findAll';},2000);

                }else {
                    layer.msg('删除失败!',{icon:1,time:2000});
                    setTimeout(function () {window.location.href='${pageContext.request.contextPath}/student/findAll';},2000);
                }
            });
        });
    }
    //导出Excel操作
    function exportInfo() {
        if (${sessionScope.adminInfo.power < 3}) {
            layer.msg('对不起，您权限不足');
            return false;
        }
        layer.confirm('确定导出所有学生数据吗？',function (index) {
            location.href="${pageContext.request.contextPath}/student/export";
            layer.close(index);
        });
    }

</script>
</body>
</html>
