// packageF/wxFriendExtend/workWxFriendExtend/workWxFriendExtend.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTime: new Date().getTime(),
    timeData: "",
    rewardFlag: false,
    details: null,
    activity_id: 0,
    progressRules: [],
    ruleLadder: [],
    stepReward: {},
    basic_info: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let basic_info = wx.getStorageSync("yz_basic_info");
    this.setData({
      basic_info
    });
    if (options.id) {
      this.setData({
        activity_id: options.id
      });
    }
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      var info_arr = [];
      info_arr = scene.split("&");
      for (let i = 0; i < info_arr.length; i++) {
        let chil_arr = [];
        chil_arr = info_arr[i].split("=");
        if (chil_arr[0] == "id") {
          this.setData({
            activity_id: chil_arr[1]
          });
        }
        if (chil_arr[0] == "mid") {
          app._setMid(chil_arr[1]);
        }
      }
    }

    if (options.mid) {
      app._setMid(options.mid);
    }
    this._getData();
  },

  setStepReward(evt) {
    let item = evt.currentTarget.dataset.item;
    this.setData({
      stepReward: item,
      rewardFlag: true
    });
  },
  setRewardFlag() {
    this.setData({
      rewardFlag: !this.data.rewardFlag
    });
  },

  _getData() {
    let urlStr = app.getNetAddresss("plugin.customer-increase.frontend.activity.activityDetail");
    app._postNetWork({
      url: urlStr,
      data: {
        id: this.data.activity_id
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        this.setData({
          details: res.data
        });
        this.setProgressData(res.data.step_rule);
      },
    });
  },

  // 活动进度
  setProgressData(rules) {
    let activity = this.data.details;
    let isFinished =  rules.some(item => activity.invite_count < item.step_people);
    let ruleLadder = JSON.parse(JSON.stringify(rules));
    
    ruleLadder.unshift({
      step_level: 0,
      step_people: isFinished?ruleLadder[0].step_people:0
    });
    this.setData({
      ruleLadder: ruleLadder
    });

    let ruleList = JSON.parse(JSON.stringify(rules));
    let addRule = {
      positionLeft: "0%",
      step_people: isFinished?activity.invite_count:0,
      step_level: 0
    };
    ruleList.unshift(addRule);

    if (ruleList.length >= 3) {
      // 后台设置超过三个进度阶梯
      if (activity.step == ruleList.length - 1) {
        ruleList = ruleList.slice(-3);
      } else if (activity.step == 0) {
        ruleList = ruleList.slice(0, 3);
      } else {
        if (ruleList.length - activity.step >= 3) {
          ruleList = ruleList.slice(activity.step, 3 + activity.step);
        } else if (ruleList.length - activity.step <= 2) {
          let diff = 3 - (ruleList.length - activity.step);
          ruleList = ruleList.slice(activity.step - diff, activity.step - diff + 3);
        }
      }
    }

    this.setData({
      progressRules: ruleList
    });

  },

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

  },

  // 计时器
  onChangeTime(e) {
    this.setData({
      timeData: e.detail,
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

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

  },

  gotoReward() {
    wx.navigateTo({
      url: '/packageF/wxFriendExtend/friendRewardRecord/friendRewardRecord',
    });
  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    if (!this.data.details) return {};
    let json = {
      title: this.data.details.share_title ? this.data.details.share_title : this.data.details.name,
      path: "/packageF/wxFriendExtend/workWxFriendExtend/workWxFriendExtend?id=" + this.data.details.id
    };
    if (this.data.details.share_logo_url) {
      json.imageUrl = this.data.details.share_logo_url;
    }
    return json;
  }
});