import recordSource from '../../utils/recordSource';
const config = require('../../config.js')
const getReq = config.getReq
const buyVGoods = config.buyVGoods

let app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    fGoodsRequesting: false,
    currentIndex: 0,  //页签索引
    rechargeGearActiveIndex: 0, //挡位项目索引
    tips:'',  //顶部tips
    inviter_tip: '',
    goods_inviter_tips: new Map(),

    goods:[], //挡位数据
    goods_id:'', //当前选中挡位的goods_id
    card_type: 'phone',
    card_no: '',
    fromSource: '',
    payInfoChecked:true,
    showShareDialog:false,
    showtermsOfServiceDialog:false
  },

  onShowShareDialog(){
    this.setData({
      showShareDialog:true
    })
  },

  onCloseShareDialog(){
    this.setData({
      showShareDialog:false
    })
  },

  onCheckedPayInfo(){
    this.setData({
      payInfoChecked:!this.data.payInfoChecked
    })
  },

  // 页签切换
  handlerClickTabItem(e) {
    var dataset = e.currentTarget.dataset
    this.setData({
      currentIndex: dataset.activeindex,
      card_no: dataset.cardno
    });
  },

  //充值档位切换
  handlerClickRechargeGear(e) {
    var dataset = e.currentTarget.dataset
    let goods_id = dataset.currentvalue
    this.setData({
      rechargeGearActiveIndex: dataset.activeindex,
      goods_id: goods_id,
      inviter_tip: this.data.goods_inviter_tips.get(goods_id)
    });
  },

  //跳转到油卡管理页面
  handlerAddCard(e){
    app.navigateto('/pages/postageManage/postageManage?card_type=phone')
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.requestGoods();
  },
  findCard(cards,card_type) {
    for (const card of cards) {
      if (card.card_type == card_type) {
        return card;
      }      
    }
    return {};
  },
  requestGoods() { 
    if (this.fGoodsRequesting) return;
    this.fGoodsRequesting = true

    let fromSource = recordSource(app, `act=index&op=card_goods`)
    this.setData({
      fromSource: app.globalData.fromSource,
    });
    var self = this;
    getReq(app, {
      act: 'index',
      op: 'card_goods',
      page_type: 'phone',
      from: fromSource
    },
    function (res) {
      wx.stopPullDownRefresh()
      if (res.code == 200) {
        if (res.datas && res.datas.goods) {
          var datas = res.datas
          let card = app.getValueByKey('phone', datas.cards)
          card = app.isNullObject(card) ? { card_no: '', card_type: 'phone' } : card

          console.log('card no:', card.card_no)
          let goods_inviter_tips = self.formGoodsInviterTips(datas.goods_inviter_tips)
          let goods_id = datas.goods[0].goods_id
          let inviter_tip = goods_inviter_tips.get(goods_id)

          self.setData({
            tips: datas.tips,
            goods: datas.goods,
            inviter_tip: inviter_tip,
            goods_inviter_tips,
            goods_id: goods_id,
            card_no: card.card_no
          });
        }
      } else {
        wx.showToast({
          icon: 'none',
          title: '支付未完成，订单将在10分钟后取消...',
          duration: 5000
        })
      }
      self.fGoodsRequesting = false
    })
  },
  formGoodsInviterTips(godosInviterTips) {
    let ret = new Map();
    godosInviterTips.forEach(item => {
      let goods_id = item.goods_id
      let tip = item.tip
      ret.set(goods_id, tip)
    });

    return ret;
  },
  // 立即充值
  handlerRecharge()
  {
    if(!this.data.payInfoChecked) {
      wx.showToast({
        icon:'none',
        title: '请同意话费充值服务说明'
      })
      return;
    }
    const { goods_id, card_no, card_type } = this.data
    if(!card_no){
      app.showToast('手机号不能为空')
      return
    }
    buyVGoods(app, goods_id, 1, { card_no, card_type}, (res) => {
      let page = getCurrentPages().pop();
      page.onLoad()
    }, (err) => {
        wx.showToast({
          icon: 'none',
          title: '支付未完成，订单将在10分钟后取消...',
          duration: 5000
        })
        let page = getCurrentPages().pop();
        page.onLoad()
    })
  },

  onShowPayInfoModal(){
    this.setData({
      showtermsOfServiceDialog:true
    })
  },

  onShowtermsOfServiceDialog(){
    this.setData({
      showtermsOfServiceDialog:false
    })
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const record = app.globalData.record || {}
    const { card_no = '' } = record
    if (card_no) {
      this.setData({ card_no })
    }
    app.setFromSource(this.data.fromSource)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

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

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

  },
    onShareAppMessage: function () {
    return app.cardShareInfo();
  }
})