<template>
  <div>
    <el-upload
      ref="upload"
      class="upload-demo"
      :auto-upload="false"
      :on-change="beforeUpload"
      :on-remove="handleRemove"
      multiple
      :limit="1000"
      :file-list="dataModel"
      action=""
      :disabled="disabled"
    >
      <el-button :disabled="disabled" slot="trigger" size="small" type="primary">选择文件</el-button>
    </el-upload>
  </div>
</template>

<script>
import Viewer from "viewerjs";
import Draggable from "vuedraggable";
import * as qiniu from "qiniu-js";
require("viewerjs/dist/viewer.css");
export default {
  components: {
    Draggable,
  },
  props: {
    value: {
      type: Array,
      default: () => [],
    },
    width: {
      type: Number,
      default: 100,
    },
    height: {
      type: Number,
      default: 100,
    },
    token: {
      type: String,
      default: "",
    },
    domain: {
      type: String,
      default: "",
    },
    multiple: {
      type: Boolean,
      default: true,
    },
    length: {
      type: Number,
      default: 9,
    },
    min: {
      type: Number,
      default: 0,
    },
    meitu: {
      type: Boolean,
      default: false,
    },
    isEdit: {
      type: Boolean,
      default: false,
    },
    disabled: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      dataModel: this.value
    };
  },
  computed: {
    miniWidth() {
      if (this.width > this.height) {
        return this.height;
      } else {
        return this.width;
      }
    },
  },
  mounted() {
    this.$emit("input", this.dataModel);
  },
  methods: {
    beforeUpload(file,fileList) { 
      this.dataModel = fileList
    },
    handleRemove(file, fileList) {
      this.dataModel = fileList
    },
    
  },
  watch: {
    value: {
      handler(val) {
        this.dataModel = val;
      },
      immediate: true,
    },
    dataModel: {
      handler(val) {
        this.$emit("input", val);
      },
      immediate: true,
    },
  },
};
</script>

<style lang="scss">
.fm-uplaod-container {
  .is-disabled {
    position: relative;

    &::after {
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      // background: rgba(0,0,0,.1);
      content: "";
      display: block;
      cursor: not-allowed;
    }
  }

  .upload-file {
    margin: 0 10px 10px 0;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    // background: #fff;
    overflow: hidden;
    background-color: #fff;
    border: 1px solid #c0ccda;
    border-radius: 6px;
    box-sizing: border-box;
    position: relative;
    vertical-align: top;
    &:hover {
      .uplaod-action {
        display: flex;
      }
    }
    .uplaod-action {
      position: absolute;
      // top: 0;
      // height: 30px;
      bottom: 0;
      left: 0;
      right: 0;
      background: rgba(0, 0, 0, 0.6);
      display: none;
      justify-content: center;
      align-items: center;
      i {
        color: #fff;
        cursor: pointer;
        margin: 0 5px;
      }
    }
    &.is-success {
      .item-status {
        position: absolute;
        right: -15px;
        top: -6px;
        width: 40px;
        height: 24px;
        background: #13ce66;
        text-align: center;
        transform: rotate(45deg);
        box-shadow: 0 0 1pc 1px rgba(0, 0, 0, 0.2);
        & > i {
          font-size: 12px;
          margin-top: 11px;
          color: #fff;
          transform: rotate(-45deg);
        }
      }
    }
    &.uploading {
      &:before {
        display: block;
        content: "";
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.3);
      }
    }
    .upload-progress {
      position: absolute;
      .el-progress__text {
        color: #fff;
        font-size: 16px !important;
      }
    }
    img {
      max-width: 100%;
      max-height: 100%;
      vertical-align: middle;
    }
  }
  .el-upload--picture-card {
    position: relative;
    overflow: hidden;
    .el-icon-plus {
      position: absolute;
      top: 50%;
      left: 50%;
    }
  }
  .upload-input {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: block;
    opacity: 0;
    cursor: pointer;
  }

  .drag-img-list {
    display: inline;

    .ghost {
      position: relative;
      &::after {
        width: 100%;
        height: 100%;
        display: block;
        content: "";
        background: #fbfdff;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        border: 1px dashed #3bb3c2;
      }
    }

    & > div {
      cursor: move;
    }
  }
}

.viewer-container {
  z-index: 9999 !important;
}
</style>
