// pages/user/user.js

var http = require("../../utils/http.js");
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopId: 0,
    orderAmount: {},
    sts: '',
    collectionCount: 0,
    canDistribution: true, //分销开关
    isAuthInfo: true,
    shopAuditStatus: null, //店铺审核状态：0 未审核 1已通过 -1未通过 null 未开店
    shopStatus: null, // 店铺运营状态: -1:未开通 0: 停业中 1:营业中 2:平台下线 3:平台下线待审核
    isSetPassword: false, //是否已经设置过支付密码
    loginResult: {},
    shopName:'',
  },

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

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      isAuthInfo: Boolean(wx.getStorageSync('loginResult').userId),
      // isAuthInfo: Boolean(wx.getStorageSync('loginResult').nickName),
      loginResult: wx.getStorageSync('loginResult')
    })
    // 查看是否授权，然后加载订单数据
    util.checkAuthInfo(this._loadOrderCount, true)
    util.checkAuthInfo(this.getUserInfo, true)
    this.getUserLevelInfo()
    this.getDistInfo()
  },
  // 加载订单数字
  _loadOrderCount() {
    var ths = this;
    wx.showLoading();
    var params = {
      url: "/p/user/centerInfo",
      method: "GET",
      dontTrunLogin: true,
      data: {},
      callBack: (res) => {
        wx.hideLoading();
        ths.setData({
          shopAuditStatus: res.shopAuditStatus,
          shopStatus: res.shopStatus,
          isSetPassword: res.isSetPassword,
          orderAmount: res.orderCountData,
          shopId: res.shopId
        });
      }
    };
    http.request(params);
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.setData({
      orderAmount: {}
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  /**
   * 更新个人信息
   */
  toPersonalInfo(e){
    if (e.detail.errMsg != "getUserInfo:fail auth deny") {
      http.updateUserInfo();
    }
  },

  /**
   * 查询分销相关信息
   */
  getDistInfo() {
    //查询分销开关是否开启
    http.request({
      url: "/p/distribution/distributionBasicSet/canDistribution",
      method: "GET",
      dontTrunLogin: true,
      // data: {
      //   shopId: 1,
      // },
      callBack: (res) => {
        this.setData({
          canDistribution: res == 1
        });
      }
    })
  },

  /**
   * 跳转分销员中心
   */
  toDistCenter: function() {
    if (!this.data.isAuthInfo){
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return
    }
    http.request({
      url: "/p/distribution/user/distributionUserInfo",
      method: "GET",
      callBack: (res) => {
        console.log(res)
        if (res && res.state == 1) {
          // wx.setStorageSync("isDister", true);
          wx.setStorageSync("distCardNo", res.cardNo);
          wx.setStorageSync('distInfo', res)
          wx.navigateTo({
            url: '/pages/dis-center/dis-center',
          })
        } else if (res && res.state == 0) {
          wx.showToast({
            title: '您提交的申请正在审核中',
            icon: "none"
          })
        } else if (res && res.state == 3) {
          wx.showModal({
            title: '',
            content: '您的申请未通过，具体原因请咨询客服，重新进行申请？',
            confirmColor: "#eb2444",
            success(res2) {
              if (res2.confirm) {
                wx.navigateTo({
                  url: '/pages/applyDist/applyDist',
                })
              } else if (res2.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else if (res && res.state == -1) {
          wx.showModal({
            title: '',
            content: '您的分销员身份已被封禁，具体原因请咨询客服',
            confirmColor: "#eb2444",
            success(res2) {
              if (res2.confirm) {
                console.log('用户点击确认')
              } else if (res2.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else if (res && res.state == 2) {
          wx.showModal({
            title: '',
            content: '您的分销员身份已被清退，具体原因请咨询客服',
            confirmColor: "#eb2444",
            success(res2) {
              if (res2.confirm) {
                console.log('用户点击确认')
              } else if (res2.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else {
          wx.navigateTo({
            url: '/pages/applyDist/applyDist',
          })
        }
      }
    })
    // }

  },


  toAfterSalesPage: function() {
    if (!this.data.isAuthInfo) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return
    }
    wx.navigateTo({
      url: '/pages/afterSales/afterSales',
    })
  },

  //积分中心
  toPointsCenter: function() {
    if (!this.data.isAuthInfo) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return
    }
    wx.navigateTo({
      url: '/packageMemberIntegral/pages/memberIndex/memberIndex',
    })
  },

  toCouponCenter: function() {
    if (!this.data.isAuthInfo) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return
    }
    wx.navigateTo({
      url: '/pages/couponCenter/couponCenter',
    })
  },

  toMyCouponPage: function() {
    if (!this.data.isAuthInfo) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return
    }
    wx.navigateTo({
      url: '/pages/myCoupon/myCoupon',
    })
  },

  toAddressList: function() {
    if (!this.data.isAuthInfo) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return
    }
    wx.navigateTo({
      url: '/pages/delivery-address/delivery-address',
    })
  },
  // 跳转账号设置
  goSetting: function() {
    if (!this.data.isAuthInfo) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return
    }
    wx.navigateTo({
      url: '/pages/setting/setting',
    })
  },
  // 跳转绑定手机号
  toBindingPhone: function() {
    wx.navigateTo({
      url: '/pages/binding-phone/binding-phone',
    })
  },
   /**
   * 获取用户信息
   */
  getUserInfo: function() {
    var that = this;

    var param = {
      url: '/p/user/userInfo',
      method: 'GET',
      data: {},
      callBack: (res) => {
        that.setData({
          shopName: res.enterprise || ''
        })
        res.channelId&&wx.setStorageSync('channelId',res.channelId)
      }
    }
    http.request(param)
  },

  toOrderListPage: function(e) {
    console.log(this.data.isAuthInfo);
    if (!this.data.isAuthInfo) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return
    }
    var sts = e.currentTarget.dataset.sts;
    wx.navigateTo({
      url: '/pages/orderList/orderList?sts=' + sts,
    })
  },

  /**
   * 查询所有的收藏量
   */
  showCollectionCount: function() {
    var ths = this;
    wx.showLoading();
    var params = {
      url: "/p/user/collection/count",
      method: "GET",
      data: {},
      callBack: function(res) {
        wx.hideLoading();
        ths.setData({
          collectionCount: res
        });
      }
    };
    http.request(params);
  },

  /**
   * 我的收藏跳转
   */
  myCollectionHandle: function() {
    if (!this.data.isAuthInfo) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return
    }
    var url = '/pages/prod-classify/prod-classify?sts=5';
    var id = 0;
    var title = "我的收藏商品";
    if (id) {
      url += "&tagid=" + id + "&title=" + title;
    }
    wx.navigateTo({
      url: url
    })
  },

  /**
   * 获取用户信息
   */
  onGotUserInfo: function(e) {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },



  /**
   * 跳转到账号设置
   */
  settingAccout: function() {
    if (!this.data.isAuthInfo) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return
    }
    wx.navigateTo({
      url: '/pages/accountSettings/accountSettings',
    })
  },
  //跳转修改店铺密码
  alterPassword: function() {
    if (!this.data.isAuthInfo) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return
    }
    wx.navigateTo({
      url: '/pages/alterShopPassword/alterShopPassword',
    })
  },

  /**
   * 跳转到我的店铺
   */
  enterMyShop: function() {
    if (!this.data.isAuthInfo) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return
    }
    if (!this.data.shopId || this.data.shopStatus != 1) {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '店铺状态异常，请联系管理员',
      })
    } else {
      wx.navigateTo({
        url: '/pages/shopPage/shopPage?shopId=' + this.data.shopId,
      })
    }
  },

  /**
   * 获取会员积分详情
   */
  getUserLevelInfo(){
    var params = {
      url:'/p/score/scoreInfo',
      method:'GET',
      dontTrunLogin: true,
      data:{
        channelId:wx.getStorageSync('channelId')||3,
      },
      callBack: res => {
        this.setData({
          userLevelInfo: res
        })
      }
    }
    http.request(params)
  },

  /**
   * 跳转购买会员
   */
  toBuyMember(){
    util.checkAuthInfo(()=>{
      wx.navigateTo({
        url: '/packageMemberIntegral/pages/buyVip/buyVip',
      })
    })
  },

  /**
   * 跳转积分中心
   */
  toMemberInteral(){
    util.checkAuthInfo(()=>{
      wx.navigateTo({
        url: '/packageMemberIntegral/pages/integralIndex/integralIndex',
      })
    })
  },

   /**
   * 跳转我的评价
   */
  toReview(){
    util.checkAuthInfo(()=>{
      wx.navigateTo({
        url: '/pages/Review/Review',
      })
    })
  },

  

  /**
	 * 退出登录
	 */
  logout: function () {
    wx.clearStorage()
    wx.removeTabBarBadge({
      index: 1
    });
    wx.reLaunch({
      url: "/pages/login/login"
    })
  }


})
