<template>
  <BasicModal
    v-bind="$attrs"
    destroyOnClose
    @register="registerModal"
    title="添加应用"
    @ok="submit"
  >
    <BasicForm @register="registerForm" :labelWidth="100" :actionColOptions="{ span: 24 }" />
  </BasicModal>
</template>

<script lang="ts" setup>
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { Events } from '/@/api/burialPoint/event/model/eventModel';
  import { ref } from 'vue';
  import { eventCreate } from '/@/api/burialPoint/event/event';

  const uuid = ref('');

  const [registerModal, { closeModal, changeOkLoading, changeLoading }] = useModalInner((data) => {
    uuid.value = data;
  });

  const schemasApply: FormSchema[] = [
    {
      field: 'eventCode',
      component: 'Input',
      label: '事件编号：',
      colProps: {
        span: 20,
      },
      componentProps: {
        placeholder: '请输入',
      },
      // 数据校验
      rules: [
        {
          required: true,
          message: '请输入！',
          trigger: 'blur',
        },
      ],
    },
    {
      field: 'eventName',
      component: 'Input',
      label: '事件名称：',
      colProps: {
        span: 20,
      },
      componentProps: {
        placeholder: '请输入',
      },
      // 数据校验
      rules: [
        {
          required: true,
          message: '请输入！',
          trigger: 'blur',
        },
      ],
    },
    {
      field: 'eventType',
      component: 'Input',
      label: '事件类型：',
      colProps: {
        span: 20,
      },
      componentProps: {
        placeholder: '请输入',
      },
      // 数据校验
      rules: [
        {
          required: true,
          message: '请输入！',
          trigger: 'blur',
        },
      ],
    },
    {
      field: 'description',
      component: 'InputTextArea',
      label: '事件描述：',
      colProps: {
        span: 20,
      },
      componentProps: {
        placeholder: '请输入',
      },
      // 数据校验
      rules: [
        {
          required: false,
          message: '请输入！',
          trigger: 'blur',
        },
      ],
    },
  ];

  const [registerForm, methods] = useForm({
    labelWidth: 120,
    schemas: schemasApply,
    actionColOptions: {
      span: 24,
    },
    showActionButtonGroup: false,
  });

  const submit = () => {
    changeOkLoading(true);
    changeLoading(true);
    methods
      .validate()
      .then(async () => {
        const data = methods.getFieldsValue();
        const params: Events = {
          id: '',
          applyId: uuid.value,
          eventName: data?.eventName,
          eventType: data?.eventType,
          eventCode: data?.eventCode,
          description: data?.description,
          createdTime: '',
        };
        await eventCreate(params).then((res) => {
          changeOkLoading(false);
          changeLoading(false);
        });
        closeModal();
      })
      .catch(() => {
        changeOkLoading(false);
        changeLoading(false);
      });
  };
</script>

<style scoped></style>
