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

  /**
   * 页面的初始数据
   */
  data: {
      page:2,
      imgBefore: 'https://jisu.shenmikj.com/canyin2/Uploads/',

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: 'loading....',
    })
    console.log(options)
    var that=this;
    // let indexSign = options.indexSign;
    // that.setData({
    //   indexSign: indexSign
    // })
    // console.log(indexSign, options,"indexSignindexSignindexSign")
    setTimeout(()=>{
      let userInfo = wx.getStorageSync('userInfo')  // 获取用户信息
      that.setData({ userInfo })
      console.log(userInfo)
      wx.hideLoading();
    },1000)
  
    
    
   
    //我的积分
    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();
    that.getFootimg();
    
    let userInfo = wx.getStorageSync('userInfo')  // 获取用户信息
    that.setData({
      userInfo: userInfo
    })
    console.log(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/center/center?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,"会员等级")
      }
    })
  },


  //获取底部LOGO图片
  getFootimg(){
    var that = this;
    wx.request({
      url: hostname + 'kaiguan/footimg',
      data: { business_id: app.globalData.business_id, openid: wx.getStorageSync("openId")},
      header:{ 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'},
      method:'POST',
      success:function(res){
        console.log(res,"底部logo图")
        let imgData = res.data.data,
        footImg = imgData.img;
        that.setData({
          imgData,
          footImg
        })

      }
    })
  },

  // 获得储蓄卡余额
  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"
    })
  },

// 前往我的订单
  navOrder: function (e) {
    wx.navigateTo({
      url: '../order/order',
    })
  },

  //前往我也想要做小程序
  navProcedure:function(){
    wx.navigateTo({
      url: '../mini/mini',
    })
  },


  //取消或确认提交申请分销
  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 = 'Please enter your name';
      } else if (that.data.applyPhone == undefined || that.data.applyPhone == '') {
        errorContent = 'Please enter your phone';
      } else if (!phoneValid) {
        errorContent = 'Please enter the right phone number';
      }
      if (errorContent) {
        wx.showModal({
          title: 'Reminder',
          content: errorContent,
          confirmText:"OK",
          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) {
       
            wx.showModal({
              title: 'Reminder',
              content: res.data.msg,
              showCancel:false,
              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: 'Tips',
          content: 'Denying authorization may affect some features',
          confirmText: 'Go to set',
          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',
    })
  },

  // 游戏中心
  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;
      // indexSign = that.data.indexSign;
    if (that.data.isSign) {
      wx.showModal({
        title: 'Reminder',
        content: 'Check-In Today',
        confirmText:"OK",
        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: "Successfully Check-In",
          })
        },
      })
      // 再次获取积分
      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) { },
      })
    }
  },
})