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

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

import {
  ElForm,
  ElFormItem,
  ElInput,
  ElInputNumber,
  ElOption,
  ElSelect,
} 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.formBasicInfo.label'),
  name: '',
  code: '',
  form_type: 'normal',
  description: '',
  sort: 0,
  auto_generate_code: false,
  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.formBasicInfo.placeholder')
        "
      />
    </ElFormItem>

    <ElFormItem
      :label="$t('ai-platform.workflow.panels.formBasicInfo.formName')"
      required
    >
      <template #label>
        <div class="flex items-center gap-2">
          <span>{{
            $t('ai-platform.workflow.panels.formBasicInfo.formName')
          }}</span>
          <span class="text-red-500">*</span>
        </div>
      </template>
      <SmartInput
        v-model="form.name"
        :current-node-id="nodeId"
        :placeholder="
          $t('ai-platform.workflow.panels.formBasicInfo.formNamePlaceholder') +
          ' {{llm_basic_name}}'
        "
      />
      <div class="text-muted-foreground mt-1 text-xs">
        {{ $t('ai-platform.workflow.panels.formBasicInfo.formNameHint')
        }}{{ llm_basic_name }}
      </div>
    </ElFormItem>

    <ElFormItem
      :label="$t('ai-platform.workflow.panels.formBasicInfo.formCode')"
    >
      <SmartInput
        v-model="form.code"
        :current-node-id="nodeId"
        :placeholder="
          $t('ai-platform.workflow.panels.formBasicInfo.formCodePlaceholder') +
          ' {{llm_basic_code}}'
        "
      />
      <div class="text-muted-foreground mt-1 text-xs">
        {{ $t('ai-platform.workflow.panels.formBasicInfo.formCodeHint') }}
      </div>
    </ElFormItem>

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

    <ElFormItem
      :label="$t('ai-platform.workflow.panels.formBasicInfo.formType')"
    >
      <ElSelect
        v-model="form.form_type"
        :placeholder="
          $t('ai-platform.workflow.panels.formBasicInfo.formTypePlaceholder')
        "
      >
        <ElOption
          :label="$t('ai-platform.workflow.panels.formBasicInfo.normalForm')"
          value="normal"
        />
        <ElOption
          :label="$t('ai-platform.workflow.panels.formBasicInfo.workflowForm')"
          value="workflow"
        />
      </ElSelect>
    </ElFormItem>

    <ElFormItem
      :label="$t('ai-platform.workflow.panels.formBasicInfo.formDesc')"
    >
      <SmartInput
        v-model="form.description"
        :current-node-id="nodeId"
        type="textarea"
        :rows="3"
        :placeholder="
          $t('ai-platform.workflow.panels.formBasicInfo.formDescPlaceholder')
        "
      />
    </ElFormItem>

    <ElFormItem :label="$t('ai-platform.workflow.panels.formBasicInfo.sort')">
      <ElInputNumber
        v-model="form.sort"
        :min="0"
        :placeholder="
          $t('ai-platform.workflow.panels.formBasicInfo.sortPlaceholder')
        "
        class="w-full"
      />
      <div class="text-muted-foreground mt-1 text-xs">
        {{ $t('ai-platform.workflow.panels.formBasicInfo.sortHint') }}
      </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">form_basic_info</span>
          <span>{{
            $t('ai-platform.workflow.panels.formBasicInfo.fullConfigOutput')
          }}</span>
        </div>
        <div class="flex justify-between">
          <span class="font-mono">form_name</span>
          <span>{{
            $t('ai-platform.workflow.panels.formBasicInfo.formNameOutput')
          }}</span>
        </div>
        <div class="flex justify-between">
          <span class="font-mono">form_code</span>
          <span>{{
            $t('ai-platform.workflow.panels.formBasicInfo.formCodeOutput')
          }}</span>
        </div>
        <div class="flex justify-between">
          <span class="font-mono">form_type</span>
          <span>{{
            $t('ai-platform.workflow.panels.formBasicInfo.formTypeOutput')
          }}</span>
        </div>
        <div class="flex justify-between">
          <span class="font-mono">form_description</span>
          <span>{{
            $t('ai-platform.workflow.panels.formBasicInfo.formDescOutput')
          }}</span>
        </div>
        <div class="flex justify-between">
          <span class="font-mono">form_sort</span>
          <span>{{
            $t('ai-platform.workflow.panels.formBasicInfo.sortOutput')
          }}</span>
        </div>
      </div>
    </div>
  </ElForm>
</template>
