<template>
  <div id="upload">
    <el-upload
      v-if="url"
      ref="upload"
      :action="url"
      :list-type="listType"
      :file-list="fileList"
      :limit="limit"
      :on-exceed="outLimit"
      :on-preview="handlePictureCardPreview"
      :before-remove="handleRemove"
      :on-success="onSuccess"
      :show-file-list="showList"
      :accept="accept"
    >
      <el-button size="small" class="isBtn" v-if="isBtn">点击上传文件</el-button>
      <template v-else>
        <i class="el-icon-plus"></i>
      </template>
      <template #tip v-if="tip">
        <span class="tip">{{ tip }}</span>
      </template>
      <template v-if="downBtn">
        <el-link :underline="false" @click="clickDown" class="links">下载模板</el-link>
      </template>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>

<script>
import _ from 'lodash';
export default {
  name: 'upload',
  props: {
    url: { type: null },
    limit: { type: Number },
    data: { type: null },
    type: { type: String },
    isBtn: { type: Boolean, default: false },
    downBtn: { type: Boolean, default: false },
    showList: { type: Boolean, default: true },
    accept: { type: String },
    tip: { type: String, default: undefined },
    listType: { type: String, default: 'picture-card' },
  },
  components: {},
  data: () => ({
    dialogVisible: false,
    dialogImageUrl: '',
    fileList: [],
  }),
  created() {
    if (this.data) {
      this.defalutProcess(this.data);
    }
  },
  watch: {
    data: {
      handler(val) {
        this.defalutProcess(val);
      },
    },
  },
  computed: {},
  methods: {
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleRemove(file) {
      return true;
    },
    outLimit() {
      this.$message.error('只允许上传1个文件');
    },
    onSuccess(response, file, fileList) {
      //将文件整理好传回父组件
      this.$emit('upload', { type: this.type, data: response });
    },
    defalutProcess(val) {
      if (typeof val === 'object' && _.get(val, length) !== undefined && val.length > 0) {
        let newArr = [];
        val.map(item => {
          let object = {};
          object.name = item.name;
          object.url = `${item.uri}`;
          newArr.push(object);
        });
        this.$set(this, `fileList`, newArr);
      } else if (typeof val === 'object' && _.get(val, length) === undefined) {
        let object = {};
        object.name = val.name;
        object.url = `${val.uri}`;
        this.$set(this, `fileList`, [object]);
      } else if (typeof val === 'string') {
        this.$set(this, `fileList`, [{ name: '附件', url: val }]);
      }
    },
    clickDown() {
      this.$emit('clickDown');
    },
  },
};
</script>

<style lang="less" scoped>
.isBtn {
  background-color: #e9021d;
  color: #ffffff;
}
.links {
  margin: 0 0 0 15px;
}
.tip {
  padding: 0 10px;
  display: inline-block;
  color: #999999;
}
</style>
