<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form ref="form" label-width="80px">
          <el-form-item label="标题">
            <el-input v-model="formData.title"/>
          </el-form-item>
          <el-form-item label="封面">
            <div style="margin: 10px">
              <el-button @click="uploadType = true" v-if="!uploadType">地址上传</el-button>
              <el-button @click="uploadType = false" v-if="uploadType">文件上传</el-button>
            </div>
            <el-upload
              v-if="!uploadType"
              class="avatar-uploader"
              :http-request="uploadFileImage"
              :before-upload="beforeAvatarUpload"
              action="https://jsonplaceholder.typicode.com/posts/"
              :on-success="uploadImgSuccess"
              :show-file-list="false">
              <img v-if="formData.cover" :src="formData.cover" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <el-input v-model="formData.cover" v-if="uploadType"></el-input>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import {uploadFileApi} from "../../service/api";
  export default {
    name: "baseBlock",
    props:[
      'formData'
    ],
    data(){
      return{
        uploadType:false
      }
    },
    methods:{
      beforeAvatarUpload(file){
        const isJPG = (file.type === 'image/jpeg' || file.type === 'image/png' );
        const isLt2M = file.size / 1024  < 200;
        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 或 PNG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 200KB!');
        }
        return isJPG && isLt2M;
      },
      uploadFileImage(a){
          uploadFileApi({file:a.file,type:'local',obj:a}).then(res=>{
            this.formData.cover = process.env.IMAGE_URL_HEAD + res.url;
            this.$message({
              type: 'success',
              message: '上传成功!'
            });
            a.onSuccess('上传成功')
          });
      },
      uploadImgSuccess(a,b,c){
      }
    }
  }
</script>

<style scoped>

</style>
