// pages/person/person.js
const app = getApp();
import recordSource from '../../utils/recordSource';
const getReq = require('../../config.js').getReq;
import bonusUp from '../../utils/bonusUp';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      nickName: '',
      avatarUrl: ''
    },
    bonus_rate: [],
    bonus_list_show: false,
    login_count: 1,
    addr_num: '',
    pre_pay_count: 0, // 待付款 10
    pre_send_count: 0, // 待发货 20
    pre_receive_count: 0, // 待收货 30
    evaluate_count: 0, // 已收货 40
    bonus_total: 0,
    fromSource: '',
    firstLoad: true,
    userInfo: app.globalData.userInfo,
    tipsFlag: false,
    tipsDatas: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  handOutBonus() {
    app.navigateto('/pages/handOutBonus/handOutBonus')
    return;
  },
  getAuth(e) {
    let { userInfo } = e.detail
    if (userInfo) {
      this.setData({
        userInfo
      })
      wx.reLaunch({
        url: '/pages/index/index'
      });
    }
  },
  onShow: function () {
    this.closeTips()
    if(!app.globalData.userInfo) {
      return;
    }
    this.setData({
      userInfo: app.globalData.userInfo
    })
    let fromSource = this.data.fromSource
    if(this.data.firstLoad) {
      fromSource = recordSource(app, `act=member_order&op=orderCountState&client_type=mini`)
      this.setData({
        fromSource: app.globalData.fromSource
      })
    }
    else {
      app.setFromSource(this.data.fromSource)
    }
    const userInfo = app.globalData.userInfo || null;
    const nickName = userInfo.nickName || '';
    const avatarUrl = userInfo.avatarUrl || '../../image/mine_logo_icon.png';
    let self = this
    getReq(app,{
      act: 'cart',
      op: 'rate_money'
    }, function (res) {
      if (res.code == 200) {
        self.setData({
          userInfo: { nickName, avatarUrl },
          bonus_rate: res.datas&&res.datas.bonus_rate || 0,
          firstLoad: false
        })
      }
    })

    getReq(app,{
      act: 'member_info',
      op: 'get'
    }, function (res) {
      if (res.code == 200) {
        self.setData({
          addr_num: res.datas.sub_titles.addr_num,
          firstLoad: false
        })
      }
    })
    getReq(app,{
      act: 'member_order',
      op: 'orderCountState',
      from: fromSource
    }, function (res) {
      if (res.code == 200) {
        self.setData({
          pre_pay_count: 0, // 待付款 10
          pre_send_count: 0, // 待发货 20
          pre_receive_count: 0, // 待收货 30
          evaluate_count: 0,
          firstLoad: false
        })
        let { order_count } = res.datas
        if(order_count.length) {
          let length = order_count.length
          for(let i = 0; i < length; i++) {
            let state_name = ''
            if(order_count[i]['order_state'] == 10) {
              state_name = 'pre_pay_count'
            }
            if(order_count[i]['order_state'] == 20) {
              state_name = 'pre_send_count'
            }
            if(order_count[i]['order_state'] == 30) {
              state_name = 'pre_receive_count'
            }
            if(order_count[i]['order_state'] == 40) {
              state_name = 'evaluate_count'
            }
            if(state_name) {
              self.setData({
                [state_name]: order_count[i]['count']
              })
            }
          }
        }
      }
    })
    this.getBonusTotal()
    // setTimeout(() => {
    //   bonusUp((tipsFlag, tipsDatas) => {
    //     if (tipsFlag) {
    //       this.getBonusTotal()
    //     }
    //     this.setData({
    //       tipsFlag,
    //       tipsDatas
    //     })
    //   })
    // }, 0);
  },
  getBonusTotal() {
    let self = this
    getReq(app,{
      act: 'member_bonus',
      op: 'predepositex'
    }, function (res) {
      if (res.code == 200) {
        let { bonus_total } = res.datas
        self.setData({
          bonus_total,
          firstLoad: false
        })
      }
    })
  },
  shareBonus() {
    app.navigateto(`/pages/shareBonus/shareBonus?type_sn=58841537878020836581`)
  },
  closeTips() {
    this.setData({
      tipsFlag: false,
      tipsDatas: null
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  bonus_list() {
    this.setData({
      bonus_list_show: !this.data.bonus_list_show
    })
  },
  skip_help() {
    app.navigateto('/pages/webView/webView?url=https://passport.lrlz.com/hfive/feed_back/question_answer.html')
  },
  skip_bonus_rule() {
    app.navigateto('/pages/webView/webView?url=https://passport.lrlz.com/hfive/panda_bonus_rule/index.html')
  },
  skip_all_order() {
    app.navigateto('/pages/order_tabs/orderTabs')
  },
  refund() {
    wx.showToast({
      title: '客官！请在APP中完成退款申请',
      icon: 'none',
      duration: 2000
    })
  }
})