<template>
  <div class="flexCard">
    <el-card class="richTextPreview" shadow="never">
      <div class="titleText">移动端预览</div>
      <p v-html="previewRichText"></p>
    </el-card>
    <!-- 表单 ref 和 prop绑定  用于重置表单 -->
    <el-card class="form-container" shadow="never">
      <div class="form">
        <el-form ref="resetFormData" :model="detailFrom" label-width="auto" size="large" :disabled="disabledRichText" >
          <div>
            <el-form-item label="添加商品图片">
              <!-- 下面的event的作用,传入当前事件对象 -->
              <PreviewSingleImgUpload :ImgUrlList="previewImgUrl" @update:goodImgArr="detailFrom.goodImg = $event"/>
            </el-form-item>
          </div>
          <div class="NamePrice">
            <el-form-item label="课程名称" prop="name">
              <el-input v-model="detailFrom.name" maxlength="12" minlength="2" show-word-limit style="width: 260px;" />
            </el-form-item>
            <el-form-item label="单次价格" prop="price">
              <el-input-number v-model="detailFrom.price" min="0" :precision="2" :step="0.5" />
              <p>元</p>
            </el-form-item>
          </div>
          <div class="IntroInven">
            <el-form-item label="课程简介" prop="intro">
              <el-input v-model="detailFrom.intro" type="textarea" placeholder="产品尺寸，服务等" maxlength="60" show-word-limit
                        style="width: 300px;" />
            </el-form-item>
          </div>
          <el-form-item label="课程详情图" prop="richText" v-loading="loading">
            <richTextUtil @richTextContent="getInfo" v-model:disableRich="disabledRichText" v-model:context="previewRichText"/>
          </el-form-item>
          <div class="btnBox">
            <el-form-item>
              <el-button type="primary" @click="onSubmit" :disabled="saveBtn">更新</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="resetForm">重置</el-button>
            </el-form-item>
          </div>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted , onBeforeMount} from 'vue';
import { type UploadUserFile} from 'element-plus';
import { SuccessInfo, WarnInfo } from '@/utils/messageInfo';   //封装ElMessage提示
import myAxios from "@/api/myAxios";
import { saveBtn } from '@/utils/entityProduct/globalVar';
import richTextUtil from '@/layout/components/richTextUtil.vue';
import { useRoute ,useRouter } from "vue-router";
import PreviewSingleImgUpload from "@/utils/imgUpload/PreviewSingleImgUpload.vue";


const route = useRoute();  //路由
const resetFormData = ref()
const ImgArr: any = ref<UploadUserFile[]>([])
const detailFrom : any = ref({
  name: '',   //商品名称
  price: '',  //商品价格
  intro: '',//产品简介
  festivalName: '', //节日名称
  type: '',//类别
  label: '',//商品标签
  goodImg: '',  //商品图片url
  inventory: '', //库存
  richText: ''
})
const previewImgUrl  = ref('')  //预览图字符串
const previewRichText = ref('')
const disabledRichText:any = ref(false);
const loading = ref(true)
const router = useRouter()

onBeforeMount(()=>{
  if(route.params.flag === '0'){
    disabledRichText.value = true;
  }
  getServiceInfo()   //编辑页查询商品信息
})

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

const getServiceInfo = async () => {
  const res = await myAxios.post("/goods/service/list/id",{ id: route.params.id })
  console.log('res--->',res.data)
  if(res.data.code == 1){
    previewImgUrl.value = res.data.data.goodImg
    detailFrom.value = res.data.data
    previewRichText.value = res.data.data.richText
    ImgArr.value.splice(0,ImgArr.value.length)
    var tempArr = res.data.data.goodImg.split(';')
    tempArr.forEach((item: any) => {
      ImgArr.value.push({
        url: item
      })
    })
  } else {
    WarnInfo('信息获取失败')
  }
}

//提交表单
const onSubmit = async () => {
  const values = Object.values(detailFrom.value );
  // 使用some()方法来检查是否有任何值为空
  if (values.some((value: any) => value === null || value === undefined || value === '' || value === 0)) {
    WarnInfo('请检查表单数据是否完整填写')
    return;  //空返回结束函数
  }
  const res = await myAxios.post('/goods/service/single/update', { ...detailFrom.value })
  // console.log(res.data);
  if (res.data.code === 1) {
    SuccessInfo('提交成功')
    await router.replace('/ProductCenter')
  } else {
    WarnInfo('服务错误')
    return;
  }
}

//清除表单
const resetForm = () => {
  resetFormData.value.resetFields()
}

// const rules = reactive<FormRules<typeof form>>({  //表单校验规则
//   name: [{ validator: validateName, trigger: 'blur' }],
//   intro: [{ validator: validateIntro, trigger: 'blur' }],
//   introDetail: [{ validator: validateDetail, trigger: 'blur' }]
// })

const getInfo = (info: any) => {  //富文本赋值
  // console.log('info', info)
  previewRichText.value = info
  detailFrom.value.richText = info
}
</script>

<style scoped>
.flexCard {
  display: flex;
  height: 100%;
}

.richTextPreview {
  height: 80vh;
  overflow-y: auto;
  word-break: break-all;
  width: 400px;
  min-width: 400px;
  margin-right: 10px;
}

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

.NamePrice {
  display: flex;
}

.IntroInven {
  display: flex;
}

.festivalBox {
  display: flex;
}

.btnBox {
  display: flex;
  justify-content: space-around;
}

.titleText {
  text-align: center;
  margin: 0 auto;
}
</style>