<template>
  <div>
    <el-dialog title="上传图片和剪裁" :visible.sync="visible" :close-on-click-modal="false" @close="close" append-to-body>
      <div style="height:350px">
        <vue-cropper
          ref="cropper"
          :img="options.img"
          :info="true"
          :autoCrop="options.autoCrop"
          :fixedBox="options.fixedBox"
          :fixed="options.fixed"
          :full="options.full"
          :fixedNumber="fixed_number"
          :centerBox="options.centerBox"
          :outputSize="options.outputSize"
          v-if="options.img"
        />
      </div>
      <div slot="footer" class="dialog-footer">
        <div class="lsw_flex">
          <div class="f">
            <div class="lsw_flex">
              <div class="f4">
                <el-upload action="#" :http-request="requestUpload" accept=".jpg,.jpeg,.png,.gif" :show-file-list="false" :before-upload="beforeUpload">
                  <el-button size="small" icon="el-icon-scissors">选取图片</el-button>
                </el-upload>
              </div>
              <div class="f lsw_left ml15"><el-button type="primary" size="small" icon="el-icon-upload" @click="uploadImg()">确认上传</el-button></div>
            </div>
          </div>
          <div class="f">
            <el-button-group v-show="options.img">
              <el-button icon="el-icon-plus" @click="changeScale(1)"></el-button>
              <el-button icon="el-icon-minus" @click="changeScale(-1)"></el-button>
              <el-button icon="el-icon-refresh-left" @click="rotateLeft()"></el-button>
              <el-button icon="el-icon-refresh-right" @click="rotateRight()"></el-button>
            </el-button-group>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { VueCropper } from 'vue-cropper';
import { uploadFile } from '@/api/common';

export default {
  components: { VueCropper },
  props: {
    //裁剪宽高比例
    fixed_number: {
      type: Array,
      default: [21, 9]
    },
    modName:{
      type:String,
      default:""
    }
  },
  data() {
    return {
      visible: false,
      options: {
        img: '', //裁剪图片的地址
        autoCrop: true, //是否默认生成截图框
        outputSize: 0.8, //裁剪生成图片的质量
        fixed: true, // 是否开启截图框宽高固定比例
        full: true, //是否输出原图比例的截图
        fixedBox: false, //固定截图框大小 不允许改变
        centerBox: true //截图框是否被限制在图片里面
      }
    };
  },
  methods: {
    editCropper(data) {
      this.visible = true;
    },
    // 向左旋转
    rotateLeft() {
      this.$refs.cropper.rotateLeft();
    },
    // 向右旋转
    rotateRight() {
      this.$refs.cropper.rotateRight();
    },
    // 图片缩放
    changeScale(num) {
      num = num || 1;
      this.$refs.cropper.changeScale(num);
    },
    // 覆盖默认的上传行为
    requestUpload() {},
    // 上传预处理
    beforeUpload(file) {
      if (file.type.indexOf('image/') == -1) {
        this.msgError('文件格式错误，请上传图片类型,如：JPG，PNG后缀的文件。');
        return;
      }
      const reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = () => {
        this.options.img = reader.result;
      };
    },
    // 上传图片
    uploadImg() {
      if (this.options.img == '') {
        this.msgError('请选取图片');
        return;
      }
      this.$refs.cropper.getCropBlob(data => {
        let formData = new FormData();
        formData.append('file', data);
        formData.append("modName",this.modName)
        
        uploadFile(formData).then(response => {
          if (response.code === 200) {
            this.$emit('img', response.fileName);
            this.msgSuccess('上传成功');
            this.visible = false;
          }
        });
      });
    },
    // 关闭弹窗表单重置
    close() {
      this.visible = false;
      this.options.img = '';
    }
  }
};
</script>
<style lang="scss">
.lsw_flex {
  display: flex;
}
.lsw_flex .f {
  flex: 1;
}
.lsw_left {
  text-align: left;
  margin-left: 20px;
}
</style>
