<template>
  <div class="create-page">
    <h2 class="page-title">创作绘本</h2>
    <p class="page-desc">上传你的画作，描述你的故事构思，AI 将为你生成一本完整的绘本</p>

    <!-- Step 1: 上传画作 -->
    <div class="step-card">
      <div class="step-header">
        <span class="step-num">1</span>
        <span class="step-title">上传你的画作</span>
      </div>
      <div class="upload-area">
        <div v-if="imagePreview" class="image-preview">
          <img :src="imagePreview" alt="画作预览" />
          <a-button type="text" size="small" class="remove-btn" @click="removeImage">
            <close-circle-outlined />
          </a-button>
        </div>
        <a-upload
          v-else
          :before-upload="handleImageUpload"
          :show-upload-list="false"
          accept="image/*"
        >
          <div class="upload-trigger">
            <camera-outlined class="upload-icon" />
            <span>拍照或从相册选择</span>
          </div>
        </a-upload>
      </div>
    </div>

    <!-- Step 2: 描述构思 -->
    <div class="step-card">
      <div class="step-header">
        <span class="step-num">2</span>
        <span class="step-title">描述你的故事构思</span>
      </div>
      <p class="step-hint">告诉 AI 你画的是什么角色，发生了什么故事</p>
      <a-textarea
        v-model:value="textInput"
        placeholder="例如：这是一只叫小星的兔子，它在森林里迷路了，遇到了一只好心的猫头鹰带它回家..."
        :rows="4"
        :maxlength="500"
        show-count
      />
    </div>

    <!-- 提交按钮 -->
    <a-button
      type="primary"
      size="large"
      block
      class="submit-btn"
      :loading="submitting"
      :disabled="!imagePreview || !textInput.trim()"
      @click="handleSubmit"
    >
      开始生成绘本
    </a-button>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { CameraOutlined, CloseCircleOutlined } from '@ant-design/icons-vue'
import { publicCreationApi } from '@/api/public'

const router = useRouter()
const imagePreview = ref<string | null>(null)
const imageFile = ref<File | null>(null)
const textInput = ref('')
const submitting = ref(false)

// 模拟图片上传（P0 阶段直接用 base64/本地 URL，后续对接 COS）
const handleImageUpload = (file: File) => {
  imageFile.value = file
  const reader = new FileReader()
  reader.onload = (e) => {
    imagePreview.value = e.target?.result as string
  }
  reader.readAsDataURL(file)
  return false // 阻止自动上传
}

const removeImage = () => {
  imagePreview.value = null
  imageFile.value = null
}

const handleSubmit = async () => {
  if (!imagePreview.value || !textInput.value.trim()) {
    message.warning('请上传画作并描述你的故事构思')
    return
  }

  submitting.value = true
  try {
    // TODO: 实际场景先上传图片到 COS，获取 URL
    // P0 阶段模拟：直接传 base64 或先上传
    const originalImageUrl = imagePreview.value // 临时用 base64

    const result = await publicCreationApi.submit({
      originalImageUrl,
      textInput: textInput.value,
    })

    message.success('创作请求已提交！')
    router.push(`/p/create/generating/${result.id}`)
  } catch (err: any) {
    message.error(err?.response?.data?.message || '提交失败')
  } finally {
    submitting.value = false
  }
}
</script>

<style scoped lang="scss">
$primary: #6366f1;

.create-page {
  max-width: 600px;
  margin: 0 auto;
}

.page-title {
  font-size: 22px;
  font-weight: 700;
  color: #1e1b4b;
  margin: 0 0 6px;
}

.page-desc {
  font-size: 13px;
  color: #9ca3af;
  margin: 0 0 24px;
}

.step-card {
  background: #fff;
  border-radius: 16px;
  padding: 20px;
  margin-bottom: 16px;
  border: 1px solid rgba($primary, 0.06);
}

.step-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;

  .step-num {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: $primary;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 700;
  }

  .step-title {
    font-size: 15px;
    font-weight: 600;
    color: #1e1b4b;
  }
}

.step-hint {
  font-size: 12px;
  color: #9ca3af;
  margin: 0 0 10px;
}

.upload-area {
  .upload-trigger {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 40px;
    border: 2px dashed rgba($primary, 0.2);
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.2s;
    color: $primary;

    &:hover {
      border-color: $primary;
      background: rgba($primary, 0.02);
    }

    .upload-icon { font-size: 32px; }
    span { font-size: 13px; }
  }

  .image-preview {
    position: relative;
    border-radius: 12px;
    overflow: hidden;

    img {
      width: 100%;
      max-height: 300px;
      object-fit: contain;
      display: block;
    }

    .remove-btn {
      position: absolute;
      top: 8px;
      right: 8px;
      color: #fff;
      background: rgba(0, 0, 0, 0.5);
      border-radius: 50%;
      font-size: 18px;
    }
  }
}

.submit-btn {
  height: 48px !important;
  border-radius: 14px !important;
  font-size: 16px !important;
  font-weight: 600 !important;
  margin-top: 8px;
}
</style>
