<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, computed } 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';
  import { useUserStore } from '/@/store/modules/user';

  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 userStore = useUserStore();
      const isDev = computed(() => userStore.getSystemEnv);
      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 年，本次新增 (年数)',
          // suffix:'年',
          defaultValue: 0,
          componentProps: {
            max: 999,
            min:0,
          },
          colProps: {
            span: 16,
          },
        },{
          field: 'highAddNum',
          component: 'InputNumber',
          label: '高级会员(月)剩余可售 0 月，本次新增 (年数)',
          defaultValue: 0,
          ifShow:false,
          // suffix:'月',
          componentProps: {
            max: 999,
            min:0,
          },
          colProps: {
            span: 16,
          },
        },{
          field: 'downAddNum',
          component: 'InputNumber',
          label: '场景下载剩余可售 0 次 本次新增 (次数)',
          // suffix:'次',
          defaultValue: 0,
          componentProps: {
            max: 999,
            min:0,
          },
          colProps: {
            span: 16,
          },
        }
      ];
      const [registerForm, { validate, resetFields, setFieldsValue, updateSchema }] = useForm({
        labelWidth: 400,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });
      async function onDataReceive(data) {
        modelRef.value = data
        resetFields()
        setFieldsValue({
          id:data.id,
        })
        setTimeout(()=>{
          updateSchema([{
            field: 'majorAddNum',
            label: `专业会员(年)：剩余可售 ${data.majorSubNum || 0} 年，本次新增 (年数)`,
          },{
            field: 'highAddNum',
            label: `高级会员(月)：剩余可售 ${data.highSubNum || 0} 月，本次新增 (月数)`,
            ifShow: isDev.value,
          },{
            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>
