<?php
/** 本源码版权所有©葫芦侠用户天奕科技工作室，侵权联系邮箱tianhkeji@qq.com删除处理 */
/**
 * 网站首页 - 短剧/AI漫剧展示
 * 全端响应式 + 封面卡片布局
 */
require __DIR__ . '/config.php';

$cat = isset($_GET['cat']) ? escape($_GET['cat']) : '';
$page = max(1, intval($_GET['page'] ?? 1));
$pagesize = 24;
$offset = ($page - 1) * $pagesize;

$where = " 1=1 ";
if (!empty($cat)) {
    $where .= " AND category='$cat' ";
}

$sql = "SELECT * FROM drama_list WHERE $where ORDER BY hot_sort DESC, id DESC LIMIT $offset, $pagesize";
$drama = mysqli_query($conn, $sql);

$totalRs = mysqli_query($conn, "SELECT count(*) cnt FROM drama_list WHERE $where");
$total = mysqli_fetch_assoc($totalRs)['cnt'];
$maxpage = max(1, ceil($total / $pagesize));

$logSql = "SELECT * FROM update_log ORDER BY add_date DESC LIMIT 7";
$logRes = mysqli_query($conn, $logSql);
$updateLogs = [];
while ($row = mysqli_fetch_assoc($logRes)) {
    $updateLogs[] = $row;
}

// 分类列表（含采集的分类）
$cateList = ["全部", "短剧", "AI漫剧"];

/**
 * 从resource_link中统计剧集数
 * 格式：第01集$url1#第02集$url2#...
 */
