<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 { agentNewUpdate } 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: 'name',
          component: 'Input',
          label: '经销商名称',
          required: true,
          componentProps: {
            maxlength: 50,
          },
          colProps: {
            span: 22,
          },
        },{
            field: 'nickName',
            component: 'Input',
            required: false,
            label: '联系人',
            componentProps: {
              maxlength: 50,
            },
            colProps: {
              span: 22,
            },
        },
      ];
      const [registerForm, { validate, resetFields, setFieldsValue }] = useForm({
        labelWidth: 120,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });
      const formatrichtext = (richtext, len = 0) => {
          let content = richtext.replace(/<.+?>/g, '');    
          content = content.replace(/ /ig, ''); /* 去除  */    
          content = content.replace(/\s/ig, ''); /* 去除空格 */
          return content && content.length;
      }
      async function onDataReceive(data) {
        modelRef.value = data
        setFieldsValue({
          id:data.id,
          name:data.name,
          nickName:data.nickName,
        });
      }
      const handleSubmit = async () => {
        const params = await validate();
        const apiData = {
          ...params as any,
        }
        try {
          await agentNewUpdate(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>
