<template>
  <RichTextEditor ref="editorRef" v-loading="loading" @change="handleChange" />
</template>

<script setup lang="ts">
import { getRichText, uploadRichText } from '@/services/api/editor'
import RichTextEditor from '@/components/RichTextEditor.vue'

const route = useRoute()
let params = {
  FID: '',
  FormId: '',
  UserId: ''
}
const editorRef = ref()
const loading = ref(true)

const handleChange = async (html: string, list: any) => {
  if (!html) return
  let data = {
    UserId: params.UserId,
    RichTextstr: html,
    Files: list || []
  }
  let res: any = await uploadRichText(data)
  if (res.code === 200) {
    setTimeout(() => {
      editorRef.value.flag = true
    }, 1000)
  } else {
    editorRef.value.flag = true
  }
}

// 初始化数据
const init = async () => {
  const res: any = await getRichText(params)
  if (res.code === 200) {
    editorRef.value.init(res.data?.RichText)
    loading.value = false
  }
}

onMounted(() => {
  if (JSON.stringify(route.query) !== '{}') {
    const { FormId, FID, UserID }: any = route.query
    params.FID = FID
    params.FormId = FormId
    params.UserId = UserID
    init()
  } else loading.value = false
})
</script>

<style lang="scss" scoped></style>
