<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroy-on-close title="创建事项" :width="600" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
    <div class="attachment-row">
      <span class="attachment-label">附件</span>
      <span class="attachment-trigger" @click="openFileUploadModal">添加附件</span>
      <TaskAttachmentUpload
        ref="fileUploadRef"
        :bussiness-id="''"
        :bussiness-secret-level="currentSecretLevel"
        :disabled="false"
        style="display: none"
      />
    </div>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { message } from 'ant-design-vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import TaskAttachmentUpload from './TaskAttachmentUpload.vue';
  import { createTaskFormSchema } from '../TaskList.data';
  import { addTask } from '../TaskList.api';
  import { checkUserSecLevel } from '../utils/taskUtils';

  const emit = defineEmits(['register', 'success']);
  const groupId = ref('');
  const mainId = ref('');
  const currentSecretLevel = ref(1);
  const currentSecretText = ref('非密');
  const fileUploadRef = ref<InstanceType<typeof TaskAttachmentUpload> | null>(null);

  const [registerForm, { setFieldsValue, resetFields, validate }] = useForm({
    labelWidth: 100,
    schemas: createTaskFormSchema,
    showActionButtonGroup: false,
    baseColProps: { span: 24 },
  });

  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    await resetFields();
    groupId.value = data?.groupId || '';
    mainId.value = data?.mainId || '';
    currentSecretLevel.value = data?.secretLevel || 1;
    currentSecretText.value = data?.secretText || '非密';
    if (groupId.value) {
      await setFieldsValue({ groupId: groupId.value });
    }
  });

  async function handleSubmit() {
    try {
      setModalProps({ confirmLoading: true });
      const values = await validate();

      if (values.assigneeId) {
        const { ok, invalidNames } = await checkUserSecLevel(values.assigneeId, currentSecretLevel.value);
        if (!ok) {
          message.warning(`以下人员的密级不满足当前计划(${currentSecretText.value})要求：${invalidNames.join('、')}`);
          return;
        }
      }

      const result = await addTask({
        mainId: mainId.value,
        taskName: values.taskName,
        taskDesc: values.taskDesc || '',
        priority: values.priority || '',
        type: '1',
        pid: groupId.value || '',
        assigneeId: values.assigneeId || '',
        startTime: values.startTime || null,
        endTime: values.endTime || null,
      });
      if (fileUploadRef.value?.hasPendingFiles()) {
        await fileUploadRef.value.bindBussinessId(result);
      }
      closeModal();
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
  function openFileUploadModal() {
    fileUploadRef.value?.handleOpenUploadModal();
  }
</script>

<style lang="less" scoped>
  .attachment-row {
    display: flex;
    align-items: center;
    padding: 0 0 16px;
    gap: 8px;
  }

  .attachment-label {
    width: 100px;
    flex-shrink: 0;
    text-align: right;
    font-size: 14px;
    color: rgba(0, 0, 0, 0.85);
  }

  .attachment-trigger {
    color: #8c8c8c;
    cursor: pointer;
    font-size: 14px;
    &:hover {
      color: #3370ff;
    }
  }
</style>
