<template>
  <transition
    appear
    name="animate__animated"
    enter-active-class="animate__slideInRight"
    leave-active-class="animate__slideOutRight"
  >
    <div class="widget-ProjectInfo">
      <div class="head">
        <div class="title">
          <div class="icon"></div>
          <span class="site-info">项目情况</span>
          <!-- <div class="tab-list">
            <div class="tab-item" :class="{ 'tab-active': currentActive == 'num' }" @click="currentActive = 'num'">
              按数量统计
            </div>
            <div class="tab-item" :class="{ 'tab-active': currentActive == 'money' }" @click="currentActive = 'money'">
              按金额统计
            </div>
          </div> -->
        </div>
      </div>
      <div class="content-info">
        <div class="content-item">
          <div class="title">
            <div class="icon"></div>
            <span class="item-name">项目状态统计</span>
          </div>
          <div class="content statusContent">
            <div class="statusItem" v-for="item of statusList" :key="item.title">
              <div class="chart">
                <ComAnimateChart :img="item.img" :fontSize="fontSize" />
              </div>
              <div class="statusInfo">
                <div class="value">{{ item.value + item.unit }}</div>
                <div class="name">{{ item.title }}</div>
              </div>
            </div>
          </div>
        </div>
        <div class="content-item">
          <div class="title">
            <div class="icon"></div>
            <span class="item-name">分公司项目统计</span>
          </div>
          <div class="content">
            <ComStatisticPieChart v-on="{ fontSize }" :chartData="branchData" />
          </div>
        </div>
        <div class="content-item">
          <div class="title">
            <div class="icon"></div>
            <span class="item-name">项目投资排行榜</span>
          </div>
          <div class="content scrollContent" ref="scrollContent">
            <ComBarChart v-on="{ fontSize }" :chartData="projectData" />
          </div>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
