<script lang="ts" setup>
import carbonAnalyse from './carbonAnalyse.vue'
import carbonElement from './carbonElement.vue'
import carbonGather from './carbonGather.vue'
const $router = useRouter()
const carbonData = ref([])
onMounted(() => {
  carbonData.value = JSON.parse(sessionStorage.getItem('计算结果') as string) || []
})
// 总排放量
const totalCarbon = computed(() => {
  return carbonData.value.reduce((pre, target: any) => {
    return pre + (Number(target.co) + Number(target.no) + Number(target.ch))
  }, 0).toFixed(2)
})
// co2 排放量
const coCarbon = computed(() => {
  return carbonData.value.reduce((pre, target: any) => {
    return pre + Number(target.co)
  }, 0).toFixed(2)
})
//  ch4排放
const chCarbon = computed(() => {
  return carbonData.value.reduce((pre, target: any) => {
    return pre + Number(target.ch)
  }, 0).toFixed(2)
})
//  n2o排放
const noCarbon = computed(() => {
  return carbonData.value.reduce((pre, target: any) => {
    return pre + Number(target.no)
  }, 0).toFixed(2)
})
// 碳排分析
const carbonAnalyseData = computed(() => {
  return carbonData.value.map((item: any) => {
    return {
      name: item.name,
      value: Number(item.co) + Number(item.no) + Number(item.ch),
    }
  })
})
// 碳排元素分析
const carbonElementData = computed(() => {
  const arr = []
  // co2
  const co2 = carbonData.value.reduce((pre, target: any) => {
    return pre + Number(target.co)
  }, 0).toFixed(2)
  arr.push({ name: 'CO₂', value: co2 })
  // CH₄
  const ch4 = carbonData.value.reduce((pre, target: any) => {
    return pre + Number(target.ch)
  }, 0).toFixed(2)
  arr.push({ name: 'CH₄', value: ch4 })
  // N₂O
  const no2 = carbonData.value.reduce((pre, target: any) => {
    return pre + Number(target.no)
  }, 0).toFixed(2)
  arr.push({ name: 'N₂O', value: no2 })
})
</script>

<template>
  <div class="container">
    <!-- <div class="bg" /> -->
    <div class="content">
      <!-- 碳排量 -->
      <div class="card-box u-f-ajsb">
        <!-- 总co2排放 -->
        <div class="card backgroundImg">
          <div class="title" style="color: rgb(255 255 255);">
            总CO₂e排放
          </div>
          <div class="content1" style="color: rgb(255 255 255);">
            <span class="count">{{ totalCarbon }}</span>
            <span class="unit">万吨</span>
          </div>
          <div class="line" />
          <div class="data" style="color: rgb(255 255 255);">
            <span>低于同行</span>
            <span>{{ 0 }}%</span>
          </div>
          <div />
          <img src="../../../assets/images/co.png" class="tu-1">
        </div>
        <!-- 分别排放 -->
        <div class="card part" style="color: rgb(0 0 0);">
          <div class="title">
            CO₂排放
          </div>
          <div class="content1">
            <span class="count">{{ coCarbon }}</span>
            <span class="unit">万吨</span>
          </div>
          <div class="line" style="background-color: #41a877;" />
          <div class="data">
            <span>低于同行</span>
            <span>{{ 0 }}%</span>
          </div>
          <!-- <img src="../../../assets/images/co.png" class="tu-1"> -->
          <div class="tu-1 logo logo-co" style="color: rgb(255 255 255);">
            CO₂
          </div>
        </div>
        <div class="card part" style="color: rgb(0 0 0);">
          <div class="title">
            CH₄排放
          </div>
          <div class="content1">
            <span class="count">{{ chCarbon }}</span>
            <span class="unit">万吨</span>
          </div>
          <div class="line" style="background-color: #41a877;" />
          <div class="data">
            <span>低于同行</span>
            <span>{{ 0 }}%</span>
          </div>
          <div />
          <!-- <img src="../../../assets/images/co.png" class="tu-1"> -->
          <div class="tu-1 logo logo-ch" style="color: rgb(255 255 255);">
            CH₄
          </div>
        </div>
        <div class="card part" style="color: rgb(0 0 0);">
          <div class="title">
            N₂O排放
          </div>
          <div class="content1">
            <span class="count">{{ noCarbon }}</span>
            <span class="unit">万吨</span>
          </div>
          <div class="line" style="background-color: #41a877;" />
          <div class="data">
            <span>低于同行</span>
            <span>{{ 0 }}%</span>
          </div>
          <div />
          <!-- <img src="../../../assets/images/co.png" class="tu-1"> -->
          <div class="tu-1 logo logo-no" style="color: rgb(255 255 255);">
            N₂O
          </div>
        </div>
      </div>
      <!-- 碳排结果分析标题 -->
      <div class="carbon-title" style="color: #000;">
        <!-- 碳排放结果分析 -->
      </div>
      <!-- echarts -->
      <div class="echarts">
        <!-- 碳排分析饼图 -->
        <carbon-analyse :data="carbonAnalyseData" />
        <!-- 碳排元素分析 -->
        <carbon-element :data="carbonElementData" />
        <!-- 碳排汇总 -->
        <carbon-gather />
      </div>
    </div>
    <div class="bak-btn">
      <el-button type="primary" size="small" class="bak" @click="$router.go(-1)">
        返回
      </el-button>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.bg {
  height: 160px;
  background-image: url("../../../assets/images/result-bg.png");
}

