<template>
  <BasicModal
    v-bind="$attrs"
    destroyOnClose
    @register="registerModal"
    :title="getTitle"
    @ok="handleSubmit"
    width="700px"
  >
    <a-tabs v-model:activeKey="activeKey">
      <a-tab-pane key="1" tab="基本">
        <BasicForm @register="registerForm" />
      </a-tab-pane>
      <a-tab-pane key="2" tab="事件"> 暂未设置 </a-tab-pane>
    </a-tabs>
  </BasicModal>
</template>

<script setup lang="ts">
  import { ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form';
  import { dataFormSchema } from './data';

  const emit = defineEmits(['close']);
  const getTitle = ref('节点操作');
  const activeKey = ref('1');
  const [registerForm, { setFieldsValue, getFieldsValue, validate }] = useForm({
    labelWidth: 160,
    schemas: dataFormSchema,
    showActionButtonGroup: false,
    baseColProps: { lg: 24, md: 24 },
    actionColOptions: {
      span: 23,
    },
  });
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async data => {
    console.log('🚀 ~ file: code.vue:21 ~ data:', data);
    setModalProps({ confirmLoading: false });
    const record = data.record;
    await setFieldsValue(setFormInfo(record));
  });

  function setFormInfo(record: any) {
    const data = {} as any;
    switch (record.type) {
      case 'bpmn:UserTask':
        data.title = '审批节点 ' + record.id;
        if (record.id == 'N1') {
          data.title = '起草节点 ' + record.id;
        }
        break;
      case 'bpmn:ReceiveTask':
        data.title = '抄送节点 ' + record.id;
        break;
      case 'bpmn:StartEvent':
        data.title = '开始节点 ' + record.id;
        break;
      case 'bpmn:EndEvent':
        data.title = '结束节点 ' + record.id;
        break;
      case 'bpmn:ScriptTask':
        data.title = '脚本节点 ' + record.id;
        break;
      case 'bpmn:ServiceTask':
        data.title = '服务节点 ' + record.id;
        break;
      case 'bpmn:ExclusiveGateway':
        data.title = '互斥网关 ' + record.id;
        break;
      case 'bpmn:ParallelGateway':
        data.title = '并行网关 ' + record.id;
        break;
      case 'bpmn:SequenceFlow':
        data.title = '连线 ' + record.id;
        data.name = '连线名称：';
        data.notes = '连线备注：';
        if (record?.source.type == 'bpmn:ExclusiveGateway') {
          data.cotag = true;
        }
        break;
      default:
        data.title = '未知节点 ' + record.id;
        break;
    }
    data.type = record.type;
    data.name = record.businessObject.name || data.title;
    getTitle.value = data.name;
    data.flway = record.businessObject.$attrs.flway;
    if (!data.flway) {
      data.flway = 1;
    }
    data.hatyp = record.businessObject.$attrs.hatyp;
    if (!data.hatyp) {
      data.hatyp = 1;
    }
    data.conds = record.businessObject.$attrs.conds;
    if (record.businessObject.$attrs.hamen) {
      console.log('record.businessObject.$attrs.hamen', record.businessObject.$attrs.hamen);
      data.hamen = '1,2,3,4';
    } else {
      data.hamen = [];
    }
    data.isShow = true;
    return data;
  }

  // 提交按钮事件
  async function handleSubmit() {
    const data = getFieldsValue();
    data.hamen = data.hamen.split(',');
    console.log('🚀 ~ file: modal.vue:110 ~ handleSubmit ~ data:', data);
    emit('close', data);
    closeModal();
  }
</script>

<style lang="less" scoped></style>
