<!-- bannerUrl: ""
iconUrl: ""
imgUrl: ""
isShow: "0"
level: "L1"
name: "test"
type: 0
wapBannerUrl: "https://4dkk.4dage.com/shop/huafa/20220302/23135195983e96.png?x-oss-process=image/resize,m_fixed,w_100,h_100" -->
<template>
  <BasicModal v-bind="$attrs" @register="register" title="新 增" @ok="handleOk">
    <div class="pt-3px pr-3px">
      <BasicForm @register="registerForm" />
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { categoryApi, uploadBannerApi, addCategoryApi } from '/@/api/product/category';
  import { TreeNode } from '/@/utils/treeUtils';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';

  const isLevel2 = (type: string) => type === 'L2';
  const schemas: FormSchema[] = [
    {
      field: 'name',
      component: 'Input',
      label: '分类名称',
      colProps: {
        span: 24,
      },
      componentProps: {
        maxLength: 15,
      },
      required: true,
    },
    {
      field: 'level',
      component: 'RadioGroup',
      label: '级别',
      defaultValue: 'L1',
      colProps: {
        span: 24,
      },
      componentProps: {
        options: [
          {
            label: '一级分类',
            value: 'L1',
          },
          {
            label: '二级分类',
            value: 'L2',
          },
        ],
      },
    },

    {
      field: 'parentId',
      component: 'ApiSelect',
      label: '上级分类',
      ifShow: ({ values }) => isLevel2(values.level),
      colProps: {
        span: 18,
      },
      componentProps: {
        api: async function (params) {
          console.log('params', params);
          const res = (await categoryApi(params)) as any as TreeNode[];
          // const treeData = makeTree(res);
          return res.filter((i) => i.level === 'L1');
        },
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      field: 'isShow',
      component: 'RadioGroup',
      label: '是否显示',
      defaultValue: '1',
      colProps: {
        span: 24,
      },
      componentProps: {
        options: [
          {
            label: '显示',
            value: '1',
          },
          {
            label: '不显示',
            value: '0',
          },
        ],
      },
    },
    {
      field: 'sortOrder',
      component: 'InputNumber',
      label: '排序',
      defaultValue: 1,
      colProps: {
        span: 24,
      },
      componentProps: {
        min: 0,
      },
    },
    {
      field: 'wapBannerUrl',
      component: 'Upload',
      label: '手机banner',
      show: false,
      rules: [{ required: true, message: '请选择上传文件' }],
      componentProps: {
        api: uploadBannerApi,
        maxNumber: 1,
        maxSize: 5,
        afterFetch: function (data) {
          Reflect.set(data, 'url', data.message.url);
          return data;
        },
      },
      required: true,
    },
  ];
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(_, { emit }) {
      const { createMessage } = useMessage();
      const [registerForm, { validate, getFieldsValue, resetFields }] = useForm({
        labelWidth: 120,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });

      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });

      function onDataReceive(data) {
        console.log('Data Received', data);
      }

      async function handleOk() {
        try {
          const values = await validate();
          console.log('values', values);
          if (values) {
            let data = getFieldsValue();
            console.log('data', data);
            await addCategoryApi({
              level: data.level,
              isShow: data.isShow,
              name: data.name,
              parentId: data.parentId,
              sortOrder: data.sortOrder,
              // wapBannerUrl: data.wapBannerUrl[0] || '',
            });
            emit('update');
            resetFields();
            createMessage.success('操作成功');
            closeModal();
          }
        } catch (error) {
          console.log('error', error);
        }
        // let res = await saveItemApi(data);
      }

      return { register, schemas, registerForm, handleOk };
    },
  });
</script>
