<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="给部门分配职级组" @ok="handleSubmit">
    <BasicForm @register="registerForm" :model="modelRef"/>
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, computed, unref, defineProps } from 'vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form';
  import { formGroupSchema } from './dept.data';

  import { getDeptList, addDept ,orgPosGroup} from '@/api/demo/system';

  defineOptions({ name: 'PosGroupModal' });
 
  const emit = defineEmits(['success', 'register']);

  const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = useForm({
    labelWidth: 100,
    baseColProps: { span: 24 },
    schemas: formGroupSchema,
    showActionButtonGroup: false,
  });
  let depeId = ref()
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    resetFields();
    setModalProps({ confirmLoading: false });

    depeId.value = data.record.id
      setFieldsValue({
        ...data.record,
      });
  });


  async function handleSubmit() {
    try {
      const values = await validate();
      let query = {
        orgId:depeId.value,
        posGroupId:values.posGroupId,
      }
      // 调用接口
      const data = await orgPosGroup(query);
      setModalProps({ confirmLoading: true });
      // TODO custom api
      closeModal();
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>