<template>
  <div class="app-container clue-container">
    <div class="common-title">商机线索</div>
    <el-tabs v-model="activeIndex" @tab-click="handleQuery" class="tabpane selfTab">
      <el-tab-pane label="按金额" name="0"></el-tab-pane>
      <el-tab-pane label="按项目" name="1"></el-tab-pane>
    </el-tabs>
    <div class="flex-box clue-box">
      <div class="clue-echarts" v-if="viewData.length>0"><div id="echartsClue" style="width: 100%;height:300px; margin: 0 auto;"></div></div>
      <div class="busc-no-data" v-else>
        <no-data />
      </div>
      <div class="table-item">
        <el-table
          :data="viewData"
          border
          style="width: 100%"
          :default-sort = "{prop: 'date', order: 'descending'}"
        >
          <el-table-column
            prop="name"
            label="名称"></el-table-column>
          <el-table-column
            prop="value"
            align="right"
            label="数量">
          </el-table-column>
          <el-table-column
            prop="percent"
            align="right"
            label="占比（%）">
          </el-table-column>
          <template slot="empty">
            <div style="padding: 30px 0">
              <no-data />
            </div>
          </template>
        </el-table>
      </div>
    </div>
    <div class="flex-box clue-type">
      <div class="flex-box clue-type-item" :class="!item.count?'disab':''" v-for="(item, index) in typeList" :key="index" @click="handleType(item, index)"><img :src="!item.count?item.dis_ico:item.ico">{{item.name}}</div>
    </div>
  </div>
</template>

<script>
import {projectTenderDataGroup} from '@/api/detail/party-a/overview'
import * as echarts from 'echarts'
import {changePath} from "@/assets/js/common"
import NoData from '../../component/noData'
export default {
  name: 'Busclue',
  props: ['companyId', 'statistic'],
  components: {
    NoData
  },
  data() {
    return {
      viewData:[],
      activeIndex:0,
      typeList:[
        {name: '土地交易', pathName: 'landtransaction', ico: require("@/assets/images/detail/overview/clue_ico1.png"), count: 0, category:'global', field:'landInfo', dis_ico: require("@/assets/images/detail/overview/clue_dis_ico1.png")},
        {name: '拟建项目', pathName: 'proposed', ico: require("@/assets/images/detail/overview/clue_ico2.png"), count: 0, category:'global', field:'proposedProject', dis_ico: require("@/assets/images/detail/overview/clue_dis_ico2.png")},
        {name: '专项债项目', pathName: 'bond', ico: require("@//assets/images/detail/overview/clue_ico3.png"), count: 0, category:'performance', field:'specialDebt', dis_ico: require("@/assets/images/detail/overview/clue_dis_ico3.png")},
        {name: '招标计划', pathName: 'biddingplan', ico: require("@/assets/images/detail/overview/clue_ico4.png"), count: 0, category:'performance', field:'bidPlan', dis_ico: require("@/assets/images/detail/overview/clue_dis_ico4.png")},
        {name: '招标公告', pathName: 'announcement', ico: require("@/assets/images/detail/overview/clue_ico5.png"), count: 0, category:'business', field:'biddingAnnouncement', dis_ico: require("@/assets/images/detail/overview/clue_dis_ico5.png")},
        {name: '标讯Pro', pathName: 'tencent', ico: require("@/assets/images/detail/overview/clue_ico6.png"), count: 0, category:'business', field:'proBiddingAnnouncement', dis_ico: require("@/assets/images/detail/overview/clue_dis_ico6.png")},
        {name: '行政许可', pathName: 'administrative', ico: require("@/assets/images/detail/overview/clue_ico7.png"), count: 0, category:'business', field:'adminLicensing', dis_ico: require("@/assets/images/detail/overview/clue_dis_ico7.png")},
      ]
    }
  },
  created() {
    this.handleQuery()
  },
  mounted() {
  },
  methods: {
    async handleQuery(){
      let res = await projectTenderDataGroup({cid: this.companyId, type: this.activeIndex})
      if(res.code==200 && res.data){
        let data = res.data, totalVal = data.map(item => item.value).reduce((prev, cur) => prev + cur)
        this.viewData = data.map(item => {
          let it = {name:item.name, value:item.value, percent:parseFloat(Number(Number(item.value)/Number(totalVal)*100).toFixed(2))}
          return it
        })
        if(this.viewData.length>0){
          this.$nextTick(() => {
            this.getDT()
          })
        }
      }
    },
    getDT(val){
      let myChart = echarts.init(document.getElementById("echartsClue")), data = val ? val : this.viewData
      let option = {
        tooltip: {
          trigger: 'item',
          borderWidth:0,
          backgroundColor:"rgba(255, 255, 255, 0.8)",
          formatter: '{b}<br/>{d}%',
          extraCssText:'width:100px!important;',
        },
        legend: {
          type: 'scroll',
          orient: 'horizontal',
          bottom: 20,
          itemWidth: 14,
          itemHeight: 9,
          textStyle: {
            padding: [-2, 20, -4, 0],
            color: 'rgba(0,0,0,0.85)',
            backgroundColor: "transparent",
            fontSize: 10,
            rich: {
              a: {
              },
            },
          },
          data: data,
          pageButtonPosition: 'end',
        },
        color: ['#8A82F3','#5B9CF7','#43BBE0','#8ECF95','#FFDC6B'],
        series: [
          {
            type: 'pie',
            radius: '55%',
            center: ['50%', '40%'],
            data: data,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            },
            label: {
              normal: {
                show: true,
                textStyle: {
                  color: 'rgba(0,0,0,0.85)',
                  fontSize: 10,
                  fontWeight: 400
                }
              }
            }
          }
        ]
      }
      myChart.setOption(option)
    },
    handleStatistic() {
      this.typeList.forEach((item, index) => {
        item.count = this.statistic[item.category]?this.statistic[item.category][item.field]:0
      })
      //this.typeList[2].count = 10 //写死代表有数据，英文统计里面没这项
      //this.typeList[3].count = 10 //写死代表有数据，英文统计里面没这项
    },
    handleType(it, idx){
      if(it.count){
        changePath(this, it.pathName)
      }
    }
  },
  watch: {
    statistic(newVal, oldVal) {
      if(newVal){
        this.handleStatistic()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  .clue-container{
    margin: 0;
    padding: 24px 16px;
    background: #FFFFFF;
    border-radius: 4px;
    .selfTab{
      margin: 24px 0 0 -12px;
      ::v-deep .el-tabs__nav-wrap::after{
        display: none;
      }
      ::v-deep .el-tabs__item{
        height: 30px;
        line-height: 30px;
        padding: 0 12px;
      }
    }
    .clue-box{
      width: 100%;
      justify-content: space-between;
      margin: 8px 0 24px 0;
      .clue-echarts{
        width: calc(50% - 8px);
      }
      .busc-no-data{
        width: calc(50% - 8px);
      }
      .table-item{
        width: calc(50% - 8px);
        ::v-deep .el-table--border .el-table__cell{
          border-bottom: 0;
        }
      }
    }
    .clue-type{
      width: 100%;
      justify-content: space-between;
      border-top: 1px solid #EEEEEE;
      padding-top: 22px;
      margin-right: -16px;
      margin-left: -16px;
      padding-left: 16px;
      padding-right: 16px;
      .clue-type-item{
        font-size: 14px;
        color: #232323;
        cursor: pointer;
        &:hover{
          color: #0081FF;
        }
        &.disab{
          color: rgba(35,35,35,0.4);
        }
        img{
          width: 40px;
          height: 40px;
          margin-right: 8px;
        }
      }
    }
  }
</style>