import { getRequestResult, getEpcProjectList } from '@/views/groupPage/apis'
import { setNullAndUndefinedEmpty, fontSize as fs } from '@/views/groupPage/util'
import ComAnimateChart from '@/views/groupPage/components/OthersChart/ComAnimateChart.vue'
import ComStatisticPieChart from '@/views/groupPage/components/PieChart/ComStatisticPieChart.vue'
import ComBarChart from '@/views/groupPage/components/BarChart/ComBarChart.vue'
export default {
  name: 'ProjectInfo', //项目情况
  components: {
    ComAnimateChart,
    ComStatisticPieChart,
    ComBarChart
  },
  props: {
    dataInfo: { type: Object, default: () => {} }
  },
  data() {
    return {
      statusList: [
        { title: '前期项目', value: 0, unit: '个', img: require('@/views/groupPage/images/模块图标/前期项目.png') },
        { title: '建设项目', value: 0, unit: '个', img: require('@/views/groupPage/images/模块图标/建设项目.png') },
        { title: '暂停项目', value: 0, unit: '个', img: require('@/views/groupPage/images/模块图标/暂停项目.png') },
        { title: '移交项目', value: 0, unit: '个', img: require('@/views/groupPage/images/模块图标/移交项目.png') }
      ],
      currentActive: 'num',
      branchData: [],
      projectData: {
        province: [],
        projectName: [],
        investment: []
      }
    }
  },
  watch: {
    dataInfo: {
      handler(n, o) {
        Object.assign(this.$data, this.$options.data())
        if (n) this.setData()
      },
      immediate: true
    }
  },
  computed: {
    setNoNull() {
      return setNullAndUndefinedEmpty
    },
    fontSize() {
      return fs
    },
    code() {
      return this.$store.state.bigScreen.groupCodeValue
    },
    groupName() {
      return this.$store.state.bigScreen.groupName
    }
  },
  mounted() {
    this.tableScroll()
    // this.getPageData()
  },
  methods: {
    tableScroll() {
      const that = this
      const area = this.$refs.scrollContent
      // 拿到元素后，对元素进行定时增加距离顶部距离，实现滚动效果(此配置为每100毫秒移动1像素)
      area.scrollTop = 0
      function scrollUp() {
        if (area.scrollTop >= area.scrollHeight - area.offsetHeight) {
          area.scrollTop = 0
        } else {
          const ratio = parseFloat((1 / window.devicePixelRatio).toFixed(2)) + 0.01 // + 0.01 为了保证 ratio >= 1
          area.scrollTop = area.scrollTop + ratio
        }
      }
      if (!this.hasScroll) {
        this.hasScroll = true
        let time = 50
        this.setAnimationFrame(scrollUp, time)
        area.onmouseover = function () {
          cancelAnimationFrame(that.timerId)
        }
        area.onmouseout = function () {
          that.setAnimationFrame(scrollUp, time)
        }
      }
    },
    //使用requestAnimationFrame重写setInterval，进行性能优化
    setAnimationFrame(render, time) {
      //当前执行时间
      var nowTime = 0
      //记录每次动画执行结束的时间
      var lastTime = Date.now()
      //我们自己定义的动画时间差值
      var diffTime = time
      //当前requestAnimationFrame的id
      //此处使用对象，对象存储在地址空间，函数内部更新了对象的值，函数外部也可以接收到
      var that = this
      function animloop() {
        //记录当前时间
        nowTime = Date.now()
        if (nowTime - lastTime > diffTime) {
          lastTime = nowTime
          render()
        }
        //timerid为数字
        that.timerId = requestAnimationFrame(animloop)
      }
      animloop()
    },
    roundFun(value, n) {
      return Math.round(value * Math.pow(10, n)) / Math.pow(10, n)
    },
    // getPageData() {
    //   let data = { blockCode: 'ycepclist' }
    //   //获取全部数据
    //   getRequestResult(data)
    //     .then((res) => {
    //       res = res.map((item) => {
    //         return {
    //           ...item,
    //           indexValue:
    //             item.unit === '万元'
    //               ? this.roundFun(this.setNoNull(item.indexValue) / 10000, 2)
    //               : this.setNoNull(item.indexValue)
    //         }
    //       })
    //       this.statusList[0].value = res.filter((item) => item.indexCode === 'A33')[0].indexValue
    //       this.statusList[1].value = res.filter((item) => item.indexCode === 'A34')[0].indexValue
    //       this.statusList[2].value = res.filter((item) => item.indexCode === 'A35')[0].indexValue
    //       this.statusList[3].value = res.filter((item) => item.indexCode === 'A36')[0].indexValue
    //       //
    //       let codeArr = ['A37', 'A38', 'A39']
    //       res.map((item) => {
    //         if (codeArr.includes(item.indexCode)) {
    //           this.branchData.unshift({ name: item.indexName, value: item.indexValue })
    //         }
    //       })
    //     })
    //     .then(() => {
    //       let data = { blockCode: 'prjInfo' }
    //       //获取全部数据
    //       getRequestResult(data).then((res) => {
    //         res.sort((a, b) => {
    //           return a.planInvestment - b.planInvestment
    //         }) //升序
    //         res.forEach((item) => {
    //           this.projectData.province.push(item.province.substring(0, 2))
    //           this.projectData.projectName.push(item.prjName)
    //           this.projectData.investment.push(this.roundFun(this.setNoNull(item.planInvestment), 2))
    //         })
    //       })
    //     })
    // },
    // getBranchPageData() {
    //   let data = { blockCode: 'ycepclist', 'paras[0].name': 'projectName', 'paras[0].val': this.code }
    //   //获取全部数据
    //   getRequestResult(data)
    //     .then((res) => {
    //       res = res.map((item) => {
    //         return {
    //           ...item,
    //           indexValue:
    //             item.unit === '万元'
    //               ? this.roundFun(this.setNoNull(item.indexValue) / 10000, 2)
    //               : this.setNoNull(item.indexValue)
    //         }
    //       })
    //       this.statusList[0].value = res.filter((item) => item.indexCode === this.code + '-24')[0].indexValue
    //       this.statusList[1].value = res.filter((item) => item.indexCode === this.code + '-25')[0].indexValue
    //       this.statusList[2].value = res.filter((item) => item.indexCode === this.code + '-26')[0].indexValue
    //       this.statusList[3].value = res.filter((item) => item.indexCode === this.code + '-27')[0].indexValue
    //       //
    //       let codeArr = [this.code + '-28', this.code + '-29', this.code + '-30', this.code + '-31']
    //       res.map((item) => {
    //         if (codeArr.includes(item.indexCode)) {
    //           this.branchData.unshift({ name: item.indexName, value: item.indexValue })
    //         }
    //       })
    //     })
    //     .then(() => {
    //       let data = { blockCode: 'prjInfo' }
    //       //获取全部数据
    //       getRequestResult(data).then((res) => {
    //         // if (this.groupName === '三峡分公司') res = res.filter((item) => item.branch == this.groupName)
    //         if (this.groupName === this.code) res = res.filter((item) => item.branch == this.groupName)
    //         res.sort((a, b) => {
    //           return a.planInvestment - b.planInvestment
    //         }) //升序
    //         res.forEach((item) => {
    //           this.projectData.province.push(item.province ? item.province.substring(0, 2) : '暂无')
    //           this.projectData.projectName.push(item.prjName)
    //           this.projectData.investment.push(this.roundFun(this.setNoNull(item.planInvestment), 2))
    //         })
    //       })
    //     })
    // },

    //new
    async setData() {
      const { previousProject, buildProject, pauseProject, turnOverProject, fgsCount } = this.dataInfo || {}
      const prjArr = [previousProject, buildProject, pauseProject, turnOverProject]
      this.statusList = this.statusList.map((item, index) => {
        return { ...item, value: prjArr[index] }
      })
      //分公司项目统计
      fgsCount.forEach((item) => {
        this.branchData.unshift({ name: item.name, value: item.value })
      })
      //获取项目数据
      let res = await getEpcProjectList()
      res = res.result
      if (this.groupName !== '三峡发展') res = res.filter((item) => item.son_company_name == this.groupName)
      res = res.map((i) => {
        return { ...i, planInvestment: i.contract_ppp_money / Math.pow(10, 8) }
      })
      res.sort((a, b) => {
        return a.planInvestment - b.planInvestment
      }) //升序
      res.forEach((item) => {
        this.projectData.province.push(item.country ? item.country.substring(0, 2) : '暂无')
        this.projectData.projectName.push(item.name)
        this.projectData.investment.push(this.roundFun(this.setNoNull(item.planInvestment), 2))
      })
      // let res = await getRequestResult({ blockCode: 'prjInfo' })
      // // if (this.groupName === this.code) res = res.filter((item) => item.branch == this.groupName)
      // if (this.groupName !== '三峡发展') res = res.filter((item) => item.branch == this.groupName)
      // res = res.map((i) => {
      //   return { ...i, planInvestment: i.planInvestment / Math.pow(10, 8) }
      // })
      // res.sort((a, b) => {
      //   return a.planInvestment - b.planInvestment
      // }) //升序
      // res.forEach((item) => {
      //   this.projectData.province.push(item.province ? item.province.substring(0, 2) : '暂无')
      //   this.projectData.projectName.push(item.prjName)
      //   this.projectData.investment.push(this.roundFun(this.setNoNull(item.planInvestment), 2))
      // })
    }
  }
}
</script>

