<script setup lang="ts">
import { ref, watch } from 'vue';

import { $t } from '@vben/locales';

import { ElForm, ElFormItem, ElInput, ElSwitch } from 'element-plus';

import SmartInput from '../components/SmartInput.vue';

const props = defineProps<{ data: any; nodeId: string }>();
const emit = defineEmits(['update']);

const form = ref({
  label: $t('ai-platform.workflow.panels.formCreate.label'),
  form_basic_info: '',
  database_design: '',
  form_ui_design: '',
  list_config: '',
  application_id: '',
  update_if_exists: true,
  ...props.data,
});

watch(
  form,
  (val) => {
    emit('update', val);
  },
  { deep: true },
);
</script>

<template>
  <ElForm label-position="top" size="small" class="space-y-4">
    <ElFormItem :label="$t('ai-platform.workflow.panels.common.nodeName')">
      <ElInput
        v-model="form.label"
        :placeholder="$t('ai-platform.workflow.panels.formCreate.placeholder')"
      />
    </ElFormItem>

    <ElFormItem>
      <template #label>
        <div class="flex items-center gap-2">
          <span>{{
            $t('ai-platform.workflow.panels.formCreate.formBasicInfo')
          }}</span>
          <span class="text-red-500">*</span>
          <span class="text-muted-foreground text-xs">(object)</span>
        </div>
      </template>
      <SmartInput
        v-model="form.form_basic_info"
        :current-node-id="nodeId"
        type="textarea"
        :rows="2"
        placeholder="{{form_basic_info.form_basic_info}}"
      />
      <div class="text-muted-foreground mt-1 text-xs">
        {{ $t('ai-platform.workflow.panels.formCreate.basicInfoHint') }}
      </div>
    </ElFormItem>

    <ElFormItem>
      <template #label>
        <div class="flex items-center gap-2">
          <span>{{
            $t('ai-platform.workflow.panels.formCreate.dbDesignConfig')
          }}</span>
          <span class="text-red-500">*</span>
          <span class="text-muted-foreground text-xs">(object)</span>
        </div>
      </template>
      <SmartInput
        v-model="form.database_design"
        :current-node-id="nodeId"
        type="textarea"
        :rows="2"
        placeholder="{{form_database_design.database_design}}"
      />
      <div class="text-muted-foreground mt-1 text-xs">
        {{ $t('ai-platform.workflow.panels.formCreate.dbDesignHint') }}
      </div>
    </ElFormItem>

    <ElFormItem>
      <template #label>
        <div class="flex items-center gap-2">
          <span>{{
            $t('ai-platform.workflow.panels.formCreate.uiDesignConfig')
          }}</span>
          <span class="text-red-500">*</span>
          <span class="text-muted-foreground text-xs">(object)</span>
        </div>
      </template>
      <SmartInput
        v-model="form.form_ui_design"
        :current-node-id="nodeId"
        type="textarea"
        :rows="2"
        placeholder="{{form_ui_design.form_ui_design}}"
      />
      <div class="text-muted-foreground mt-1 text-xs">
        {{ $t('ai-platform.workflow.panels.formCreate.uiDesignHint') }}
      </div>
    </ElFormItem>

    <ElFormItem>
      <template #label>
        <div class="flex items-center gap-2">
          <span>{{
            $t('ai-platform.workflow.panels.formCreate.listDesignConfig')
          }}</span>
          <span class="text-red-500">*</span>
          <span class="text-muted-foreground text-xs">(object)</span>
        </div>
      </template>
      <SmartInput
        v-model="form.list_config"
        :current-node-id="nodeId"
        type="textarea"
        :rows="2"
        placeholder="{{form_list_design.list_config}}"
      />
      <div class="text-muted-foreground mt-1 text-xs">
        {{ $t('ai-platform.workflow.panels.formCreate.listDesignHint') }}
      </div>
    </ElFormItem>

    <ElFormItem>
      <template #label>
        <div class="flex items-center gap-2">
          <span>{{ $t('ai-platform.workflow.panels.formCreate.appId') }}</span>
          <span class="text-muted-foreground text-xs">{{
            $t('ai-platform.workflow.panels.llm.optional')
          }}</span>
        </div>
      </template>
      <SmartInput
        v-model="form.application_id"
        :current-node-id="nodeId"
        :placeholder="
          $t('ai-platform.workflow.panels.formCreate.appIdPlaceholder')
        "
      />
    </ElFormItem>

    <ElFormItem
      :label="$t('ai-platform.workflow.panels.formCreate.updateIfExists')"
    >
      <ElSwitch v-model="form.update_if_exists" />
      <div class="text-muted-foreground mt-1 text-xs">
        {{ $t('ai-platform.workflow.panels.formCreate.updateIfExistsHint') }}
      </div>
    </ElFormItem>

    <!-- 输出变量说明 -->
    <div
      class="mt-4 rounded border border-green-200 bg-green-50 p-3 dark:border-green-800 dark:bg-green-950"
    >
      <div class="mb-2 font-medium text-green-700 dark:text-green-300">
        {{ $t('ai-platform.workflow.panels.common.outputVars') }}
      </div>
      <div class="text-muted-foreground space-y-1 text-xs">
        <div class="flex justify-between">
          <span class="font-mono">form_id</span>
          <span>{{
            $t('ai-platform.workflow.panels.formCreate.formIdOutput')
          }}</span>
        </div>
        <div class="flex justify-between">
          <span class="font-mono">form_code</span>
          <span>{{
            $t('ai-platform.workflow.panels.formCreate.formCodeOutput')
          }}</span>
        </div>
        <div class="flex justify-between">
          <span class="font-mono">form_meta</span>
          <span>{{
            $t('ai-platform.workflow.panels.formCreate.formMetaOutput')
          }}</span>
        </div>
      </div>
    </div>

    <!-- 工作流说明 -->
    <div
      class="mt-4 rounded border border-blue-200 bg-blue-50 p-3 dark:border-blue-800 dark:bg-blue-950"
    >
      <div class="mb-2 font-medium text-blue-700 dark:text-blue-300">
        {{ $t('ai-platform.workflow.panels.formCreate.fullWorkflow') }}
      </div>
      <div class="text-muted-foreground text-xs">
        <div class="flex items-center gap-1">
          <span>{{
            $t('ai-platform.workflow.panels.formCreate.formBasicInfo')
          }}</span>
          <span>→</span>
          <span>{{
            $t('ai-platform.workflow.panels.formCreate.dbDesign')
          }}</span>
          <span>→</span>
          <span>{{
            $t('ai-platform.workflow.panels.formCreate.dbCreate')
          }}</span>
        </div>
        <div class="mt-1 flex items-center gap-1">
          <span>→</span>
          <span>{{
            $t('ai-platform.workflow.panels.formCreate.uiDesign')
          }}</span>
          <span>→</span>
          <span>{{
            $t('ai-platform.workflow.panels.formCreate.listDesign')
          }}</span>
          <span>→</span>
          <span class="font-medium text-blue-600 dark:text-blue-400">{{
            $t('ai-platform.workflow.panels.formCreate.formCreateStep')
          }}</span>
          <span>→</span>
          <span>{{
            $t('ai-platform.workflow.panels.formCreate.formPublish')
          }}</span>
        </div>
      </div>
    </div>
  </ElForm>
</template>
