var wxCharts = require('../../../utils/wxcharts.js');
var data = require('../../../utils/util.js');
const api = require('../../../utils/api.js');
var ringChart = null;
Page({
  data: {
  }, 
  bindDateChange(e) {
    this.setData({
      startdata: e.detail.value
    })
    this.req(e.detail.value,this.data.enddata)
  },
  bindendDateChange(e){
    this.setData({
      enddata: e.detail.value
    })
    this.req(this.data.startdata, e.detail.value)
  },
  touchHandler: function (e) {
    console.log(ringChart.getCurrentDataIndex(e));
  },
  onLoad(options){

  },
  req: function (start,end){
    let that = this
    wx.showLoading({
      title: '正在加载',
    })
    wx.request({
      url: api.selfInfo,
      data: {
        loginId: wx.getStorageSync('loginId'),
        start: start,
        end: end
      },
      success(res) {
        // res.data.data.nomalInTimes=130
        // res.data.data.nomalOutTimes = 40
        // res.data.data.laterTimes = 50
        // res.data.data.UnreturnedTimes = 20
        wx.hideLoading()
        that.setData({
          data: res.data.data
        })
        ringChart.updateData({
          title: {
            name: '汇总'
          },
          subtitle: {
            color: '#333333'
          },
          series: [{
            name: '正常进入',
            data: res.data.data.nomalInTimes,
            stroke: false,
            color: '#6282f4'
          }, {
            name: '正常外出',
            data: res.data.data.nomalOutTimes,
            stroke: false,
            color: '#ed5c68'
          }, {
              name: '晚归',
              data: res.data.data.laterTimes,
              stroke: false,
              color: '#89c997'
          }, {
              name: '未归寝',
              data: res.data.data.UnreturnedTimes,
              stroke: false,
              color: '#f29b76'
          }],
        });
      }
    })
  },
  onReady: function (e) {
    let that=this
    this.setData({
      enddata:data.formatDate(),
      checkenddate: data.formatDate(),
      startdata: data.getDateStr(data.formatDate(),-30)
    })
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    ringChart = new wxCharts({
      animation: true,
      canvasId: 'ringCanvas',
      type: 'ring',
      extra: {
        ringWidth: 25,
        pie: {
          offsetAngle: -45
        }
      },
      title: {
        name: '汇总',
        color: '#333',
        fontSize: 18
      },
      subtitle: {
        name: '',
        color: '#6282f4',
        fontSize: 15
      },
      series: [{
        name: '正常进入',
        data: 0,
        stroke: false,
        color:'#6282f4'
      }, {
        name: '正常外出',
        data: 0,
        stroke: false,
        color:'#ed5c68'
      }, {
        name: '晚归',
        data: 0,
        stroke: false,
        color:'#89c997'
      }, {
        name: '未归寝',
        data: 0,
        stroke: false,
        color:'#f29b76'
      }],
      disablePieStroke: true,
      width: 200,
      height: 200,
      dataLabel: false,
      legend: false,
      background: '#fff',
      padding: 0
    });
    ringChart.addEventListener('renderComplete', () => {
      console.log('renderComplete');
    });
    setTimeout(() => {
      ringChart.stopAnimation();
    }, 500);
    this.data.startdata = data.getDateStr(data.formatDate(), -30)
    this.data.enddata = data.formatDate()
    this.req(this.data.startdata, this.data.enddata)
    this.setData({
      user: wx.getStorageSync('user')
    })
  }
});