<?php
if(!file_exists('articles')) mkdir('articles',0755);
$files = glob('articles/*.md');
rsort($files);
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>文章系统</title>
<link rel="stylesheet" href="assets/style.css">
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
</head>
<body>
<div class="container">
    <header>
        <h1>文章阅览</h1>
        <a href="admin.php" class="btn">管理员后台</a>
    </header>

    <div class="article-list">
        <?php foreach($files as $file):
            $filename = basename($file);
            $raw = file_get_contents($file);
            $lines = explode("\n",$raw);
            $title = trim($lines[0] ?? '无标题');
            $mtime = date('Y-m-d H:i',filemtime($file));
        ?>
        <div class="article-item">
            <h2><?=htmlspecialchars($title)?></h2>
            <div class="time">更新时间：<?=$mtime?></div>
            <button class="btn read-btn" data-file="<?=$filename?>">阅读全文</button>
        </div>
        <?php endforeach; ?>
    </div>

    <div id="article-view" class="markdown-body" style="margin-top:30px;display:none"></div>
</div>

<script>
const viewBox = document.getElementById('article-view');
document.querySelectorAll('.read-btn').forEach(btn=>{
    btn.onclick = async ()=>{
        const file = btn.dataset.file;
        const res = await fetch(`articles/${encodeURIComponent(file)}`);
        const md = await res.text();
        viewBox.innerHTML = marked.parse(md);
        viewBox.style.display = 'block';
        viewBox.scrollIntoView({behavior:'smooth'});
    }
})
</script>
</body>
</html>