<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">

    <BasicForm @register="registerForm"  >

    </BasicForm>
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, computed, unref, reactive } from 'vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form';
  import { formSchema,UserListParams } from './dept.data';
 
  import { getDeptList, addNotice, updateNotice,getUserList, getRoleListByPage } from '@/api/demo/system';
  import { useMessage } from '@/hooks/web/useMessage';
  const { createMessage } = useMessage();
  defineOptions({ name: 'DeptModal' });

  
  const emit = defineEmits(['success', 'register']);

  const isUpdate = ref(true);

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

  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    resetFields();
    setModalProps({ confirmLoading: false });
    isUpdate.value = !!data?.isUpdate;
    if (unref(isUpdate)) {
      setFieldsValue({
        ...data.record,
      });
    }


    const roleList = await getRoleListByPage()
    updateSchema({
      field: 'roles',
      componentProps: {
        options:roleList.items
      },
    });
    
    const query:UserListParams = {key:"",page:1,limit:999};
    const userList = await getUserList(query);
    updateSchema({
      field: 'recipientUserId',
      componentProps: {
        options:userList.items
      },
    });

    const treeData = await getDeptList();

    updateSchema({
      field: 'orgs',
      componentProps: { treeData },
    });

  });

  const getTitle = computed(() => (!unref(isUpdate) ? '新增通知' : '编辑通知'));

  async function handleSubmit() {
    try {
      const values = await validate();
      let query = values;
      if(query.orgs){
        query.orgs.forEach((it,idx)=>{
          query.orgs[idx] = it.toString();
        })
      }
      if(query.recipientUserId){
        query.recipientUserId.forEach((it,idx)=>{
          query.recipientUserId[idx] = it.toString();
        })
      }
      if(query.roles){
        query.roles.forEach((it,idx)=>{
          query.roles[idx] = it.toString();
        })
      }
      // delete query.orgs;
      // delete query.recipientUserId;
      // delete query.roles;
      // 调用接口
      if (!unref(isUpdate)) {
        const data = await addNotice(query);
        if (data) {
          setModalProps({ confirmLoading: true });
          closeModal();
          emit('success');
          return createMessage.success('新增成功');
        } else {
          return createMessage.error('新增失败');
        }
      } else {
        const data = await updateNotice(query);
        if (data) {
          setModalProps({ confirmLoading: true });
          closeModal();
          emit('success');
          return createMessage.success('编辑成功');
        } else {
          return createMessage.error('编辑失败');
        }
      }
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }

  // function handleRoleOptions(){

  // }
</script>
