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

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

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

import ConfirmModeSelect from '../components/ConfirmModeSelect.vue';
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.formPublish.label'),
  form_id: '',
  form_code: '',
  require_confirmation: 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.formPublish.placeholder')"
      />
    </ElFormItem>

    <ElFormItem>
      <template #label>
        <div class="flex items-center gap-2">
          <span>{{
            $t('ai-platform.workflow.panels.formPublish.formId')
          }}</span>
          <span class="text-red-500">*</span>
          <span class="text-muted-foreground text-xs">(string)</span>
        </div>
      </template>
      <SmartInput
        v-model="form.form_id"
        :current-node-id="nodeId"
        placeholder="{{form_create.form_id}}"
      />
      <div class="text-muted-foreground mt-1 text-xs">
        {{ $t('ai-platform.workflow.panels.formPublish.formIdHint') }}
      </div>
    </ElFormItem>

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

    <ElFormItem :label="$t('ai-platform.workflow.panels.common.confirmMode')">
      <ConfirmModeSelect
        v-model="form.require_confirmation"
        :current-node-id="nodeId"
      />
    </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">menu_id</span>
          <span>{{
            $t('ai-platform.workflow.panels.formPublish.menuIdOutput')
          }}</span>
        </div>
        <div class="flex justify-between">
          <span class="font-mono">route_path</span>
          <span>{{
            $t('ai-platform.workflow.panels.formPublish.routePathOutput')
          }}</span>
        </div>
        <div class="flex justify-between">
          <span class="font-mono">publish_result</span>
          <span>{{
            $t('ai-platform.workflow.panels.formPublish.publishResultOutput')
          }}</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.common.usage') }}
      </div>
      <div class="text-muted-foreground text-xs">
        <p>{{ $t('ai-platform.workflow.panels.formPublish.usageTip1') }}</p>
        <p class="mt-1">
          {{ $t('ai-platform.workflow.panels.formPublish.usageTip2') }}
        </p>
      </div>
    </div>
  </ElForm>
</template>
