// shoppingGuid/page/pages/achievement/achievement.js
const wxService = require('../../../../utils/wxService');
const app = getApp();
// import wxCharts from '../../../../charts/wxcharts-min.js'
const wxCharts = require('../../../../charts/wxcharts-min.js')

wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    filterBar: [
      { name: '本月', isActive: true, index: 0 }, 
      { name: '上月', isActive: false, index: 1 }, 
      { name: '3个月', isActive: false, index: 2 }
    ],
    current : 0,
    currentOrderData : {},
    
    info : {},
    achievement : {},
    
  },

  //切换
  onTapChangeFilter(e){
    let index = e.currentTarget.dataset.index;
    this.data.filterBar.forEach(item => {
      item.isActive = false;
    });

    this.data.filterBar[index].isActive = true;
    this.data.current = this.data.filterBar[index].index;
    this.data.currentOrderData = this.data.achievement.monthAchievement[this.data.current];
    this.setData({
      filterBar: this.data.filterBar,
      current : this.data.current,
      currentOrderData: this.data.currentOrderData
    });

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let currentUserInfo = wx.getStorageSync('guidBaseInfo');
    if (!currentUserInfo) { //未登录成功
      wx.redirectTo({
        url: '/shoppingGuid/page/pages/welcomGuider/welcomGuider',
      });
      return ;
    }

    let userInfo = wx.getStorageSync('guidInfo');
    if (currentUserInfo.member) {
      if (!currentUserInfo.member.qiyeInfo.name) {
        currentUserInfo.member.name = userInfo.nickName;
      }
      else{
        currentUserInfo.member.name = currentUserInfo.member.qiyeInfo.name;
      }
    }

    this.setData({
      info: currentUserInfo
    });

    //获取导购业绩
    // this.echartsComponnet = this.selectComponent('#barChart');
    this.getGuidAchievement();
    // let series = [
    //   {
    //     name: '本月',
    //     color: 'rgba(0,145,255,0.2)',
    //     data: [10, 15, 100],
    //   },
    //   {
    //     name: '累计',
    //     color: 'rgba(0, 145, 255, 1)',
    //     data: [20, 30, 300],
    //   }
    // ];

    // this.initBarChart(series);

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  //初始化barchart
  initBarChart(series){
    let width = app.globalData.systemInfo.screenWidth - 30;
    let chart = new wxCharts({
      canvasId: 'columnCanvas',
      type: 'column',
      animation: true,
      categories: ['普通会员', '付费会员', '销售额'],
      series: series,
      legend : false,
      yAxis: {
        format: function (val) {
          return val;
        },
        min: 0
      },
      xAxis: {
        disableGrid: false,
        type: 'calibration'
      },
      extra: {
        column: {
          width: 20
        }
      },
      width: width,
      height: 250,
    });
  },

  //获取导购业绩
  getGuidAchievement(){
    wxService.post('/marketing/shoppingguide/achievement').then(res => {
      if(!res) return ;
      this.data.currentOrderData = res.data.data.monthAchievement[this.data.current];
      this.setData({
        achievement: res.data.data,
        currentOrderData: this.data.currentOrderData,
      });

      let obj = res.data.data;
      let series = [
        {
          name: '本月',
          color: 'rgba(0,145,255,0.2)',
          data: [obj.memberRecruitAmountMonth, obj.plusMemberRecruitAmountMonth, obj.achievementMonth],
        },
        {
          name: '累计',
          color: 'rgba(0, 145, 255, 1)',
          data: [obj.memberRecruitAmountTotal, obj.plusMemberRecruitAmountTotal, obj.achievementTotal],
        }
      ];

      this.initBarChart(series);
    });
  },
})