<template>
  <div class="flexCard">
    <!-- 表单 ref 和 prop绑定  用于重置表单 -->
    <el-card class="form-container" shadow="never" >
      <el-form >
        <el-form-item label="编辑限时优惠价格">
          <el-input-number v-model="favorablePrice" min="0" :precision="2" :step="1" size="large" />
          <el-button style="margin-left: 50px" type="primary" @click="favorableOnSubmit" size="large">保存</el-button>
        </el-form-item>
        <el-form-item label="编辑预约须知" v-loading="loading">
          <richTextUtil @richTextContent="getInfo" v-model:context="previewRichText"/>
        </el-form-item>
        <el-button style="margin-left: 100px" type="primary" @click="NoticeOnSubmit" size="large">保存</el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import { SuccessInfo, WarnInfo, CommInfo } from '@/utils/messageInfo';   //封装ElMessage提示
import myAxios from "@/api/myAxios";
import RichTextUtil from "@/layout/components/richTextUtil.vue";


const previewRichText = ref('')   //回显
const noticeRichText = ref('')  //用于提交的
const favorablePrice = ref(0)   //限时优惠价格
const loading = ref(true)   //回显没那么快

onMounted(() => {
  getNotice()
  getFavorable()
  setTimeout(()=>{
    loading.value = false
  },1500)
})


const getNotice = async () => {   //获取预约须知
  const res = await myAxios.get('/global/query/notice')
  console.log(res.data)
  if(res.data.code === 1) {
    previewRichText.value = res.data.data
  }
}

const getFavorable = async () => {   //获取优惠价格
  const res = await myAxios.get('/global/query/coupon/price')
  if(res.data.code === 1) {
    favorablePrice.value = res.data.data
  }
}

const favorableOnSubmit = async () => {
  const res = await myAxios.get('/global/update/coupon/price',{ params: { bigDecimal: favorablePrice.value } })
  if(res.data.code === 1) {
    await getFavorable()
    SuccessInfo('更新成功')
  } else {
    WarnInfo('服务错误')
  }
}

//提交表单
const NoticeOnSubmit = async () => {    //提交预约须知
  if(noticeRichText.value === '<p><br></p>') {
    WarnInfo('预约须知不能为空')
    return;
  }
  const res = await myAxios.get('/global/update/notice',{params:{ richText: encode64(noticeRichText.value) }})
  if(res.data.code === 1) {
    await getNotice()
    SuccessInfo('更新成功')
  } else {
    WarnInfo('服务错误')
  }
}

const getInfo = (info: any) => {  //富文本赋值
  previewRichText.value = info
  noticeRichText.value = info
}

//字符串转为base64编码
function encode64(text: string): string {
  return btoa(String.fromCharCode(...new TextEncoder().encode(text)))
}


</script>

<style scoped>
.flexCard {
  display: flex;
  height: 100%;
  min-width: 800px;
  margin: 0 auto;
}


.form-container {
  /*width: 700px;*/
  margin: 0 auto;
  height: 800px;
  overflow: auto;
}
.form {
  /* width: 500px; */
  margin: 0 auto;
}


.btnBox {
  display: flex;
  justify-content: space-around;
}
</style>