<?php
$pageTitle = '学业详情';
$pageCss = '/css/academic.css';
require_once '../includes/header.php';

$userId = $_SESSION['user_id'];

// 从SESSION获取学业进度数据（不再从数据库读取）
$progressData = isset($_SESSION['progress_data']) ? $_SESSION['progress_data'] : null;
$progressTime = isset($_SESSION['progress_update_time']) ? $_SESSION['progress_update_time'] : '';

// 兼容：courses 数据可能存储在 'course' 键（progress.courses 嵌套结构被整体存入）
if ($progressData && !isset($progressData['courses']) && isset($progressData['course'])) {
    if (is_array($progressData['course']) && (isset($progressData['course']['compulsory']) || isset($progressData['course']['elective']) || isset($progressData['course']['restricted']) || isset($progressData['course']['other']))) {
        $progressData['courses'] = $progressData['course'];
    }
}

// 检查数据是否过期（超过7天）
$syncTime = $_SESSION['progress_sync_time'] ?? '';
$isExpired = false;
$daysSinceSync = 0;
if ($syncTime) {
    $daysSinceSync = floor((time() - strtotime($syncTime)) / 86400);
    if ($daysSinceSync > 7) {
        $isExpired = true;
    }
}

// PHP 版状态样式映射
function getStatusClass($status) {
    $map = [
        '通过'     => 'passed',
        '已通过'   => 'passed',
        '未通过'   => 'failed',
        '在修'     => 'studying',
        '未修'     => 'unfinished',
    ];
    return $map[$status] ?? 'unknown';
}
?>

<!-- 数据过期提示 -->
<?php if ($isExpired): ?>
<div class="sync-expire-banner">
    <i class="fas fa-exclamation-triangle"></i>
    <span>学业数据已<?php echo $daysSinceSync; ?>天未更新，请重新同步知岁OS获取最新数据</span>
    <button class="expire-sync-btn" onclick="syncData('progress')">立即更新</button>
</div>
<?php endif; ?>

<!-- 页面头部 -->
<div class="page-header-simple">
    <div class="header-title">学业详情</div>
    <div class="header-sub"><?php echo $progressTime ? '更新于 ' . $progressTime : '暂未同步'; ?></div>
    <div class="sync-btn" onclick="syncData('progress')">
        <i class="fas fa-cloud-download-alt"></i> 同步
    </div>
</div>

<?php if ($progressData): ?>
<!-- GPA卡片 -->
<div class="gpa-card">
    <div class="gpa-title">我的GPA</div>
    <div class="gpa-value"><?php echo $progressData['gpa'] ?? 0; ?></div>
</div>

<!-- 学业统计 -->
<div class="stats-grid">
    <div class="stat-card">
        <div class="stat-value"><?php echo $progressData['all'] ?? 0; ?></div>
        <div class="stat-label">总课程</div>
    </div>
    <div class="stat-card pass">
        <div class="stat-value"><?php echo $progressData['pass'] ?? 0; ?></div>
        <div class="stat-label">已通过</div>
    </div>
    <div class="stat-card fail">
        <div class="stat-value"><?php echo $progressData['fail'] ?? 0; ?></div>
        <div class="stat-label">未通过</div>
    </div>
    <div class="stat-card">
        <div class="stat-value"><?php echo $progressData['studying'] ?? 0; ?></div>
        <div class="stat-label">在修</div>
    </div>
</div>

<!-- 课程进度列表 -->
<?php if (isset($progressData['courses']) && is_array($progressData['courses'])): ?>
<?php
$courseTypes = [
    'compulsory' => ['label' => '必修', 'icon' => 'fa-book'],
    'elective'   => ['label' => '选修', 'icon' => 'fa-lightbulb'],
    'restricted' => ['label' => '限选', 'icon' => 'fa-filter'],
    'other'      => ['label' => '其他', 'icon' => 'fa-ellipsis-h'],
];
?>
<?php foreach ($courseTypes as $typeKey => $typeInfo): ?>
<?php if (!empty($progressData['courses'][$typeKey])): ?>
<div class="course-section">
    <div class="section-title">
        <i class="fas <?php echo $typeInfo['icon']; ?>"></i>
        <?php echo $typeInfo['label']; ?>课程
        <span class="section-count"><?php echo count($progressData['courses'][$typeKey]); ?>门</span>
    </div>
    <div class="course-list">
        <?php foreach ($progressData['courses'][$typeKey] as $course): ?>
        <div class="course-item">
            <div class="course-name"><?php echo htmlspecialchars($course['name'] ?? ''); ?></div>
            <div class="course-meta">
                <span><?php echo htmlspecialchars($course['academicYear'] ?? ''); ?></span>
                <span><?php echo htmlspecialchars($course['semester'] ?? ''); ?></span>
            </div>
            <div class="course-status <?php echo getStatusClass($course['status'] ?? ''); ?>">
                <?php echo htmlspecialchars($course['status'] ?? '未知'); ?>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
</div>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>

<?php else: ?>
<!-- 空状态 -->
<div class="empty-state">
    <i class="fas fa-chart-line"></i>
    <p>暂无学业数据</p>
    <div class="empty-hint">点击"同步"按钮从知岁OS获取学业数据</div>
</div>
<?php endif; ?>

<!-- 底部导航 -->
<div class="bottom-nav">
    <div class="nav-item" onclick="location.href='../index.php'">
        <div class="nav-icon"><i class="fas fa-home"></i></div>
        <div class="nav-name">大厅</div>
    </div>
    <div class="nav-item" onclick="location.href='schedule.php'">
        <div class="nav-icon"><i class="fas fa-calendar-alt"></i></div>
        <div class="nav-name">课表</div>
    </div>
    <div class="nav-item" onclick="location.href='profile.php'">
        <div class="nav-icon"><i class="fas fa-user"></i></div>
        <div class="nav-name">我的</div>
    </div>
</div>

<script>
function syncData(type) {
    location.href = '<?php echo SITE_URL; ?>/pages/zsos-import.php?type=' + (type || 'progress');
}

function getStatusClass(status) {
    var map = {
        '通过': 'passed',
        '已通过': 'passed',
        '未通过': 'failed',
        '在修': 'studying',
        '未修': 'unfinished',
    };
    return map[status] || 'unknown';
}
</script>

<?php require_once '../includes/footer.php'; ?>
