<template>
  <div class="contents">
    <div id="capture" ref="xingdownLoad" style="height: 100vh; overflow: hidden">
      <img src="@/assets/images/list_img.png" alt="" />
      <div class="box">
        <div class="title">
          <p class="organ">{{ orderList.manageComNameFull }}</p>
          <p class="name">{{ orderList.name }}</p>
        </div>
        <div class="pName">{{ orderList.productNameFull }}</div>
        <div class="type">{{ orderList.outOrderType == 'accept' ? '承保规保' : '预收规保' }}</div>
        <div class="yuan">{{ orderList.prem }}<span style="color: #333333">元</span></div>
        <div class="time">{{ orderList.outOrderType == 'accept' ? '承保时间：' : '预收时间：' }}{{ orderList.appntDate }}</div>
      </div>
    </div>
    <div class="flex justify-content-s bottom-btn border-color">
      <!-- <van-button square type="default" :disabled="shareJin" size="large" @click="shareImg" v-no-more-click="1000"> 分享 </van-button> -->
      <!-- <van-button square type="danger" :disabled="downLoadJin" size="large" @click="downLoadImagesFunc" v-no-more-click="1000"> 下载 </van-button> --> 
      <button class="shareImg"  @click="fenxiang()" v-no-more-click="1000"> 分享 </button>
      <button class="downLoad"  @click="downLoadImagesFunc()" v-no-more-click="1000"> 下载 </button>
    </div>
    <div class="close" @click="close()">
      <van-icon name="cross" size="38" color="#fff" />
    </div>
  </div>
</template>

<script>
import html2canvas from 'html2canvas'
import { saveAs } from 'file-saver'
import { base64Excel } from '@/api/ebiz/nbs'
export default {
  name: 'generateImg',
  // generateImgData
  props: {
    // generateImgData: {
    //   type: Object,
    //   default: {}
    // },
  },
  data() {
    return {
      orderList: {},
      isIOS: false,
      shareJin: false,
      downLoadJin: false,
    }
  },
  // beforeCreate() {
  //   this.$refs.xingdownLoad.innerHTML = ''; // 清空内容
  // },
  created() {
    this.isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
    if(this.isIOS) {
      document.getElementsByClassName('box').style.top = '38%'
    }
  },
  mounted() {
    this.getList()
  },
  beforeDestroy(){
    this.$refs.xingdownLoad.innerHTML = ''; // 清空内容
  },
  methods: {
    getList() {
      this.orderList = JSON.parse(localStorage.getItem('orderList'))
      // console.log('页面数据',this.orderList);
    },
    // 将 Blob转base64
    blobToBase64(blob) {
      return new Promise((resolve, reject) => {
        const reader = new FileReader()
        reader.onloadend = () => resolve(reader.result)
        reader.onerror = reject
        reader.readAsDataURL(blob)
      })
    },
    // 分享
    shareImg() {
      // const element = document.getElementById('capture')
      // const canvas = await html2canvas(element)
      html2canvas(this.$refs.xingdownLoad, {
        width: this.$refs.xingdownLoad.offsetWidth,
        height: this.$refs.xingdownLoad.offsetHeight,
        backgroundColor: '#fff',
        scale: 2,
        dpi: 300
      }).then((canvas) => {
        // 将 canvas 转换为 Blob
        canvas.toBlob((blob) => {
          this.blobToBase64(blob).then((base64String) => {
            let base64Manane = base64String.split(',')[1]
            // console.log(base64String)
            EWebBridge.webCallAppInJs('bridge', {
              flag: 'share',
              extra: {
                shareType: '1',
                base64: base64Manane
              }
            })
          })
          setTimeout(() => {
            this.close();
          }, 3000);
        }, 'image/png')
      })
    },
    // 生成图片并下载
    domToImage(domElement, quality, callback) {
      console.log(domElement);
      html2canvas(domElement, {
        width: domElement.offsetWidth,
        height: domElement.offsetHeight,
        backgroundColor: '#fff',
        scale: 1,
        dpi: 300
      }).then((canvas) => {
        // 转换canvas为PNG图片并压缩
        canvas.toBlob(
          (blob) => {
            this.blobToBase64(blob).then((base64String) => {
              base64Excel({ base64Img: base64String }).then(res => {
                console.log(res);
                if (res.result == '0') {
                  EWebBridge.webCallAppInJs('download', {
                    name: '业绩贺报.png',
                    url: res.path
                  })
                  .then(() => {
                    console.log('下载完成')
                    setTimeout(() => {
                      this.close();
                    }, 3000);
                  })
                  .catch(() => {
                    this.$toast.fail('图片下载失败，请重新下载！')
                  })
                }

              })

            })
          },
          'image/png'
          // quality
        )
      })
    },
    downLoadImagesFunc() {
      this.domToImage(this.$refs.xingdownLoad)
    },


    close() {
      console.log(15434678);
      document.getElementById('capture').innerHTML = '';
      this.$emit('child-colse', '1')
    },
  }
}
</script>

<style lang="scss" scoped>
.contents {
  width: 100%;
  height: 100vh;
  background-color: #fff;
  position: fixed;
  top: 0;
  left: 0;
}
img {
  height: 100%;
  width: 100%;
  position: relative;
}

.box {
  position: absolute;
  width: 80%;
  height: 40%;
  top: 38%;
  left: 10%;

  .title {
    position: absolute;
    width: 100%;
    // left: 30%;
    top: 24%;
    font-weight: bold;

    p {
      display: flex;
      justify-content: center;
    }

    .organ {
      font-size: 12px;
      color: #333333;
    }

    .name {
      font-size: 17px;
      color: #333333;
    }
  }

  .pName {
    position: absolute;
    top: 44%;
    font-size: 15px;
    color: #B1170C;
    width: 100%;
    display: flex;
    justify-content: center;
    padding-left: 5px;
  }

  .type {
    font-size: 22px;
    color: #B1170C;
    position: absolute;
    top: 53%;
    width: 100%;
    display: flex;
    justify-content: center;
  }

  .yuan {
    font-size: 30px;
    color: #B1170C;
    position: absolute;
    top: 67%;
    width: 100%;
    text-align: center;
    display: inline-block;
    vertical-align: bottom;

    span {
      font-size: 14px;
      margin-left: 5px;
    }
  }

  .time {
    font-size: 14px;
    color: #333333;
    position: absolute;
    top: 85%;
    width: 100%;
    display: flex;
    justify-content: center;
  }
}

.border-color {
  z-index: 9999;
  border: 2px solid;
  border-image: radial-gradient(circle, rgba(255, 233, 124, 1), rgba(222, 144, 34, 1)) 2 2;
}
.shareImg {
  border-radius: 12px;
  width: 35%;
  height: 50px;
  border: none !important;
}
.downLoad {
  border-radius: 12px;
  width: 35%;
  height: 49px;
  background: linear-gradient(to right, #f26e43, #ac0209) !important;
  /*设置按钮为渐变颜色*/
  color: #fff;
  border: none !important;
}
.close {
  position: fixed;
  top: 0;
  right: 0;
}
</style>