<template>
  <div class="div-box-new-margin" >
    <div class="div-border-box" id="invoiceAmountAnalysisFull">
      <div class="out">
        <div>
          <div class="div-line div-line-right"></div>
          <div class="title" style="min-width: 220px;">{{$t(`开票金额趋势分析`)}}</div>
        </div>

        <div class="in">
          <div class="inline-16 mt-10">
            <departmentSalesperson ref="departmentSalesperson"  @depSelect="depSelect" @personSelect="personSelect" :isFull="isFull" :is-new-dep="true"></departmentSalesperson>
          </div>
          <div class="mt-10 inline-16">
            <p class="search__label">{{$t('状态')}}:</p>
            <el-select v-model="param.content.where.isleave" clearable style="margin-right:10px" size="small" :placeholder="$t('请选择状态')" @change="queryModel(param.content.dataid,param.content.where.isleave,'状态')">
              <el-option :label="$t('在职')" value="1"></el-option>
              <el-option :label="$t('离职')" value="2"></el-option>
            </el-select>
          </div>
          <div class="mt-10 inline-15">
            <span class="search__label inline-16">{{$t(`年度`)}}:</span>
            <el-date-picker
                :append-to-body="!isFull"
                v-model="yearNow"
                @change="changeDate"
                :picker-options="pickerOptions"
                :clearable="false"
                type="year"
                size="small"
                :placeholder="$t(`选择年`)">
            </el-date-picker>
          </div>
          <fullScreen  domId="invoiceAmountAnalysisFull" @onFull="onFull" @backFull="backFull"></fullScreen>
        </div>
      </div>

      <div class="chart">
        <div id="invoiceAmountAnalysisChart" :style="{height: heightChart}"></div>
      </div>
    </div>

  </div>
</template>

<script>
import departmentSalesperson from "@/views/salesData/components/departmentSalesperson";
import fullScreen from "@/views/salesData/components/fullScreen";
import { DualAxes } from '@antv/g2plot';
export default {
  name: "invoiceAmountAnalysis",
  props:['dataid','scrollHeight','windowWidth'],
  components:{departmentSalesperson,fullScreen},
  data(){
    return {
      pickerOptions: {
        disabledDate(time){
          // 获取当前时间
          const today = new Date();
          //  比较日期，将未来的年份禁用
          return time.getFullYear() > today.getFullYear();
        }
      },
      chartDualAxes:null,
      param:{
        "id": 20231016095304,
        "content": {
          "type": 0,
          "dataid": 54,
          "year": "2023",
          "where":{
            "isleave":"1"
          }
        }
      },
      yearNow:new Date(),
      list:[],
      list1:[],
      heightChart:'98%',
      isFull:false
    }
  },
  methods:{
    async listData(val){
      this.renderPie(val)
    },
    async queryModel(val,isleave,state){
      if (state == '状态'){
        this.$refs.departmentSalesperson.person = ''
        this.param.content.type = 1
        this.param.content.dataid = this.$refs.departmentSalesperson.depment ? this.$refs.departmentSalesperson.depment : -1
      }else {
        this.param.content.dataid = val?val : this.dataid
      }
      this.param.content.where.isleave = isleave
      this.param.content.year = new Date(this.yearNow).getFullYear()
      const res = await this.$api.requested(this.param)
      let amount = res.data.map(item=>{
        let value = this.tool.unitConversion(item.amount,10000)
        return {
          "value":value,
          "date":item.date,
          "type":this.$t('本期金额(万元)')
        }
      })
      let oldamount = res.data.map(item=>{
        let value = this.tool.unitConversion(item.oldamount,10000)
        return {
          "date":item.date,
          "value":value,
          "type":this.$t('去年同期金额(万元)')
        }
      })
      this.list = oldamount.concat(amount)
      this.list1 = res.data.map(item=>{
        return {
          "date":item.date,
          "tbzzl":Math.round((item.tbzzl * 100)*100)/100
        }
      })
      this.chartDualAxes.changeData([this.list,this.list1])
      state == '状态' ? this.personData(this.$refs.departmentSalesperson.depment) : ''
    },
    renderPie(val){
      this.chartDualAxes = new DualAxes('invoiceAmountAnalysisChart', {
        data: [this.list,this.list1],
        xField: 'date',
        yField: ['value','tbzzl'],
        meta:{
          tbzzl:{
            alias:this.$t('同比增长率')
          }
        },
        geometryOptions: [
          {
            geometry: 'column',
            isGroup: true,
            seriesField: 'type',
            color:['#62daab','#6395fa'],
            label:{
              position:top,
              formatter: (datum) =>{
                return '¥' + this.tool.formatAmount(datum.value,2)
              }
            }
          },
          {
            geometry: 'line',
            lineStyle: {
              lineWidth: 2,
            },
            color: '#F6903D',
            smooth: true,
            label:{
              formatter: (datum) =>{
                return  datum.tbzzl + '%'
              }
            }
          },
        ],
        tooltip: {
          fields: ['type','tbzzl', 'value'],
          formatter: (datum) => {
            return {
              name:datum.type === undefined?this.$t('同比增长率'):datum.type,
              value:datum.value === undefined?datum.tbzzl + '%':'¥' + this.tool.formatAmount(datum.value,2)
            }
          }
        }
      });
      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?val:-1
      this.personData(this.param.content.dataid)
      this.queryModel(this.param.content.dataid,this.param.content.where.isleave)
    },
    /*选择业务员*/
    personSelect(val){
      if (val || this.$refs.departmentSalesperson.depment){
        this.param.content.type = val?0:1
        this.param.content.dataid = val?val:this.$refs.departmentSalesperson.depment
      }else {
        this.param.content.type = 1
        this.param.content.dataid = -1
      }
      this.queryModel(this.param.content.dataid,this.param.content.where.isleave)
    },
    /*获取新的业务员列表*/
    async personData(depid){
      let param = {
        id: 20230620102004,
        content: {
          isleave:this.param.content.where.isleave,
          depid:depid
        },
      }
      const res = await this.$api.requested(param)
      this.$refs.departmentSalesperson.personnelList = res.data.hr
    },
    /*全屏*/
    onFull(){
      this.heightChart = 'calc(100vh - 85px)'
      this.isFull = true
    },
    /*退出全屏*/
    backFull(val){
      this.heightChart = '98%'
      this.isFull = false
      this.$emit('backFull',val)
    }
  }
}
</script>

<style scoped>

</style>
