<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*, org.json.JSONArray, org.json.JSONObject" %>
<%
    String college = request.getParameter("college");
    String teacher = request.getParameter("teacher");
    String course = request.getParameter("course");

    if (college == null || teacher == null || course == null) {
        response.sendRedirect("evaluation.jsp");
        return;
    }

    JSONArray dimensions = new JSONArray();
    int totalQuestionCount = 0;
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "SchoolST", "123456");

        String sql = "SELECT d.dimension_id, d.name AS dimension_name, d.weight, q.question_id, q.question_text " +
                "FROM dimensions d " +
                "JOIN questions q ON d.dimension_id = q.dimension_id " +
                "ORDER BY d.dimension_id, q.question_id";
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);

        JSONObject currentDimension = null;
        int lastDimensionId = -1;

        while (rs.next()) {
            int dimensionId = rs.getInt("dimension_id");
            if (dimensionId != lastDimensionId) {
                if (currentDimension != null) {
                    dimensions.put(currentDimension);
                }
                currentDimension = new JSONObject();
                currentDimension.put("dimension_id", dimensionId);
                currentDimension.put("dimension_name", rs.getString("dimension_name"));
                currentDimension.put("weight", rs.getFloat("weight"));
                currentDimension.put("questions", new JSONArray());
                lastDimensionId = dimensionId;
            }

            JSONObject question = new JSONObject();
            question.put("question_id", rs.getInt("question_id"));
            question.put("question_text", rs.getString("question_text"));
            currentDimension.getJSONArray("questions").put(question);

            totalQuestionCount++;
        }

        if (currentDimension != null) {
            dimensions.put(currentDimension);
        }

        rs.close();
        stmt.close();
        conn.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
%>
<!DOCTYPE html>
<html>
<head>
    <title>评教页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            margin: 0;
            padding: 20px;
        }
        h1 {
            text-align: center;
            color: #333;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
            background-color: #fff;
            box-shadow: 0 2px 3px rgba(0,0,0,0.1);
        }
        th, td {
            padding: 12px;
            border: 1px solid #ddd;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        .dimension-title {
            font-size: 18px;
            font-weight: bold;
            margin-top: 20px;
            margin-bottom: 10px;
            color: #333;
        }
        .question-text {
            font-weight: normal;
        }
        .form-container {
            max-width: 800px;
            margin: 0 auto;
        }
        button {
            display: block;
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
<div class="form-container">
    <h1>评教页面</h1>
    <form action="submitEvaluation.jsp" method="post" onsubmit="return validateForm()">
        <input type="hidden" name="college" value="<%= college %>">
        <input type="hidden" name="teacher" value="<%= teacher %>">
        <input type="hidden" name="course" value="<%= course %>">

        <%
            for (int i = 0; i < dimensions.length(); i++) {
                JSONObject dimension = dimensions.getJSONObject(i);
                out.println("<div class='dimension-title'>" + dimension.getString("dimension_name") + "（权重：" + dimension.getFloat("weight") + "%）</div>");
                out.println("<table>");
                out.println("<thead><tr><th>题目</th><th>符合</th><th>不符合</th></tr></thead>");
                out.println("<tbody>");

                JSONArray questions = dimension.getJSONArray("questions");
                for (int j = 0; j < questions.length(); j++) {
                    JSONObject question = questions.getJSONObject(j);
                    out.println("<tr>");
                    out.println("<td class='question-text'>" + question.getString("question_text") + "</td>");
                    out.println("<td><input type='radio' name='question_" + question.getInt("question_id") + "' value='1' required></td>");
                    out.println("<td><input type='radio' name='question_" + question.getInt("question_id") + "' value='0' required></td>");
                    out.println("</tr>");
                }

                out.println("</tbody></table>");
            }
        %>

        <button type="submit">提交评价</button>
    </form>
</div>

<script>
    const totalQuestionCount = <%= totalQuestionCount %>;

    function validateForm() {
        const allQuestions = document.querySelectorAll("input[type='radio']");
        const answered = new Set();

        allQuestions.forEach(input => {
            if (input.checked) {
                const questionId = input.name.replace("question_", "");
                answered.add(questionId);
            }
        });

        if (answered.size !== totalQuestionCount) {
            alert("请完成所有题目的评价后再提交！");
            return false;
        }
        return true;
    }
</script>
</body>
</html>