<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.dashboardCreate.label'),
  dashboard_basic_info: '',
  page_config: '',
  application_id: '',
  update_if_exists: true,
  ...props.data,
});

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

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

      <ElFormItem
        :label="$t('ai-platform.workflow.panels.dashboardCreate.basicInfo')"
        required
      >
        <SmartInput
          v-model="form.dashboard_basic_info"
          :placeholder="
            $t(
              'ai-platform.workflow.panels.dashboardCreate.basicInfoPlaceholder',
            )
          "
          :current-node-id="nodeId"
        />
      </ElFormItem>

      <ElFormItem
        :label="$t('ai-platform.workflow.panels.dashboardCreate.pageConfig')"
        required
      >
        <SmartInput
          v-model="form.page_config"
          :placeholder="
            $t(
              'ai-platform.workflow.panels.dashboardCreate.pageConfigPlaceholder',
            )
          "
          :current-node-id="nodeId"
        />
      </ElFormItem>

      <ElFormItem
        :label="$t('ai-platform.workflow.panels.dashboardCreate.appId')"
      >
        <SmartInput
          v-model="form.application_id"
          :placeholder="
            $t('ai-platform.workflow.panels.dashboardCreate.appIdPlaceholder')
          "
          :current-node-id="nodeId"
        />
      </ElFormItem>

      <ElFormItem
        :label="
          $t('ai-platform.workflow.panels.dashboardCreate.updateIfExists')
        "
      >
        <ElSwitch v-model="form.update_if_exists" />
        <span class="text-muted-foreground ml-2 text-xs">
          {{
            $t('ai-platform.workflow.panels.dashboardCreate.updateIfExistsHint')
          }}
        </span>
      </ElFormItem>
    </ElForm>

    <div class="bg-muted/50 rounded-lg p-3">
      <div class="text-muted-foreground text-xs">
        <p class="mb-2 font-medium">
          {{ $t('ai-platform.workflow.panels.common.outputVars') }}:
        </p>
        <ul class="list-inside list-disc space-y-1">
          <li>
            <code>dashboard_id</code> -
            {{
              $t(
                'ai-platform.workflow.panels.dashboardCreate.dashboardIdOutput',
              )
            }}
          </li>
          <li>
            <code>dashboard_code</code> -
            {{
              $t(
                'ai-platform.workflow.panels.dashboardCreate.dashboardCodeOutput',
              )
            }}
          </li>
          <li>
            <code>page_meta</code> -
            {{
              $t('ai-platform.workflow.panels.dashboardCreate.pageMetaOutput')
            }}
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>
