<template>
  <view class="page__view">
		<navbar title="报告对比" leftClick @leftClick="$utils.navigateBack" color="#191919" bgColor="#FFFFFF" />

    <view class="main">
      <reportCard :data="detail">
        <view class="report" v-if="list.length">
          <view class="flex section">
            <view 
              v-for="(item, index) in list" 
              :key="item.id"
              :class="['section-item-box', `section-item-box-${index == 0 ? 'left' : 'right'}`]"
            >
              <view class="section-item info-score">
                <view class="time">{{ item.createTime }}</view>
                <view class="flex score">
                  综合得分
                  <text class="score-value">{{ item.score }}</text>
                  <text class="score-unit">分</text>
                </view>
                <view class="score-detail">
                  <view class="score-detail-item" v-for="(scoreItem, sIdx) in item.scoreDetail" :key="sIdx">
                    <progressBar :progress="scoreItem.score" :total="100"></progressBar>
                    <view class="score-detail-item-score">{{ scoreItem.name }}<text class="highlight">{{ scoreItem.score }}</text></view>
                  </view>
                </view>
              </view>
            </view>
          </view>
          <view class="flex section">
            <view 
              v-for="(item, index) in list" 
              :key="item.id"
              :class="['section-item-box', `section-item-box-${index == 0 ? 'left' : 'right'}`]"
            >
              <view class="section-item info-BMI">
                <view class="flex label">
                  <text>BMI 指数</text>
                  <text class="highlight">{{ item.bmi || '-' }}</text>
                </view>
                <view class="progress" v-if="item.bmi">
                  <!-- todo: check -->
                  <progressBar :progress="item.bmi" :total="30"></progressBar>
                </view>
              </view>
            </view>
          </view>
          <view class="flex section"
            v-for="(step, sIdx) in list[0].children" :key="step.key"
          >
            <view 
              v-for="(report, rIdx) in list" 
              :key="rIdx"
              :class="['section-item-box', `section-item-box-${rIdx == 0 ? 'left' : 'right'}`]"
            >
              <view class="section-item">
                <view class="section-header">
                  <view class="section-header-zh">{{ `${sIdx + 1}.${step.name}` }}</view>
                  <view class="section-header-en">{{ step.enTitle }}</view>
                </view>
                <view class="section-content">
                  <!-- 普通标签 -->
                  <template v-if="step.type == '0'">
                    <view class="tags">
                      <view class="tag" v-for="(item, tIdx) in report.children[sIdx].schemes" :key="tIdx">{{ item.title }}</view>
                    </view>
                  </template>
                  <!-- 暗色标签 -->
                  <template v-else-if="step.type === '1'">
                    <view class="tags">
                      <view class="tag highlight" v-for="(item, tIdx) in report.children[sIdx].schemes" :key="tIdx">{{ item.title }}</view>
                    </view>
                  </template>
                  <!-- 有序文本 -->
                  <template v-else-if="step.type === '2'">
                    <!-- todo -->
                    <!-- <view class="flex section-content-tag">
                      <image class="section-content-tag-icon" :src="report.children[sIdx].tag.icon" mode="widthFix"></image>
                      <text class="section-content-tag-text">{{ report.children[sIdx].tag.text }}</text>
                    </view> -->
                    <view>
                      <view class="section-content-item" v-for="(item, idx) in report.children[sIdx].schemes" :key="idx">
                        <view class="flex section-content-item-header">
                          <view class="section-content-item-index">{{ `${idx + 1}.` }}</view>
                          <view class="section-content-item-title">{{ item.title }}</view>
                        </view>
                        <view class="section-content-item-detail" v-if="item.info">{{ item.info }}</view>
                      </view>
                    </view>
                  </template>
                  <!-- 无序文本 -->
                  <template v-else-if="step.type === '3'">
                    <view>
                      <view class="section-content-item" v-for="(item, idx) in report.children[sIdx].schemes" :key="idx">
                        <view class="flex section-content-item-header">
                          <view class="section-content-item-title">{{ item.title }}</view>
                        </view>
                        <view class="section-content-item-detail" v-if="item.info">{{ item.info }}</view>
                      </view>
                    </view>
                  </template>
                </view>
              </view>
            </view>
          </view>
        </view>
      </reportCard>
    </view>
  </view>
</template>

<script>
  import reportCard from '@/pages_order/components/reportCard.vue';
  import progressBar from '@/pages_order/components/progressBar.vue';

  export default {
    components: {
      reportCard,
      progressBar,
    },
    data() {
      return {
        detail: {},
        list: [],
        axis: ['饮食', '运动', '心理', '体质', '作息']
      }
    },
    onLoad(arg) {
      console.log('onLoad', arg)
      const ids = JSON.parse(arg.ids)
      this.getData(ids)
    },
    methods: {
      async fetchReportData(id) {
        console.log('fetchReportData', id)

        try {
          const result = await this.$fetch('getReportDetail', { id })

          const { 
            realName,
            age,
            sex,
            createTime,
            score,
            scoreDetail,
            json,
          } = result
          

          const obj = {
            id,
            realName,
            age,
            sex,
            createTime: this.$dayjs(createTime).format('YYYY-MM-DD'),
            score: parseInt(score),
            scoreDetail: JSON.parse(scoreDetail).map(item => ({ name: item.name, score: parseInt(item.score) })),
            children: JSON.parse(json)
          }

          return obj

        } catch (err) {
          console.log('getReportDetail err', err)
          return {}
        }

      },
      async getData(ids) {
        console.log('ids', ids)
        const results = await Promise.allSettled(ids.map(id => { return this.fetchReportData(id) }))
        console.log('results', results)

        this.list = results.map(item => item.value)
        this.detail = this.list[0]
        console.log('list', this.list)
      },
    },
  }