// .container {
//   position: relative;
// }

// .bak {
//   position: absolute;
//   top: 30px;
//   left: 10%;
// }
.bak-btn {
  position: fixed;
  right: 7px;
  top: 52px;
  z-index: 99;
}

.content {
  // width: 80%;
  background: #fff;
  margin: 0 auto;
  min-height: 100vh;
  // margin-top: -85px;
  border-radius: 8px;

  // background-image: url("../../../assets/images/result-bg.png") no-repeat;
  .echarts {
    display: flex;
    justify-content: space-around;
    height: 25rem;
  }

  .carbon-title {
    // width: 60%;
    margin: 0 auto;
    background: url("../../../assets/images/carbon-result-bg-1.png") no-repeat;
    height: 115px;
  }

  .part {
    border: 2px solid #41a877 !important;
    width: 24% !important;
    border-radius: 6px;
    height: 8rem !important;
    // background-color: #ccc;
    padding: 1.5rem 3rem 4rem 2.5rem !important;

    // padding: 0 !important;
    .logo {
      top: 1rem !important;
      border-radius: 4px;
    }

    .logo-co {
      background-color: #41a877;
    }

    .logo-ch {
      background-color: #6686f3;
    }

    .logo-no {
      background-color: #bc86ff;
    }
  }

  .card-part {
    width: 70%;

    .item {
      border: 3px solid #41a877;
      border-radius: 6px;
    }
  }

  .data {
    padding: 0 15px;
    display: flex;
    justify-content: space-between;
  }

  .u-f-ajsb {
    display: flex;
    align-items: center;
    justify-content: space-around;
  }

  .card {
    overflow: hidden;
    position: relative;
    padding: 4rem 3rem 4rem 2.5rem;
    width: 25%;
    height: 12rem;
    border: 1px solid #fff;
    border-radius: 6px;

    .title {
      font-size: 16px;
      font-family: "Source Han Sans SC";
      font-weight: 400;
      margin-bottom: 0.5rem;
    }

    .tu-1 {
      position: absolute;
      right: 2.5rem;
      top: 2.5rem;
      width: 45px;
      height: 45px;
      text-align: center;
      line-height: 45px;
    }

    .line {
      width: 96%;
      height: 3px;
      background-color: #fff;
      margin: 0 auto;
      margin-top: 10px;
      border-radius: 4px;
    }

    .content1 {
      margin-top: 10px;

      .count {
        font-size: 20px;
        font-weight: 700;
      }
    }
  }

  .backgroundImg {
    background: url("../../../assets/images/result1.png") no-repeat;
    background-size: 100% 100%;
  }
}
</style>
