<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="form" size="large" >
          <div>
            <el-form-item label="服装图片">
              <ImgSingleUpload @update:goodImgArr="form.image = $event"/>
            </el-form-item>
          </div>
          <el-form-item label="服装名称">
            <el-input v-model="form.name" maxlength="12" minlength="2" show-word-limit style="width: 260px;" />
          </el-form-item>
          <el-form-item label="服装类别" prop="type" style="width: 240px;">
            <el-select v-model="form.categoryId" placeholder="请选择" >
              <el-option v-for="item in typeList" :key="item" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="租赁天数">
            <el-input-number v-model="form.period" min="0" :precision="0" :step="1" />
          </el-form-item>
          <el-form-item label="租赁价格">
            <el-input-number v-model="form.price" min="0" :precision="2" :step="1" />
          </el-form-item>
          <el-form-item label="服装效果图">
            <richTextUtil @richTextContent="getInfo"/>
          </el-form-item>
          <el-form-item label="是否上架">
            <el-checkbox v-model="form.isShelves" label="上架" size="large" />
          </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, inject } from 'vue';
import { SuccessInfo, WarnInfo, CommInfo } from '@/utils/messageInfo';
import myAxios from "@/api/myAxios";
import { saveBtn, invInput } from '@/utils/entityProduct/globalVar';
import ImgSingleUpload from "@/utils/imgUpload/ImgSingleUpload.vue";
import richTextUtil from '@/layout/components/richTextUtil.vue';

const resetFormData = ref()
const reload: any = inject("reload")  //导入组件刷新
const form : any = ref({   //添加实体类商品的表单
  name: '',
  categoryId: '',
  price: 0,
  period: 0,
  image: '',
  richText: '',
  isShelves: false
})
const typeList : any = ref([])
const previewRichText : any = ref('')

onMounted(() => {
  getOutfitType()
})

const getOutfitType = async () => {
  const res= await myAxios.post('/clothesCategory/list/web')
  // console.log(res.data)
  if(res.data.code === 1) {
    typeList.value = res.data.data
  }
}

//提交表单
const onSubmit = async () => {
  // console.log('表单--->',form.value)
  const values = Object.values(form.value );
  // 使用some()方法来检查是否有任何值为空
  if (values.some((value: any) => value === null || value === undefined || value === '') || form.value.period === 0 || form.value.price === 0) {
    WarnInfo('请检查表单数据是否完整填写')
    return;  //空返回结束函数
  }
  const res = await myAxios.post('/clothes/add', {
    name: form.value.name,
    categoryId: form.value.categoryId,
    price: form.value.price,
    period: form.value.period,
    image: form.value.image,
    richText: form.value.richText,
    isShelves: form.value.isShelves ? 1 : 0
  })
  console.log('后端--->',res.data)
  if (res.data.code === 1) {
    SuccessInfo('提交成功')
    form.value = {}
    reload() //上传完后重置表单
  } else {
    WarnInfo('服务错误')
    return;
  }
}

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

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

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;
}

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

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

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

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