import type { FormSchemaGetter } from '#/adapter/form';

import { questionCategoryTree } from '#/api/exam/question/category';

export const drawerFormSchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
    fieldName: 'id',
  },
  {
    component: 'ApiTreeSelect',
    componentProps: {
      api: async () => {
        const data: any = await questionCategoryTree();
        // 增加一个根目录
        data.rows = [
          {
            id: 0,
            name: '根目录',
            children: [],
          },
          ...data.rows,
        ];
        return data.rows;
      },
      labelField: 'name',
      valueField: 'id',
      childrenField: 'children',
      checkStrictly: true,
    },
    dependencies: {
      if(values: any) {
        return values.parentId !== 0 || !values.isUpdate;
      },
      triggerFields: ['parentId'],
    },
    fieldName: 'parentId',
    label: '上级分类',
    rules: 'selectRequired',
  },
  {
    component: 'Input',
    fieldName: 'name',
    componentProps: {
      maxlength: 30,
    },
    label: '分类名称',
    rules: 'required',
  },
];
