const app = getApp()
// pages/list/list.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.urls.baseImgUrl,
    cardList: [],
    currentIndex: 0,
    optionsList: [],
    isNeedPay: false, //是否需要交费
    needPayPrice: 0, //个人需要交费
    needPayTeamPrice: 0, //团体需要交费
    curTypeId: '',
    isRefreshing: false, //是否在刷新
    isRefresh: false,
    isShowCodeInput: false, //测试true
    code: '', //邀请码
    conentHeight: 550, //滚动内容的高度
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getPayState()
    //屏幕高度-标题的高度
    this.setData({
      conentHeight: app.globalData.windowHeight
    })
  },
  onShow() {
    this.dorefreshList()
  },
  //列表刷新
  dorefreshList() {
    var _self = this
    _self.setData({
      isRefresh: false,
      isRefreshing: true,
      cardList: []
    })
    _self.getPayState()
  },
  //获取类型
  getOptionsList() {
    var _self = this
    wx.showLoading({
      title: '加载中...',
    })
    app.http.get(app.urls.getDictionaryList.format({
        dataParentId: '4bf68c60-eac5-480d-b5e1-15203d0282f9'
      }), {
        header: {
          token: app.globalData.token
        }
      })
      .then(res => {
        wx.hideLoading({})
        if (res.data.length > 0) {
          _self.setData({
            optionsList: res.data,
            curTypeId: res.data[_self.data.currentIndex].dataId,
            currentIndex: _self.data.currentIndex
          })
          //根据第一条获取数据
          _self.getList()
        }
      })
      .catch(err => {
        console.log(err)
        _self.setData({
          isRefreshing: false
        })
      })
  },
  getList() {
    var self = this
    wx.showLoading({
      title: '加载中...',
    })
    app.http.get(app.urls.getCardList, {
        header: {
          token: app.globalData.token
        },
        data: {
          templateType: self.data.curTypeId
        }
      }).then(res => {
        wx.hideLoading({})
        res.data.forEach(it => {
          if (it.picturesTemplateNumber > 1000 && it.picturesTemplateNumber < 10000) {
            it.number = it.picturesTemplateNumber / 1000 + 'K+'
          } else if (it.picturesTemplateNumber > 10000 && it.picturesTemplateNumber < 1000000) {
            it.number = it.picturesTemplateNumber / 10000 + 'W+'
          } else {
            it.number = it.picturesTemplateNumber
          }
        })
        self.setData({
          cardList: res.data,
          isRefreshing: false
        })
      })
      .catch(err => {
        wx.hideLoading({})
        self.setData({
          isRefreshing: false
        })
      })
  },

  goDetail: function (e) {
    var _self = this
    var id = e.currentTarget.dataset.id
    var isPay = e.currentTarget.dataset.ispay //是否需要交费
    var price = e.currentTarget.dataset.price //价格
    var payState = e.currentTarget.dataset.paystate //交费转态
    if (_self.data.isNeedPay) {
      wx.navigateTo({
        url: '/packagecard/paypage/paypage?price=' + _self.data.needPayPrice + '&teamprice=' + _self.data.needPayTeamPrice + '&displayTreaty=1&type=1',
      })
    } else {
      //已经支付过
      if (isPay == '1') {
        //需要付款
        //判断是否交过费
        if (payState) {
          wx.navigateTo({
            url: '../cardDetail/cardDetail?id=' + id,
          })
        } else {
          //交费 
          wx.navigateTo({
            url: '/packagecard/paypage/paypage?price=' + price + '&type=2',
          })
        }
      } else {
        //免费
        wx.navigateTo({
          url: '../cardDetail/cardDetail?id=' + id,
        })
      }
    }
  },

  onChange(e) {
    var _self = this
    _self.setData({
      currentIndex: e.detail.index,
      curTypeId: _self.data.optionsList[e.detail.index].dataId,
      cardList: []
    })
    //重置所有数据重新加载
    this.getList()
  },
  //获取支付状态
  getPayState() {
    var _self = this
    wx.showLoading({
      title: '加载中...',
    })
    app.http.get(app.urls.getPayState, {
        header: {
          token: app.globalData.token
        }
      })
      .then(res => {
        wx.hideLoading({})
        //支付过跳转名片创建页面,没有支付跳转支付页面
        _self.setData({
          isNeedPay: res.data.cardCharge, //是否需要交费
          needPayPrice: res.data.cardChargePrice, //个人
          needPayTeamPrice: res.data.cardChargeDepartmentPrice, //团体
        })
        _self.getOptionsList()
      })
      .catch(err => {
        _self.setData({
          isRefreshing: false
        })
      })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getList()
  },
  onClose(e) {
    this.setData({
      isShowCodeInput: false
    })
  },
  inpuWatch(e) {
    this.setData({
      code: e.detail.value
    })
  },
  //邀请码创建
  codeCreate(e) {
    var _self = this
    if (_self.data.code == '') {
      wx.showToast({
        title: '请输入邀请码',
        icon: 'none'
      })
    } else {
      //未支付过跳转支付
      wx.navigateTo({
        url: '/packagecard/paypage/paypage?price=' + _self.data.needPayPrice + '&teamprice=' + _self.data.needPayTeamPrice + '&displayTreaty=1' + '&type=1&code=' + _self.data.code,
      })
    }
  },
  //无邀请码创建
  noCodeCreate(e) {
    wx.navigateTo({
      url: '/packagecard/paypage/paypage?price=' + _self.data.needPayPrice + '&teamprice=' + _self.data.needPayTeamPrice + '&displayTreaty=1&type=1',
    })
  }
})