<template>
  <el-dialog v-model="dialogVisible" title="场景图生成" width="600px" :close-on-click-modal="false"
    :close-on-press-escape="false" custom-class="scene-prompt-dialog" @close="handleClose">
    <div class="scene-prompt-container">
      <!-- 场景提示词输入区域 -->
      <div class="input-section">
        <div class="input-wrapper">
          <el-input v-model="scenePrompt" type="textarea" :rows="8" placeholder="请输入场景提示词" class="scene-input"
            resize="none" maxlength="500"  />
          <!-- AI帮我写按钮 -->
          <div class="ai-help-button-container">
            <el-button type="primary"  @click="handleAIHelp" :loading="aiLoading" class="ai-help-button">
              AI帮我写
            </el-button>
          </div>
        </div>
      </div>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleConfirm" :disabled="!canConfirm" class="confirm-button">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { expandCameraWordsApi } from '@/apis/other'

// 定义组件的 props
interface Props {
  modelValue: boolean
  initialPrompt?: string
}

const props = defineProps<Props>()

// 定义组件的事件
const emit = defineEmits<{
  'update:modelValue': [value: boolean]
  confirm: [prompt: string]
  cancel: []
}>()

// Dialog 显示状态
const dialogVisible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

// 场景提示词
const scenePrompt = ref('')

// 本地缓存 key
const SCENE_PROMPT_CACHE_KEY = 'scene_prompt_cache'

// 从本地缓存读取
const loadScenePromptFromCache = () => {
  try {
    const cache = localStorage.getItem(SCENE_PROMPT_CACHE_KEY)
    if (cache && !props.initialPrompt) {
      scenePrompt.value = cache
    }
  } catch {}
}

// 写入本地缓存
const saveScenePromptToCache = () => {
  try {
    const value = (scenePrompt.value || '').trim()
    if (value) {
      localStorage.setItem(SCENE_PROMPT_CACHE_KEY, value)
    }
  } catch {}
}

// AI 加载状态
const aiLoading = ref(false)

// 是否可以确认
const canConfirm = computed(() => {
  return scenePrompt.value.trim().length > 0
})

// AI 帮我写
const handleAIHelp = async () => {
  if (aiLoading.value) return

  aiLoading.value = true
  try {
    // 调用后端接口生成场景提示词
    const res: any = await expandCameraWordsApi()

    const result = res?.data?.result || ''
    if (result) {
      scenePrompt.value = result
      ElMessage.success('AI 已为您生成场景提示词')
    } else {
      ElMessage.error('AI 未返回结果，请稍后重试')
    }
  } catch (error) {
    ElMessage.error('AI 生成失败，请重试')
  } finally {
    aiLoading.value = false
  }
}

// 确认
const handleConfirm = () => {
  if (!canConfirm.value) return

  saveScenePromptToCache()
  emit('confirm', scenePrompt.value.trim())
  dialogVisible.value = false
  resetForm()
}


// 关闭弹窗
const handleClose = () => {
  emit('cancel')
}

// 重置表单
const resetForm = () => {
  scenePrompt.value = ''
}

// 监听弹窗显示状态变化，初始化场景提示词
watch(dialogVisible, (newValue) => {
  if (newValue) {
    if (props.initialPrompt) {
      scenePrompt.value = props.initialPrompt
    } else {
      loadScenePromptFromCache()
    }
  }
}, { immediate: true })
</script>

<style lang="scss" scoped>
.scene-prompt-container {
  padding: 20px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: #EAECED;
}

.input-section {
  position: relative;
}

.input-wrapper {
  position: relative;
  background: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #e4e7ed;
}

.scene-input {
  :deep(.el-textarea__inner) {
    border: none;
    background: transparent;
    font-size: 16px;
    line-height: 1.6;
    color: #303133;
    resize: none;

    &::placeholder {
      color: #c0c4cc;
    }

    &:focus {
      box-shadow: none;
    }
  }
}

.ai-help-button-container {
  position: absolute;
  bottom: 15px;
  height: 25px;
  right: 6px;
}

.ai-help-button {
  background: #2957FF;
  border: none;
  border-radius: 20px;
  padding: 8px 16px;
  font-size: 13px;
  font-weight: 500;
  color: white;
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(64, 158, 255, 0.4);
  }

  &:active {
    transform: translateY(0);
  }

  .ai-icon {
    margin-right: 6px;
    font-size: 14px;
  }
}

.dialog-footer {
  display: flex;
  justify-content: center;
  gap: 16px;
  padding: 14px 20px;
}

.confirm-button {
  background: linear-gradient(135deg, #409eff 0%, #a855f7 100%);
  border: none;
  border-radius: 6px;
  height: 36px;
  padding: 12px 32px;
  font-size: 14px;
  font-weight: 500;
  color: white;
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(64, 158, 255, 0.4);
  }

  &:active {
    transform: translateY(0);
  }

  &:disabled {
    background: #c0c4cc;
    transform: none;
    box-shadow: none;
    cursor: not-allowed;
  }
}
</style>

<style lang="scss">
.scene-prompt-dialog {
  .el-dialog__body {
    padding: 0;
  }

  .el-dialog__footer {
    padding: 0 !important;
    background: #EAECED;
  }

  .el-dialog {
    border-radius: 12px !important;
    overflow: hidden;
  }

  .el-dialog__header {
    background: #f8f9fa;
  }

  .el-dialog__title {
    font-size: 18px !important;
    font-weight: 600 !important;
    color: #303133 !important;
    text-align: center !important;
  }
}
</style>
