<template>
  <div class="upload-control">
    <div class="section">
      <div class="upload" @click="chooseFile">
        <input type="file" v-show="false" ref="fileInput" :accept="accept" @change="onFileInputChange" />
        <span class="iconfont icon-add"></span>
      </div>
      <template v-if="showImage">
        <div class="remove"><span class="iconfont icon-delete"></span></div>
        <div class="preview"><img src="" alt="" /></div>
      </template>
    </div>
    <div class="tip" v-if="tipText" v-text="tipText"></div>
  </div>
</template>

<script>
export default {
  props: {
    tipText: String,
    action: {
      type: String,
      default: () => process.env.BASE_URL + '/wx/upload/image',
    },
    limit: {
      type: Number,
      default: 1,
    },
    accept: {
      type: String,
      default: '.jpg,.png,.gif',
    },
    fileList: {
      type: Array,
      default: () => [],
    },
  },
  computed: {
    showImage() {
      return this.fileList.length > 0
    },
  },
  methods: {
    chooseFile() {
      this.$nextTick(() => {
        this.$refs.fileInput.click()
      })
    },
    onFileInputChange(e) {
      console.log(e.target.files)
    },
  },
}
</script>

<style lang="scss" scoped>
@media screen and (min-width: 768px) {
  .upload-control {
    .section {
      width: 180px;
      height: 180px;
      position: relative;
      margin: 0 auto;

      .remove {
        position: absolute;
        right: 0;
        top: 0;
        width: 28px;
        height: 28px;
        line-height: 28px;
        text-align: center;
        background: rgba(0, 0, 0, 0.4);
        z-index: 1;
        cursor: pointer;
        .icon-delete {
          font-size: 18px;
          color: #f1f1f1;
        }
      }

      .preview {
        position: absolute;
        width: 100%;
        height: 100%;
        left: 0;
        top: 0;

        img {
          display: block;
          width: 100%;
          height: 100%;
          object-fit: contain;
        }
      }

      .upload {
        width: 100%;
        height: 100%;
        border: 1px dashed #bec3cc;
        display: flex;
        justify-content: center;
        align-items: center;
        background: #fbfbfb;
        box-sizing: border-box;
        .icon-add {
          font-size: 36px;
          color: #bec3cc;
        }
      }
    }

    .tip {
      width: 266px;
      margin: 0 auto;
      margin-top: 36px;
      line-height: 24px;
      font-size: 14px;
      color: #b2b2b2;
      text-align: center;
    }
  }
}

@media screen and (max-width: 768px) {
  .upload-control {
    .section {
      width: 42vw;
      height: 42vw;
      position: relative;
      margin: 0 auto;

      .remove {
        position: absolute;
        right: 0;
        top: 0;
        width: 5vw;
        height: 5vw;
        line-height: 5vw;
        text-align: center;
        background: rgba(0, 0, 0, 0.4);
        z-index: 1;
        cursor: pointer;
        .icon-delete {
          font-size: 3.6vw;
          color: #f1f1f1;
        }
      }

      .preview {
        position: absolute;
        width: 100%;
        height: 100%;
        left: 0;
        top: 0;

        img {
          display: block;
          width: 100%;
          height: 100%;
          object-fit: contain;
        }
      }

      .upload {
        width: 100%;
        height: 100%;
        border: 0.1vw dashed #bec3cc;
        display: flex;
        justify-content: center;
        align-items: center;
        background: #fbfbfb;
        box-sizing: border-box;
        .icon-add {
          font-size: 7.2vw;
          color: #bec3cc;
        }
      }
    }

    .tip {
      width: 57vw;
      margin: 0 auto;
      margin-top: 4.8vw;
      line-height: 5.6vw;
      font-size: 3.2vw;
      color: #b2b2b2;
      text-align: center;
    }
  }
}
</style>
