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

$msg = '';
$msg_type = '';

if ($_POST) {
    $title = escape($_POST['title'] ?? '');
    $category = escape($_POST['category'] ?? '');
    $tags = escape($_POST['tags'] ?? '');
    $link = escape($_POST['resource_link'] ?? '');
    $hot = intval($_POST['hot_sort'] ?? 0);

    if (empty($title)) {
        $msg = "剧名不能为空";
        $msg_type = "error";
    } else {
        $sql = "INSERT INTO drama_list(title, category, tags, resource_link, hot_sort) VALUES('$title', '$category', '$tags', '$link', $hot)";
        if (mysqli_query($conn, $sql)) {
            updateDailyLog();
            $msg = "添加成功！ID：" . mysqli_insert_id($conn);
            $msg_type = "success";
        } else {
            $msg = "添加失败：" . mysqli_error($conn);
            $msg_type = "error";
        }
    }
}

$cateList = getCategories();
?>
<!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>
    .form-card {
        background: white;
        border-radius: var(--radius);
        box-shadow: var(--shadow);
        padding: 28px;
        max-width: 640px;
        margin: 0 auto;
    }
    .form-card h2 {
        font-size: 20px;
        font-weight: 700;
        margin-bottom: 24px;
        padding-bottom: 16px;
        border-bottom: 1px solid var(--border);
        display: flex;
        align-items: center;
        gap: 8px;
    }
    .form-group {
        margin-bottom: 20px;
    }
    .form-group label {
        display: block;
        font-size: 14px;
        font-weight: 600;
        color: var(--text);
        margin-bottom: 8px;
    }
    .form-group label .required {
        color: var(--danger);
        margin-left: 2px;
    }
    .form-group .hint {
        font-size: 12px;
        color: var(--text-light);
        margin-top: 6px;
    }
    .form-actions {
        display: flex;
        gap: 12px;
        margin-top: 28px;
        padding-top: 20px;
        border-top: 1px solid var(--border);
    }
    @media (max-width: 480px) {
        .form-card { padding: 20px 16px; }
        .form-actions { flex-direction: column; }
        .form-actions .btn { width: 100%; }
    }
</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_list.php">短剧列表</a>
        <a href="logout.php" style="color:var(--danger);">退出</a>
    </div>
</nav>

<div class="container">
    <div class="form-card animate-fade-in">
        <h2>📝 添加新短剧</h2>

        <?php if ($msg): ?>
        <div class="alert alert-<?php echo $msg_type; ?>">
            <?php echo $msg_type === 'success' ? '✅' : '⚠️'; ?> <?php echo $msg; ?>
        </div>
        <?php endif; ?>

        <form method="post">
            <div class="form-group">
                <label>短剧名称<span class="required">*</span></label>
                <input type="text" name="title" class="input" placeholder="请输入短剧名称" required>
            </div>

            <div class="form-group">
                <label>分类</label>
                <select name="category" class="select">
                    <?php foreach ($cateList as $c): if ($c === '全部') continue; ?>
                    <option value="<?php echo htmlspecialchars($c); ?>"><?php echo htmlspecialchars($c); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div class="form-group">
                <label>标签</label>
                <input type="text" name="tags" class="input" placeholder="多个标签用逗号分隔，例如：逆袭,打脸,甜宠">
                <div class="hint">标签用于搜索和分类展示</div>
            </div>

            <div class="form-group">
                <label>网盘资源链接</label>
                <textarea name="resource_link" class="textarea" rows="3" placeholder="百度网盘 / 夸克 / 阿里云盘等链接"></textarea>
            </div>

            <div class="form-group">
                <label>热搜权重</label>
                <input type="number" name="hot_sort" value="0" class="input" placeholder="数字越大排名越靠前">
                <div class="hint">首页按权重从高到低排序，热门剧可设置较大值</div>
            </div>

            <div class="form-actions">
                <button type="submit" class="btn btn-primary">✅ 提交新增</button>
                <a href="drama_list.php" class="btn btn-outline">取消</a>
            </div>
        </form>
    </div>
</div>
</body>
</html>
