const navigationBarHeight = (getApp().statusBarHeight + 60) + 'px'
const util = require("../../utils/util.js");
const Http = require("../../utils/HttpBasics");
const config = require("../../config/config");
const imgurl = require("../../utils/imgurl");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navigationBarHeight,
    code: ""
  },
  // 兑换
  exchange(e) {
    console.log(e, 'e');
    let that = this;
    let code = e.detail.value.code;
    let formId = e.detail.formId;
    if (!code) {
      wx.showToast({
        title: '请输入兑换码',
        icon: "none",
        duration: 2500
      })
      return;
    }
    that.checkPhoneStatus(code, formId);
  },

  goScanCode() {
    console.log('scan!!!');
    const that = this
    wx.scanCode({
      success: (res) => {
        console.log(res, 'res');
        const num = res.result
        that.setData({
          code: num
        })
        const e = {
          detail:
          {
            value:
            {
              code:
                num
            }
          }
        }
        that.exchange(e)
      },
      fail: (res) => {
        console.log(res, 'fail');
      }
    })
  },

  checkPhoneStatus: function (password, formId) {
    let that = this;
    Http.get({
      url: config.api.checkPhoneStatus,
      data: {}
    })
      .then(res => {
        that.getCouponOrderByPassword(password, formId);
      })
      .catch(err => {
        if (err.code == 11005) {
          /**
           * 手机号没有授权，将值传到用户手机号授权的页面
           *
           */
          wx.redirectTo({
            url: "/pages/getphoneInfo/index",
          })
        } else {
          wx.showToast({
            title: err.message,
            icon: 'none',
            duration: 2500
          })
        }
      })
  },
  getCouponOrderByPassword(password, formId) {
    let that = this;
    Http.post({
      url: config.api.getCouponOrderByPassword,
      data: {
        password: password,
        formId: formId
      }
    })
      .then(res => {
        wx.showModal({
          title: '兑换成功',
          content: '消费卡已发放到"我的卡包"',
          showCancel: true,
          cancelText: "知道了",
          cancelColor: '',
          confirmText: "去查看",
          confirmColor: '#FD832D',
          success: function (res) {
            if (res.cancel) {
              //点击取消,默认隐藏弹框
            } else {
              wx.redirectTo({
                url: '/pages/cardorder/index/index',
              })
            }
          },
          fail: function (res) { },//接口调用失败的回调函数
          complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会执行）
        })
      })
      .catch(err => {
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000
        })
      })
  }
})