<template>
  <div class="div-box-new-margin">
    <div class="div-border-box" id="uninvoiceAmountFull">
      <div class="out">
        <div>
          <div class="div-line div-line-right"></div>
          <div class="title" style="min-width: 264px;"> 近12月出货未开票金额趋势分析</div>
        </div>
        <div class="in">
          <div class="inline-16 mt-10">
            <departmentSalesperson ref="departmentSalesperson"  @depSelect="depSelect" @personSelect="personSelect" :isFull="isFull"></departmentSalesperson>
          </div>
          <div class="mt-10 inline-15">
            <span class="search__label inline-16">分析日期:</span>
            <el-date-picker
                :append-to-body="!isFull"
                v-model="endDate"
                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="uninvoiceAmountFull" @onFull="onFull" @backFull="backFull"></fullScreen>
        </div>
      </div>
      <div class="chart">
        <div id="uninvoiceAmountChart" :style="{height: heightChart}"></div>
      </div>
    </div>

  </div>
</template>

<script>
import departmentSalesperson from "@/views/salesData/components/departmentSalesperson";
import fullScreen from "@/views/salesData/components/fullScreen";
import { Line } from '@antv/g2plot';
const seriesKey = 'series';
const valueKey = 'value';
const meta = {
  date: {
    alias: '日期',
  },
  zerotothree: {
    alias: '0-3月出货未开票金额(万元)',
  },
  threetosix: {
    alias: '3-6月出货未开票金额(万元)',
  },
  sixtotwelve:{
    alias: '6-12月出货未开票金额(万元)'
  },
  twelveup:{
    alias: '一年以上出货未开票金额(万元)'
  },
};
export default {
  name: "uninvoiceAmountAnalysis",
  props:['dataid','scrollHeight','windowWidth'],
  components:{departmentSalesperson,fullScreen},
  data(){
    return {
      chartLine:null,
      param:{
        "id": 20231016122504,
        "content": {
          "type": 0,
          "dataid": '',
          "enddate": "2023-09-21"
        }
      },
      list:[],
      endDate:new Date().getFullYear() + '-' +  (new Date().getMonth() + 1) + '-' + new Date().getDate(),
      heightChart:'98%',
      isFull:false
    }
  },
  methods:{
    async listData(val){
      this.renderPie(val)
    },
    processData(data, yFields, meta) {

      const result = [];
      data.forEach((d) => {
        yFields.forEach((yField) => {
          const name = meta?.[yField]?.alias || yField;
          result.push({ ...d, date: d.date, [seriesKey]: name, [valueKey]: d[yField] });
        });
      });
      return result;
    },
    /*图表更新*/
    async queryModel(val){
      this.param.content.dataid = val || this.dataid
      this.param.content.enddate = this.endDate
      const res = await this.$api.requested(this.param)
      this.list = res.data.map(item=>{
        return {
          "zerotothree":Math.round(((item.zerotothree/10000)*100)/100),
          "threetosix":Math.round(((item.threetosix/10000)*100)/100),
          "date":item.date,
          "sixtotwelve":Math.round(((item.sixtotwelve/10000)*100)/100),
          "twelveup":Math.round(((item.twelveup/10000)*100)/100),
        }
      })
      this.chartLine.changeData(this.processData(this.list, ['zerotothree', 'threetosix','sixtotwelve','twelveup'], meta))
    },
    renderPie(val){
      this.chartLine = new Line('uninvoiceAmountChart',{
        data:this.processData(this.list, ['zerotothree', 'threetosix','sixtotwelve','twelveup',], meta),
        xField: 'date',
        yField: valueKey,
        seriesField: seriesKey,
        tooltip: {
          formatter: (datum) => {
            return {
              name:datum.series,
              value:'¥'+this.tool.formatAmount(datum.value,2)
            }
          }
        }
      });
      this.chartLine.render();
      this.queryModel(val)
    },
    changeDate(){
      this.queryModel(this.param.content.dataid)
    },
    /*选择部门*/
    depSelect(val){
      this.param.content.type = 1
      this.param.content.dataid = val
      this.queryModel(val)
    },
    /*选择业务员*/
    personSelect(val){
      this.param.content.type = 0
      this.param.content.dataid = val
      this.queryModel(val)
    },
    /*全屏*/
    onFull(){
      this.heightChart = this.windowWidth > 1180 ?'calc(100vh - 85px)':'calc(100vh - 124px)'
      this.isFull = true
    },
    /*退出全屏*/
    backFull(val){
      this.heightChart = '98%'
      this.isFull = false
      this.$emit('backFull',val)
    }
  }
}
</script>

<style scoped>

</style>
