<?php
/**
 * 我要投稿
 */

$error = '';
$success = false;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title = trim($_POST['title'] ?? '');
    $channel_id = intval($_POST['channel_id'] ?? 0);
    $content = trim($_POST['content'] ?? '');
    $description = trim($_POST['description'] ?? '');
    $litpic = trim($_POST['litpic'] ?? '');

    if (empty($title)) {
        $error = '标题不能为空';
    } elseif (empty($content)) {
        $error = '内容不能为空';
    } else {
        // 获取栏目名称
        $channel = $db->fetch("SELECT channel_name FROM {$db->table('channel')} WHERE id = ?", [$channel_id]);

        $db->insert('contribute', [
            'member_id' => $_SESSION['member_id'],
            'title' => $title,
            'channel_id' => $channel_id,
            'channel_name' => $channel['channel_name'] ?? '',
            'content' => $content,
            'description' => $description,
            'litpic' => $litpic,
            'status' => 0,
            'add_time' => time()
        ]);
        require_once ROOT_PATH . 'core/lib/common.php';
        $memberInfo = $db->fetch("SELECT username FROM {$db->table('member')} WHERE id = ?", [$_SESSION['member_id']]);
        sendAdminMessage('📝 新投稿：' . $title, "投稿人：{$memberInfo['username']}\n栏目：{$channel['channel_name']}\n摘要：{$description}\n内容：" . msubstr($content, 0, 200), $memberInfo['username'] ?? '会员');
        $success = true;
    }
}

// 获取可投稿栏目
$channels = $db->fetchAll("SELECT id, channel_name FROM {$db->table('channel')} WHERE channel_type IN (1,2) ORDER BY id ASC");
?>
<div class="page-header">
    <h1 class="page-title">
        <i class="icon-edit-3"></i>
        我要投稿
    </h1>
</div>
<div class="page-content">
    <?php if ($error): ?>
    <div class="alert alert-error"><?php echo htmlspecialchars($error); ?></div>
    <?php endif; ?>

    <?php if ($success): ?>
    <div class="alert alert-success">✓ 投稿成功，等待审核</div>
    <?php endif; ?>

    <form method="post" class="contribute-form">
        <div class="form-group">
            <label>标题 <span class="required">*</span></label>
            <input type="text" name="title" class="form-control" placeholder="请输入标题" required>
        </div>

        <div class="form-group">
            <label>栏目</label>
            <select name="channel_id" class="form-control">
                <?php foreach ($channels as $ch): ?>
                <option value="<?php echo $ch['id']; ?>"><?php echo htmlspecialchars($ch['channel_name']); ?></option>
                <?php endforeach; ?>
            </select>
        </div>

        <div class="form-group">
            <label>内容 <span class="required">*</span></label>
            <textarea name="content" class="form-control" rows="12" placeholder="请输入详细内容" required></textarea>
        </div>

        <div class="form-group">
            <label>简介</label>
            <textarea name="description" class="form-control" rows="3" placeholder="简要描述（可选）"></textarea>
        </div>

        <div class="form-group">
            <label>封面图</label>
            <input type="text" name="litpic" class="form-control" placeholder="输入图片URL">
        </div>

        <div class="form-actions">
            <button type="submit" class="btn btn-primary">提交投稿</button>
            <a href="?page=contribute" class="btn btn-outline">返回列表</a>
        </div>
    </form>
</div>
<style>
.contribute-form { max-width: 800px; }
.form-row { display: flex; gap: 20px; }
.form-group { flex: 1; }
.required { color: #ef4444; }
.form-actions { margin-top: 24px; display: flex; gap: 12px; }
.type-switch {
    display: flex;
    gap: 16px;
}
.type-option {
    display: flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    padding: 8px 16px;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    transition: all 0.2s;
}
.type-option input:checked + span {
    color: #10b981;
    font-weight: 600;
}
.type-option:has(input:checked) {
    border-color: #10b981;
    background: #ecfdf5;
}
.type-option input { display: none; }
.alert {
    padding: 12px 16px;
    border-radius: 8px;
    font-size: 14px;
    margin-bottom: 20px;
}
.alert-error { background: #fee2e2; color: #dc2626; }
.alert-success { background: #d1fae5; color: #059669; }
</style>
