<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" showFooter :title="getTitle" width="50%" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
import { defineComponent, ref, computed, unref } from 'vue';
import { BasicForm, useForm } from '/@/components/Form/index';
import { formSchema } from './app.data';
import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
import { v4 as uuidv4 } from 'uuid';
import { save } from '/@/api/authorize/authorize';
import { listToTree } from '/@/utils/menuUtil';

import { useI18n } from '/@/hooks/web/useI18n';
import { metaModel } from '/@/api/system/model/menuModel';
import { message } from 'ant-design-vue';

export default defineComponent({
  name: 'MenuDrawer',
  components: { BasicDrawer, BasicForm },
  emits: ['success', 'register'],
  setup(_, { emit }) {
    const isUpdate = ref(true);
    let menuId;

    const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = useForm({
      labelWidth: 100,
      schemas: formSchema,
      showActionButtonGroup: false,
      // baseColProps: { lg: 12, md: 24 },
    });
    const { t } = useI18n(); //加载国际化

    //默认传递页面数据
    const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
      resetFields();
      setDrawerProps({ confirmLoading: false });
      isUpdate.value = !!data?.isUpdate;

      //初始化，菜单名称为可用
      updateSchema({ field: 'title', componentProps: { disabled: false } });
      //如果是编辑操作，设置页面数据
      if (unref(isUpdate)) {
        // // 动态设置 表单值
        Reflect.set(data.record, 'name', data.record.NAME);
        Reflect.set(data.record, 'note', data.record.NOTE);
        //为表单赋值
        setFieldsValue({
          ...data.record,
        });
        //编辑模式，菜单名称为不可用
        updateSchema({ field: 'title', componentProps: { disabled: false } });
      }
      if (isUpdate.value) {
        menuId = Reflect.get(data.record, 'SYSID');
      }
    });

    //得到页面标题
    const getTitle = computed(() => !unref(isUpdate) ? ('新增应用') : ('编辑应用'));

    //提交按钮
    async function handleSubmit() {
      try {
        const v = await validate();
        console.log(v);
        if (isUpdate.value) {

        } else {
          menuId = null;
        }
        console.log("1111:", v);
        var data = {
          "system": {
            "id": menuId,
            "name": v.name,
            "key": v.name,
            "note": v.note
          },
          // "userIds": v.userid,
          "clientSecretStr": "test",
          "scope": "app",//v.scope,
          "grantTypes": ['authorization_code'],
          "autoapprove": true//v.autoapprove
        }
        //保存
        await save(data).then(r => {
          closeDrawer(); //关闭侧框
          emit('success');
          message.success(r.resp_msg);
        }).catch(e => {
          message.error(e.resp_msg);
        })
      } finally {
        setDrawerProps({ confirmLoading: false });
      }
    }

    return { registerDrawer, registerForm, getTitle, handleSubmit };
  },
});
</script>
