var app = getApp();
const config = require('../../../config/config.js')
const util = require('../../../utils/util.js')
const Http = require('../../../utils/http.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    billTypeValue: '',
    billId: '',
    flag: false,
    detailData: {},
    list: []
  },

  onLoad: function (options) {
    let that = this;
    console.log(options, 'options');
    that.setData({
      billTypeValue: options.billTypeValue,
      billId: options.billId,
      flag: options.flag === 'mall'
    })
    this.getData()
  },
  getData() {
    Http.getRequest(this.data.flag ? config.api.mallbillDetail : config.api.billDetail, app.globalData.token, '', {
      billId: this.data.billId,
      billTypeValue: this.data.billTypeValue,
    }, (res) => {
      if (res.code === 200) {
        res.data.starttime = res.data.starttime ? util.formatTime(Number(res.data.starttime), "yyyy.MM.dd ") : '';
        res.data.endtime = res.data.starttime ? util.formatTime(Number(res.data.endtime), "yyyy.MM.dd ") : '';
        this.setData({
          detailData: res.data
        })
      }
    })

    Http.getRequest(config.api.billActionlist, app.globalData.token, '', {
      billId: this.data.billId,
      pageNum: 1,
      pageSize: 1000,
    }, (res) => {
      if (res.code === 200) {
        res.data.list.forEach(ele => {
          ele.createtime = ele.createtime ? util.formatTime(Number(ele.createtime), "yyyy.MM.dd ") : '';
        })
        this.setData({
          list: res.data.list
        })
      }
    })
  },
  goBack() {
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 查看结算单详情
   */
  godetail() {
    wx.navigateTo({
      url: `/pages/statementsDetail/index?id=${this.data.billId}`,
    })
  },

  makePhoneCall(e) {
    const phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },

  /**
   * 更新订单的状态
   */
  updatePayBill: function (billId, status, reason, type) {
    let that = this;
    Http.postRequest(config.api.updatePayBill, app.globalData.token, '', {
      billId: billId,
      status: status,
      reason: reason
    }, (res) => {
    })
  },
  /**
   * @去支付
   */
  gotopay: function () {
    let that = this;
    wx.showLoading({
      title: '加载中',
    })
    let bUserId = wx.getStorageSync("bUserId") ? wx.getStorageSync("bUserId") : app.globalData.bUserId;
    if (bUserId && that.data.billId && app.globalData.openId && config.weapp.appId) {
      Http.postRequest(config.api.createorder, app.globalData.token, '', {
        bUserId: bUserId,
        billId: that.data.billId,
        openId: app.globalData.openId,
        appId: config.weapp.appId,
        billTypeValue: that.data.billTypeValue,
        merchantId: that.data.merchantId
      }, (res) => {
        console.log(res);
        if (res.code == 200) {
          // that.setData({
          //   payBillId: res.data.payBillId
          // })
          wx.requestPayment({
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            package: res.data.package,
            signType: res.data.signType ? res.data.signType : 'MD5',
            paySign: res.data.paySign,
            success(res) {
              console.log(res);
              wx.hideLoading();
              that.updatePayBill(that.data.billId, 1, res.errMsg, "fail");
              wx.showModal({
                title: '提示',
                content: '支付成功',
                showCancel: false,
                success: function (res) {
                  if (res.confirm) {
                    wx.switchTab({
                      url: '/pages/bill/bill',
                    })
                  }
                }
              })
            },
            fail(res) {
              console.log(res);
              wx.hideLoading();
              that.updatePayBill(that.data.billId, 2, res.errMsg, "cancel");
              wx.showModal({
                title: '支付消息',
                showCancel: false,
                content: "支付取消",
                success: function (res) {
                  console.log(res);
                  wx.switchTab({
                    url: '/pages/bill/bill',
                  })
                },
                fail: function (res) {
                  console.log(res);
                }
              })
            }
          })
        } else {
          wx.hideLoading();
          wx.showModal({
            title: '支付失败',
            showCancel: false,
            content: res.message,
            success: function (res) {
              console.log(res);
              wx.switchTab({
                url: '/pages/bill/bill',
              })
            },
            fail: function (res) {
              console.log(res);
            }
          })
        }
      })
    } else {
      wx.hideLoading();
    }
  }
})