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,
    tabIndex: 0,
    pdwSwitch: 0,
    isShowPwd: false,
    isChangePhone: false,
    isSHowInfoCard: false,
    code: "",
    title: "",
    name: "",
    phone: "",
    tempPhone: "",
    password: "",
    supportTransfer: "",
    isPhone: ""
  },

  // 查询
  searchCard(e) {
    if (!this.data.isPhone) {
      wx.navigateTo({
        url: '/pages/getphoneInfo/index?path=exchangeCard',
      })
      return
    }
    let code = e.detail.value.code;
    this.setData({
      code: code
    })
    this.getCardDetailOrGoGive(code)
  },

  // 更改手机号
  changePhone() {
    this.setData({
      isChangePhone: true,
      tempPhone: this.data.phone
    })
  },

  // 输入手机号
  phoneInput(e) {
    this.setData({
      phone: e.detail.value
    })
  },

  // 输入密码
  pwdInput(e) {
    this.setData({
      password: e.detail.value,
    })
  },

  // 确认手机号更改
  confirmPhone() {
    const phoneReg = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/
    const phoneValid = phoneReg.test(this.data.phone)
    if (!phoneValid) {
      this.Toast('请输入正确的手机号！', 'none')
      return
    }
    this.setData({
      isChangePhone: false,
    })
  },

  // 取消更改
  cancelChangePhone() {
    this.setData({
      isChangePhone: false,
      phone: this.data.tempPhone
    })
  },

  // 更改tab栏
  changeTabs(e) {
    console.log(e, 'changeTabs');
    this.setData({
      tabIndex: e.target.id,
      code: '',
      password: '',
      isSHowInfoCard: false
    })
  },

  // 支付安全设置
  securityChange(e) {
    this.setData({
      pdwSwitch: e.detail.value * 1
    })
  },

  codeInput(e) {
    console.log(e);
    this.setData({
      code: e.detail.value
    })
  },

  // 扫描二维码
  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.searchCard(e)
      },
      fail: (res) => {
        console.log(res, 'fail');
      }
    })
  },

  /** 根据卡id查询卡详情*/
  getCardDetailOrGoGive(cardId, goGive) {
    const that = this
    Http.get({
      url: config.api.getCardDetail,
      data: { cardId }
    })
      .then(res => {
        console.log(res, 'res');
        // 取不到时清空参数
        that.setData({
          title: res.data.title || "",
          phone: res.data.ownerPhone || "",
          hidePhone: res.data.ownerPhone ? (res.data.ownerPhone.slice(0, 3) + `****` + res.data.ownerPhone.slice(7)) : '',
          ownerUserId: res.data.ownerUserId || "",
          eCardId: res.data.eCardId || "",
          owned: res.data.owned || "",
          remainAmount: res.data.remainAmount / 100 || "",
          isSHowInfoCard: true, // 显示卡详情
          supportTransfer: res.data.supportTransfer || ""
        })
        if (goGive) {
          if ((this.data.ownerUserId && this.data.eCardId) || this.data.code) {
            if ((this.data.owned && this.data.owned == 1) || this.data.code) {
              if (this.data.supportTransfer == 1) {
                wx.navigateTo({
                  url: `/pages/ConsumeDetail/ConsumeDetail?cardId=${this.data.eCardId}`,
                })
              } else {
                wx.showToast({
                  title: '该卡不支持转赠！',
                  icon: 'none'
                })
              }
            } else {
              this.Toast('当前用户不是该卡的持有者，无法转赠！', 'none')
            }
          } else {
            this.Toast('该卡未绑定', 'none')
          }
        }
      })
      .catch(err => {
        console.log(err);
        this.Toast(err.message, 'none')
      })
  },

  getCouponOrderByPassword(password) {
    const that = this
    Http.post({
      url: config.api.getCouponOrderByPassword,
      data: {
        password,
        payCheck: that.data.pdwSwitch,
        payPassword: that.data.password
      }
    })
      .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
        })
      })
  },

  setPwdShow() {
    const isShowPwd = this.data.isShowPwd
    this.setData({
      isShowPwd: !isShowPwd
    })
  },

  submit() {
    if (!this.data.isPhone) {
      wx.navigateTo({
        url: '/pages/getphoneInfo/index?path=exchangeCard',
      })
      return
    }
    if (this.data.pdwSwitch) {
      const password = this.data.password
      const numReg = /^\d+$/

      if (!password) {
        wx.showToast({
          title: '密码不能为空！',
          icon: 'none'
        })
        return
      }

      if (!numReg.test(password) || password.length != 6) {
        wx.showToast({
          title: '密码只能为6位数字！',
          icon: 'none'
        })
        return
      }
    }
    const e = {
      detail: { value: { code: this.data.code } }
    }
    this.searchCard(e)
    this.getCouponOrderByPassword(this.data.code)
  },

  goGive() {
    if (!this.data.isPhone) {
      wx.navigateTo({
        url: '/pages/getphoneInfo/index?path=exchangeCard',
      })
      return
    }
    this.getCardDetailOrGoGive(this.data.code, true)
  },

  // 检查用户登录状态
  checkUserPhone() {
    const that = this
    Http.get({
      url: config.api.checkPhoneStatus
    }).then(res => {
      that.setData({
        isPhone: true
      })
      console.log(this.data.isPhone, 'isPhone');
    }).catch(err => {
      that.setData({
        isPhone: false
      })
      console.log(this.data.isPhone, 'isPhone');
    })
  },

  Toast(message, icon) {
    wx.showToast({
      title: message,
      icon: icon
    })
  },

  onLoad() {
    this.checkUserPhone()
  },

  onShow() {
    this.checkUserPhone()
  }
})
