<?php
require __DIR__ . '/lib.php';

$data = store_load();
$items = $data['items'];
usort($items, function ($a, $b) {
    return (int)$b['created_at'] - (int)$a['created_at'];
});
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<meta name="theme-color" content="#0f3b66">
<title><?php echo h(cfg('site_title')); ?></title>
<link rel="stylesheet" href="assets/app.css?v=20261118b">
</head>
<body class="page-public">

<header class="hero">
    <div class="hero-inner">
        <div class="brand">
            <div class="brand-mark">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
                    <path d="M6 2h9l5 5v15a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V3a1 1 0 0 1 1-1z"/>
                    <path d="M14 2v6h6"/>
                    <path d="M9 13h6M9 17h6"/>
                </svg>
            </div>
            <div class="brand-text">
                <h1><?php echo h(cfg('site_title')); ?></h1>
                <p><?php echo h(cfg('site_subtitle')); ?></p>
            </div>
        </div>
    </div>
</header>

<main class="wrap">
    <div class="list-head">
        <span>全部说明书</span>
        <span class="count"><?php echo count($items); ?> 份</span>
    </div>

    <?php if (empty($items)): ?>
        <div class="empty">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round">
                <path d="M6 2h9l5 5v15a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V3a1 1 0 0 1 1-1z"/>
                <path d="M14 2v6h6"/>
            </svg>
            <p>暂无说明书</p>
            <span>管理员上传后会显示在这里</span>
        </div>
    <?php else: ?>
        <div class="cards">
            <?php foreach ($items as $item):
                $kind = file_kind(isset($item['ext']) ? $item['ext'] : '');
                ?>
                <a class="card" href="download.php?id=<?php echo (int)$item['id']; ?>" aria-label="下载 <?php echo h($item['name']); ?>">
                    <div class="file-icon <?php echo h($kind['class']); ?>">
                        <span><?php echo h($kind['label']); ?></span>
                    </div>
                    <div class="card-body">
                        <h2 class="card-title"><?php echo h($item['name']); ?></h2>
                        <p class="card-meta">
                            <?php echo h(format_size($item['size'])); ?>
                            <i></i>
                            <?php echo h(format_date($item['created_at'])); ?>
                        </p>
                    </div>
                    <div class="dl-btn">
                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                            <path d="M12 3v12"/>
                            <path d="M7 10l5 5 5-5"/>
                            <path d="M5 21h14"/>
                        </svg>
                        <span>下载</span>
                    </div>
                </a>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
</main>

<footer class="foot">
    <p class="foot-company">宁波安信亿自动化科技有限公司</p>
    <p class="foot-contact">
        <button type="button" class="wechat-btn" id="wechatBtn">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
                <path d="M9 3C4.9 3 2 5.7 2 9c0 1.8 1 3.4 2.5 4.5L4 16l2.8-1.4c.7.2 1.4.3 2.2.3"/>
                <path d="M22 14.5c0-2.8-2.7-5-6-5s-6 2.2-6 5 2.7 5 6 5c.7 0 1.4-.1 2-.3l2.3 1.2-.5-2c1.3-.9 2.2-2.3 2.2-3.9z"/>
                <path d="M7 7.5h.01M11 7.5h.01M13.5 13.5h.01M17.5 13.5h.01"/>
            </svg>
            点击加好友
        </button>
    </p>
    <p class="foot-tip">如无法下载，请换用手机浏览器打开本页</p>
    <p>&copy; <?php echo date('Y'); ?> <?php echo h(cfg('site_title')); ?></p>
</footer>

<div class="qr-mask" id="qrMask">
    <div class="qr-dialog" role="dialog" aria-modal="true" aria-label="微信二维码">
        <button type="button" class="qr-close" id="qrClose" aria-label="关闭">&times;</button>
        <p class="qr-title">微信扫码加好友</p>
        <p class="qr-sub">宁波安信亿自动化科技有限公司</p>
        <img class="qr-img" src="assets/wechat-qr.jpg?v=20261118" alt="微信二维码">
        <p class="qr-tip">长按二维码即可识别添加</p>
    </div>
</div>

<script>
document.querySelectorAll('.card').forEach(function (card) {
    card.addEventListener('click', function () {
        card.classList.add('is-loading');
        setTimeout(function () { card.classList.remove('is-loading'); }, 1200);
    });
});
(function () {
    var mask = document.getElementById('qrMask');
    var btn = document.getElementById('wechatBtn');
    var close = document.getElementById('qrClose');
    function open() { mask.className = 'qr-mask is-open'; document.body.style.overflow = 'hidden'; }
    function shut() { mask.className = 'qr-mask'; document.body.style.overflow = ''; }
    btn.addEventListener('click', open);
    close.addEventListener('click', shut);
    mask.addEventListener('click', function (e) { if (e.target === mask) { shut(); } });
    document.addEventListener('keydown', function (e) { if (e.key === 'Escape') { shut(); } });
})();
</script>
</body>
</html>