<style lang='scss' scoped>
.animate__slideInRight,
.animate__slideOutRight {
  animation-duration: 3s; //动画持续时间
  animation-delay: 0s; //动画延迟时间
}
.widget-ProjectInfo {
  $size10: 0.052083rem /* 10/192 */;
  $size20: 0.104167rem /* 20/192 */;
  z-index: 2;
  //position
  bottom: $size10 /* 10/192 */;
  margin-right: $size20 /* 20/192 */;
  right: 0;
  position: absolute;
  //size
  height: calc(100% - 0.557292rem /* 107/192 */);
  width: 2.083333rem /* 400/192 */;
  //background
  // background-color: rgba(20, 24, 47, 0.5);
  color: #eee;
  // box-shadow: 0 0 20px rgba(1, 9, 20, 1);
  //font
  font-family: Source Han Sans CN-HEAVY;
  .head {
    height: 0.166667rem /* 32/192 */;
    width: 100%;
    background: linear-gradient(-90deg, rgba(43, 167, 255, 0.2) 0%, rgba(43, 167, 255, 0.08) 100%);
    .title {
      width: 100%;
      height: 100%;
      display: flex;
      font-weight: 400;
      .icon {
        height: 0.166667rem /* 32/192 */;
        width: 0.34375rem /* 66/192 */;
        background: url('~@/views/groupPage/images/模块图标/项目情况.png') no-repeat center center;
        background-size: 100% 100%;
      }
      span {
        font-weight: bold;
        font-size: 0.083333rem /* 16/192 */;
        color: #ffffff;
        padding: 0.041667rem /* 8/192 */;
        background: linear-gradient(0deg, #9bd2fa 0%, #ffffff 100%);
        background-clip: text;
        -webkit-text-fill-color: transparent;
        flex: 0.5;
      }
      .tab-list {
        display: flex;
        align-items: center;
        justify-content: space-around;
        width: 0.78125rem /* 150/192 */;
        flex: 0.5;
        font-family: Source Han Sans CN;
        font-size: 0.072917rem /* 14/192 */;
        font-weight: 400;
        .tab-item {
          cursor: pointer;
          height: 100%;
          display: flex;
          align-items: center;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
        .tab-active {
          color: #119cff;
          border-bottom: 3px solid;
          border-image: linear-gradient(0deg, rgba(17, 156, 255, 0.9) 0%, rgba(17, 156, 255, 0.02) 3%) 1;
        }
      }
    }
  }
  .content-info {
    width: 100%;
    height: calc(100% - 0.166667rem);
    overflow: auto;
    .content-item {
      font-family: Source Han Sans CN-MEDIUM;
      width: 100%;
      height: 33%;
      float: left;
      overflow: hidden;
      .title {
        width: 100%;
        display: flex;
        // padding: .145833rem 0 .104167rem 0;
        padding: 0.145833rem 0 0.026042rem /* 5/192 */ 0;
        .icon {
          height: 0.072917rem /* 14/192 */;
          width: 0.078125rem /* 15/192 */;
          margin-right: 0.046875rem /* 9/192 */;
          background: url('~@/views/groupPage/images/三角.png') no-repeat center center;
          background-size: 100% 100%;
        }
        .item-name {
          color: #ffffff;
          font-size: 0.072917rem /* 14/192 */;
          font-weight: 500;
        }
        .item-info {
          color: #2ba7ff;
          font-size: 0.072917rem /* 14/192 */;
          font-weight: 500;
        }
      }
      .content {
        height: calc(100% - 0.234375rem /* 45/192 */);
        width: 100%;
      }
      .scrollContent {
        overflow: hidden;
      }
      .statusContent {
        display: grid;
        grid-template-columns: 1fr 1fr; //有几列
        grid-template-rows: 50% 50%; //有几行
        grid-gap: 0.104167rem /* 20/192 */;
        padding: 0.15625rem /* 30/192 */ 0.052083rem /* 10/192 */;
        .statusItem {
          width: 100%;
          height: 100%;
          display: flex;
          align-items: center;
          .chart {
            flex: 0.3;
            height: 100%;
          }
          .statusInfo {
            height: 80%;
            flex: 0.7;
            display: flex;
            flex-flow: column;
            align-items: center;
            justify-content: center;
            margin-left: -15%;
            background-image: radial-gradient(circle at -10% 50%, transparent 30%, rgba(43, 167, 255, 0.16) 21px);
            .value,
            .name {
              height: 50%;
              width: 100%;
              display: flex;
              align-items: center;
              justify-content: center;
              font-family: Source Han Sans CN;
            }
            .value {
              font-size: 0.083333rem /* 16/192 */;
              font-weight: bold;
              color: #ffffff;
              text-shadow: 0px 3px 8px rgba(3, 15, 32, 0.6);
            }
            .name {
              font-size: 0.072917rem /* 14/192 */;
              font-weight: 400;
              color: #2ba7ff;
            }
          }
        }
      }
    }
  }
}
</style>