<template>
  <div>
    <div class="cameraDiv1">
      <div class="imgPre" v-if="imgArr.length">
        <van-icon
          class="photobrowsing"
          name="expand-o"
          size="22px"
          color="#1989fa"
          @click="deleteImgs" />
        <img :src="imgUrlArr[0]" alt="" />
      </div>
      <p class="coverImg" @click="uploadImg">
        <van-icon class="photo ico" name="photograph" size="16px" color="#969696">{{
          imgText
        }}</van-icon>
      </p>
    </div>
    <p style="text-align: center">{{ imgText }}</p>
  </div>
</template>

<script>
import { ImagePreview } from 'vant';
import axios from 'axios';
import { uploadImagev } from '@/api/index';

export default {
  name: 'uploadImg',
  props: {
    uploadid: {
      type: String,
      default: '',
    },
    imgText: {
      type: String,
      default: '',
    },
    visitsId: {
      type: String,
      default: '',
    },
    taskId: {
      type: String,
      default: '',
    },
    collectionId: {
      type: String,
      default: '',
    },
    type: {
      type: Number,
      default: 1,
    },
    imgArr: {
      type: [String, Array],
    },
    count: {
      type: Number,
      default: 1,
    },
  },
  data() {
    return {
      shows: false,
      url: '',
      localIds: [],
      num: 0,
      serverIdArr: [],
      imgUrlArr: [],
    };
  },
  watch: {
    imgArr: {
      handler(val) {
        this.imgUrlArr = val.split(',');
      },
      immediate: true,
    },
  },
  methods: {
    deleteImgs() {
      ImagePreview(this.imgUrlArr);
    },
    uploadImg() {
      if (localStorage.getItem('chainName') == null) {
        this.$toast('请输入名称！');
        return;
      }
      this.wx.ready(() => {
        this.wx.chooseImage({
          count: this.count,
          sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
          sourceType: ['camera'], // 可以指定来源是相册还是相机，默认二者都有
          defaultCameraMode: 'normal', //表示进入拍照界面的默认模式，目前有normal与batch两种选择，normal表示普通单拍模式，batch表示连拍模式，不传该参数则为normal模式。从3.0.26版本开始支持front和batch_front两种值，其中front表示默认为前置摄像头单拍模式，batch_front表示默认为前置摄像头连拍模式。（注：用户进入拍照界面仍然可自由切换两种模式）
          isSaveToAlbum: 0,
          success: (chooseRes) => {
            this.localIds = chooseRes.localIds;
            console.log('count=' + this.count);
            console.log('chooseRes=' + chooseRes);
            console.log('localIds=' + this.localIds);
            // if (chooseRes.localIds != undefined) {
            //   localIds = chooseRes.localIds[0];
            // } else {
            //   localIds = chooseRes.localId;
            // }
            this.num = 0;
            this.serverIdArr = [];
            this.forUploadImage();
          },
        });
      });
    },
    forUploadImage() {
      this.wx.uploadImage({
        localId: this.localIds[this.num],
        isShowProgressTips: 1,
        success: (uploadRes) => {
          this.serverIdArr.push(uploadRes.serverId);
          console.log('serverId=' + uploadRes.serverId);
          this.num++;
          if (this.num == this.localIds.length) {
            this.uploadImagev(this.serverIdArr.join(','));
          } else {
            this.forUploadImage();
          }
        },
      });
    },
    uploadImagev(meidaId) {
      var that = this;
      var form = {
        mediaId: meidaId,
        storeName: localStorage.getItem('chainName'),
        locationRemark: localStorage.getItem('locationRemark'),
        deptName: localStorage.getItem('deptName'),
      };
      var loind1 = that.$toast.loading({
        duration: 0,
        message: '上传中...',
        forbidClick: true,
      });
      uploadImagev(form).then((res) => {
        if (res.code == 200) {
          // that.imgArr = res.data.url;
          let imgArr = [];
          res.data.forEach((item) => {
            imgArr.push(item.url);
          });
          loind1.clear();
          that.$toast('上传成功！');
          that.$emit('newimgarr', { fileUrl: imgArr.join(','), type: that.type });
        } else {
          that.$toast('上传失败！');
        }
      });
    },
  },
};
</script>

<style scoped>
.cameraDiv1 {
  position: relative;
  height: 164px;
  width: 100%;
  background-color: white;
}

.cameraDiv1 img {
  position: absolute;
  width: 100%;
  display: block;
  height: 164px;
  top: 0;
}

.imgPre {
  height: 164px;
  width: 100%;
  background-color: white;
  border-radius: 6px;
  overflow: hidden;
}

.photos1 {
  margin: 70px auto;
  left: 50%;
  margin-left: -14px;
}

.photobrowsing {
  position: absolute;
  padding: 4px;
  right: 0;
  top: 0;
  z-index: 99;
  background-color: rgba(255, 255, 255, 0.8);
  border-bottom-left-radius: 3px;
  border-top-left-radius: 3px;
}

.coverImg {
  text-align: center;
  position: absolute;
  top: 0;
  left: 0;
  height: 164px;
  width: 100%;
}

.coverImg .ico {
  top: 42%;
}
</style>
