
var app = getApp()
var that;
var chart1;
var chart2;
Page({
  data: {
    date: app.globalData.month,
    data_b: [],
    chartData: [],//折线图数据
    pieData: [1],//饼型图数据
  },
  onLoad() {
    that = this;
    chart1,chart2='';
    dd.setNavigationBar({ title: "我的B分统计" });
  
  },
    //打开更多
  openMore(){
    dd.navigateTo({
      url: '../../statistics/integralEvent/integralEvent?month=' + this.data.date + '&type=BF'
    })
  },
  //则线图
  onInitChart(F2, config) {
    chart1 = new F2.Chart(config);
    chart1.source(that.data.chartData, {
      date: {
        range: [0, 1],
        tickCount: 5
      }
    });
    chart1.line().position('date*value');
    chart1.point().position('date*value').style({
      stroke: '#fff',
      lineWidth: 1
    });
    chart1.render();
    return chart1;
  },
  //饼型图
  onInitChart2(F2, config) {
    chart2 = new F2.Chart(config);
    chart2.source(that.data.pieData, {
      percent: {
        formatter: function formatter(val) {
          return val * 100 + '%';
        }
      }
    });
    chart2.legend({
      position: 'right',
      itemFormatter: function itemFormatter(val,index) {
        var str='';
        that.data.pieData.forEach(item=>{
          if(item.name==val){
              str=val + '  ' + item.percent+'分';
          }
        })
        return str;
      }
    });
    chart2.tooltip(false);
    chart2.coord('polar', {
      transposed: true,
      radius: 0.85
    });
    chart2.axis(false);
    chart2.interval()
      .position('a*percent')
      .color('name', ['#1890FF', '#13C2C2', '#2FC25B', '#FACC14', '#F04864', '#8543E0'])
      .adjust('stack')
      .style({ lineWidth: 1, stroke: '#fff', lineJoin: 'round', lineCap: 'round' }).animate({
        appear: { duration: 1200, easing: 'bounceOut' }
      });
    chart2.render();
    // 注意：需要把chart return 出来
    that.getIncidentLsit();
    return chart2;
  },
  //时间选择
  openDate() {
    dd.datePicker({
      format: 'yyyy-MM',
      currentDate: that.data.date,
      success: (res) => {
        that.setData({
          date: res.date
        })
        that.getIncidentLsit();
      },
    });
  },
  //获取积分事件
  getIncidentLsit() {
    var incident = app.$get("api/integral/statistics/b", { employee_id: 0, month: that.data.date });
    var pei = app.$get("api/integral/statistics/pie/b", { employee_id: 0, month: that.data.date });
    Promise.all([incident, pei]).then(resArr => {
      if (resArr[0].data.code == 1) {
        const { b, chart, good, bad } = resArr[0].data.data
        const points = chart.reward.map((rewardItem, index) => {
          return { date: rewardItem.month, value: rewardItem.point - (chart.deduction[index].point || 0) }
        })
        chart1.changeData(points);
        that.setData({
          data_b: b,
          data_good: good,
          data_bad: bad,
        });
      }
      if (resArr[1].data.code == 1) {
        let totalPoint = 0
        const pieData = resArr[1].data.data.list.map((item, index) => {
          totalPoint += item.point
          return { name: item.name, percent: item.point<0? 0:item.point, a: '1' }
        })
        that.setData({
          pieData: pieData
        });
        chart2.changeData(pieData);
      }
    }).catch(e => {
      console.error(e)
    })
  },
});
