<template>
  <div class="lineBarCharts" ref="lineBarCharts" />
</template>

<script>
import * as echarts from 'echarts'
import { setPriceMax, setNumberMax } from "./twoYEcharts.js";
export default {
  name: '',
  props: {
    twoYAxisData: {
      type: Object,
      default: ()=>{}
    }
    
  },
  data () {
    return {
      uEcharts: null,
      uOption: null,
    }
  },
  watch: {
    twoYAxisData: {
      handler(newValue, oldValue){
        console.log(newValue, oldValue)
        if(newValue){
          this.setSeriesData(newValue)
        }
      },
      deep: true,
      // immediate: true
    }
  },
  created () {
  },
  mounted () {
    this.$nextTick(() => {
      this.initChart()
      this.setSeriesData()
    })
  },
  methods: {
    initChart(){
      this.uEcharts = echarts.init(this.$refs.lineBarCharts)

      let color = ['#42CEAF', '#1890ff', '#16B4FE', '#747AFE'];
      this.uOption = {
        // title: {
        //   text: this.title + '图',
        //   left: 'center'
        // },
        color: color,
        tooltip: {
          trigger: 'axis'
        },
        grid: {
          left: '5%',
          right: '5%',
          bottom: '10%'
        },
        legend: {
          type: 'scroll',
          width: '60%',
          // orient: 'vertical',
          // right: 90,
          // left: 20,
          top: 0,
          // bottom: 20,
          // selectedMode: 'single',
          padding: [
              5,  // 上
              150, // 右
              0,  // 下
              20, // 左
          ],
        },
        toolbox: {
          show: true,
          feature: {
            // dataView: { readOnly: false }, // 展示数据--不过没用
            // magicType: { type: ['bar', 'line'] },
            // restore: {}, // 还原
            saveAsImage: {}
          }
        },
        tooltip: {
          trigger: 'axis',
          formatter: function (params) {
            // console.log(params)
            let str = params[0].name + "<br />";
            params.forEach((item) => {
              var unit = item.seriesType === 'line' ? '个' : '万元'
              var num = item.seriesType === 'line' ? item.value : (Number(item.value) / 10000).toFixed(2)
              str +=
                '<span style="display:inline-block;margin-right:5px;border-radius:50%;width:10px;height:10px;left:5px;background-color:'+item.color+'"></span>' + item.seriesName + " : " + num + unit + "<br />";
            });
            return str;
          },
        },
        // dataZoom: [
        //   {
        //     startValue: '2023-02-01'
        //   },
        //   {
        //     type: 'inside'
        //   }
        // ],
        xAxis: {
          type: 'category',
          // boundaryGap: true, // 解决数据溢出X轴的问题
          data: this.xAxisData
        },
        yAxis: [
          {
            type: 'value',
            // min: 0,
            // max: this.numMax,
            // interval: Math.ceil(this.numMax / 6),
            axisLabel: {
              // 可以动态设置是数量还是金额
              formatter: '{value} '
            }
          },
          {
            type: 'value',
            // min: 0,
            // max: this.priceMax,
            // interval: Math.ceil(this.priceMax / 6),
            axisLabel: {
              // 可以动态设置是数量还是金额
              // formatter: '{value} '
              formatter: function (value, index) {
                if(value > 0){
                  return (Number(value) / 10000).toFixed(1) + "万"
                } else {
                  return value;
                }
              }
            }
          },
        ],
        series: [
          // {
          //   name: '合同数量',
          //   type: 'line',
          //   yAxisIndex: 0,
          //   data: [50, 31, 53, 51, 42, 48, 59]
          // },
          // {
          //   name: '合同金额',
          //   type: 'bar',
          //   yAxisIndex: 1,
          //   barWidth: '30%',
          //   data: [500, 310, 530, 510, 420, 480, 590]
          // },
        ]
      };

      this.uOption && this.uEcharts.setOption(this.uOption);
      window.addEventListener('resize', ()=>{
        this.uEcharts.resize()
      })
    },
    setSeriesData(data){
      // console.log(this.uEcharts)
      console.log(data)
      var twoYAxisData = data ? data : this.twoYAxisData
      // 模拟数据
      // var twoYAxisData = {
      //   contractTotal: [2, 0, 0, 0, 0, 0],
      //   // contractTotal: [5, 45, 23, 56.7, 78, 9.9],
      //   contractAmount: [50000, 655265722.63, 25368457, 25369428, 263588, 555542637.1]
      // }
      console.log(twoYAxisData)
      // var numMax = Math.max(...twoYAxisData.contractTotal)
      // console.log(numMax)
      // var priceMax = Math.max(...twoYAxisData.contractAmount)
      var maxNumber = Math.max.apply(null, twoYAxisData.contractTotal)
      var maxPrice = Math.max.apply(null, twoYAxisData.contractAmount)

      this.uOption.xAxis.data = twoYAxisData.xaxisData
      this.uOption.yAxis = [
        {
          type: 'value',
          // min: 0,
          // max: numMax,
          // interval: Math.ceil(numMax / 6),
          min: 0,
          max: setNumberMax(maxNumber),
          interval: setNumberMax(maxNumber) / 5,
          axisLabel: {
            // 可以动态设置是数量还是金额
            formatter: '{value} '
          }
        },
        {
          type: 'value',
          // min: 0,
          // max: priceMax,
          // interval: Math.ceil(priceMax / 6),
          min: 0,
          max: setPriceMax(maxPrice),
          interval: setPriceMax(maxPrice) / 5,
          axisLabel: {
            // 可以动态设置是数量还是金额
            formatter: function (value, index) {
              if(value > 0){
                return (Number(value) / 10000).toFixed(1) + "万"
              } else {
                return value;
              }
            }
          }
        },
      ]
      this.uOption.series = [
        {
          name: '合同数量',
          type: 'line',
          yAxisIndex: 0,
          data: twoYAxisData.contractTotal,
        },
        {
          name: '合同金额',
          type: 'bar',
          yAxisIndex: 1,
          barWidth: '30%',
          data: twoYAxisData.contractAmount,
        }
      ]
      this.uEcharts.hideLoading()
      // // console.log(this.uOption)
      this.uEcharts.setOption(this.uOption, true)
    }
  }
}
</script>

<style lang="less" scoped>
.lineBarCharts{
  width: 100%;
  height: 100%;
}
</style>
