<template>
  <div>
    <el-button v-if="disabled" disabled="true" type="primary" :style="{'width': width}" class="mt10"> {{placeholder}}</el-button>

    <el-upload action="" v-else :on-change='changeUpload' :auto-upload="false" :multiple="false" :show-file-list="false"
      accept="image/jpeg" class="image-uploader">
      <el-button type="primary" :style="{'width': width}" style="margin-top: 8px;">
        {{placeholder}}
      </el-button>
    </el-upload>
    <!-- <h1>图片上传</h1>
    <div>
      <el-upload
        class="avatar-uploader"
        action="https://jsonplaceholder.typicode.com/posts/"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
      >
      <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </div> -->

    <el-dialog title="图片剪裁" :visible.sync="dialogVisible" append-to-body width="350px">
      <div class="cropper-content">
        <div class="cropper" style="text-align:center">
          <vueCropper ref="cropper" :img="option.img" :outputSize="option.size" :outputType="option.outputType" :info="true"
            :full="option.full" :canMove="option.canMove" :canMoveBox="option.canMoveBox" :original="option.original"
            :autoCrop="option.autoCrop" :fixed="option.fixed" :fixedNumber="option.fixedNumber" :centerBox="option.centerBox"
            :infoTrue="option.infoTrue" :fixedBox="option.fixedBox"></vueCropper>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="finish" :loading="loading">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {
    VueCropper
  } from 'vue-cropper'
  import {
    uploadImg
  } from "@/components/httpServer/httpServer.js";
  export default {
    components: {
      VueCropper
    },
    props: ['placeholder', 'disabled', 'imgType', 'width'],
    data() {
      return {
        imageUrl: '',
        dialogVisible: false,
        // 裁剪组件的基础配置option
        option: {
          img: '', // 裁剪图片的地址
          info: true, // 裁剪框的大小信息
          outputSize: 0.8, // 裁剪生成图片的质量
          outputType: 'jpg', // 裁剪生成图片的格式
          canScale: true, // 图片是否允许滚轮缩放
          autoCrop: true, // 是否默认生成截图框
          autoCropWidth: 200, // 默认生成截图框宽度
          autoCropHeight: 240, // 默认生成截图框高度
          fixedBox: false, // 固定截图框大小 不允许改变
          fixed: false, // 是否开启截图框宽高固定比例
          fixedNumber: [5,6], // 截图框的宽高比例
          full: false, // 是否输出原图比例的截图
          canMoveBox: true, // 截图框能否拖动
          original: false, // 上传图片按照原始比例渲染
          centerBox: false, // 截图框是否被限制在图片里面
          infoTrue: true // true 为展示真实输出图片宽高 false 展示看到的截图框宽高
        },
        picsList: [], //页面显示的数组
        // 防止重复提交
        loading: false,
        fileinfo: {}
      }
    },
    methods: {
      changeUpload(file, fileList) {
        const isLt5M = file.size / 1024 / 1024 < 5
        if (!isLt5M) {
          this.$message.error('上传文件大小不能超过 5MB!')
          return false
        }
        this.fileinfo = file
        // 上传成功后将图片地址赋值给裁剪框显示图片
        this.$nextTick(() => {
            this.option.img = URL.createObjectURL(file.raw);
            this.dialogVisible = true
         })
      },

      // handleAvatarSuccess(res, file, fileList) {
      //   //上传成功后将图片地址赋值给裁剪框显示图片
      //   this.$nextTick(() => {
      //     this.option.img = URL.createObjectURL(file.raw);
      //     this.fileinfo = res
      //     this.dialogVisible = true
      //   })
      // },
      // beforeAvatarUpload(file) {
      //   const isJPG = file.type === 'image/jpeg';
      //   const isLt2M = file.size / 1024 / 1024;

      //   if (!isJPG) {
      //     this.$message.error('上传头像图片只能是 JPG 格式!');
      //   }
      //   if (!isLt2M) {
      //     this.$message.error('上传头像图片大小不能超过 2MB!');
      //   }
      //   return isJPG && isLt2M;
      // },
      finish() {
        let that = this
        this.$refs.cropper.getCropBlob((data) => {
          var fileName = 'goods' + this.fileinfo.uid
          this.loading = true
          const formData = new FormData()
          formData.append('avatar', data)
          uploadImg(that.imgType, formData).then(res => {
            that.onupload = false
            if (res.code == 200) {
              this.loading = false
              that.$emit('onFinish', res.data.url)
            }
          })
          //上传阿里云服务器
          //请求
        })
      }
    }
  }
</script>
<style scoped>
  .avatar-uploader {
    background: red !important;
    width: 150px;
    height: 150px;
    text-align: center;
    line-height: 150px;
  }

  .el-icon-plus {
    width: 150px;
    height: 150px;
    font-size: 30px;
  }

  .cropper-content{
    width:300px;
    height:300px;
    background: pink;
  }
  .cropper{
    width:300px;
    height:300px;
    margin: 0px auto;
    background: yellow;
  }
</style>
