<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="title"
    :width="600"
    @visible-change="handleVisibleChange"
    @cancel="resetFields"
    @ok="handleSubmit"
  >
    <div class="pt-2px pr-3px">
      <BasicForm @register="registerForm" >
        <template #text="{ model, field }">
          {{ model[field]  }}
        </template>
      </BasicForm>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, nextTick, onMounted, reactive, h } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { addIncrementNum } from '/@/api/dealer'
  import { useI18n } from '/@/hooks/web/useI18n';

  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const modelRef = ref({});
      const title = ref('编辑经销商');
      const preventAutoFill = ref(true);
      const fileFlow = reactive({
        coverImageUrl:''
      })
      const { createMessage } = useMessage();
      const schemas: FormSchema[] = [
        {
          field: 'id',
          component: 'Input',
          show:false,
          label: 'id',
          required: false,
        },{
          field: 'majorAddNum',
          component: 'InputNumber',
          label: '专业会员(年)剩余可售 0 个 新增',
          defaultValue: 0,
          componentProps: {
            maxlength: 50,
            min:0,
          },
          colProps: {
            span: 22,
          },
        },{
          field: 'highAddNum',
          component: 'InputNumber',
          label: '高级会员(年)剩余可售 0 个 新增',
          defaultValue: 0,
          ifShow:false,
          componentProps: {
            maxlength: 50,
            min:0,
          },
          colProps: {
            span: 22,
          },
        },{
          field: 'downAddNum',
          component: 'InputNumber',
          label: '场景下载剩余可售 0 次 新增',
          defaultValue: 0,
          componentProps: {
            maxlength: 50,
            min:0,
          },
          colProps: {
            span: 22,
          },
        }
      ];
      const [registerForm, { validate, resetFields, setFieldsValue, updateSchema }] = useForm({
        labelWidth: 250,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });
      async function onDataReceive(data) {
        modelRef.value = data
        console.log('downSubNum',data)
        resetFields()
        setFieldsValue({
          id:data.id,
        })
        setTimeout(()=>{
          updateSchema([{
            field: 'majorAddNum',
            label: `专业会员(年)剩余可售 ${data.majorSubNum || 0} 个 新增`,
          },{
            field: 'highAddNum',
            label: `高级会员(年)剩余可售 ${data.highSubNum || 0} 个 新增`,
            ifShow: data.highSubNum>0,
          },{
            field: 'downAddNum',
            label: `场景下载剩余可售 ${data.downSubNum || 0} 次 新增`,
          }])
        },500)
      }
      const handleSubmit = async () => {
        const params = await validate();
        console.log('次',params)
        const apiData = {
          id:params.id,
          majorAddNum:params.majorAddNum || 0,
          highAddNum:params.highAddNum || 0,
          downAddNum:params.downAddNum || 0,
        }
        try {
          await addIncrementNum(apiData);
          closeModal();
          resetFields();
          createMessage.success(t('common.optSuccess'));
          emit('update');
        } catch (error) {
          console.log('not passing', error);
        }
      };
      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }
      return {
        register,
        schemas,
        registerForm,
        model: modelRef,
        fileFlow,
        preventAutoFill,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        resetFields,
        title,
        t,
      };
    },
  });
</script>
