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

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


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    let userInfo = wx.getStorageSync('userInfo')  // 获取用户信息
    that.setData({ userInfo, avatarurl: userInfo.avatarUrl})
    //我的积分
    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.request({
      url: hostname + 'UserOther/get_user_info',
      method: 'POST',
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
      data: {
        openid: wx.getStorageSync('openId'),
        // user_id:wx.getStorageSync("users_id"),
        business_id:app.globalData.business_id
      },
      success:function(res){
        console.log(res,"用户信息")
        if (res.data.status == 1) {
          wx.setStorageSync('userInfo', res.data.data)
        //   console.log(wx.getStorageSync("salesInfo"), "用户信息缓存")
        //   that.setData({
        //     salesInfo: wx.getStorageSync('salesInfo'),
        
        //   })
        }        
      }
      
    })
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that =this;
    that.getStoreInfo()  // 获取商店信息
    that.judgeSign(that);
    that.getvipLevel();
    that.getCardsMoney();
    that.getReCharge();
    let userInfo = wx.getStorageSync('userInfo')  // 获取用户信息
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  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) {
        // 转发失败
      },
    }
  },


  // 获取会员等级
  getvipLevel(){
    let that = this
    var page = that.data.page
    wx.request({
      url: hostname + 'UserOther/get_member_level',
      data:{
        openid:wx.getStorageSync("openId"),
        business_id:app.globalData.business_id,
        page
      },
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
      method:'POST',
      success:function(res){
        console.log(res,"会员等级")
      }
    })
  },

  // 获得储蓄卡余额
  getCardsMoney(){
    let that = this
    wx.request({
      url: hostname + 'Cards/balance',
      data: { 
        openid:wx.getStorageSync("openId"),
        business_id: app.globalData.business_id/*  */
        },
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
      method: 'POST',
      success:function(res){
        console.log(res,"充值余额")
        if(res.data.status == 1 ){
          that.setData({
            balanceInfo: res.data.data
          })
        }
      }
    })
  },

  // 获取充值记录

  getReCharge() {
    let that = this
    var page = that.data.page
    wx.request({
      url: hostname + 'Cards/log',
      data: {
        openid: wx.getStorageSync("openId"),
        business_id: app.globalData.business_id,
        page
      },
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
      method: 'POST',
      success: function (res) {
        console.log(res, "充值记录")
      }
    })
  },

  //姓名
  getName: function (e) {
    var value = e.detail.value;
    this.setData({
      applyName: value,
    })
  },
  //手机号
  getPhone: function (e) {
    var value = e.detail.value;
    this.setData({
      applyPhone: value,
    })
  },

  // 前往积分商城
  goPointsMall() {
    wx.navigateTo({
      url: '../pointsMall/pointsMall',
    })
  },

// 前往分销页面
  seeDownLine: function (e) {
    console.log(e,"分销")
    var that = this;
    var share_status = e.currentTarget.dataset.share_status;
    console.log(share_status);
    if (share_status != 1) {
      that.setData({
        showApply: true,
      })
      return;
    }
    wx.navigateTo({
      url: "../downLine/downLine"
    })
  },


  //取消或确认提交申请分销
  whetherApply: function (e) {
    var that = this;
    var kind = e.currentTarget.dataset.kind;
    if (kind == 'cancle') {
      that.setData({
        showApply: false,
      })
    } else {
      //   var applyData = that.data.applyData;

      var errorContent;
      var reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
      var phoneValid = reg.test(that.data.applyPhone);
      if (that.data.applyName == undefined || that.data.applyName == '') {
        errorContent = '请输入姓名';
      } else if (that.data.applyPhone == undefined || that.data.applyPhone == '') {
        errorContent = '请输入电话';
      } else if (!phoneValid) {
        errorContent = '请输入正确的手机号码';
      }
      if (errorContent) {
        wx.showModal({
          title: '温馨提示',
          content: errorContent,
          showCancel: false,
        })
        return false;
      }
      // console.log(applyData,'applyData');
      // return false;
      wx.request({
        url: hostname + 'ShareLogic/share_apply',
        data: {
          openid:wx.getStorageSync("openId"),
          user_id:wx.getStorageSync("users_id"),
          name: that.data.applyName,
          phone: that.data.applyPhone
        },
        method: 'POST',
        header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
        success: function (res) {
          if (res.data.code == 200) {
            wx.showToast({
              title: '已提交申请',
              duration: 1500,
              success: function () {
                that.setData({
                  showApply: false,
                })
              }
            })
          }
        },
        fail: function (res) { },
        complete: 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,
    })
  },
  // 会员充值
  navrecharge() {
    wx.navigateTo({
      url: '../recharge/recharge'
    })
  },
  // 地址管理中心
  navAddressCenter() {
    wx.chooseAddress({
      fail: function (chooseRes) {
        wx.showModal({
          title: '提示',
          content: '拒绝授权可能会影响部分功能使用，请删除小程序或设置授权',
          confirmText: '去设置',
          success: res => {
            if (res.confirm) {
              wx.openSetting({

              })
            }
          }
        })
      },
    })
  },
  // 优惠券中心
  navcoupon(){
    wx.navigateTo({
      url: '../coupon/coupon'
    })
  },
  // 招商加盟
  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',
    })
  },
  // 游戏中心
  navgames(){
    wx.navigateTo({
      url: '../game_area/game_area',
    })
  },
  // 推客记录
  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) { },
      })
    }
  },
})