<?php
/**
 * 朝心阁仿作 —— 分类/列表页
 * 结构：页头 → 视频列表（分页）→ 页脚
 */
require __DIR__ . '/lib.php';

$t = isset($_GET['t']) ? (int)$_GET['t'] : 0;
$pg = isset($_GET['pg']) ? max(1, (int)$_GET['pg']) : 1;
$per = 24;

$page_key = 'category';
$page_title = $t ? category_name($t) : '全部视频';
if ($page_title === '') $page_title = '全部视频';
$active_type = $t;
require __DIR__ . '/inc_header.php';

[$list, $total] = page_videos($t ? $t : null, $pg, $per);
$pagecount = max(1, (int)ceil($total / $per));
$nextPg = $pg + 1;
?>
<style>
    .pagination { gap: 20px; align-items: center; justify-content: center; flex-wrap: nowrap; padding: 16px 0 4px; }
    .pagination a { color: white; font-size: 14px; border-radius: 8px; font-weight: 500; padding: 8px 16px; background-color: rgb(67,76,94); }
    .pagination input { font-size: 16px; border-width: 2px; border-style: solid; border-radius: 8px; border-color: rgb(76,86,106); background-color: rgb(67,76,94); padding: 6px 16px; width: 112px; color: rgb(216,222,233); }
    .page-info { text-align: center; color: #7a8398; font-size: 13px; padding: 4px 0 0; }
</style>
<div class="list-container container">
    <div class="list-title">
        <div class="flex align-center">
            <span class="title"><?php echo esc($page_title); ?></span>
        </div>
        <span style="color:#7a8398;font-size:13px;">共 <?php echo (int)$total; ?> 条</span>
    </div>
    <?php if ($list): ?>
        <?php echo video_list_html($list); ?>
    <?php else: ?>
        <p style="color:#7a8398;padding:24px 0;text-align:center;">暂无视频，请到后台采集</p>
    <?php endif; ?>

    <?php if ($pagecount > 1): ?>
        <div class="pagination">
            <form style="position: relative;" onsubmit="return pagejump();">
                <input maxlength="6" value="<?php echo $pg; ?>" id="jumpPage">
                <div style="position:absolute;display:flex;align-items:center;justify-content:space-between;flex-wrap:nowrap;top:0;bottom:0;right:0;padding-right:12px;">
                    <span style="color:rgb(107,114,128);font-weight:bold">/<?php echo $pagecount; ?></span>
                </div>
            </form>
            <?php if ($pg < $pagecount): ?>
                <a href="category.php?t=<?php echo $t; ?>&pg=<?php echo $nextPg; ?>">下一页</a>
            <?php else: ?>
                <a href="javascript:;" style="opacity:.4;pointer-events:none;">已是最后一页</a>
            <?php endif; ?>
        </div>
        <div class="page-info">第 <?php echo $pg; ?> / <?php echo $pagecount; ?> 页</div>
        <script>
            var pageTotal = <?php echo $pagecount; ?>;
            var pageType = <?php echo $t; ?>;
            function pagejump() {
                var jumpPage = parseInt($('#jumpPage').val());
                var total = pageTotal;
                if (jumpPage === 1) return false;
                if (isNaN(jumpPage) || typeof jumpPage != "number") return false;
                if (jumpPage <= 0) jumpPage = 1;
                if (jumpPage > total) jumpPage = total;
                window.location.href = 'category.php?t=' + pageType + '&pg=' + jumpPage;
                return false;
            }
        </script>
    <?php endif; ?>
</div>

<?php require __DIR__ . '/inc_footer.php'; ?>
