<template>
  <div>
    <el-upload
      ref="upload"
      :action="newUrl"
      :headers="myHeaders"
      list-type="picture-card"
      :multiple="false"
      :limit="1"
      :file-list="imgList"
      :on-success="handleAvatarSuccess"
      :disabled="limit <= value.length"
    >
      <i slot="default" class="el-icon-plus" />
      <div slot="file" slot-scope="{ file }">
        <img class="el-upload-list__item-thumbnail" :src="file.url" alt="">
        <span class="el-upload-list__item-actions">
          <span
            class="el-upload-list__item-preview"
            @click="handlePictureCardPreview(file)"
          >
            <i class="el-icon-zoom-in" />
          </span>
          <!-- <span
            v-if="!disabled"
            class="el-upload-list__item-delete"
            @click="handleDownload(file)"
          >
            <i class="el-icon-download" />
          </span> -->
          <span
            v-if="!disabled"
            class="el-upload-list__item-delete"
            @click="handleRemove(file)"
          >
            <i class="el-icon-delete" />
          </span>
        </span>
      </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible" :modal="false">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
import { UPLOAD_URL } from '@/const/urlKey'

export default {
  name: 'Upload',
  props: {
    value: {
      type: Array,
      default: () => []
    },
    limit: {
      type: Number,
      default: 1
    },
    params: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      url: UPLOAD_URL,
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false,
      myHeaders: {
        authorization: 'Bearer swagger-ui'
      },
      imgList: this.value,
      tmp: []
    }
  },

  computed: {
    newUrl() {
      const params = this.params
      if (params === '') {
        return UPLOAD_URL
      } else {
        return UPLOAD_URL + `?${params}`
      }
    }
  },

  watch: {
    value: {
      handler(val) {
        // if (this.imgList.length < val.length) {
        this.imgList = val
        // }
      },
      immediate: true
    }
  },

  methods: {
    beforeAvatarUpload(file) {},
    handleAvatarSuccess(res, file, fileList) {
      if (file.response.code !== 200) {
        this.$refs.upload.clearFiles()
        this.$message.error('上传失败，请稍后再试~')
        this.$emit('input', [])
        return false
      }
      const result =
    fileList.length > 0
      ? fileList.map(x => ({
        name: x.name,
        url: process.env.VUE_APP_BASE_IMG + x.response.data.path
      }))
      : []
      this.$emit('input', result)
    },
    // handleChange(file, fileList) {
    //   if (file.response) {
    //     // this.imgList = this.tmp
    //   }
    //   console.log(file)
    //   console.log(fileList)
    //   // const result = fileList.length > 0 ? fileList.map(x => ({
    //   //   name: x.name,
    //   //   url: x.response.data.path
    //   //   // url: x.url
    //   // })) : []
    //   // this.imgList = result
    //   // const result = this.tmp
    //   // this.imgList = JSON.parse(JSON.stringify(this.tmp))
    // },
    handleRemove(file, fileList) {
      const result = fileList
        ? fileList.map(x => ({
          name: x.name,
          url: x.response.data.path
        }))
        : []
      this.$emit('input', result)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    }
  }
}
</script>

<style scoped>
.page-select {
 display: flex;
 justify-content: flex-end;
 align-items: center;
 padding: 20px;
}
</style>
