<template>
  <div class="div-box-new-margin">
    <div class="div-border-box" id="clueAddAnalysisFull">
      <div class="out">
        <div>
          <div class="div-line div-line-right"></div>
          <div class="title" style="min-width: 220px;">近12月线索新增分析</div>
        </div>
        <div class="in">
          <div class="inline-16 mt-10">
            <departmentSalesperson id="department" ref="departmentSalesperson"  @depSelect="depSelect" @personSelect="personSelect" :isFull="isFull"></departmentSalesperson>
          </div>
          <div class="mt-10 inline-16">
            <p class="search__label">状态:</p>
            <el-select v-model="param.content.where.isleave" clearable style="margin-right:10px" size="small" placeholder="请选择状态" @change="queryModel(param.content.dataid,param.content.where.isleave)" :disabled="param.content.type == '0'">
              <el-option label="在职" value="1"></el-option>
              <el-option label="离职" value="2"></el-option>
            </el-select>
          </div>
          <div class="mt-10 inline-15">
            <span class="search__label inline-16">分析日期:</span>
            <el-date-picker
                v-model="enddate"
                :append-to-body="!isFull"
                type="date"
                :clearable="false"
                @change="changeDate"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                size="small"
                range-separator="至"
                start-placeholder="开始月份"
                end-placeholder="结束月份">
            </el-date-picker>
          </div>
         <fullScreen  domId="clueAddAnalysisFull" @onFull="onFull" @backFull="backFull" :scrollHeight="scrollHeight"></fullScreen>
        </div>
      </div>
      <div class="chart">
        <div id="clueAddAnalysisChart" :style="{height: heightChart}"></div>
      </div>
    </div>
  </div>
</template>

<script>
import departmentSalesperson from "@/views/salesData/components/departmentSalesperson";
import fullScreen from "@/views/salesData/components/fullScreen";
import {Bar, DualAxes} from "@antv/g2plot";
export default {
  name: "clueAdd",
  components:{departmentSalesperson,fullScreen},
  props:["dataid",'scrollHeight','windowWidth'],
  data(){
    return {
      chartDualAxes:null,
      param:{
        "id": 20231015123304,
        "content": {
          "type": 0,
          "dataid": '',
          "enddate": "",
          "where":{
            "isleave":""
          }
        }
      },
      enddate:new Date().getFullYear() + '-' +  (new Date().getMonth() + 1) + '-' + new Date().getDate(),
      histogram:[],
      lineChart:[],
      heightChart:'100%',
      isFull:false,
      newHistogram:[]
    }
  },
  methods:{
    async listData(val){
      this.renderPie(val)
    },
    async queryModel(val,isleave){
      this.param.content.dataid = val || this.dataid
      this.param.content.where.isleave = isleave
      this.param.content.enddate = this.enddate
      const res = await this.$api.requested(this.param)
      this.histogram = res.data.histogram
      let lastYear = []
      let nowYear = []
      let k=0
      for (var i=0;i<this.histogram.length;i++){
        if (this.histogram[i].type === '去年同期新增'){
          lastYear[k]=this.histogram[i]
          k++
        }
      }
      let x=0
      for (var i=0;i<this.histogram.length;i++){
        if (this.histogram[i].type === '本期新增'){
          nowYear[x]=this.histogram[i]
          x++
        }
      }
      this.lineChart = res.data.lineChart.map(item=>{
        return {
          date: item.date,
          type:"同比增长率",
          value: item.value
        }
      })
      this.newHistogram = lastYear.concat(nowYear)
      this.chartDualAxes.changeData([this.newHistogram,this.lineChart])
    },
    renderPie(val){
      this.chartDualAxes = new DualAxes('clueAddAnalysisChart', {
        data:[this.newHistogram,this.lineChart],
        xField: 'date',
        yField: ['value','value'],
        seriesField: 'type',
        geometryOptions: [
          {
            geometry: 'column',
            isGroup: true,
            seriesField: 'type',
            color:['#62daab','#6395fa'],
            label: {
              position:top
            },
          },
          {
            geometry: 'line',
            seriesField: 'type',
            color: '#F6903D',
            smooth: true,
            label:{
              position:top,
              formatter: (datum) =>{
                return  datum.value + '%'
              }
            },
            lineStyle: {
              lineWidth: 2,
            },
          },
        ],
        tooltip: {
          formatter: (datum) => {
            return { name: datum.type, value: datum.type == '同比增长率'? datum.value + '%' : datum.value };
          },
        },
      });
      this.chartDualAxes.render();
      this.queryModel(val,this.param.content.where.isleave)
    },
    changeDate(){
      this.queryModel(this.param.content.dataid,this.param.content.where.isleave)
    },
    /*选择部门*/
    depSelect(val){
      this.param.content.type = 1
      this.param.content.dataid = val
      this.queryModel(val,this.param.content.where.isleave)
    },
    /*选择业务员*/
    personSelect(val){
      this.param.content.type = 0
      this.param.content.dataid = val
      this.param.content.where.isleave = ''
      this.queryModel(val,this.param.content.where.isleave)
    },
    /*全屏*/
    onFull(){
      this.heightChart = this.windowWidth > 1136 ? 'calc(100vh - 70px)':'calc(100vh - 123px)'
      this.isFull = true
    },
    /*退出全屏*/
    backFull(val){
      this.heightChart = '100%'
      this.isFull = false
      this.$emit('backFull',val)
    }
  }
}
</script>

<style scoped>

</style>
