<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    showFooter
    :title="getTitle"
    width="50%"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicForm, useForm } from '@/components/Form';
  import { formSchema } from './position.data';
  import { BasicDrawer, useDrawerInner } from '@/components/Drawer';
  import { useMessage } from '@/hooks/web/useMessage';
  const { createMessage } = useMessage();
  import {
    addPosGroup,
    getDeptList,
    addPosition,
    getPosInfo,
    updatePosition,
    getPositionsTree,
  } from '@/api/demo/system';

  defineOptions({ name: 'MenuDrawer' });

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

  const isUpdate = ref(true);

  const [registerForm, { resetFields, setFieldsValue, validate, updateSchema }] = useForm({
    labelWidth: 100,
    schemas: formSchema,
    showActionButtonGroup: false,
    baseColProps: { lg: 12, md: 24 },
  });
  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    resetFields();
    setDrawerProps({ confirmLoading: false });
    isUpdate.value = !!data?.isUpdate;
    if (unref(isUpdate)) {
      let obj: {
        type?: string;
      } = {};
      obj = await getPosInfo({
        id: data.record.id,
      });
      obj.type = data.record.type;
      setFieldsValue({
        ...obj,
      });
    }
    const posData = await getPositionsTree();
    const treeData = await getDeptList();
    updateSchema([
      {
        field: 'parentId',
        componentProps: { treeData:posData },
      },
      {
        field: 'orgId',
        componentProps: { treeData },
      },
    ]);
  });

  const getTitle = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));
  async function handleSubmit() {
    try {
      const values = await validate();
      setDrawerProps({ confirmLoading: true });
      // TODO custom api
      if (values.type == 1) {
        delete values.type;
        // 新增职级组
        if (!unref(isUpdate)) {
          const data = await addPosGroup(values);
          if (data) {
            closeDrawer();
            emit('success');
            return createMessage.success('新增成功');
          } else {
            return createMessage.error('新增失败');
          }
        } else {
          // const data = await editMenu(values);
          // if (data) {
          //   closeDrawer();
          //   emit('success');
          //   return createMessage.success('编辑成功');
          // } else {
          //   return createMessage.error('编辑失败');
          // }
        }
      } else {
        // 新增职级
        delete values.type;
        if (!unref(isUpdate)) {
          const data = await addPosition(values);
          if (data) {
            closeDrawer();
            emit('success');
            return createMessage.success('新增成功');
          } else {
            return createMessage.error('新增失败');
          }
        } else {
          const data = await updatePosition(values);
          if (data) {
            closeDrawer();
            emit('success');
            return createMessage.success('编辑成功');
          } else {
            return createMessage.error('编辑失败');
          }
        }
      }
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
