<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    title="邮件模板设置"
    :width="800"
    @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 { uploadApi } from '/@/api/product/index';
  import { getInfoMail, updateMail } from '/@/api/operate'
  import { useI18n } from '/@/hooks/web/useI18n';
  import { Tinymce } from '/@/components/Tinymce/index';

  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const modelRef = ref({});
      const fileFlow = reactive({
        coverImageUrl:''
      })
      const { createMessage } = useMessage();
      const schemas: FormSchema[] = [
        {
          field: 'subject',
          component: 'Input',
          label: '邮件主题',
          required: true,
          componentProps: {
            maxLength: 50,
          },
          colProps: {
            span: 22,
          },
        },
        {
          field: 'msg',
          label: '邮件正文',
          component: 'Input',
          required: true,
          colProps: {
            span: 22,
          },
          render: ({ model, field }) => {
            return h(Tinymce, {
              value: model[field],
              maxlength: 200,
              onChange: (value: string) => {
                model[field] = value;
              },
              showImageUpload: true,
            });
          },
        },{
            field: 'sendMail',
            component: 'Input',
            required: true,
            label: '发件人邮箱',
            componentProps: {
              maxLength: 50,
            },
            colProps: {
              span: 22,
            },
        },{
            field: 'sendPassword',
            component: 'Input',
            label: '发件人授权码',
            required: true,
            componentProps: {
              maxLength: 50,
            },
            colProps: {
              span: 22,
            },
        }
      ];
      const [registerForm, { validate, resetFields, setFieldsValue, updateSchema }] = useForm({
        labelWidth: 120,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner(() => {
        console.log('useModalInner')
        onDataReceive();
      });
      async function onDataReceive() {
        resetFields();
        const detail = await getInfoMail({mailTemplateId:2})
        console.log('getInfoMail',detail)
        setFieldsValue({
          ...detail,
          coverImageUrl:detail.coverImageUrl?[detail.coverImageUrl]:''
        });
      }
      function NewTypeChange(val){
        console.log('NewTypeChange',val)
        updateSchema([
          { field: 'content', ifShow:val == 2,},
          { field: 'newsUrl', ifShow:val != 2,},
        ])
      } 
      const handleSubmit = async () => {
        const params = await validate();
        let apiUrl = updateMail
        const apiData = {
          ...params as any,
          id:2,
          coverImageUrl:params.coverImageUrl && params.coverImageUrl[0],
          content:params.newType == 1?params.newsUrl:params.content
        }
        try {
          await apiUrl(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,
        NewTypeChange,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        resetFields,
        t,
      };
    },
  });
</script>
