// 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: 1 }, 
      { name: '上月', isActive: false, index: 2 }, 
      { name: '3个月', isActive: false, index: 3 }
    ],
    current : 1,
    currentOrderData : {},
    avator: 'https://bigaka-xie.oss-cn-shanghai.aliyuncs.com/biaka_default_u_logo.png',
    
    info : {},
    achievement : {},
    memberPreview : {},
    
  },

  //切换
  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.setData({
      filterBar: this.data.filterBar,
      current : this.data.current,
    });


    this.getGuidOrderPreview(this.data.current);
  },

  //获取头像
  getAvator() {
    wx.qy.getAvatar({
      success: res => {
        this.setData({
          avator: res.avatar
        });
      },
      fail: res => {
      }
    });
  },

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

    // currentUserInfo = { member: { qiyeInfo: { name : '张三'}}}

    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.getGuidAchievement();
    //获取导购业绩月统计
    this.getGuidAchievementMonth();
    //获取导购业界订单概览
    this.getGuidOrderPreview(this.data.current);
    //获取导购业绩日统计
    this.getGuidAchievementDay();
    //获取导购头像
    this.getAvator();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  //跳转到我的会员列表
  onTapToMyMemberList(){
    wx.navigateTo({
      url: '/shoppingGuid/page/pages/myMemberList/myMemberList',
    });
  },

  //初始化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,
    });
  },
  
  //导购招募会员概览
  getGuidMemberPreview(){
    wxService.post('/marketing/shoppingguide/achievement/memberPreview').then(res => {
      if (!res) return;
      let data = res.data.data;
      let series = [
        {
          name: '本月',
          color: 'rgba(0,145,255,0.2)',
          data: [data.memberRecruitAmountMonth, data.plusMemberRecruitAmountMonth, this.data.achievement.achievementMonth],
        },
        {
          name: '累计',
          color: 'rgba(0, 145, 255, 1)',
          data: [data.memberRecruitAmountTotal, data.plusMemberRecruitAmountTotal, this.data.achievement.achievementTotal],
        }
      ];

      this.initBarChart(series);
    });
    
  },
  //获取导购业绩月统计
  getGuidAchievementMonth(){
    wxService.post('/marketing/shoppingguide/achievement/month').then(res => {
      if (!res) return;
      this.data.achievement = res.data.data;
      this.setData({
        achievement: this.data.achievement
      });
      //获取会员统计
      this.getGuidMemberPreview();
    });
  },
  //导购业绩订单概览
  getGuidOrderPreview(type){
    wxService.post('/marketing/shoppingguide/achievement/orderPreview?type=' + type).then(res => {
      if (!res) return;
      this.data.currentOrderData = res.data.data;
      this.setData({
        currentOrderData: this.data.currentOrderData
      })
    });
  },
  //获取导购业绩日统计
  getGuidAchievementDay(){
    wxService.post('/marketing/shoppingguide/achievement/recruit').then(res => {
      if (!res) return;
      this.data.memberPreview = res.data.data;
      this.setData({
        memberPreview: this.data.memberPreview
      });
    });
  },
})