// center.js
const app = getApp(),
  { hostname, business_id } = app.globalData
Page({

  /**
   * 页面的初始数据
   */
  data: {
   

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    let userInfo = wx.getStorageSync('userInfo')  // 获取用户信息
    that.setData({ userInfo })
    //我的积分
    wx.request({
      url: app.globalData.hostname + 'Int/get_my_int',
      method: 'POST',
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
      data: {
        openid: wx.getStorageSync('openId'),business_id
      },
      success: function (res) {
        console.log(res);
        that.setData({
          integralData: res.data.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that =this;
    that.getStoreInfo()  // 获取商店信息
    that.judgeSign(that);
  },

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

  },

  /**
     * 用户点击右上角分享
     */
  onShareAppMessage: function (res) {
    var userid = wx.getStorageSync('openId')
    // var userid = 1

    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: app.globalData.share_title,
      path: '/pages/index/index?u_openid=' + userid,
      success: function (res) {
        var pages = getCurrentPages()    //获取加载的页面
        var currentPage = pages[pages.length - 1]    //获取当前页面的对象
        //点击分享传进userid 截取userid 从onload拿到
        var url = currentPage.route + '?u_openid=' + userid;  //当前页面url
        console.log(url, '/pages/index/index?u_openid=' + userid);
      },
      fail: function (res) {
        // 转发失败
      },
    }
  },

  

  // 获取商店信息
  getStoreInfo() {
    let that = this
    wx.request({
      url: hostname + 'Goods/get_business_info',
      data: { business_id },
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
      method: 'POST',
      dataType: 'JSON',
      success: function (res) {
        let storeInfo = JSON.parse(res.data).data
        that.setData({ storeInfo })
        wx.setStorageSync('address', storeInfo.business_address)
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 联系商家
  contactStore() {
    console.log(this.data.storeInfo)
    let { business_phone } = this.data.storeInfo
    wx.makePhoneCall({
      phoneNumber: business_phone,
    })
  },
  // 招商加盟
  navAbout() {
    wx.navigateTo({
      url: '../about/about'
    })
  },
  // 地址导航
  navAddress() {
    wx.navigateTo({
      url: '../navPage/navPage',
    })
  },
  // 资讯信息
  navInfo() {
    wx.navigateTo({
      url: '../Information/Information?business_id=' + app.globalData.business_id
    })
  },
  // 更多门店
  navStores(){
    wx.switchTab({
      url: '../ManyStore/ManyStore',
    })
  },
  // 我的订单
  navOrder() {
    wx.switchTab({
      url: '../order/order',
    })
  },
  // 大转盘
  navwheel(){
    wx.navigateTo({
      url: '../luckydraw/luckydraw',
    })
  },
  // 推客记录
  navreferer() {
    wx.navigateTo({
      url: '../pushoff/pushoff',
    })
  },
  // 推客记录
  navintegral() {
    wx.navigateTo({
      url: '../order2/order2',
    })
  },

  // 签到
  sign: function () {
    var that = this;
    if (that.data.isSign) {
      wx.showModal({
        title: '温馨提示',
        content: '今天您已签到',
        showCancel: false,
      })
    } else {
      wx.request({
        url: app.globalData.hostname + 'User/UserSignIn',
        method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded'},
        data: { 
                openid: wx.getStorageSync('openId'), 
                business_id: business_id
              },
        dataType: 'json',
        success: function (signRes) {
          that.setData({
            isSign: true,
          })
          wx.showToast({
            title: signRes.data.msg,
          })
        },
      })
      // 再次获取积分
      wx.request({
        url: app.globalData.hostname + 'Int/get_my_int',
        method: 'POST',
        header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
        data: {
          openid: wx.getStorageSync('openId'), business_id
        },
        success: function (res) {
          console.log(res);
          that.setData({
            integralData: res.data.data
          })
        }
      })
    }

    // wx.navigateTo({
    //   url: 'sign/sign',
    // })
  },
  //判断是否已签到
  judgeSign: function (that) {
    if (!that.data.isSign) {
      wx.request({
        url: app.globalData.hostname + 'User/CheckUserSignIn',
        method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        data: {
          openid: wx.getStorageSync('openId'),
          business_id: business_id
        },
        success: function (judgeRes) {
          console.log(judgeRes);
          that.setData({
            isSign: judgeRes.data.status == -2 ? true : false //待定
          })
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  },
})