// pages/pianoDetail/pianoDetail.js
const app = getApp()
const util = require("../../utils/util")
const urlDef = require("../../utils/urls")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 1,
    initMoney: 19.90,
    id: "",
    payFlag: 1,
    schoolList: [],
    moreFlag: 0,
    isBuy: true,
    downLoadShow: false
  },

  getMoreSchool: function () {
    if (this.data.moreFlag == 0) {
      this.setData({
        moreFlag: 1
      })
    } else {
      this.setData({
        moreFlag: 0
      })
    }
  },

  remNum: function () {
    var num = this.data.num
    if (num <= 1) {
      return false
    } else {
      num--
      var money = this.data.initMoney
      money = parseFloat(money) * parseFloat(num).toFixed(2)
      this.setData({
        num: num,
        'item.money': money.toFixed(2)
      })
    }
  },

  addNum: function () {
    var num = this.data.num
    if (num >= 10) {
      wx.showToast({
        title: '已达单次购买上限',
        icon: "none"
      })
      return
    }
    num++;
    var money = this.data.initMoney
    money = parseFloat(money) * parseFloat(num).toFixed(2)
    this.setData({
      num: num,
      'item.money': money.toFixed(2)
    })
  },

  callPhone(e) {
    const index = e.currentTarget.dataset.index
    const phone = this.data.schoolList[index].CONTRACT_TELEPHONE
    if (phone) {
      wx.makePhoneCall({
        phoneNumber: phone
      })
    } else {
      wx.showToast({
        title: '暂无联系电话',
        icon: "none"
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let urls = urlDef.urls
    let item = JSON.parse(options.data)
    if (item.imgUrl == null || item.imgUrl == undefined) {
      item.imgUrl = urls.oss_file + 'image/0e02f9fc-1d41-4551-a150-b1be8196b49d.jpg'
    }
    this.setData({ item: item, initMoney: item.money })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }, 
  tabBuy: function () {
    let stu = wx.getStorageSync('student');
    util.doPost(
      'buyPianoCard',{
        id: this.data.item.id,
        studentId: stu.studentId
      }).then(res =>{
        if(res.success == 1){
          this.payFunction(res.data)
          wx.showLoading({
            title: '正在唤醒支付',
          })
        }
      })
  },
  payFunction: function (_this) {
    util.doPost(
      'payment', {
        payDesc: _this.cardName,
        bussType: '15',
        sourceId: _this.id,
        amount: _this.payMoney,
        openId: wx.getStorageSync('openId'),
        appId: app.globalData.appId
      }).then(res => {
        wx.hideLoading({
          success: (res) => {},
        })
      if (res.success > 0) {
        // 唤醒支付
        var payData = res.data;
        var that = this
        wx.requestPayment({
          timeStamp: payData.timeStamp,
          nonceStr: payData.nonceStr,
          package: payData.package,
          signType: payData.signType,
          paySign: payData.paySign,
          success: function (res) {
            // wx.redirectTo({
            //   url: '/pages/payDone/payDone?money=' + that.data.orderInfo.paidAmount + "&type=App",
            // })
            console.log('payment success ... ' + JSON.stringify(res));
          },
          fail: function (res) {
            console.log('payment fail ... ' + JSON.stringify(res));
          },
          complete: function (res) {
            that.setData({
              flag: true
            })
           
            console.log('payment complete ... ' + JSON.stringify(res));
          }
        })
      } else {
        const tip = res.errMsg
        wx.showToast({
          title: tip,
          icon: 'none'
        })
      }
    })
  },
})