function countEpisodes($link) {
    if (empty($link)) return 0;
    $parts = explode('#', $link);
    $count = 0;
    foreach ($parts as $p) {
        if (trim($p) !== '') $count++;
    }
    return $count;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0">
<meta name="theme-color" content="#667eea">
<title>短剧-短剧搜 - 短剧/AI漫剧在线观看</title>
<link rel="stylesheet" href="static/style.css">
<style>
    .hero {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 16px;
        padding: 28px 24px;
        margin-bottom: 16px;
        color: white;
        position: relative;
        overflow: hidden;
    }
    .hero::after {
        content: '';
        position: absolute;
        top: -50%; right: -10%;
        width: 280px; height: 280px;
        background: rgba(255,255,255,0.08);
        border-radius: 50%;
    }
    .hero h1 { font-size: 26px; font-weight: 800; margin-bottom: 6px; position: relative; z-index: 1; }
    .hero p { opacity: 0.9; font-size: 13px; margin-bottom: 16px; position: relative; z-index: 1; }
    .search-box { display: flex; gap: 8px; position: relative; z-index: 1; }
    .search-box input {
        flex: 1; padding: 12px 16px; border: none; border-radius: 10px;
        font-size: 15px; background: rgba(255,255,255,0.95);
    }
    .search-box input:focus { outline: none; background: white; box-shadow: 0 0 0 3px rgba(255,255,255,0.3); }
    .search-box button {
        padding: 12px 24px; border: none; border-radius: 10px;
        background: white; color: #667eea; font-weight: 700;
        cursor: pointer; white-space: nowrap;
    }
    .search-box button:hover { transform: translateY(-1px); box-shadow: 0 4px 12px rgba(0,0,0,0.15); }

    .category-bar { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
    .category-bar a {
        padding: 7px 14px; border-radius: 20px; font-size: 13px; font-weight: 500;
        background: white; color: var(--text-light); border: 1px solid var(--border);
        transition: all 0.2s;
    }
    .category-bar a:hover { border-color: var(--primary); color: var(--primary); }
    .category-bar a.active {
        background: linear-gradient(135deg, #667eea, #764ba2);
        color: white; border-color: transparent;
    }

    .main-layout { display: grid; grid-template-columns: 1fr 260px; gap: 16px; }

    .list-card {
        background: white; border-radius: var(--radius);
        box-shadow: var(--shadow); padding: 16px;
    }
    .list-header {
        display: flex; align-items: center; justify-content: space-between;
        margin-bottom: 14px; padding-bottom: 12px; border-bottom: 1px solid var(--border);
    }
    .list-header h2 {
        font-size: 17px; font-weight: 700;
        display: flex; align-items: center; gap: 8px;
    }
    .list-header h2::before {
        content: ''; width: 4px; height: 18px;
        background: linear-gradient(180deg, #667eea, #764ba2); border-radius: 2px;
    }
    .list-header .count { font-size: 13px; color: var(--text-light); }

    /* 封面卡片网格 */
    .drama-grid {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        gap: 14px;
    }
    .drama-card {
        border-radius: 10px; overflow: hidden; background: #f8fafc;
        transition: transform 0.2s, box-shadow 0.2s; cursor: pointer;
        display: block;
    }
    .drama-card:hover { transform: translateY(-3px); box-shadow: var(--shadow-md); }
    .drama-cover {
        position: relative; width: 100%; padding-top: 140%;
        background: #e2e8f0; overflow: hidden;
    }
    .drama-cover img {
        position: absolute; top: 0; left: 0; width: 100%; height: 100%;
        object-fit: cover;
    }
    .drama-cover .no-cover {
        position: absolute; top: 0; left: 0; width: 100%; height: 100%;
        display: flex; align-items: center; justify-content: center;
        font-size: 36px; background: linear-gradient(135deg, #667eea20, #764ba220);
    }
    .drama-cover .remarks {
        position: absolute; bottom: 0; left: 0; right: 0;
        background: linear-gradient(transparent, rgba(0,0,0,0.7));
        color: white; font-size: 11px; padding: 16px 8px 6px; text-align: center;
    }
    .drama-cover .cat-tag {
        position: absolute; top: 6px; left: 6px;
        background: rgba(102,126,234,0.9); color: white;
        font-size: 10px; padding: 2px 7px; border-radius: 4px; font-weight: 600;
    }
    .drama-cover .cat-tag.comic { background: rgba(124,58,237,0.9); }
    .drama-info { padding: 8px 10px 10px; }
    .drama-name {
        font-size: 13px; font-weight: 600; line-height: 1.4;
        display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
        overflow: hidden; min-height: 36px;
    }

    /* 侧边栏 */
    .sidebar-card {
        background: white; border-radius: var(--radius);
        box-shadow: var(--shadow); overflow: hidden; margin-bottom: 16px;
    }
    .sidebar-header {
        padding: 12px 16px; border-bottom: 1px solid var(--border);
        font-weight: 700; font-size: 14px;
        display: flex; align-items: center; gap: 8px;
        background: linear-gradient(135deg, #667eea, #764ba2); color: white;
    }
    .update-item {
        padding: 10px 16px; border-bottom: 1px solid #f1f5f9;
        display: flex; justify-content: space-between; align-items: center; font-size: 13px;
    }
    .update-item:last-child { border-bottom: none; }
    .update-date { color: var(--text-light); }
    .update-count { color: #059669; font-weight: 600; background: #d1fae5; padding: 2px 8px; border-radius: 10px; font-size: 12px; }

    .empty-state { text-align: center; padding: 60px 20px; color: var(--text-light); grid-column: 1/-1; }
    .empty-state .icon { font-size: 48px; margin-bottom: 12px; opacity: 0.5; }

    footer { text-align: center; padding: 24px 16px; color: var(--text-light); font-size: 12px; }

    /* 响应式 */
    @media (max-width: 1024px) {
        .drama-grid { grid-template-columns: repeat(3, 1fr); }
    }
    @media (max-width: 768px) {
        .main-layout { grid-template-columns: 1fr; }
        .sidebar { order: -1; display: none; }
        .drama-grid { grid-template-columns: repeat(3, 1fr); gap: 10px; }
        .hero { padding: 20px 16px; }
        .hero h1 { font-size: 20px; }
        .search-box { flex-direction: column; }
        .search-box button { width: 100%; }
    }
    @media (max-width: 480px) {
        .drama-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
        .category-bar a { padding: 5px 10px; font-size: 12px; }
        .list-card { padding: 12px; }
    }
</style>
</head>
<body>
<div class="container">

    <div class="hero animate-fade-in">
        <h1>🎬 短剧 & AI漫剧</h1>
        <p>海量短剧、AI漫剧在线观看，每日实时更新</p>
        <div class="search-box">
            <input id="searchInput" type="text" placeholder="输入短剧名称搜索..."
                   onkeydown="if(event.key==='Enter')goSearch()">
            <button onclick="goSearch()">🔍 搜索</button>
        </div>
    </div>

    <div class="category-bar animate-fade-in">
        <?php foreach ($cateList as $c): ?>
        <a href="?cat=<?php echo $c === '全部' ? '' : urlencode($c); ?>"
           class="<?php echo ($cat === $c || ($c === '全部' && empty($cat))) ? 'active' : ''; ?>">
            <?php echo htmlspecialchars($c); ?>
        </a>
        <?php endforeach; ?>
    </div>

    <div class="main-layout">
        <div class="list-card animate-fade-in">
            <div class="list-header">
                <h2>
                    <?php echo !empty($cat) ? htmlspecialchars($cat) : '最新更新'; ?>
                </h2>
                <span class="count">共 <?php echo $total; ?> 部</span>
            </div>

            <div class="drama-grid">
                <?php if ($total == 0): ?>
                <div class="empty-state">
                    <div class="icon">📭</div>
                    <p>暂无数据，请先在后台采集</p>
                </div>
                <?php endif; ?>

                <?php while ($row = mysqli_fetch_assoc($drama)):
                    $isComic = $row['category'] === 'AI漫剧';
                    $hasCover = !empty($row['cover']);
                ?>
                <a href="play.php?id=<?php echo $row['id']; ?>" class="drama-card">
                    <div class="drama-cover">
                        <?php if ($hasCover): ?>
                        <img src="<?php echo htmlspecialchars($row['cover']); ?>" alt="<?php echo htmlspecialchars($row['title']); ?>"
                             loading="lazy" onerror="this.style.display='none';this.nextElementSibling.style.display='flex';">
                        <div class="no-cover" style="display:none;">🎬</div>
                        <?php else: ?>
                        <div class="no-cover">🎬</div>
                        <?php endif; ?>
                        <span class="cat-tag <?php echo $isComic ? 'comic' : ''; ?>">
                            <?php echo htmlspecialchars($row['category'] ?: '短剧'); ?>
                        </span>
                        <?php if (!empty($row['tags'])): ?>
                        <span class="remarks"><?php echo htmlspecialchars(mb_substr($row['tags'], 0, 10)); ?></span>
                        <?php endif; ?>
                    </div>
                    <div class="drama-info">
                        <div class="drama-name"><?php echo htmlspecialchars($row['title']); ?></div>
                        <?php $epCnt = countEpisodes($row['resource_link']); if ($epCnt > 0): ?>
                        <div style="font-size:11px;color:var(--text-light);margin-top:2px;">📺 共<?php echo $epCnt; ?>集</div>
                        <?php endif; ?>
                    </div>
                </a>
                <?php endwhile; ?>
            </div>

            <?php if ($maxpage > 1): ?>
            <div class="pagination" style="margin-top:20px;">
                <?php if ($page > 1): ?>
                <a href="?cat=<?php echo urlencode($cat); ?>&page=<?php echo $page - 1; ?>">‹ 上一页</a>
                <?php endif; ?>
                <span class="current"><?php echo $page; ?> / <?php echo $maxpage; ?></span>
                <?php if ($page < $maxpage): ?>
                <a href="?cat=<?php echo urlencode($cat); ?>&page=<?php echo $page + 1; ?>">下一页 ›</a>
                <?php endif; ?>
            </div>
            <?php endif; ?>
        </div>

        <div class="sidebar">
            <div class="sidebar-card">
                <div class="sidebar-header">📅 每日更新</div>
                <?php foreach ($updateLogs as $log): ?>
                <div class="update-item">
                    <span class="update-date"><?php echo $log['add_date'] . ' ' . substr($log['add_time'], 0, 5); ?></span>
                    <span class="update-count">+<?php echo $log['add_count']; ?></span>
                </div>
                <?php endforeach; ?>
                <?php if (empty($updateLogs)): ?>
                <div style="padding:16px;text-align:center;color:var(--text-light);font-size:13px;">暂无更新记录</div>
                <?php endif; ?>
            </div>
            <div class="sidebar-card" style="text-align:center;padding:16px;">
                <a href="admin/maccms.php" style="font-size:12px;color:var(--text-light);">⚙️ 后台采集管理</a>
            </div>
        </div>
    </div>

    <footer>
        <p>短剧-短剧搜 &copy; <?php echo date('Y'); ?></p>
        <p style="margin-top:6px;">本站不储存、复制、传播任何文件</p>
        <p style="margin-top:2px;">仅做个人公益学习，请勿非法传播</p>
        <p style="margin-top:2px;">如有侵权请联系删除处理</p>
    </footer>

</div>
<script>
function goSearch() {
    let val = document.getElementById('searchInput').value.trim();
    if (val) location.href = "search.php?kw=" + encodeURIComponent(val);
}
</script>
</body>
</html>
