<%@ CODEPAGE=65001 %>
<%
Response.Charset = "UTF-8"
Session.CodePage = 65001
%>
<%
' 数据库连接
Dim conn, dbPath
dbPath = Server.MapPath("data/Exam.asp")
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath & ";Persist Security Info=False;Locale Identifier=2052"
%>
<html>
<head>
<title>数据库升级 - 添加题目统计字段</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
body { font-family: Arial, sans-serif; padding: 20px; background: #f5f5f5; }
.container { max-width: 800px; margin: 0 auto; background: white; padding: 30px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
h2 { color: #333; border-bottom: 2px solid #007bff; padding-bottom: 10px; }
.log { margin: 10px 0; padding: 12px; background: #f8f9fa; border-radius: 5px; border-left: 4px solid #007bff; }
.success { color: #28a745; border-left-color: #28a745; background: #d4edda; }
.error { color: #dc3545; border-left-color: #dc3545; background: #f8d7da; }
.info { color: #17a2b8; border-left-color: #17a2b8; background: #d1ecf1; }
.warning { color: #ffc107; border-left-color: #ffc107; background: #fff3cd; }
.btn { display: inline-block; padding: 10px 20px; margin: 10px 5px; background: #007bff; color: white; text-decoration: none; border-radius: 5px; }
.btn:hover { background: #0056b3; }
.btn-success { background: #28a745; }
.btn-success:hover { background: #218838; }
</style>
</head>
<body>
<div class="container">
<h2>数据库升级工具</h2>

<%
On Error Resume Next

Response.Write "<div class='log info'><strong>数据库路径:</strong> " & dbPath & "</div>"

If Err.Number <> 0 Then
    Response.Write "<div class='log error'><strong>连接错误：</strong>" & Err.Description & "</div>"
    Response.End
End If

Dim tables, tableName
tables = Array("Choice", "MultiChoice", "Judge", "Blank", "Answer")

For Each tableName In tables
    Response.Write "<div class='log info'>正在处理 " & tableName & " 表...</div>"
    
    ' 添加解字段
    Err.Clear
    conn.Execute "ALTER TABLE " & tableName & " ADD COLUMN Analysis MEMO"
    If Err.Number <> 0 Then
        If InStr(Err.Description, "Duplicate") > 0 Or InStr(Err.Description, "重复") > 0 Then
            Response.Write "<div class='log warning'>  - Analysis 字段已存在，跳过</div>"
            Err.Clear
        Else
            Response.Write "<div class='log error'>  - 添加 Analysis 字段失败: " & Err.Description & "</div>"
            Err.Clear
        End If
    Else
        Response.Write "<div class='log success'>  - Analysis 字段添加成功</div>"
    End If
    
    ' 添加答题次数字段
    Err.Clear
    conn.Execute "ALTER TABLE " & tableName & " ADD COLUMN AnswerCount INT DEFAULT 0"
    If Err.Number <> 0 Then
        If InStr(Err.Description, "Duplicate") > 0 Or InStr(Err.Description, "重复") > 0 Then
            Response.Write "<div class='log warning'>  - AnswerCount 字段已存在，跳过</div>"
            Err.Clear
        Else
            Response.Write "<div class='log error'>  - 添加 AnswerCount 字段失败: " & Err.Description & "</div>"
            Err.Clear
        End If
    Else
        Response.Write "<div class='log success'>  - AnswerCount 字段添加成功</div>"
    End If
    
    ' 添加答对次数字段
    Err.Clear
    conn.Execute "ALTER TABLE " & tableName & " ADD COLUMN CorrectCount INT DEFAULT 0"
    If Err.Number <> 0 Then
        If InStr(Err.Description, "Duplicate") > 0 Or InStr(Err.Description, "重复") > 0 Then
            Response.Write "<div class='log warning'>  - CorrectCount 字段已存在，跳过</div>"
            Err.Clear
        Else
            Response.Write "<div class='log error'>  - 添加 CorrectCount 字段失败: " & Err.Description & "</div>"
            Err.Clear
        End If
    Else
        Response.Write "<div class='log success'>  - CorrectCount 字段添加成功</div>"
    End If
    
    ' 初始化现有数据的默认值
    Err.Clear
    conn.Execute "UPDATE " & tableName & " SET AnswerCount = 0 WHERE AnswerCount IS NULL"
    conn.Execute "UPDATE " & tableName & " SET CorrectCount = 0 WHERE CorrectCount IS NULL"
    If Err.Number = 0 Then
        Response.Write "<div class='log success'>  - 初始化默认值完成</div>"
    Else
        Err.Clear
    End If
    
Next

' 关闭连接
conn.Close
Set conn = Nothing
%>

<div class="log success">
<strong>数据库升级完成！</strong>
</div>

<div style="margin-top: 30px;">
<a href="admin/Main.asp" class="btn btn-success">返回后台管理</a>
</div>

</div>
</body>
</html>