<template>
  <a-tabs>
    <a-tab-pane :key="item.key" :tab="item.title" v-for="item in apiParams">
      <a-table
        :dataSource="item.tableInfo"
        :columns="apiConfigColumns"
        :pagination="false"
        :scroll="{ y: '400px' }"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'assignmentType'">
            <a-select
              v-model:value="record.assignmentType"
              style="width: 100%"
              :placeholder="t('请选择赋值类型')"
              @change="
                () => {
                  record.config = null;
                }
              "
              allowClear
            >
              <a-select-option :value="bind.value" v-for="bind in type" :key="bind.value">
                {{ bind.label }}
              </a-select-option>
            </a-select>
          </template>
          <template v-else-if="column.key === 'value'">
            <a-select
              v-if="record.assignmentType === 'data' && props.paramConfigs"
              v-model:value="record.config"
              style="width: 100%"
              :placeholder="t('请选择流程参数')"
              allowClear
            >
              <a-select-option :value="it.id" v-for="it in props.paramConfigs" :key="it.id">
                {{ it.name }}
              </a-select-option>
            </a-select>

            <a-input
              v-else
              v-model:value="record.value"
              :placeholder="record.type ? t('请填写值') : t('请先选择赋值类型后再配置值')"
            />
          </template>
        </template>
      </a-table>
    </a-tab-pane>
  </a-tabs>
</template>

<script setup lang="ts">
  import { useI18n } from '/@/hooks/web/useI18n';
  import { ApiParams } from '/@/components/ApiConfig/src/interface';
  import { computed } from 'vue';
  const { t } = useI18n();
  const props = withDefaults(
    defineProps<{
      paramConfigs: Array<any>;
      apiParams: Array<ApiParams>;
      needHideComponents?: Boolean;
      isGlobalParams?: Boolean;
    }>(),
    {
      paramConfigs: () => {
        return [];
      },
      apiParams: () => {
        return [];
      },
      needHideComponents: () => {
        return false;
      },
      isGlobalParams: () => {
        return false;
      },
    },
  );
  let bindType = [
    {
      label: t('值'),
      value: 'value',
    },
  ];
  let globalBindType = [
    {
      label: t('值'),
      value: 'value',
    },
    {
      label: t('变量'),
      value: 'data',
    },
  ];
  let apiConfigColumns = [
    {
      title: t('API入参名称'),
      dataIndex: 'name',
      key: 'name',
      align: 'center',
    },
    {
      title: t('API入参类型'),
      dataIndex: 'dataType',
      key: 'dataType',
      align: 'center',
    },
    {
      title: t('赋值类型'),
      dataIndex: 'assignmentType',
      key: 'assignmentType',
      align: 'center',
    },
    {
      title: t('赋值配置'),
      dataIndex: 'value',
      key: 'value',
      align: 'center',
    },
  ];

  const type = computed(() => {
    return props.paramConfigs && props.paramConfigs.length > 0 ? globalBindType : bindType;
  });
</script>

<style scoped></style>
