<script lang="ts" setup>
import { useVbenDrawer } from '@vben/common-ui';

import { useVbenForm } from '#/adapter/form';

import { drawerFormSchema } from './config-data';

import { defineEmits } from 'vue';
import { isArray } from 'lodash-es';
import dayjs from 'dayjs';

defineOptions({
  name: 'teVisitorTask',
});

const props = defineProps<{
  VisitorTaskloing?: boolean;
}>();

const emit = defineEmits<{ submit: [data: any] }>();

const [Form, formApi] = useVbenForm({
  schema: drawerFormSchema(),
  showDefaultActions: false,
});
const [Drawer, drawerApi] = useVbenDrawer({
  onCancel() {
    drawerApi.close();
  },
  onConfirm: async () => {
    try {
      const { valid } = await formApi.validate();
      if (!valid) {
        return;
      }
      const formData = await formApi.getValues();
      const { attachments } = formData;
      if (isArray(attachments) && attachments.length > 0) {
        const mapattachments = attachments
          .map((item) => {
            return item.response.data.fileName;
          })
          .join(',');
        formData.attachments = mapattachments;
      }
      formData.startTime = dayjs(formData.startTime).format(
        'YYYY-MM-DD HH:mm:ss',
      );
      formData.endTime = dayjs(formData.endTime).format('YYYY-MM-DD HH:mm:ss');
      emit('submit', formData);
    } catch (error) {
      console.error('提交失败:', error);
    }
  },
  onOpenChange(isOpen: boolean) {  
    if (isOpen) {
      const { values } = drawerApi.getData<Record<string, any>>();
      if (values) {
        formApi.setValues(values);
      }
    }
  },
  title: '创建访客任务',
});
</script>
<template>
  <Drawer>
    <Form v-loading="VisitorTaskloing">
    </Form>
  </Drawer>
</template>
