<?php
/**
 * 媒体库卡片预览页
 * 自动扫描 shipin 目录下的图片与视频，卡片式展示，点击可在线查看/播放
 */
$dir = __DIR__ . '/shipin';
$imageExts = array('jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp');
$videoExts = array('mp4', 'webm', 'mov', 'm4v', 'ogv');

$items = array();
if (is_dir($dir)) {
    $files = scandir($dir);
    if ($files !== false) {
        foreach ($files as $file) {
            if ($file === '.' || $file === '..') {
                continue;
            }
            $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
            $type = null;
            if (in_array($ext, $imageExts, true)) {
                $type = 'image';
            } elseif (in_array($ext, $videoExts, true)) {
                $type = 'video';
            }
            if ($type !== null) {
                $items[] = array(
                    'type' => $type,
                    'name' => $file,
                    'url'  => 'shipin/' . rawurlencode($file),
                    'mtime'=> is_file($dir . '/' . $file) ? filemtime($dir . '/' . $file) : 0,
                );
            }
        }
    }
}

// 按修改时间倒序（最新在前）
usort($items, function ($a, $b) {
    return $b['mtime'] - $a['mtime'];
});
?>
<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>媒体库 · ax.1103.life</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }

        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
                         "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
            background: linear-gradient(135deg, #0f2027 0%, #203a43 50%, #2c5364 100%);
            min-height: 100vh;
            color: #fff;
            padding: 30px 20px 60px;
        }

        .header {
            text-align: center;
            margin-bottom: 32px;
        }
        .header h1 {
            font-size: 28px;
            font-weight: 600;
            letter-spacing: 1px;
        }
        .header p {
            margin-top: 8px;
            color: rgba(255, 255, 255, .65);
            font-size: 14px;
        }

        .grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
            gap: 18px;
            max-width: 1200px;
            margin: 0 auto;
        }

        .card {
            position: relative;
            background: rgba(255, 255, 255, .06);
            border: 1px solid rgba(255, 255, 255, .1);
            border-radius: 14px;
            overflow: hidden;
            cursor: pointer;
            transition: transform .2s ease, box-shadow .2s ease;
            aspect-ratio: 4 / 3;
        }
        .card:hover {
            transform: translateY(-6px);
            box-shadow: 0 16px 32px rgba(0, 0, 0, .4);
        }

        .card .thumb {
            position: absolute;
            inset: 0;
            width: 100%;
            height: 100%;
            object-fit: cover;
            background: #000;
        }

        .card .overlay {
            position: absolute;
            inset: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(180deg, rgba(0,0,0,0) 40%, rgba(0,0,0,.55) 100%);
            transition: background .2s ease;
        }
        .card:hover .overlay { background: linear-gradient(180deg, rgba(0,0,0,.1) 40%, rgba(0,0,0,.65) 100%); }

        .card .play {
            width: 58px;
            height: 58px;
            border-radius: 50%;
            background: rgba(0, 0, 0, .5);
            border: 2px solid rgba(255, 255, 255, .85);
            display: flex;
            align-items: center;
            justify-content: center;
            transition: background .2s ease, transform .2s ease;
        }
        .card:hover .play { background: rgba(255, 255, 255, .9); transform: scale(1.08); }
        .card .play::after {
            content: "";
            width: 0;
            height: 0;
            border-top: 11px solid transparent;
            border-bottom: 11px solid transparent;
            border-left: 17px solid #fff;
            margin-left: 4px;
        }
        .card:hover .play::after { border-left-color: #203a43; }

        .card .name {
            position: absolute;
            left: 0;
            right: 0;
            bottom: 0;
            padding: 30px 12px 10px;
            font-size: 13px;
            color: #fff;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            text-shadow: 0 1px 3px rgba(0,0,0,.6);
        }

        .card .badge {
            position: absolute;
            top: 10px;
            right: 10px;
            font-size: 11px;
            padding: 3px 9px;
            border-radius: 20px;
            background: rgba(0, 0, 0, .45);
            color: #fff;
            backdrop-filter: blur(4px);
        }

        .empty {
            text-align: center;
            color: rgba(255, 255, 255, .6);
            padding: 80px 20px;
            grid-column: 1 / -1;
        }

        /* 灯箱 */
        .lightbox {
            position: fixed;
            inset: 0;
            background: rgba(0, 0, 0, .88);
            display: none;
            align-items: center;
            justify-content: center;
            z-index: 1000;
            padding: 20px;
        }
        .lightbox.show { display: flex; }

        .lightbox .stage {
            position: relative;
            max-width: 92vw;
            max-height: 86vh;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .lightbox img, .lightbox video {
            max-width: 92vw;
            max-height: 78vh;
            border-radius: 8px;
            box-shadow: 0 20px 50px rgba(0,0,0,.6);
        }
        .lightbox .caption {
            margin-top: 12px;
            color: rgba(255, 255, 255, .85);
            font-size: 14px;
            max-width: 92vw;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        .lightbox .close {
            position: absolute;
            top: -44px;
            right: 0;
            width: 36px;
            height: 36px;
            border: none;
            border-radius: 50%;
            background: rgba(255, 255, 255, .15);
            color: #fff;
            font-size: 20px;
            line-height: 1;
            cursor: pointer;
        }
        .lightbox .close:hover { background: rgba(255, 255, 255, .3); }
    </style>
</head>
<body>
    <div class="header">
        <h1>媒体库</h1>
        <p>共 <?php echo count($items); ?> 个文件 · 点击卡片在线查看 / 播放</p>
    </div>

    <div class="grid" id="grid">
        <?php if (count($items) === 0): ?>
            <div class="empty">shipin 目录下暂无图片或视频文件</div>
        <?php else: ?>
            <?php foreach ($items as $item): ?>
                <div class="card"
                     data-type="<?php echo htmlspecialchars($item['type'], ENT_QUOTES, 'UTF-8'); ?>"
                     data-url="<?php echo htmlspecialchars($item['url'], ENT_QUOTES, 'UTF-8'); ?>"
                     data-name="<?php echo htmlspecialchars($item['name'], ENT_QUOTES, 'UTF-8'); ?>">
                    <?php if ($item['type'] === 'image'): ?>
                        <img class="thumb" src="<?php echo htmlspecialchars($item['url'], ENT_QUOTES, 'UTF-8'); ?>"
                             alt="<?php echo htmlspecialchars($item['name'], ENT_QUOTES, 'UTF-8'); ?>" loading="lazy">
                        <span class="badge">图片</span>
                    <?php else: ?>
                        <video class="thumb" muted playsinline preload="metadata"
                               src="<?php echo htmlspecialchars($item['url'], ENT_QUOTES, 'UTF-8'); ?>"></video>
                        <div class="overlay"><div class="play"></div></div>
                        <span class="badge">视频</span>
                    <?php endif; ?>
                    <div class="name"><?php echo htmlspecialchars($item['name'], ENT_QUOTES, 'UTF-8'); ?></div>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>

    <!-- 灯箱 -->
    <div class="lightbox" id="lightbox">
        <div class="stage">
            <button class="close" id="lbClose" aria-label="关闭">&times;</button>
            <div id="lbContent"></div>
            <div class="caption" id="lbCaption"></div>
        </div>
    </div>

    <script>
        (function () {
            var lightbox = document.getElementById('lightbox');
            var lbContent = document.getElementById('lbContent');
            var lbCaption = document.getElementById('lbCaption');
            var lbClose = document.getElementById('lbClose');

            function open(item) {
                lbContent.innerHTML = '';
                var url = item.getAttribute('data-url');
                var name = item.getAttribute('data-name');
                var type = item.getAttribute('data-type');

                if (type === 'video') {
                    var v = document.createElement('video');
                    v.setAttribute('src', url);
                    v.setAttribute('controls', 'controls');
                    v.setAttribute('autoplay', 'autoplay');
                    v.setAttribute('playsinline', 'playsinline');
                    lbContent.appendChild(v);
                } else {
                    var img = document.createElement('img');
                    img.setAttribute('src', url);
                    img.setAttribute('alt', name);
                    lbContent.appendChild(img);
                }
                lbCaption.textContent = name;
                lightbox.classList.add('show');
            }

            function close() {
                lightbox.classList.remove('show');
                lbContent.innerHTML = '';
                lbCaption.textContent = '';
            }

            var cards = document.querySelectorAll('.card');
            for (var i = 0; i < cards.length; i++) {
                cards[i].addEventListener('click', function () {
                    open(this);
                });
            }

            lbClose.addEventListener('click', function (e) {
                e.stopPropagation();
                close();
            });

            lightbox.addEventListener('click', function (e) {
                if (e.target === lightbox) {
                    close();
                }
            });

            document.addEventListener('keydown', function (e) {
                if (e.key === 'Escape') {
                    close();
                }
            });
        })();
    </script>
</body>
</html>
