/**
* @Author Sean
* @Date 2019/4/19
*/
<template>
  <div>
    <div class="public-style file-info">
      <list-item v-for="(item_list,index) in cdd_item" :key="index">
        <item>
          <img slot="left-icon" src="../../../../assets/image/contractMainten/item@2x.png" class="left-icon">
          <div  slot="name" style="width: 70%;font-size: 12px">
            <div class="cont-text" style="color: #5a5959;font-size: 14px">合同金额&nbsp;&nbsp;<span style="font-weight: 600;">12333333</span></div>
            <div class="cont-text" style="color: #5a5959;font-size: 14px">承租人&nbsp;&nbsp;&nbsp;&nbsp;张三</div>
          </div>
          <img  src="../../../../assets/image/right-arrow@2x.png" class="right-icon">
          <img  src="../../../../assets/image/right-arrow@2x.png" class="right-icon">
        </item>
      </list-item>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    cdd_item: Array,
    dowload_list: Array,
    upload_list: Array,
  },
  data () {
    return {

    }
  },
  created: function () {
  },
  mounted: function () {
  },
  updated: function () {
  },
  methods: {
    // 打开sheet操作
    show (checkId) {
      let vm = this
      wx.chooseImage({
        count: 9, // 微信最多能选择9张图片
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (data) {
          hlsPopup.showLoading('请稍候')
          vm.localIds = data.localIds // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
          data.localIds.forEach((localId, index) => {
            let obj = {
              'table_pk_value': checkId,
              'table_name': 'PRJ_CDD_ITEM_CHECK',
              'check_id': checkId,
              'picture': '',
              'filePath': localId,
              'attachment_id': '',
              'mediaId': '',
            }
            wx.getLocalImgData({
              localId: localId, // 图片的localID
              success: function (res) {
                obj.picture = res.localData
                vm.upload_list.push(obj)
              },
            })
          })
          vm.wxImageUpload(checkId)
        },
      })
    },
    // 图片上传到微信服务器 同时上传到业务系统
    wxImageUpload (checkId) {
      let vm = this
      let localId = vm.localIds.pop()
      wx.uploadImage({
        localId: localId, // 需要上传的图片的本地ID，由chooseImage接口获得
        isShowProgressTips: 0, // 默认为1，显示进度提示
        success: function (res) {
          if (vm.localIds.length > 0) {
            setTimeout(() => {
              vm.wxImageUpload(checkId)
            }, 100)
          }
          else {
            hlsPopup.hideLoading()
            hlsPopup.showLongCenter('图片上传成功')
          }
          vm.savePicture(res.serverId, checkId)
          for (let j = 0; j < vm.upload_list.length; j++) {
            if (vm.upload_list[j].filePath === localId) {
              vm.upload_list[j].mediaId = res.serverId
              break
            }
          }
        },
      })
    },
    savePicture (mediaId, checkId) {
      let vm = this
      let url = process.env.rootPath + '/media/upload' + '?index=' + mediaId // 附件查询
      let param = {
        'wechatCode': $config.wxCode,
        'mediaId': mediaId,
        'sourceType': 'PRJ_CDD_ITEM_CHECK',
        'pkValue': checkId,
        'fileName': new Date().getTime() + '.jpg',
        'userId': window.localStorage.user_id,
        'uploadApiName': process.env.uploadApiName,
        'uploadSysName': process.env.uploadSysName,
      }
      hlsHttp.post(url, param).then(function (res) {
        if (res.success === 'true') {
          for (let j = 0; j < vm.uploadList.length; j++) {
            if (vm.uploadList[j].mediaId === res.mediaId) {
              vm.uploadList[j].attachment_id = res.attachmentId
              break
            }
          }
        }
      })
    },
    /**
       * 删除图片
       *
       */
    delete_pic (attachment_id) {
      let vm = this
      let delete_list = []
      let obj = {'attachment_id': attachment_id}
      delete_list.push(obj)
      let url = process.env.basePath + 'attment_delete' // 附件删除
      let param = {
        'picturelist': delete_list,
      }
      hlsPopup.showLoading('请稍候')
      vm.$post(url, param).then(function (res) {
        hlsPopup.hideLoading()
        if (res.result === 'S') {
          hlsPopup.showLongCenter('删除成功!')
          vm.remove_dowload_list(attachment_id)
        } else if (res.result === 'E') {
          hlsPopup.showLongCenter(res.message)
        }
      })
    },
    /**
       * 实现刷新效果，同时为了避免从新调用接口浪费流量
       * 从附件列表中移除删除的图片
       */
    remove_dowload_list (attachment_id) {
      let vm = this
      for (var i = 0; i < vm.dowload_list.length; i++) {
        var list = vm.dowload_list[i]
        for (var j = 0; j < list.length; j++) {
          var li = list[j]
          if (li.attachment_id === attachment_id) {
            list.remove(j)
            break
          }
        }
      }
    },
    // 移除待上传列表中的某条记录
    remove_pic () {
      let vm = this
      if (vm.upload_list[index].attachment_id) {
        vm.delete_pic(vm.upload_list[index].attachment_id)
      }
      vm.upload_list.remove(index)
    },
    /**
       * 图片放大预览
       * @param imgObject.imgUrl
       */
    showBigPicture (imgUrl) {
      hlsPopup.showBigPicture({
        imgUrl: imgUrl,
      })
    }
    ,
  },
}
</script>

<style scoped lang="less" rel="stylesheet">
  .file-info {
    .hls-item .contents .add-name {
      flex: 9 1 0% !important;
      width: 90%!important;
    }

  }
</style>
