// pages/member/distribution/distribution.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    language: '',
    invalid: 0,
    expect: 0,
    unliquidated: 0,
    liquidated: 0,
    not_yet_withdrawed: 0,
    withdrawed: 0,
    manage: 0,
    is_manage: '',
    count: 0,
    distrbution_name: '',
    distrbution_tiem: '',
    first_level: 0,
    second_level: 0,
    third_level: 0,
    userName: '',
    vipId: '',
    userAvatar: '',
    mailLanguage: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    try {
      // 推广中心是否开启该功能，没开启跳转到指定路径
      let mailLanguage = JSON.parse(wx.getStorageSync('mailLanguage'));
      this.setData({ mailLanguage });
      console.log(mailLanguage.commission);
      wx.setNavigationBarTitle({
        title: mailLanguage.commission.agent,
      });
      let basic_info = wx.getStorageSync("yz_basic_info");
      let stop_info = basic_info.popularize_page.mini.vue_route;
      for (let i = 0; i < stop_info.length; i++) {
        if (stop_info[i] == 'distribution') {
          console.log(basic_info.popularize_page.mini.mini_url + "跳转的路径");

          wx.showToast({
            title: '未开启推广权限',
            duration: 1000,
            icon: 'none',
            success: function () {
              setTimeout(() => {
                wx.redirectTo({
                  url: basic_info.popularize_page.mini.mini_url,
                });
              }, 1000);
            }
          });
          return;
        }
      }
    } catch (e) {
      console.log(e);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this._inidata();
    this._setUserInfo(); //设置用户信息
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

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

  },

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

  },

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

  },

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

  },
  //获取分销佣金
  _getDistributionCommissionData() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.commission.api.commission.get-commission");
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setDistributionCommissionData(res.data);
          that.setData({
            invalid: res.data.invalid
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //设置分销佣金
  setDistributionCommissionData(data) {
    this.setData({
      expect: data.expect,
      unliquidated: data.unliquidated,
      liquidated: data.liquidated,
      not_yet_withdrawed: data.not_yet_withdrawed,
      withdrawed: data.withdrawed,
      manage: data.manage
    });
    this.data.is_manage = data.is_manage == "1" ? true : false;
  },
  //获取我的下线总数
  _getAgentCountData() {
    let that = this;
    let urlStr = app.getNetAddresss("member.member.getMyAgentCount");
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setAgentCountData(res.data);
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //设置我的下线总数
  setAgentCountData(data) {
    this.setData({
      count: data.count
    });
  },
  //获取数据
  _getLevelData() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.commission.api.commission.get-agent-level");
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setDistributionInfo(res.data);
          that.setDistributionScaleInfo(res.data);
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //设置分销商 基本信息
  setDistributionInfo(data) {
    this.setData({
      distrbution_name: data.name,
      distrbution_tiem: data.created_at
    });
  },
  //设置分销比例信息
  setDistributionScaleInfo(data) {
    this.setData({
      first_level: data.first_level,
      second_level: data.second_level,
      third_level: data.third_level
    });
  },
  //设置用户信息
  _setUserInfo() {
    var that = this;
    let urlStr = app.getNetAddresss("member.member.getUserInfo");
    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          if (!res.data.is_agent) {
            wx.showModal({
              title: '提示',
              content: '请先申请成为推广员',
              success (res) {
                if (res.confirm) {
                  wx.redirectTo({
                    url: '/packageA/member/extension/ExtensionApply/ExtensionApply'
                  })
                } else if (res.cancel) {
                  wx.navigateBack({
                    delta: 1,
                  });
                }
              }
            })
            return;
          }
          this._getDistributionCommissionData(); //获取分销佣金
          this._getAgentCountData(); //获取我的下线总数
          this._getLevelData(); //获取用户分销商等级
          that.setData({
            userName: res.data.nickname,
            vipId: res.data.uid,
            userAvatar: res.data.avatar
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  gomyRelationship() {
    wx.navigateTo({
      url: '/packageD/member/myRelationship/myRelationship'
    });
  },
  msg() {
    wx.showToast({
      icon: 'none',
      title: '因商品、供应商可以独立设置佣金，您最终获得的佣金可能与此比例不同',
      duration: 3000
    });
  },
  gocommission() {
    wx.navigateTo({
      url: '/packageA/member/extension/commission/commission'
    });
  },
  gounsettled() {
    wx.navigateTo({
      url: '/packageA/member/extension/unsettled/unsettled'
    });
  },
  goalreadySettled() {
    wx.navigateTo({
      url: '/packageA/member/extension/alreadySettled/alreadySettled'
    });
  },
  gonotPresent() {
    wx.navigateTo({
      url: '/packageA/member/extension/notPresent/notPresent'
    });
  },
  gopresent() {
    wx.navigateTo({
      url: '/packageA/member/extension/present/present?type=0'
    });
  },
  goinvalid() {
    wx.navigateTo({
      url: '/packageA/member/extension/present/present?type=1'
    });
  },
  godistributionOrder(e) {
    let idx = e.currentTarget.dataset.idx;
    wx.navigateTo({
      url: '/packageA/member/extension/distributionOrder/distributionOrder?selected=' + idx
    });
  },
  _inidata() {
    this.setData({
      invalid: 0,
      expect: 0,
      unliquidated: 0,
      liquidated: 0,
      not_yet_withdrawed: 0,
      withdrawed: 0,
      manage: 0,
      count: 0,
      distrbution_name: '',
      distrbution_tiem: '',
      first_level: 0,
      second_level: 0,
      third_level: 0,
      userName: '',
      vipId: '',
      userAvatar: '',
    });
    this.data.is_manage = '';
  }

});
