<template>
  <BzForm @register="register" @submit="handleSubmit">
    <template #add="{ field }">
      <bz-button v-if="Number(field) === 0" @click="add">+</bz-button>
      <bz-button class="ml-2" v-if="Number(field) === 0" @click="batchAdd"
        >批量添加表单配置</bz-button
      >
      <bz-button v-if="field > 0" @click="del(field)">-</bz-button>
    </template>
  </BzForm>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BzForm, useForm } from '/@/index';

  import { Input } from 'ant-design-vue';

  export default defineComponent({
    components: { BzForm, [Input.name]: Input },
    setup() {
      const [register, { appendSchemaByField, removeSchemaByFiled, validate }] = useForm({
        schemas: [
          {
            field: 'field0a',
            component: 'Input',
            label: '字段0',
            colProps: {
              span: 8,
            },
            required: true,
          },
          {
            field: 'field0b',
            component: 'Input',
            label: '字段0',
            colProps: {
              span: 8,
            },
            required: true,
          },
          {
            field: '0',
            component: 'Input',
            label: ' ',
            colProps: {
              span: 8,
            },
            slot: 'add',
          },
        ],
        labelWidth: 100,
        actionColOptions: { span: 24 },
      });

      async function handleSubmit() {
        try {
          const data = await validate();
          console.log(data);
        } catch (e) {
          console.log(e);
        }
      }

      const n = ref(1);

      function add() {
        appendSchemaByField(
          {
            field: `field${n.value}a`,
            component: 'Input',
            label: '字段' + n.value,
            colProps: {
              span: 8,
            },
            required: true,
          },
          '',
        );
        appendSchemaByField(
          {
            field: `field${n.value}b`,
            component: 'Input',
            label: '字段' + n.value,
            colProps: {
              span: 8,
            },
            required: true,
          },
          '',
        );

        appendSchemaByField(
          {
            field: `${n.value}`,
            component: 'Input',
            label: ' ',
            colProps: {
              span: 8,
            },
            slot: 'add',
          },
          '',
        );
        n.value++;
      }
      /**
       * @description: 批量添加
       */
      function batchAdd() {
        appendSchemaByField(
          [
            {
              field: `field${n.value}a`,
              component: 'Input',
              label: '字段' + n.value,
              colProps: {
                span: 8,
              },
              required: true,
            },
            {
              field: `field${n.value}b`,
              component: 'Input',
              label: '字段' + n.value,
              colProps: {
                span: 8,
              },
              required: true,
            },
            {
              field: `${n.value}`,
              component: 'Input',
              label: ' ',
              colProps: {
                span: 8,
              },
              slot: 'add',
            },
          ],
          '',
        );
        n.value++;
      }
      function del(field) {
        removeSchemaByFiled([`field${field}a`, `field${field}b`, `${field}`]);
        n.value--;
      }

      return { register, handleSubmit, add, del, batchAdd };
    },
  });
</script>
