<template>
  <div>
    <el-upload
      ref="uploadImg"
      :auto-upload="false"
      :file-list="fileList"
      :on-change="onChange"
      :limit="limit"
      :on-exceed="handleExceed"
      accept=".jpg,.png"
      action="#"
      list-type="picture-card">
      <i slot="default" class="el-icon-plus"/>
      <div slot="file" slot-scope="{file}">
        <el-image :src="file.url" alt=""/>
        <span class="el-upload-list__item-actions">
          <span @click="handlePictureCardPreview(file)">
            <i class="el-icon-zoom-in"/>
          </span>
          <span class="el-upload-list__item-delete" @click="handleRemove(file)">
            <i class="el-icon-delete"/>
          </span>
        </span>
      </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible" :append-to-body="appendToBody">
      <img :src="dialogImageUrl" width="100%" alt="">
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'UploadImg',
  props: {
    fileList: {
      type: Array,
      default() {
        return []
      }
    },
    limit: {
      type: Number,
      default: 1
    },
    appendToBody: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false
    }
  },
  methods: {
    handleRemove(file) {
      this.fileList.splice(this.fileList.findIndex(item => item.uid === file.uid), 1)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    onChange(file, fileList) {
      this.$emit('update:fileList', fileList)
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 ${this.limit} 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
    }
  }
}
</script>
<style scoped>
</style>
