<template>
  <view class="page__view">
		<navbar title="报告" leftClick @leftClick="$utils.navigateBack" />

    <template v-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>
        <image class="qr" :src="configList.company_qrcode" :show-menu-by-longpress="true" mode="widthFix"></image>
      </view>
      
      <view>
        <image class="img" :src="configList.company_info" mode="widthFix"></image>
        <view class="logo">
          <image class="img" :src="configList.company_logo" mode="widthFix"></image>
        </view>
      </view>
    </template>

    <view class="bottom">
      <button class="btn" @click="onDownload">下载</button>
    </view>

  </view>
</template>

<script>
  import progressCircle from './progressCircle.vue';
  import reportTableView from './reportTableView.vue';

  export default {
    components: {
      progressCircle,
      reportTableView,
    },
    data() {
      return {
        batchNo: null,
        detail: null,
        tableList: [],
      }
    },
    onLoad(arg) {
      const { batchNo } = arg
      this.batchNo = batchNo
      this.getData(batchNo)
    },
    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) {

        }
      },
      onDownload() {
				uni.navigateTo({
					url: `/pages_order/report/export?batchNo=${this.batchNo}`
				})
      },
    },
  }
</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>