<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 './menu.data';
  import { BasicDrawer, useDrawerInner } from '@/components/Drawer';

  import { getMenuList, addMenu, editMenu, addButton, editButton } from '@/api/demo/system';
  import { useMessage } from '@/hooks/web/useMessage';
  import { cloneDeep } from 'lodash-es';
  const { createMessage } = useMessage();
  defineOptions({ name: 'MenuDrawer' });

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

  const isUpdate = ref(true);

  const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = 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)) {
      const { record } = data;
      if (record.type == '2') {
        record.status = record.status ? 1 : 0;
      }
      setFieldsValue({
        ...record,
      });
    }
    // const treeData = await getMenuList();
    const treeData = cloneDeep(props.treeData);
    treeData.unshift({
      id: '0',
      key: '0',
      name: '根节点',
    });
    updateSchema({
      field: 'parentId',
      componentProps: { treeData },
    });
    updateSchema({
      field: 'moduleId',
      componentProps: { treeData },
    });
  });

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

  async function handleSubmit() {
    try {
      const values = await validate();
      values.sortNo = Number(values.sortNo);
      setDrawerProps({ confirmLoading: true });
      // TODO custom api
      if (values.type == '1') {
        delete values.type;
        // 新增菜单
        if (!unref(isUpdate)) {
          const data = await addMenu(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;
        values.status = values.status == 1 ? true : false;
        if (!unref(isUpdate)) {
          const data = await addButton(values);
          if (data) {
            closeDrawer();
            emit('success');
            return createMessage.info('创建成功');
          } else {
            return createMessage.error('创建失败');
          }
        } else {
          const data = await editButton(values);
          if (data) {
            closeDrawer();
            emit('success');
            return createMessage.success('编辑成功');
          } else {
            return createMessage.error('编辑失败');
          }
        }
      }
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