</script>

<style scoped lang="scss">
	.page__view {
		width: 100vw;
		min-height: 100vh;
		background-color: $uni-bg-color;
		position: relative;

    /deep/ .nav-bar__view {
      position: fixed;
      top: 0;
      left: 0;
    }
  }
  
  .main {
    padding: calc(var(--status-bar-height) + 152rpx) 32rpx 64rpx 32rpx;

    /deep/ .report-card__view {
      
      .card-content {
        padding-top: 232rpx;
        padding-left: 0;
        padding-right: 0;
      }
    }
  }

  .section {
    align-items: flex-start;
    width: 100%;

    & + & {
      .section-item {
        padding-top: 32rpx;
        border-top: 2rpx dashed #989898;
      }
    }

    &-item {
      padding-bottom: 32rpx;
      &-box {
        flex: 1;
        padding: 0 32rpx;

        &-left {
          padding-left: 24rpx;
        }
        &-right {
          padding-right: 24rpx;
          background: #FBFCFE;
        }
      }
    }
   
    &-header {
      font-family: PingFang SC;
      line-height: 1.4;
      color: #252545;

      &-zh {
        font-weight: 600;
        font-size: 32rpx;
      }

      &-en {
        font-weight: 400;
        font-size: 24rpx;
      }
    }

    &-content {
      padding-top: 18rpx;

      &-item {
        font-family: PingFang SC;
        font-weight: 400;
        line-height: 1.5;
        color: #252545;

        & + & {
          margin-top: 24rpx;
        }

        &-header {
          align-items: flex-start;
          justify-content: flex-start;
        }

        &-index {
          display: inline-block;
          width: 44rpx;
          font-size: 24rpx;
          text-align: right;
          line-height: 44rpx;
          padding-right: 10rpx;
          box-sizing: border-box;
        }
        &-title {
          font-size: 28rpx;
        }
        &-detail {
          margin-top: 8rpx;
          font-size: 24rpx;
          color: #989898;
        }
      }

      &-tag {
        margin-bottom: 24rpx;
        padding: 16rpx 0;
        background: #E5E4EB8C;
        border-radius: 16rpx;
        column-gap: 16rpx;

        &-icon {
          width: 40rpx;
          height: 40rpx;
        }

        &-text {
          font-family: PingFang SC;
          font-weight: 400;
          font-size: 28rpx;
          line-height: 1.5;
          color: #252545;
        }
      }
    }
  }
  
  .tags {
    display: flex;
    flex-wrap: wrap;
    gap: 16rpx;

    .tag {
      padding: 8rpx 40rpx;
      font-family: PingFang SC;
      font-weight: 400;
      font-size: 28rpx;
      line-height: 1.5;
      color: #252545;
      background: #E5E4EB;
      border-radius: 30rpx;

      &.highlight {
        font-weight: 600;
        color: #FFFFFF;
        background: #252545;
      }
    }
  }

  .info {
    &-score {
      padding: 40rpx 0;

      .time {
        display: inline-flex;
        width: auto;
        padding: 8rpx 16rpx;
        font-family: PingFang SC;
        font-weight: 400;
        font-size: 28rpx;
        line-height: 1.5;
        color: #252545;
        border: 2rpx solid #252545;
        border-radius: 30rpx;
      }

      .score {
        margin-top: 4rpx;
        justify-content: flex-start;
        column-gap: 8rpx;
        font-family: PingFang SC;
        font-weight: 400;
        font-size: 36rpx;
        line-height: 1.2;
        color: #252545CC;

        &-value {
          font-weight: 600;
          font-size: 64rpx;
          line-height: 1.4;
          color: transparent;
          background-image: linear-gradient(to right, #4B348F, #845CFA);
          background-clip: text;
          display: inline-block;
        }

        &-unit {
          font-weight: 600;
          font-size: 40rpx;
          line-height: 1.4;
          color: #252545;
        }
      }

      .score-detail {
        margin-top: 20rpx;

        &-item {
          padding-top: 10rpx;

          &-score {
            padding: 10rpx 0 0 16rpx;
            font-family: PingFang SC;
            font-weight: 400;
            font-size: 24rpx;
            line-height: 1;
            color: #8B8B8B;

            .highlight {
              margin-left: 12rpx;
              font-weight: 500;
              font-size: 28rpx;
              line-height: 1.4;
              color: #000000;
            }
          }
        }
      }
    }

    &-BMI {
      .label {
        white-space: nowrap;
        font-family: PingFang SC;
        font-weight: 400;
        font-size: 36rpx;
        line-height: 1.2;
        color: #252545CC;

        .highlight {
          margin-left: 8rpx;
          font-size: 64rpx;
          font-weight: 600;
          line-height: 1.4;
          font-family: PingFang SC;
          color: transparent;
          background-image: linear-gradient(to right, #4B348F, #845CFA);
          background-clip: text;
          display: inline-block;
        }
      }

      .progress {
        margin: 18rpx 0 34rpx 0;
        width: 100%;
      }
    }
  }

</style>