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

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

import {
  ElForm,
  ElFormItem,
  ElInput,
  ElInputNumber,
  ElOption,
  ElSelect,
  ElSwitch,
} 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.formUIDesign.label'),
  database_design: '',
  form_name: '',
  form_code: '',
  layout_mode: 'auto',
  label_width: 120,
  enable_grouping: true,
  hide_system_fields: true,
  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.formUIDesign.placeholder')
        "
      />
    </ElFormItem>

    <ElFormItem>
      <template #label>
        <div class="flex items-center gap-2">
          <span>{{
            $t('ai-platform.workflow.panels.formUIDesign.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="3"
        :placeholder="
          $t(
            'ai-platform.workflow.panels.formUIDesign.dbDesignConfigPlaceholder',
          ) + ' {{form_database_design.database_design}}'
        "
      />
      <div class="text-muted-foreground mt-1 text-xs">
        {{ $t('ai-platform.workflow.panels.formUIDesign.dbDesignConfigHint') }}
      </div>
    </ElFormItem>

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

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

    <ElFormItem
      :label="$t('ai-platform.workflow.panels.formUIDesign.layoutMode')"
    >
      <ElSelect
        v-model="form.layout_mode"
        :placeholder="
          $t('ai-platform.workflow.panels.formUIDesign.layoutModePlaceholder')
        "
      >
        <ElOption
          :label="$t('ai-platform.workflow.panels.formUIDesign.layoutAuto')"
          value="auto"
        />
        <ElOption
          :label="$t('ai-platform.workflow.panels.formUIDesign.layoutSingle')"
          value="single"
        />
        <ElOption
          :label="$t('ai-platform.workflow.panels.formUIDesign.layoutDouble')"
          value="double"
        />
        <ElOption
          :label="$t('ai-platform.workflow.panels.formUIDesign.layoutTriple')"
          value="triple"
        />
      </ElSelect>
      <div class="text-muted-foreground mt-1 text-xs">
        {{ $t('ai-platform.workflow.panels.formUIDesign.layoutAutoHint') }}
      </div>
    </ElFormItem>

    <ElFormItem
      :label="$t('ai-platform.workflow.panels.formUIDesign.labelWidth')"
    >
      <ElInputNumber
        v-model="form.label_width"
        :min="60"
        :max="200"
        :step="10"
        controls-position="right"
      />
      <div class="text-muted-foreground mt-1 text-xs">
        {{ $t('ai-platform.workflow.panels.formUIDesign.labelWidthHint') }}
      </div>
    </ElFormItem>

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

    <ElFormItem
      :label="$t('ai-platform.workflow.panels.formUIDesign.hideSystemFields')"
    >
      <ElSwitch v-model="form.hide_system_fields" />
      <div class="text-muted-foreground mt-1 text-xs">
        {{
          $t('ai-platform.workflow.panels.formUIDesign.hideSystemFieldsHint')
        }}
      </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_ui_design</span>
          <span>{{
            $t('ai-platform.workflow.panels.formUIDesign.fullConfigOutput')
          }}</span>
        </div>
        <div class="flex justify-between">
          <span class="font-mono">form_code</span>
          <span>{{
            $t('ai-platform.workflow.panels.formUIDesign.formCodeOutput')
          }}</span>
        </div>
        <div class="flex justify-between">
          <span class="font-mono">field_count</span>
          <span>{{
            $t('ai-platform.workflow.panels.formUIDesign.fieldCountOutput')
          }}</span>
        </div>
        <div class="flex justify-between">
          <span class="font-mono">group_count</span>
          <span>{{
            $t('ai-platform.workflow.panels.formUIDesign.groupCountOutput')
          }}</span>
        </div>
      </div>
    </div>

    <!-- 智能映射说明 -->
    <div
      class="mt-4 rounded border border-purple-200 bg-purple-50 p-3 dark:border-purple-800 dark:bg-purple-950"
    >
      <div class="mb-2 font-medium text-purple-700 dark:text-purple-300">
        {{ $t('ai-platform.workflow.panels.formUIDesign.mappingRules') }}
      </div>
      <div class="text-muted-foreground space-y-1 text-xs">
        <div>• {{ $t('ai-platform.workflow.panels.formUIDesign.mappingRule1') }}</div>
        <div>• {{ $t('ai-platform.workflow.panels.formUIDesign.mappingRule2') }}</div>
        <div>• {{ $t('ai-platform.workflow.panels.formUIDesign.mappingRule3') }}</div>
        <div>• {{ $t('ai-platform.workflow.panels.formUIDesign.mappingRule4') }}</div>
        <div>• {{ $t('ai-platform.workflow.panels.formUIDesign.mappingRule5') }}</div>
        <div>• {{ $t('ai-platform.workflow.panels.formUIDesign.mappingRule6') }}</div>
      </div>
    </div>
  </ElForm>
</template>
