<?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 = '';
$import_count = 0;

if ($_POST) {
    $text = trim($_POST['batch_text'] ?? '');
    $category_default = escape($_POST['category_default'] ?? '');

    if (!empty($text)) {
        $lines = explode("\n", $text);
        $success = 0;
        $failed = 0;

        foreach ($lines as $line) {
            $line = trim($line);
            if (empty($line)) continue;

            $parts = explode('|', $line);
            $title = escape(trim($parts[0] ?? ''));
            $category = !empty($parts[1]) ? escape(trim($parts[1])) : $category_default;
            $tags = !empty($parts[2]) ? escape(trim($parts[2])) : '';
            $link = !empty($parts[3]) ? escape(trim($parts[3])) : '';
            $hot = !empty($parts[4]) ? intval(trim($parts[4])) : 0;

            if (empty($title)) {
                $failed++;
                continue;
            }

            $sql = "INSERT INTO drama_list(title, category, tags, resource_link, hot_sort) VALUES('$title', '$category', '$tags', '$link', $hot)";
            if (mysqli_query($conn, $sql)) {
                $success++;
            } else {
                $failed++;
            }
        }

        if ($success > 0) {
            $today = date('Y-m-d');
            $now = date('H:i:s');
            $res = mysqli_query($conn, "SELECT id FROM update_log WHERE add_date='$today'");
            if (mysqli_num_rows($res) > 0) {
                mysqli_query($conn, "UPDATE update_log SET add_count=add_count+$success, add_time='$now' WHERE add_date='$today'");
            } else {
                mysqli_query($conn, "INSERT INTO update_log(add_date, add_count, add_time) VALUES('$today', $success, '$now')");
            }
        }

        $import_count = $success;
        $msg = "批量导入完成：成功 {$success} 条，失败 {$failed} 条";
        $msg_type = $failed > 0 ? 'warning' : 'success';
    } else {
        $msg = "请输入要导入的内容";
        $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: 720px;
        margin: 0 auto;
    }
    .form-card h2 {
        font-size: 20px;
        font-weight: 700;
        margin-bottom: 20px;
        padding-bottom: 16px;
        border-bottom: 1px solid var(--border);
        display: flex;
        align-items: center;
        gap: 8px;
    }
    .format-guide {
        background: linear-gradient(135deg, #eff6ff 0%, #f5f3ff 100%);
        border: 1px solid #bfdbfe;
        border-radius: var(--radius-sm);
        padding: 16px;
        margin-bottom: 24px;
        font-size: 13px;
        color: #1e40af;
    }
    .format-guide h4 {
        font-size: 14px;
        font-weight: 700;
        margin-bottom: 8px;
        color: #1e3a8a;
    }
    .format-guide code {
        background: white;
        padding: 2px 6px;
        border-radius: 4px;
        font-family: 'Courier New', monospace;
        font-size: 12px;
    }
    .format-guide .example {
        background: white;
        padding: 10px 12px;
        border-radius: 6px;
        margin-top: 8px;
        font-family: 'Courier New', monospace;
        font-size: 12px;
        line-height: 1.8;
        color: var(--text);
        border: 1px solid #e0e7ff;
    }
    .form-group {
        margin-bottom: 20px;
    }
    .form-group label {
        display: block;
        font-size: 14px;
        font-weight: 600;
        color: var(--text);
        margin-bottom: 8px;
    }
    .form-group textarea {
        font-family: 'Courier New', monospace;
        font-size: 13px;
        line-height: 1.8;
        min-height: 240px;
    }
    .form-actions {
        display: flex;
        gap: 12px;
        margin-top: 24px;
        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
            $icon = $msg_type === 'success' ? '✅' : ($msg_type === 'warning' ? '⚠️' : '❌');
            echo $icon . ' ' . $msg;
            ?>
        </div>
        <?php endif; ?>

        <!-- 格式说明 -->
        <div class="format-guide">
            <h4>📋 格式说明</h4>
            <p>每行一部短剧，字段用 <code>|</code> 竖线分隔，顺序为：</p>
            <p style="margin-top:6px;font-weight:600;">剧名 | 分类 | 标签 | 网盘链接 | 热搜权重</p>
            <p style="margin-top:6px;">分类、标签、链接、权重均可留空，留空时使用下方默认分类。</p>
            <div class="example">
闪婚后傅先生马甲藏不住了|都市爱情|先婚后爱,甜宠|https://pan.baidu.com/s/xxx|9999<br>
女王|女性成长|马甲,打脸|https://pan.quark.cn/s/xxx|9998<br>
重生后我成了首富||||9000
            </div>
        </div>

        <form method="post">
            <div class="form-group">
                <label>默认分类（未指定分类时使用）</label>
                <select name="category_default" 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>
                <textarea name="batch_text" class="textarea"
                          placeholder="每行一部短剧，用 | 分隔字段"></textarea>
            </div>

            <div class="form-actions">
                <button type="submit" class="btn btn-success">🚀 开始导入</button>
                <a href="drama_list.php" class="btn btn-outline">返回列表</a>
            </div>
        </form>
    </div>
</div>
</body>
</html>
