
var app = getApp()
var that;
var chart1;
var chart2;
Page({
  data: {
    date: app.globalData.month,
    employee_id:0,
    isDb:false,
  },
  onLoad(e) {
    that = this;
    dd.setNavigationBar({ title: "奖扣统计" });
    if (e.id && e.month) {
      that.setData({
        date: e.month,
        employee_id: e.id
      })
    }
    that.getUserData();
    that.getIncidentLsit();
  },
  onInitChart(F2, config) {
    chart1 = new F2.Chart(config);
    chart1.source(that.data.chartData, {
      time: {
        type: 'timeCat',
        tickCount: 4,
        range: [0, 1]
      }
    });
    chart1.legend(false);
    chart1.axis('time', {
      // line: {
      //   lineWidth: 2,
      //   stroke: '#F56C6C',
      //   top: true, // 展示在最上层
      // },
      label: function label(text, index, total) {
        const textCfg = {};
        if (index === 0) {
          textCfg.textAlign = 'left';
        } else if (index === total - 1) {
          textCfg.textAlign = 'right';
        }
        return textCfg;
      },
    });
    chart1.line().position('time*value').color('type', [ '#F56C6C', '#67C23A']);
    chart1.render();
    return chart1;
  },
  onInitChart2(F2, config) {
    chart2 = new F2.Chart(config);
    chart2.source(that.data.chartData, {
      time: {
        type: 'timeCat',
        tickCount: 4,
        range: [0, 1]
      }
    });
    chart2.legend(false);
    chart2.axis('time', {
      // line: {
      //   lineWidth: 2,
      //   // stroke: '#F56C6C',
      //   top: true, // 展示在最上层
      // },
      label: function label(text, index, total) {
        const textCfg = {};
        if (index === 0) {
          textCfg.textAlign = 'left';
        } else if (index === total - 1) {
          textCfg.textAlign = 'right';
        }
        return textCfg;
      },
    });
    chart2.line().position('time*value').color('type', [ '#F56C6C', '#67C23A']);
    chart2.render();
    return chart2;
  },
  //获取个人统计
  getUserData() {
    app.$get("api/integral/statistics", { employee_id: that.data.employee_id, month: that.data.date }).then((res) => {
      var data = res.data.data;
      var task = data.task;
      dd.setNavigationBar({ title: data.name });
      var ratio = task.reward.point === 0 || task.deduction.point === 0 ? '-' : `${task.ratio.ratio}:1`
      var target_ratio = task.ratio.target_ratio <= 0 ? '0:0' : `${task.ratio.target_ratio}:1`
      if(data.task.reward.point>=data.task.reward.target_point&&data.task.deduction.point>=data.task.deduction.target_point&&data.task.exec.count>=data.task.exec.target_count){
          if(task.ratio.ratio>=task.ratio.target_ratio){
              this.setData({
                isDb: true
              }) 
          }
      }
      data.ratio = {
        ratio: ratio,
        target_ratio: target_ratio
      }
      this.setData({
        userData: data
      })
    })
  },
  getIncidentLsit() {
    app.$get("api/integral/statistics/prize", { employee_id: that.data.employee_id, month: that.data.date }).then((res) => {
      const { img_url, name, chart } = res.data.data;
      const { reward, deduct } = chart;
      var rewardArr = reward.map((item) => {
        return { time: item.date, value: item.point, type: '奖扣' };
      })
      var deductArr = deduct.map((item) => {
        return { time: item.date, value: item.point, type: '扣分' };
      })
      var rewardArrCount = reward.map((item) => {
        return { time: item.date, value: item.count, type: '奖扣' };
      })
      var deductArrCount = deduct.map((item) => {
        return { time: item.date, value: item.count, type: '扣分' };
      })
      var charData = rewardArr.concat(deductArr);
      var charData2 = rewardArrCount.concat(deductArrCount);
    //  console.log(charData)
    //   console.log(charData2)
      setTimeout(() => {
        chart1.changeData(charData);
        chart2.changeData(charData2);
      }, 1000);
      that.setData({
        img_url: img_url,
        name: name
      });
    })
  },
  //时间选择
  openDate() {
    dd.datePicker({
      format: 'yyyy-MM',
      currentDate: that.data.date,
      success: (res) => {
        that.setData({
          date: res.date
        })
          that.getUserData();
          that.getIncidentLsit();
      },
    });
  }
});
