<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 === 'approvalResult'"
              v-model:value="record.config"
              style="width: 100%"
              :placeholder="t('请选择节点')"
              allowClear
            >
              <a-select-option :value="it.id" v-for="it in data.nodeList" :key="it.id">
                {{ it.name }}
              </a-select-option>
            </a-select>
            <!-- 流程参数 -->
            <a-select
              v-else-if="record.assignmentType === 'processParameter'"
              v-model:value="record.config"
              style="width: 100%"
              :placeholder="t('请选择流程参数')"
              allowClear
            >
              <a-select-option :value="it.id" v-for="it in data.processParameterTree" :key="it.id">
                {{ it.name }}
              </a-select-option>
            </a-select>

            <!-- 发起人信息 -->
            <a-tree-select
              v-else-if="record.assignmentType === 'originator'"
              v-model:value="record.config"
              show-search
              style="width: 100%"
              :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
              :placeholder="t('请选择发起人信息')"
              allow-clear
              tree-default-expand-all
              :tree-data="data.originatorTree"
              :field-names="{
                children: 'children',
                label: 'title',
                value: 'key',
              }"
            />
            <!-- 表单数据 -->
            <a-tree-select
              v-else-if="record.assignmentType === 'formData'"
              v-model:value="record.config"
              show-search
              style="width: 100%"
              :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
              :placeholder="t('请选择表单数据')"
              allow-clear
              tree-default-expand-all
              :tree-data="data.formDataTree"
              :field-names="{
                children: 'children',
                label: 'title',
                value: 'key',
              }"
            />
            <!-- 流程数据 -->
            <a-tree-select
              v-else-if="record.assignmentType === 'data'"
              v-model:value="record.config"
              show-search
              style="width: 100%"
              :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
              :placeholder="t('请选择流程数据')"
              allow-clear
              tree-default-expand-all
              :tree-data="data.paramTree"
              :field-names="{
                children: 'children',
                label: 'title',
                value: 'key',
              }"
            />
            <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 { TreeProps } from 'ant-design-vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { ApiParams } from '/@/components/ApiConfig/src/interface';
  import { reactive, computed } from 'vue';
  import { getNodeList, getProcessParamConfigs, getVariablesTree } from '../../config/info';
  import { ProcessDataVariable } from '/@bpmn/config/rules';
  import { InfoItem } from '/@/model/workflow/bpmnConfig';
  import { BpmnNodeKey } from '/@/enums/workflowEnum';
  const { t } = useI18n();
  const props = withDefaults(
    defineProps<{
      apiParams: Array<ApiParams>;
      needHideComponents?: Boolean;
      isGlobalParams?: Boolean;
    }>(),
    {
      apiParams: () => {
        return [];
      },
      needHideComponents: () => {
        return false;
      },
      isGlobalParams: () => {
        return false;
      },
    },
  );
  let data: {
    processParameterTree: Array<{ id: string; name: string }>;
    originatorTree: TreeProps['treeData'];
    formDataTree: TreeProps['treeData'];
    paramTree: TreeProps['treeData'];
    nodeList: Array<InfoItem>;
  } = reactive({
    processParameterTree: getProcessParamConfigs(),
    originatorTree: [
      {
        title: t('发起人ID'),
        key: '#{initiator_id}#',
      },
      {
        title: t('发起人所属组织架构名称'),
        key: '#{initiator_dept_name}#',
      },
    ],
    formDataTree: getVariablesTree({ needHideComponents: props.needHideComponents as boolean }),
    paramTree: ProcessDataVariable,
    nodeList: getNodeList([BpmnNodeKey.USER]),
  });
  let bindType = [
    {
      label: t('值'),
      value: 'value',
    },
    {
      label: t('流程数据'),
      value: 'data',
    },
    {
      label: t('流程参数'),
      value: 'processParameter',
    },
    {
      label: t('发起人信息'),
      value: 'originator',
    },
    {
      label: t('表单数据'),
      value: 'formData',
    },
    {
      label: t('节点审批结果'),
      value: 'approvalResult',
    },
  ];
  let globalBindType = [
    {
      label: t('值'),
      value: 'value',
    },
    {
      label: t('流程数据'),
      value: 'data',
    },
    {
      label: t('流程参数'),
      value: 'processParameter',
    },
    {
      label: t('发起人信息'),
      value: 'originator',
    },
    {
      label: t('节点审批结果'),
      value: 'approvalResult',
    },
  ];
  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.isGlobalParams ? globalBindType : bindType;
  });
</script>

<style scoped></style>
