<script setup lang="ts">
import { computed } from 'vue';

import { Code, FilePlus, FileText, Search, Trash2 } from '@vben/icons';
import { $t } from '@vben/locales';

import BaseNode from './BaseNode.vue';

const props = defineProps<{
  data: any;
  id: string;
  selected?: boolean;
  type: string;
}>();

// 根据节点类型获取显示信息
const nodeInfo = computed(() => {
  switch (props.type) {
    case 'form_data_create': {
      return {
        label: $t('ai-platform.workflow.nodes.form_data.create'),
        icon: FilePlus,
        desc: $t('ai-platform.workflow.nodes.form_data.createDesc'),
      };
    }
    case 'form_data_delete': {
      return {
        label: $t('ai-platform.workflow.nodes.form_data.delete'),
        icon: Trash2,
        desc: $t('ai-platform.workflow.nodes.form_data.deleteDesc'),
      };
    }
    case 'form_data_list': {
      return {
        label: $t('ai-platform.workflow.nodes.form_data.list'),
        icon: Search,
        desc: $t('ai-platform.workflow.nodes.form_data.listDesc'),
      };
    }
    case 'form_data_read': {
      return {
        label: $t('ai-platform.workflow.nodes.form_data.read'),
        icon: FileText,
        desc: $t('ai-platform.workflow.nodes.form_data.readDesc'),
      };
    }
    case 'form_data_update': {
      return {
        label: $t('ai-platform.workflow.nodes.form_data.update'),
        icon: FileText,
        desc: $t('ai-platform.workflow.nodes.form_data.updateDesc'),
      };
    }
    case 'form_schema_to_llm': {
      return {
        label: $t('ai-platform.workflow.nodes.form_data.schemaToLLM'),
        icon: Code,
        desc: $t('ai-platform.workflow.nodes.form_data.schemaToLLMDesc'),
      };
    }
    default: {
      return {
        label: $t('ai-platform.workflow.nodes.form_data.label'),
        icon: FileText,
        desc: $t('ai-platform.workflow.nodes.form_data.desc'),
      };
    }
  }
});

const label = computed(() => props.data?.label || nodeInfo.value.label);
</script>

<template>
  <BaseNode
    :selected="selected"
    :execution-status="data.executionStatus"
    :execution-result="data.executionResult"
    :label="label"
    :inputs="[{ id: 'target' }]"
    :outputs="[{ id: 'source' }]"
    :node-id="id"
    :node-type="type"
  >
    <template #icon>
      <component :is="nodeInfo.icon" class="size-5" />
    </template>
    <template #desc>{{ nodeInfo.desc }}</template>

    <div class="space-y-2">
      <!-- 表单编码 -->
      <div
        v-if="type !== 'form_schema_to_llm'"
        class="flex items-center justify-between rounded bg-blue-50 px-2 py-1 text-blue-700 dark:bg-blue-950 dark:text-blue-300"
      >
        <span class="font-medium">{{
          $t('ai-platform.workflow.nodes.form_data.formCode')
        }}</span>
        <span class="font-mono text-xs">{{
          data.form_code ||
          $t('ai-platform.workflow.nodes.common.notConfigured')
        }}</span>
      </div>

      <!-- 表单转LLM特殊显示 -->
      <div
        v-if="type === 'form_schema_to_llm'"
        class="flex items-center justify-between rounded bg-purple-50 px-2 py-1 text-purple-700 dark:bg-purple-950 dark:text-purple-300"
      >
        <span class="font-medium">{{
          $t('ai-platform.workflow.nodes.form_data.formCode')
        }}</span>
        <span class="font-mono text-xs">{{
          data.form_code ||
          $t('ai-platform.workflow.nodes.common.notConfigured')
        }}</span>
      </div>

      <!-- 配置信息 -->
      <div class="text-muted-foreground space-y-1 text-xs">
        <div v-if="type === 'form_data_read'" class="flex justify-between">
          <span>{{ $t('ai-platform.workflow.nodes.form_data.recordId') }}</span>
          <span>{{
            data.record_id
              ? $t('ai-platform.workflow.nodes.common.configured')
              : '-'
          }}</span>
        </div>
        <div v-if="type === 'form_data_update'" class="flex justify-between">
          <span>{{ $t('ai-platform.workflow.nodes.form_data.recordId') }}</span>
          <span>{{
            data.record_id
              ? $t('ai-platform.workflow.nodes.common.configured')
              : '-'
          }}</span>
        </div>
        <div v-if="type === 'form_data_update'" class="flex justify-between">
          <span>{{
            $t('ai-platform.workflow.nodes.form_data.updateData')
          }}</span>
          <span>{{
            data.data ? $t('ai-platform.workflow.nodes.common.configured') : '-'
          }}</span>
        </div>
        <div v-if="type === 'form_data_create'" class="flex justify-between">
          <span>{{
            $t('ai-platform.workflow.nodes.form_data.createData')
          }}</span>
          <span>{{
            data.data ? $t('ai-platform.workflow.nodes.common.configured') : '-'
          }}</span>
        </div>
        <div v-if="type === 'form_data_delete'" class="flex justify-between">
          <span>{{ $t('ai-platform.workflow.nodes.form_data.recordId') }}</span>
          <span>{{
            data.record_id
              ? $t('ai-platform.workflow.nodes.common.configured')
              : '-'
          }}</span>
        </div>
        <div v-if="type === 'form_data_list'" class="flex justify-between">
          <span>{{
            $t('ai-platform.workflow.nodes.form_data.filterCondition')
          }}</span>
          <span>{{
            data.filters
              ? $t('ai-platform.workflow.nodes.common.configured')
              : '-'
          }}</span>
        </div>
      </div>

      <!-- 输出变量 -->
      <div class="rounded bg-green-50 px-2 py-1 text-xs dark:bg-green-950">
        <div
          class="flex items-center justify-between text-green-700 dark:text-green-300"
        >
          <span>{{
            $t('ai-platform.workflow.nodes.common.outputVariable')
          }}</span>
          <span class="font-mono">{{
            data.output_variable ||
            $t('ai-platform.workflow.nodes.common.notConfigured')
          }}</span>
        </div>
      </div>
    </div>
  </BaseNode>
</template>
