const {
  default: api
} = require("../../js/api");
const utils = require('../../utils/util')

Page({
  data: {
    isLoggedIn: false,
    site:{},
    userInfo: {},
    scanTitle: '扫码加油',
    scanTip: '请扫描加油机键盘上的二维码'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  onShow(){
    this.getSiteInfo()
  },

  /** 获取站点信息 */
  getSiteInfo(){
    api.request_GetSiteInfo().then(res => {
      if(res.data.statusCode == 203) {
        console.log("获取站点信息失败",res)
        return
      }
      console.log("站点信息",res)
      var formatePhone = utils.formatPhone(res.data.data.userInfo.userPhoneNumber);
      this.setData({
        isLoggedIn: true,
        site:res.data.data.site,
        userInfo: {
          userAddress:res.data.data.userInfo.userAddress,
          userAvatarUrl:res.data.data.userInfo.userAvatarUrl,
          userPhoneNumber:formatePhone,
          userName:res.data.data.userInfouserName
        },
        scanTitle: '扫码加油',
        scanTip: '请扫码加油机键盘上的二维码'
      })
    }).catch(err => {
      console.log("获取站点信息失败",err)
    })
  },
  toLoginOrOrderPage: function () {
    if (!this.data.isLoggedIn) {
      wx.navigateTo({
        url: '../login/login'
      })
    } else {
      wx.navigateTo({
        url: '../historyOrder/historyOrder?paymentMode=' + this.data.site.paymentMode,
      })
    }
  },

  // 打开扫码功能
  scanCode: function () {
    wx.showLoading({
      title: '正在跳转',
    })
    const that = this;
    wx.scanCode({
      success(res) {
        console.log("扫码内容",res.result)
        const scanStr = res.result;
        if(scanStr.includes('yuwxapp')) {
          const id = scanStr.split('yuwxapp?id=')[1];
          wx.redirectTo({
            url: '../quantify/quantify?id=' + id,
            fail(err){
              wx.showToast({
                title: '跳转页面失败',
                icon: 'none'
              });
            }
          })
          return
        }
        if(scanStr.includes('wxapp')) {
          const id = scanStr.split('wxapp?id=')[1];
          wx.redirectTo({
            url: '../TransactionPage/TransactionPage?id=' + id,
            fail(err){
              wx.showToast({
                title: '跳转页面失败',
                icon: 'none'
              });
            }
          })
        }
        
        
      },
      fail(err) {
        console.error('扫码失败：', err);
        wx.showToast({
          title: '扫码失败',
          icon: 'none'
        });
      }
    });
  }
});