<?php
$pageTitle = '考试安排';
$pageCss = '/css/grades.css';
require_once '../includes/header.php';

$userId = $_SESSION['user_id'];

// 从session获取考试数据
$exams = isset($_SESSION['exam_data']) ? $_SESSION['exam_data'] : [];
$examUpdateTime = isset($_SESSION['exam_update_time']) ? $_SESSION['exam_update_time'] : '';

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

<!-- 数据过期提示 -->
<?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('exam')">立即更新</button>
</div>
<?php endif; ?>

<!-- 页面头部 -->
<div class="page-header-simple">
    <div class="header-title">考试安排</div>
    <div class="header-sub"><?php echo $examUpdateTime ? '更新于 ' . $examUpdateTime : '暂未同步'; ?></div>
    <div class="sync-btn" onclick="syncData('exam')">
        <i class="fas fa-cloud-download-alt"></i> 同步
    </div>
</div>

<!-- 考试列表 -->
<div class="data-content">
    <?php if (empty($exams)): ?>
    <div class="empty-state">
        <i class="fas fa-clipboard-list"></i>
        <p>暂无考试安排</p>
        <div class="empty-hint">点击"同步"按钮从知岁OS获取考试数据</div>
    </div>
    <?php else: ?>
        <?php foreach ($exams as $exam): ?>
        <div class="exam-card">
            <div class="exam-name"><?php echo htmlspecialchars($exam['name'] ?? '未知考试'); ?></div>
            <div class="exam-detail-grid">
                <div class="detail-item">
                    <span class="detail-label">日期</span>
                    <span class="detail-value"><?php echo htmlspecialchars($exam['date'] ?? '未设置'); ?></span>
                </div>
                <div class="detail-item">
                    <span class="detail-label">时间</span>
                    <span class="detail-value"><?php echo htmlspecialchars($exam['time'] ?? '未设置'); ?></span>
                </div>
                <div class="detail-item">
                    <span class="detail-label">地点</span>
                    <span class="detail-value"><?php echo htmlspecialchars($exam['location'] ?? '未设置'); ?></span>
                </div>
                <div class="detail-item">
                    <span class="detail-label">座位号</span>
                    <span class="detail-value"><?php echo htmlspecialchars($exam['seat'] ?? '未设置'); ?></span>
                </div>
                <div class="detail-item full-width">
                    <span class="detail-label">类型</span>
                    <span class="type-tag"><?php echo htmlspecialchars($exam['type'] ?? '未设置'); ?></span>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
    <?php endif; ?>
</div>

<!-- 底部导航 -->
<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 || 'exam');
}
</script>

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