<?php
/**
 * 芋头CMS
 * ============================================================================
 * 版权所有 2016-2028 沅陵县乌托邦网络服务有限公司，并保留所有权利。
 * 网站地址: http://www.yutoucms.cn
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 芋头 <mxgsf@qq.com>
 * Date: 2026-5-20
 */
require_once dirname(__DIR__) . '/../core/lib/common.php';

$memberId = intval($_SESSION['member_id'] ?? 0);
$id = intval($_GET['id'] ?? 0);
$error = '';
$success = false;

$row = $db->fetch(
    "SELECT * FROM {$db->table('contribute')} WHERE id = ? AND member_id = ?",
    [$id, $memberId]
);

if (!$row) {
    echo '<div class="page-empty">投稿不存在或无权编辑</div>';
    return;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    requireMemberCsrfToken();
    $title = trim($_POST['title'] ?? '');
    $channelId = intval($_POST['channel_id'] ?? 0);
    $contentType = intval($_POST['content_type'] ?? 1);
    $litpic = trim($_POST['litpic'] ?? '');
    $price = floatval($_POST['price'] ?? 0);
    $description = trim($_POST['description'] ?? '');
    $content = trim($_POST['content'] ?? '');

    if ($title === '') {
        $error = '标题不能为空';
    } elseif ($channelId <= 0) {
        $error = '请选择投稿栏目';
    } elseif ($content === '') {
        $error = '内容不能为空';
    } else {
        $channel = $db->fetch(
            "SELECT channel_name FROM {$db->table('channel')} WHERE id = ?",
            [$channelId]
        );

        if (!$channel) {
            $error = '投稿栏目不存在';
        } else {
            $db->update('contribute', [
                'title' => $title,
                'channel_id' => $channelId,
                'channel_name' => $channel['channel_name'],
                'content_type' => in_array($contentType, [1, 2], true) ? $contentType : 1,
                'litpic' => $litpic,
                'price' => $price,
                'description' => $description,
                'content' => $content,
                'status' => 0,
                'update_time' => time(),
            ], 'id = ? AND member_id = ?', [$id, $memberId]);

            $row = $db->fetch(
                "SELECT * FROM {$db->table('contribute')} WHERE id = ? AND member_id = ?",
                [$id, $memberId]
            );
            $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">
    <div class="page-header-actions">
        <a href="?page=contribute" class="btn btn-outline">返回列表</a>
    </div>
    <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">
        <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars(getMemberCsrfToken()); ?>">
        <div class="form-group">
            <label>标题 <span class="required">*</span></label>
            <input type="text" name="title" class="form-control" value="<?php echo htmlspecialchars($row['title'] ?? ''); ?>" placeholder="请输入标题" required>
        </div>

        <div class="form-row">
            <div class="form-group">
                <label>栏目 <span class="required">*</span></label>
                <select name="channel_id" class="form-control" required>
                    <option value="">请选择栏目</option>
                    <?php foreach ($channels as $ch): ?>
                    <option value="<?php echo $ch['id']; ?>" <?php echo intval($row['channel_id'] ?? 0) === intval($ch['id']) ? 'selected' : ''; ?>><?php echo htmlspecialchars($ch['channel_name']); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group">
                <label>类型</label>
                <select name="content_type" class="form-control">
                    <option value="1" <?php echo intval($row['content_type'] ?? 1) === 1 ? 'selected' : ''; ?>>文章</option>
                    <option value="2" <?php echo intval($row['content_type'] ?? 1) === 2 ? 'selected' : ''; ?>>产品</option>
                </select>
            </div>
        </div>

        <div class="form-row">
            <div class="form-group">
                <label>封面图</label>
                <input type="text" name="litpic" class="form-control" value="<?php echo htmlspecialchars($row['litpic'] ?? ''); ?>" placeholder="请输入图片URL">
            </div>
            <div class="form-group">
                <label>价格</label>
                <input type="number" step="0.01" min="0" name="price" class="form-control" value="<?php echo htmlspecialchars((string)($row['price'] ?? '0')); ?>" placeholder="产品投稿可填写价格">
            </div>
        </div>

        <div class="form-group">
            <label>摘要</label>
            <textarea name="description" class="form-control" rows="3" placeholder="简要描述，可选"><?php echo htmlspecialchars($row['description'] ?? ''); ?></textarea>
        </div>

        <div class="form-group">
            <label>内容 <span class="required">*</span></label>
            <textarea name="content" class="form-control" rows="12" placeholder="请输入详细内容" required><?php echo htmlspecialchars($row['content'] ?? ''); ?></textarea>
        </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; margin-bottom: 18px; }
.required { color: #ef4444; }
.form-actions { margin-top: 24px; display: flex; gap: 12px; }
.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; }
@media (max-width: 768px) {
    .form-row { flex-direction: column; gap: 0; }
}
</style>
