<template>
  <view ref="report" class="page__view export">
		<!-- <navbar title="报告" leftClick @leftClick="$utils.navigateBack" /> -->

    <template v-if="tempFilePath">
      <img 
        :src="tempFilePath" 
        @contextmenu.prevent
        style="display: block; width: 100vw; height: auto;"
      />
    </template>
    <template v-else-if="detail">
      <view class="title">{{ detail.title }}</view>

      <view class="section">
        <view class="flex section-header">
          <view class="line"></view>
          <view>总概述</view>
        </view>
        <view class="section-content">
          <view class="summary">
            <view class="text">
              经过本次问答信息收集的综合分析，检测出风险点共<text class="highlight">{{ detail.levelAllNum - detail.level3Num }}</text>项，其中高风险{{ detail.level2Num }}项，中风险{{ detail.level1Num }}项，低风险{{ detail.level0Num }}项
            </view>
            <view class="flex charts">
              <progressCircle label="高风险" :value="detail.level2Num" color="#FF0000"></progressCircle>
              <progressCircle label="中风险" :value="detail.level1Num" color="#FFA800"></progressCircle>
              <progressCircle label="低风险" :value="detail.level0Num" color="#014FA2"></progressCircle>
              <progressCircle label="合规" :value="detail.level3Num" color="#5AC725"></progressCircle>
            </view>
          </view>
          <view class="table">
            <!-- 全部合规 -->
            <template v-if="detail.level3Num === detail.levelAllNum">
              <image class="img-succ" :src="configList.compliance_img" mode="widthFix"></image>
            </template>
            <template v-else>
              <reportTableView :list="tableList"></reportTableView>
            </template>
          </view>
        </view>
      </view>

      <view class="flex flex-column contact">
        <view>扫下方二维码联系我们给你1V1解决方案</view>
        <img class="qr" style="display: block;" :src="configList.company_qrcode" crossorigin=“anonymous />
      </view>
      
      <view>
        <img class="img" style="display: block;" :src="configList.company_info" crossorigin=“anonymous />
        <view class="logo">
          <img class="img" style="display: block;" :src="configList.company_logo" crossorigin=“anonymous />
        </view>
      </view>
    </template>
    

    <!-- <view class="bottom" data-html2canvas-ignore="true">
      <button class="btn" @click="onDownload">长按图片保存到手机</button>
    </view> -->

  </view>
</template>

<script>
  import html2canvas from 'html2canvas'

  import progressCircle from './progressCircle.vue';
  import reportTableView from './reportTableView.vue';

  export default {
    components: {
      progressCircle,
      reportTableView,
    },
    data() {
      return {
        detail: null,
        tableList: [],
        canvas: null,
        tempFilePath: null
      }
    },
    async onLoad(arg) {
			uni.showLoading({
				title: '生成中...'
			})
      
      document.addEventListener('UniAppJSBridgeReady', function() {
        console.log('UniAppJSBridgeReady', uni)
        uni.getEnv(function(res) {
            console.log('当前环境：' + JSON.stringify(res));
        });
      })
      
      const { batchNo, token } = arg
      token && uni.setStorageSync('token', token)
      
      await this.getData(batchNo)

      setTimeout(async () => {
        await this.createImage()
        uni.hideLoading()
        
        // H5环境下提示用户长按保存
        uni.showModal({
          title: '保存报告',
          content: '请长按图片，选择"保存图片"即可保存到手机',
          showCancel: false,
          confirmText: '知道了'
        })
      }, 800)
    },
    methods: {
      async getData(batchNo) {

        try {
          const result = await this.$fetch('queryReportById', { batchNo })
          const {
            title,
            level0Num, // 低风险
            level1Num, // 中风险
            level2Num, // 高风险
            level3Num, // 合规
            levelAllNum,
            pageList,
          } = result

          this.tableList = pageList.reduce((arr, item) => {
            const { id, risk, reason, level, consequence } = item

            if (level == '3') {
              return arr
            }

            const obj = {
              id,
              reason,
              level,
              result: consequence,
            }

            const index = arr.findIndex(row => row.title === risk)

            if (index === -1) {
              arr.push({
                id: arr.length,
                title: risk,
                children: [obj]
              })
            } else {
              arr[index].children.push(obj)
            }

            return arr
          }, [])

          this.detail = {
            title,
            level0Num, // 低风险
            level1Num, // 中风险
            level2Num, // 高风险
            level3Num, // 合规
            levelAllNum,
          }
        } catch (err) {

        }
      },
      async createImage() {
        console.log('createImage')
        const node = document.querySelector('.export')
        const canvas = await html2canvas(node, { allowTaint: true, useCORS: true, })
        const image = canvas.toDataURL('image/png')
        this.tempFilePath = image

        uni?.postMessage?.({
          data: {
            imageData: image,
          }
        });
        
      },
      onDownload() {
				if (!this.tempFilePath) {
					uni.showToast({
						title: '图片还未生成完成',
						icon: 'none'
					})
					return
				}

				// H5环境下提示用户长按保存
				uni.showModal({
					title: '保存图片',
					content: '请长按上方图片，选择"保存图片"即可保存到手机',
					showCancel: false,
					confirmText: '知道了'
				})
      },
    },
  }
</script>

<style scoped lang="scss">
  .title {
    width: 100%;
    padding: 42rpx 0 40rpx 0;
    box-sizing: border-box;
    text-align: center;
    font-size: 30rpx;
    font-weight: 600;
    color: #000000;
  }

  .section {
    padding: 0 12rpx;

    &-header {
      justify-content: flex-start;
      column-gap: 9rpx;
      padding: 0 17rpx;
      font-size: 28rpx;
      font-weight: 600;
      color: #014FA2;

      .line {
        width: 9rpx;
        height: 33rpx;
        background: #014FA2;
        border-radius: 6rpx;
      }
    }

    &-content {
      margin-top: 30rpx;
    }
  }

  .summary {
    .text {
      padding: 20rpx 10rpx;
      font-size: 26rpx;
      color: #014FA2;
      background: rgba($color: #014FA2, $alpha: 0.16);

      .highlight {
        color: #DB5742;
      }
    }

    .charts {
      margin-top: 54rpx;
      justify-content: space-between;
      padding: 0 42rpx;
    }
  }

  .table {
    margin-top: 49rpx;
  }

  .img-succ {
    width: 100%;
    height: auto;
  }
  
  .contact {
    margin-top: 53rpx;
    row-gap: 40rpx;
    width: 100%;
    padding: 30rpx 0 22rpx 0;
    box-sizing: border-box;
    font-size: 28rpx;
    color: #014FA2;
    border: 1rpx dashed #014FA2;

    .qr {
      width: 157rpx;
      height: auto;
    }
  }

  .img {
    width: 100%;
    height: auto;
  }

  .logo {
    width: 100%;
    padding: 42rpx 127rpx 46rpx 127rpx;
    box-sizing: border-box;
  }

  
  .bottom {
    position: sticky;
    left: 0;
    bottom: 0;
    width: 100%;
    padding: 35rpx 56rpx;
    padding-bottom: calc(env(safe-area-inset-bottom) + 35rpx);
    background: #FFFFFF;
    box-sizing: border-box;

    .btn {
      width: 100%;
      padding: 29rpx 0;
      font-size: 30rpx;
      line-height: 1.5;
      color: #FFFFFF;
      background: #014FA2;
      border-radius: 50rpx;
    }
  }
</style>