#!/bin/bash

# =============================================
# 测试脚本：验证评分面板修复
# =============================================

echo "=========================================="
echo " 验证评分面板修复"
echo "=========================================="
echo ""

# 颜色定义
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'

echo -e "${BLUE}步骤 1: 清理缓存${NC}"
echo "执行: php artisan view:clear"
php artisan view:clear
if [ $? -eq 0 ]; then
    echo -e "${GREEN}✅ 视图缓存清理成功${NC}"
else
    echo "❌ 清理失败"
fi
echo ""

echo -e "${BLUE}步骤 2: 验证文件修改${NC}"

# 检查 GradingPanel.php
if grep -q "public function mount" app/Livewire/UploadExam/GradingPanel.php; then
    echo -e "${GREEN}✅ GradingPanel.php - mount() 方法已添加${NC}"
else
    echo "❌ GradingPanel.php - mount() 方法未找到"
fi

if grep -q "updatedSelectedPaperId" app/Livewire/UploadExam/GradingPanel.php; then
    echo -e "${GREEN}✅ GradingPanel.php - updatedSelectedPaperId() 方法已添加${NC}"
else
    echo "❌ GradingPanel.php - updatedSelectedPaperId() 方法未找到"
fi

# 检查视图文件
if grep -q ":selectedPaperId" resources/views/filament/pages/upload-exam-paper.blade.php; then
    echo -e "${GREEN}✅ upload-exam-paper.blade.php - selectedPaperId 参数已传递${NC}"
else
    echo "❌ upload-exam-paper.blade.php - selectedPaperId 参数未传递"
fi

echo ""

echo -e "${BLUE}步骤 3: 验证数据库数据${NC}"
echo "正在查询吴同学的试卷数据..."

# 使用 tinker 验证数据（如果可用）
php artisan tinker --execute="
\$paper = \App\Models\Paper::where('paper_name', 'like', '%吴%')->latest()->first();
if (\$paper) {
    \$count = \$paper->questions()->count();
    echo \"✅ 找到试卷: \" . \$paper->paper_name . \"\\n\";
    echo \"✅ 题目数量: \" . \$count . \" 道题\\n\";
    if (\$count > 0) {
        echo \"✅ 数据完整性验证通过\\n\";
    } else {
        echo \"⚠️  警告: 试卷存在但没有题目数据\\n\";
    }
} else {
    echo \"❌ 未找到吴同学的试卷\\n\";
}
" 2>/dev/null

echo ""

echo -e "${BLUE}步骤 4: 手动测试指南${NC}"
echo "请按照以下步骤手动验证修复："
echo ""
echo "1. 打开浏览器访问: http://fa.test/admin/upload-exam-paper"
echo ""
echo "2. 点击【选择已有试卷评分】按钮"
echo ""
echo "3. 选择老师（例如：默认老师）"
echo ""
echo "4. 选择学生（例如：吴同学）"
echo ""
echo "5. 选择最新的一份试卷"
echo ""
echo "6. 检查评分面板："
echo "   ✅ 应该显示试卷的题目列表"
echo "   ❌ 不应显示'暂无题目数据'"
echo ""
echo "7. 验证题目信息："
echo "   - 每道题应显示题目内容"
echo "   - 显示题目类型和分值"
echo "   - 如果有题库ID，应显示参考答案"
echo ""

echo "=========================================="
echo " 验证完成！"
echo "=========================================="
echo ""
echo "如果仍有问题，请检查："
echo "  1. Laravel Herd 是否正在运行"
echo "  2. FilamentAdmin 是否可以正常访问"
echo "  3. 查看浏览器开发者工具的控制台错误"
echo "  4. 查看 Laravel 日志: storage/logs/laravel.log"
echo ""
