<template>
  <div>
    <el-form ref="resetFormData" :model="form" label-width="auto" style="width: 750px" size="large">
          <el-form-item label="添加服装图片">
            <el-upload
                ref="uploadProductImg"
                action="#"
                list-type="picture-card"
                :auto-upload="false"
                multiple="true"
                :on-remove="handleRemove"
                @change="(event: any) => handleChange(event, 0)"
                :on-exceed="Exceed_ProductImg"
                limit="1" >
              <el-icon>
                <Plus />
              </el-icon>
            </el-upload>
          </el-form-item>
          <el-form-item label="添加效果图">
            <el-upload
                ref="uploadProductDetail"
                action="#"
                list-type="picture-card"
                :auto-upload="false"
                multiple="true"
                :on-change="(event: any) => handleChange(event, 1)"
                :on-exceed="Exceed_ProductDetail"
                limit="1"
                :on-remove="handleRemove"
            >
              <el-icon>
                <Plus />
              </el-icon>
            </el-upload>
          </el-form-item>
          <el-form-item label="服装名称" prop="name">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item label="服装价格" prop="price">
            <el-input v-model="form.price" />
          </el-form-item>
          <el-form-item label="服装类别" prop="intro">
            <el-input v-model="form.clothesType" />
          </el-form-item>
          <el-form-item label="服装简介" prop="intro">
            <el-input v-model="form.intro" />
          </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">

import {Plus} from "@element-plus/icons-vue";
import {ElMessage, genFileId, type UploadProps, type UploadRawFile} from "element-plus";
import myAxios from "@/api/myAxios";
import {inject, ref} from "vue";
const fileSimple = ref()
const uploadProductImg : any = ref()
const uploadProductDetail : any = ref()
const form = ref({
  name:'',
  image:'',
  effectImg:'',
  intro:'',
  price:'',
  clothesType:''
})
const handleRemove: UploadProps['onRemove'] = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles)
}
const handleChange = async (file: any, flag: number) => {
  fileSimple.value = file
  let formData = new FormData()
  formData.append("file", fileSimple.value.raw)
  const res = await myAxios({
    url: '/file/upload/server/not_login',
    method: 'post',
    headers: {
      'content-Type': 'multipart/form-data'
    },
    data: {
      biz: "test",
      file: formData.get("file")
    }
  })
  if (res.data.code === 1) {
    flag ? form.value.effectImg = res.data.data : form.value.image = res.data.data
  }
}
const Exceed_ProductImg: UploadProps['onExceed'] = (files) => { //覆盖商品照片
  uploadProductImg.value!.clearFiles()
  const file = files[0] as UploadRawFile
  file.uid = genFileId()
  uploadProductImg.value!.handleStart(file)
}
const Exceed_ProductDetail: UploadProps['onExceed'] = (files) => {  //覆盖商品详情图片
  uploadProductDetail.value!.clearFiles()
  const file = files[0] as UploadRawFile
  file.uid = genFileId()
  uploadProductDetail.value!.handleStart(file)
}
const reload : any = inject("reload")
const onSubmit = async () => {
  const values = Object.values(form.value);
  // 使用some()方法来检查是否有任何值为空
  if (values.some(value => value === null || value === undefined || value === '')) {
    ElMessage({
      type:'warning',
      message:'请检查表单数据是否完整填写'
    })
    return; 
  }
  const res = await myAxios.post('/clothesInfo/add', { ...form.value })
  if (res.data.code === 1) {
    ElMessage({
      type: 'success',
      message: "提交成功"
    })
    reload() //上传完后重置表单
  } else {
    ElMessage.error("服务错误")
    return;
  }
}

</script>

<style scoped>

</style>