const util = require('../../utils/util.js');
const app = getApp();
Page({
  data: {
    info:{
      sharerInfo:{}
    },
    sharerLevelInfo:{},
    shareStatus:0,
    showPopup:false,
    swipeList:[],
    organStatus:0
  },
  onLoad(options){
    console.log(options)
    if (options) {
   
      if (options.shareStatus) {
        this.userInfo()
    this.onShareAppMessage()
        this.setData({shareStatus:options.organStatus})
      }else{
        this.userInfo()
    this.onShareAppMessage()
        this.setData({organStatus:options.organStatus})
      }
    }
  },
  onShow: function () {
    this.userInfo()
    this.onShareAppMessage()
  },
  // 获取提现轮播
  getPriceSwipe(){
    let that=this;
    util.ajax({
      func:'v2/user/coin/message',
      data:{type:this.data.info.roleType=='sharer'?1:(this.data.info.roleType=='organ'?2:0)},
      load:false
    },function (res) {
      if (res.code==0) {
        if (res.data&&res.data.length>0) {
          that.setData({swipeList:res.data})
        }
      } else {
        util.showTips(res.reason);
      }
    })
  },
  //右上角分享功能
  onShareAppMessage: function (res) {

    var that = this;
    let flag=0
    let imgurl=""
    let path=""
    // 0代表普通用户 1代表是机构 2代表是分享家
    console.log(that.data.organStatus);
    if(that.data.organStatus){
      flag=1
    }else{
      flag=2
    }
     if(this.data.type=="organ"){
      imgurl="https://img.bbztx.com/image_test/upload/thumbs/20221024/logo/1666598262608021902.jpg"
      path=`pages/sharingHome/index?organStatus=${1}`
    }else if(this.data.type=="sharer"){
      imgurl="https://img.bbztx.com/image_test/upload/thumbs/20221024/logo/1666598584892027021.jpg"
      path=`pages/sharingHome/index?organStatus=${2}`
    }
  
    let flags=false
    return {
      title: `${that.data.organStatus?'机构提现中心':'分享家提现中心'}`,
      // path: path,  //要跳转的路径
      path:`/pages/home/account?flags=`+flags,
      imageUrl:imgurl,   //自定义图片路径
      success: function (res) {
        console.log(res);
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },



  // 显示分享家停用弹窗
  showInstract(){
    this.setData({showPopup:true})
  },
  closePopup(){
    this.setData({showPopup:false})
  },
  userInfo(){
    let that = this;
    util.ajax({
      func: "v2/user/info",
      load: false
    }, function (res) {
      console.log(res);
      if (res.code == 0 && res.data) {
        that.setData({info:res.data})
        that.coinStat();
        if (res.data.roleType=='organ'||res.data.roleType=='sharer') {
          that.getPriceSwipe()
        }
        if (res.data.roleType=='sharer') {
          that.sharerLevelInfo()
        }
        // if(res.data.roleType=="normal"){
        //      wx.navigateTo({
        //        url: '/pages/public/swiper/web?url=https://mp.weixin.qq.com/s/H8WuXW_VeCeWtxQXFBGu2Q&title=宝贝走天下',
        //      })
        // }
        // 普通用户进行跳转
        // if(res.data.roleType=="normal"){
          
        // }

        that.setData({
          type:res.data.roleType
        })
        that.inviteeStat(res.data.roleType);
        that.activityStat(res.data.roleType);

      }
    });
  },
  // 用户登录
  getUserProfile(){
    let that = this;
    this.setData({clickTimes:true})
    wx.login({
      success: res => {
        that.setData({code:res.code})
      }
    });
    that.userProfile();
  },
  userProfile(){
    let that=this;
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中
      success: (res) => {
        console.log(res);
        let it = {};
        if (!res.encryptedData && !res.iv) {
          it.version="applet_v1.0.0";
          it.code=that.data.code;
          it.target='challengerhome';
          it.avatarUrl=res.userInfo.avatarUrl;
          it.nickName=res.userInfo.nickName
        }else{
          it.version="applet_v1.0.0";
          it.code=that.data.code;
          it.target='challengerhome';
          it.encryptedData=res.encryptedData;
          it.iv=res.iv;
        }
        util.ajax({
          func: "applet_login",
          data: it
        }, function (data) {
          if (data.code == 0) {
            if (data.data.firstLogin == 1) {
              that.setData({ tempUser: data.data, firstLogin: 1 });
            } else {
              data.data.role = util.userRole(Math.round(data.data.score));
              app.globalData.userInfo = data.data;
              wx.setStorageSync("WXuserInfo", data.data);
              if (that.data.type == "share") {
                wx.reLaunch({
                  url: '/pages/home/index',
                });
              } else {
                wx.navigateBack();
              }
            }
          } else{util.showTips(data.reason);}
          that.setData({clickTimes:false})
        });
      }
    });
  },
  // 获取佣金
  coinStat(){
    let that = this;
    util.ajax({
      func: "v2/user/coin/stat",
      data:{type:0},
      load: false
    }, function (res) {
      if (res.code == 0 && res.data) {
        that.setData({coin: res.data})
      }
    });
  },
  // 直接会员、直接分享家、间接会员
  inviteeStat(roleType){
    let that = this;
    util.ajax({
      func: `v2/${roleType}/invitee/stat`,
      load: false
    }, function (res) {
      if (res.code == 0 && res.data) {
        that.setData({invitee: res.data})
      }
    });
  },
  // 获取分享家等级信息
  sharerLevelInfo(){
    let that = this;
    util.ajax({
      func: "v2/user/coin/sharerLevel",
      data:{levelMax:that.data.info.sharerInfo.levelMax},
      load: false
    }, function (res) {
      console.log(res);
      if (res.code == 0) {
        let data=res.data;
        if (that.data.info.sharerInfo.levelMax<5) {
          data.percent1=(data.level_exp1/data.levelPlan1)*100==100?'calc(100% - 14rpx)':(data.level_exp1/data.levelPlan1)*100 +'%';
          data.percent2=(data.level_exp2/data.levelPlan2)*100==100?'calc(100% - 14rpx)':(data.level_exp2/data.levelPlan2)*100 +'%';
        } else {
          data.percent1=0;
          data.percent2=0;
        }
        data.scaleCurrent=(data.scaleCurrent*100).toFixed()
        data.scaleNext=(data.scaleNext*100).toFixed()
        
        that.setData({sharerLevelInfo:data})
      }
    });
  },
  // 分销产品
  activityStat(roleType){
    let that = this;
    util.ajax({
      func: `v2/${roleType}/product/stat`,
      load: false
    }, function (res) {
      if (res.code == 0 && res.data) {
        that.setData({product: res.data})
      }
    });
  },
  // 跳转规则说明页
  jumptoIns(e){
    let url1='https://mp.weixin.qq.com/s?__biz=MzkzNjE5NTU3Nw==&mid=2247490779&idx=1&sn=0a3ae0e62986c5e21f62892df8058a0e&chksm=c2a3384df5d4b15b3ddce0bea8d877fb04c4e94dc0f1721e2b311943cf0137b3c781dc980bbd&token=458771444&lang=zh_CN#rd',
    url2="https://mp.weixin.qq.com/s?__biz=MzkzNjE5NTU3Nw==&mid=2247490766&idx=1&sn=cca6ce9fd52dd9182d37831a551bac67&chksm=c2a33858f5d4b14ea52efb062b4dbab17adbc29307f67e74792d44d1d7369b43bb30e6a5829d#rd",
    type=e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/pages/public/swiper/web?url='+encodeURIComponent(this.data.info.roleType=='sharer'?url1:url2)+'&title='+(this.data.info.roleType=='sharer'?(type==0?'分享家规则说明':'分享家晋级说明'):'机构规则说明'),
    })
  },
  navigatorURl(e){
    util.navigator(e.currentTarget.dataset.url);
  },
})