<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-FormContainer" shadow="never">
      <el-form ref="resetFormData" :model="form" size="large">
        <el-form-item label="添加展示图片">
          <ImgSingleUpload @update:goodImgArr="form.goodImg = $event"/>
        </el-form-item>
        <el-form-item label="服装名称" prop="name" style="width: 300px;">
          <el-input v-model="form.name" />
        </el-form-item>
        <div style="display: flex">
        <el-form-item label="价格" prop="price">
          <el-input-number v-model="form.price" min="0" :precision="2" :step="0.5" />
          <p>元</p>
        </el-form-item>
        <el-form-item label="服装类别" style="width: 232px;" label-width="80">
          <el-select v-model="form.categoryName" placeholder="请选择" @change="(event: any) => loadCategory(event)" >
            <el-option v-for="item in typeList" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        </div>
        <!-- 拍摄场地：户外、室内、均可 -->
        <div class="kinds">
          <el-form-item label="拍摄场地" style="width: 240px;">
            <el-select v-model="form.shotScene" placeholder="请选择" @change="(event: any) => loadScene(event)" >
              <el-option v-for="item in siteList" :key="item" :label="item" :value="item" />
            </el-select>
          </el-form-item>
          <el-form-item label="限定人数" style="width: 228px;" label-width="80">
            <el-select v-model="form.count">
              <el-option v-for="item in minArr" :label="item" :value="item" />
            </el-select>
          </el-form-item>
        </div>

        <el-form-item label="效果图">
          <richTextUtil @richTextContent="getRichTextInfo"/>
        </el-form-item>
        <div class="totalButton">
          <el-form-item>
            <el-button type="primary" @click="onSubmit">保存</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="resetForm">重置</el-button>
          </el-form-item>
        </div>
      </el-form>
    </el-card>
    <div class="diyConponent">
      <el-card class="form-RichTextContainer" shadow="never">
        <outfitCalendarUtil @calendar-info="getDateInfo"></outfitCalendarUtil>
      </el-card>
    </div>
  </div>

</template>

<script setup lang="ts">
import { ref, onMounted, inject, toRaw } from 'vue';
import { ElMessage } from 'element-plus';
import myAxios from "@/api/myAxios";
import outfitCalendarUtil from '@/layout/components/outfitCalendarUtil.vue';
import richTextUtil from '@/layout/components/richTextUtil.vue';
import ImgSingleUpload from '@/utils/imgUpload/ImgSingleUpload.vue'


const resetFormData = ref()
const form = ref({
  name: '',  //服装名字
  price: '',  //商品价格
  shotScene: '', //拍摄场地
  categoryName: '', //服装类别
  goodImg: '',  //商品图片url
  count: 0,
  richText: '',
  bookingDateAddRequestList: []  //预约时间点，是否可预约，人数范围
})
//导入组件刷新
const reload: any = inject("reload")
const typeList = ref<String[]>([])  //服装类别数组
const siteList = ref<String[]>(['室外','室内','均可'])  //场地类型
const minArr = ref<Number[]>([])  //最人数数组
const previewRichText = ref('')  //预览富文本


onMounted(() => {
  getOutfitKind() //获取服装类别
  for(let i = 0;i <=50 ; i++) {  //初始化人数选择的数组
    minArr.value[i] = 1+i
  }
})

//将 服装类别 赋值到表格里
const loadCategory = (value: any) => {
  form.value.categoryName = value  //三元运算符 flag=1 是关于"商品类别"选项的值  flag=0 是关于“是否为节日限定”选项的值
}
//将 商品类别 赋值到表格里
const loadScene = (value: any) => {
  form.value.shotScene = value  //三元运算符 flag=1 是关于"商品类别"选项的值  flag=0 是关于“是否为节日限定”选项的值
}
//提交表单
const onSubmit = async () => {
  console.log('提交前的表单---->',form.value)
  const values : any = Object.values(form.value);
  // 使用some()方法来检查是否有任何值为空
  console.log(form.value.bookingDateAddRequestList)
  if (values.some((value:any) => value === null || value === undefined || value === '' || value === 'PHA+PGJyPjwvcD4=') || form.value.bookingDateAddRequestList.length == 0) {
    ElMessage({
      type: 'warning',
      message: '请检查表单数据是否完整填写'
    })
    return;  //空返回结束函数
  }
  // console.log(form.value);
  const res = await myAxios.post('/photoProducts/add', {
    name: form.value.name,
    price: form.value.price,
    count: form.value.count,
    shotScene: form.value.shotScene,
    categoryName: form.value.categoryName,
    introImg: form.value.goodImg,
    richText: form.value.richText,
    bookingDateAddRequestList: toRaw(form.value.bookingDateAddRequestList)
  })
  // console.log(res.data);
  if (res.data.code === 1) {
    ElMessage({
      type: 'success',
      message: "提交成功"
    })
    reload() //上传完后重置表单
  } else {
    ElMessage.error("服务错误")
    return;
  }
}

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

const getRichTextInfo =(info:any)=>{   //子组件富文本传值
  // console.log('info-->',info);
  form.value.richText = encode64(info)
  previewRichText.value = info
}

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

const getDateInfo = ( info:any )=>{   //日期组件传值
  // console.log('info---->',info)
  form.value.bookingDateAddRequestList = info
}

const getOutfitKind = async ()=> {   //获取服装类别
  const res = await myAxios.post('/photoCategory/list',{})
  for(let key in res.data.data) {
    typeList.value.push(res.data.data[key].name)
  }
}

</script>

<style scoped>

.form-FormContainer {
  width: 540px;
  min-width: 540px;
  height: 800px;
  overflow: auto;
  /* margin: 0 auto; */
}

.form-RichTextContainer {
  width: 460px;
  min-width: 460px;
  height: 800px;
  overflow: auto;
}

.diyConponent /deep/ .el-card__body {
  padding: 20px 0 10px 0 !important;
}

.flexCard {
  display: flex;
  height: 100%;
}

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

.kinds {
  display: flex;
}

.totalButton {
  display: flex;
  justify-content: space-evenly;
  /*  盒子里面水平分隔开 */
}

.numberSelect {
  display: flex;
}

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