<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    title="处理留言"
    @visible-change="handleVisibleChange"
    okText="完成处理"
    @cancel="resetFields"
    @ok="handleSubmit"
  >
    <div class="pt-2px pr-3px">
      <BasicForm @register="registerForm" :model="model" >
        <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 { newAddNews, addOrUpdate } from '/@/api/operate'
  import { useI18n } from '/@/hooks/web/useI18n';
  import { Tinymce } from '/@/components/Tinymce/index';
  import { intercomMessageHandle } from '/@/api/operate'

  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: 'id',
          component: 'Input',
          show:false,
          label: 'id',
          required: false,
        },{
            field: 'noteContent',
            component: 'InputTextArea',
            required: true,
            label: '备注',
            componentProps: {
              maxLength: 200,
              placeholder: '请备注处理结果',
            },
            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((data) => {
        data && onDataReceive(data);
      });
      async function onDataReceive(data) {
        modelRef.value = data
        resetFields();
        setFieldsValue(data);
      }
      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();
        const apiData = {
          ...params as any,
          coverImageUrl:params.coverImageUrl && params.coverImageUrl[0],
          content:params.newType == 1?params.newsUrl:params.content
        }
        try {
          await intercomMessageHandle(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>
