<?php
/** 本源码版权所有©葫芦侠用户天奕科技工作室，侵权联系邮箱tianhkeji@qq.com删除处理 */
/**
 * 短剧列表管理
 * 全端响应式 + 现代化UI
 */
session_start();
if (!isset($_SESSION['admin_login']) || !$_SESSION['admin_login']) {
    header("Location: index.php");
    exit;
}
require __DIR__ . '/../config.php';

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

$where = " 1=1 ";
if (!empty($kw)) {
    $where .= " AND (title LIKE '%$kw%' OR tags LIKE '%$kw%' OR category LIKE '%$kw%') ";
}

$res = mysqli_query($conn, "SELECT * FROM drama_list WHERE $where ORDER BY id DESC LIMIT $offset, $pagesize");
$total = mysqli_fetch_assoc(mysqli_query($conn, "SELECT count(*) cnt FROM drama_list WHERE $where"))['cnt'];
$maxpage = max(1, ceil($total / $pagesize));
?>
<!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">
<title>短剧列表管理 - 后台管理</title>
<link rel="stylesheet" href="../static/style.css">
<style>
    .toolbar {
        background: white;
        border-radius: var(--radius);
        box-shadow: var(--shadow);
        padding: 16px 20px;
        margin-bottom: 16px;
        display: flex;
        gap: 10px;
        align-items: center;
        flex-wrap: wrap;
    }
    .toolbar .search-form {
        display: flex;
        gap: 8px;
        flex: 1;
        min-width: 200px;
    }
    .toolbar .search-form input {
        flex: 1;
    }
    .table-card {
        background: white;
        border-radius: var(--radius);
        box-shadow: var(--shadow);
        overflow: hidden;
    }
    .action-links {
        display: flex;
        gap: 12px;
        justify-content: center;
    }
    .action-links a {
        font-size: 13px;
        white-space: nowrap;
    }
    .action-links .edit { color: var(--primary); }
    .action-links .delete { color: var(--danger); }
    .count-badge {
        display: inline-block;
        background: var(--primary-light);
        color: var(--primary-dark);
        padding: 2px 10px;
        border-radius: 10px;
        font-size: 12px;
        font-weight: 600;
    }
    @media (max-width: 768px) {
        .toolbar { padding: 12px; }
        .toolbar .search-form { flex-direction: column; }
        .hide-mobile { display: none; }
    }
</style>
</head>
<body>

<nav class="navbar">
    <div class="navbar-brand">📋 短剧列表管理</div>
    <div class="navbar-links">
        <a href="main.php">后台首页</a>
        <a href="maccms.php" style="color:var(--primary);font-weight:600;">🎬 采集</a>
        <a href="drama_add.php" style="color:var(--primary);font-weight:600;">+ 新增</a>
        <a href="batch_import.php" style="color:var(--success);font-weight:600;">批量导入</a>
        <a href="logout.php" style="color:var(--danger);">退出</a>
    </div>
</nav>

<div class="container">

    <!-- 搜索工具栏 -->
    <div class="toolbar animate-fade-in">
        <form method="get" class="search-form">
            <input type="text" name="kw" value="<?php echo htmlspecialchars($kw); ?>"
                   class="input" placeholder="搜索剧名 / 标签 / 分类...">
            <button type="submit" class="btn btn-primary">🔍 搜索</button>
            <?php if (!empty($kw)): ?>
            <a href="drama_list.php" class="btn btn-outline">重置</a>
            <?php endif; ?>
        </form>
        <span class="count-badge">共 <?php echo $total; ?> 条</span>
    </div>

    <!-- 数据表格 -->
    <div class="table-card animate-fade-in">
        <div class="table-wrap">
            <table class="table">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>剧名</th>
                        <th class="hide-mobile">分类</th>
                        <th class="hide-mobile">标签</th>
                        <th class="hide-mobile">权重</th>
                        <th class="hide-mobile">录入时间</th>
                        <th style="text-align:center;">操作</th>
                    </tr>
                </thead>
                <tbody>
                <?php if ($total == 0): ?>
                    <tr>
                        <td colspan="7">
                            <div class="empty-state">
                                <div class="icon">📭</div>
                                <p>暂无数据</p>
                                <a href="drama_add.php" class="btn btn-primary" style="margin-top:12px;">去添加短剧</a>
                            </div>
                        </td>
                    </tr>
                <?php endif; ?>
                <?php while ($row = mysqli_fetch_assoc($res)): ?>
                    <tr>
                        <td><strong>#<?php echo $row['id']; ?></strong></td>
                        <td style="max-width:200px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;"
                            title="<?php echo htmlspecialchars($row['title']); ?>">
                            <?php echo htmlspecialchars($row['title']); ?>
                        </td>
                        <td class="hide-mobile">
                            <span class="tag tag-blue"><?php echo htmlspecialchars($row['category']); ?></span>
                        </td>
                        <td class="hide-mobile" style="max-width:150px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;"
                            title="<?php echo htmlspecialchars($row['tags']); ?>">
                            <?php echo htmlspecialchars($row['tags']); ?>
                        </td>
                        <td class="hide-mobile"><?php echo $row['hot_sort']; ?></td>
                        <td class="hide-mobile" style="color:var(--text-light);font-size:13px;">
                            <?php echo substr($row['create_time'], 0, 10); ?>
                        </td>
                        <td>
                            <div class="action-links">
                                <a href="drama_edit.php?id=<?php echo $row['id']; ?>" class="edit">✏️ 编辑</a>
                                <a href="drama_delete.php?id=<?php echo $row['id']; ?>"
                                   onclick="return confirm('确定删除这部短剧吗？此操作不可恢复！')"
                                   class="delete">🗑️ 删除</a>
                            </div>
                        </td>
                    </tr>
                <?php endwhile; ?>
                </tbody>
            </table>
        </div>
    </div>

    <!-- 分页 -->
    <?php if ($maxpage > 1): ?>
    <div class="pagination">
        <?php if ($page > 1): ?>
        <a href="?kw=<?php echo urlencode($kw); ?>&page=<?php echo $page - 1; ?>">‹ 上一页</a>
        <?php endif; ?>
        <span class="current"><?php echo $page; ?> / <?php echo $maxpage; ?></span>
        <?php if ($page < $maxpage): ?>
        <a href="?kw=<?php echo urlencode($kw); ?>&page=<?php echo $page + 1; ?>">下一页 ›</a>
        <?php endif; ?>
    </div>
    <?php endif; ?>

</div>
</body>
</html>
