// pages/member/rankingListSecond/rankingListSecond.js
var app = getApp();
var WxParse = require("../../../../wxParse/html2json.js");
// const RECORD_URL = "plugin.scoring-dividend.api.scoring-dividend.scoring-record";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabOption: ["关注会员", "加盟店"],
    currentIndex: 0,
    allMemberShow: true,

    buyMemberShow: false,

    buyList: [],

    allMember: [],

    title: "",

    // allMember more
    m_loading: false,
    m_allLoaded: false,
    m_goload: true,
    m_isLoadMore: true,
    m_page: 1,
    m_total_page: 0,

    // buyList more
    b_loading: false,
    b_allLoaded: false,
    b_goload: true,
    b_isLoadMore: true,
    b_page: 1,
    b_total_page: 0,

    // menber总数
    m_total: 0,

    b_total: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.checkrouter();
    this.init();
    this.getBuyData();
    this.getMemberData();

    this.getTitle();
  },
  checkrouter() {
    try {
      // 推广中心是否开启该功能，没开启跳转到指定路径
      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] == "ExtensionPage") {
          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 () {},

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

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

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

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if (this.data.m_isLoadMore) {
      this.getMoreData("member", this.data.m_page);
    } else {
      wx.showToast({
        title: "没有更多数据",
        icon: "none",
      });
    }
    if (this.data.b_isLoadMore) {
      this.getMoreData("buy", this.data.b_page);
    } else {
      wx.showToast({
        title: "没有更多数据",
        icon: "none",
      });
    }
  },

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

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

  // 获取数据
  tabChoose(val) {
    let tag = val.target.dataset.index || val.currentTarget.dataset.index;
    if (tag == 1) {
      this.setData({
        allMemberShow: true,
        buyMemberShow: false,
      });
    } else if (tag == 2) {
      this.setData({
        allMemberShow: false,
        buyMemberShow: true,
      });
    } else {
      return;
    }
  },
  init() {
    this.initData("member");
    this.initData("buy");
  },
  getBuyData() {
    let urlStr = app.getNetAddresss("plugin.member-return.frontend.ranking.index");
    urlStr += "&page=" + this.data.b_page;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          this.setData({
            buyList: resdata.data.data,
          });
          var buyList = this.data.buyList;
          buyList.forEach((e, i) => {
            let sum = "";
            for (let i = 0; i < e.star; i++) {
              sum += "<icon class='iconfont icon-extension-stars icon-custom-undefined' style='font-size: 1.25rem; color: rgb(241, 83, 83);'></icon>";
            }

            e.star_count = sum;
            e[i]["star_count"] = WxParse.html2json(e[i]["star_count"], "returnData");
          });
          this.setData({
            buyList,
            b_total_page: resdata.data.lastPage || 0,
            b_total: resdata.data.total,
          });
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getMemberData() {
    let urlStr = app.getNetAddresss("plugin.member-return.frontend.follow.index");
    urlStr += "&page=" + this.data.m_page;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          this.setData({
            allMember: resdata.data.data,
          });
          let arr = resdata.data.data;

          arr.forEach((e, index) => {
            if (app._isTextEmpty(e.has_one_queue)) {
              e.star_count = "";
            } else {
              let sum = "";
              for (let i = 0; i < e.has_one_queue.star; i++) {
                sum += "<icon class='iconfont icon-extension-stars icon-custom-undefined' style='font-size: 1.25rem; color: rgb(241, 83, 83);'></icon>";
              }
              e.star_count = sum;
              e[index]["star_count"] = WxParse.html2json(e[index]["star_count"], "returnData");
            }
          });
          this.setData({
            allMember: arr,
            m_total_page: resdata.data.lastPage,
            m_total: resdata.data.total,
          });
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getMoreData(tag, page) {
    if (tag == "member") {
      if (this.data.m_page == this.data.m_total_page) {
        return;
      }

      if (this.data.m_page >= this.data.m_total_page) {
        this.setData({
          m_loading: true,
          m_allLoaded: true,
        });
        return;
      } else {
        let page = this.data.m_page + 1;
        this.setData({
          m_page: page,
        });
        let urlStr = app.getNetAddresss("plugin.member-return.frontend.follow.index");
        urlStr += "&page=" + this.data.m_page;
        app._getNetWork({
          url: urlStr,
          success: (res) => {
            let resdata = res.data;
            if (resdata.result === 1) {
              let allMember = this.data.allMember.concat(resdata.data.data);
              this.setData({
                m_loading: false,
                m_allLoaded: false,
                allMember,
              });
              allMember.forEach((e, index) => {
                if (app._isTextEmpty(e.has_one_queue)) {
                  e.star_count = "";
                } else {
                  let m_sum = "";
                  for (let i = 0; i < e.has_one_queue.star; i++) {
                    m_sum += "<i class='iconfont icon-extension-stars icon-custom-undefined' style='font-size: 1.25rem; color: rgb(241, 83, 83);'></i>";
                  }
                  e.star_count = m_sum;
                  e[index]["star_count"] = WxParse.html2json(e[index]["star_count"], "returnData");
                }
              });
              this.setData({
                allMember,
              });
            } else {
              wx.showToast({
                title: resdata.msg,
                icon: "none",
              });
              var m_page = this.data.m_page - 1;
              this.setData({
                m_page,
                m_loading: true,
                m_allLoaded: true,
                m_isLoadMore: false,
              });
              return;
            }
          },
          fail: function (res) {
            console.log(res);
          },
        });
      }
    } else if (tag == "buy") {
      if (this.data.b_page == this.data.b_total_page) {
        return;
      }

      if (this.data.b_page >= this.data.b_total_page) {
        this.setData({
          b_loading: true,
          b_allLoaded: true,
        });
        return;
      } else {
        var b_page = this.data.b_page + 1;
        this.setData({
          b_page,
        });
        let urlStr = app.getNetAddresss("plugin.member-return.frontend.ranking.index");
        urlStr += "&page=" + this.data.b_page;
        app._getNetWork({
          url: urlStr,
          success: (res) => {
            let resdata = res.data;
            if (resdata.result === 1) {
              var buyList = this.data.buyList.concat(resdata.data.data);
              this.setData({
                b_loading: false,
                b_allLoaded: false,
                buyList,
              });
              buyList.forEach((e) => {
                if (app._isTextEmpty(e.star)) {
                  e.star_count = "";
                } else {
                  let b_sum = "";
                  for (let i = 0; i < e.star; i++) {
                    b_sum += "<i class='iconfont icon-extension-stars icon-custom-undefined' style='font-size: 1.25rem; color: rgb(241, 83, 83);'></i>";
                  }
                  e.star_count = b_sum;
                }
              });
              this.setData({
                buyList,
              });
            } else {
              wx.showToast({
                title: resdata.msg,
                icon: "none",
              });
              var b_page = this.data.b_page - 1;
              this.setData({
                b_page,
                b_loading: true,
                b_allLoaded: true,
                b_isLoadMore: false,
              });
              return;
            }
          },
          fail: function (res) {
            console.log(res);
          },
        });
      }
    }
  },

  initData(tag) {
    if (tag == "member") {
      this.setData({
        m_loading: false,
        m_allLoaded: false,
        m_goload: true,
        m_isLoadMore: true,
        m_page: 1,
        m_total_page: 0,
        allMember: [],
      });
    } else if (tag == "buy") {
      this.setData({
        b_loading: false,
        b_allLoaded: false,
        b_goload: true,
        b_isLoadMore: true,
        b_page: 1,
        b_total_page: 0,
        buyList: [],
      });
    }
  },
  getTitle() {
    let urlStr = app.getNetAddresss("plugin.member-return.frontend.set.get-name");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          this.setData({
            title: resdata.data.p_name,
          });
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  b64Decode(input) {
    var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;
    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
    while (i < input.length) {
      enc1 = _keyStr.indexOf(input.charAt(i++));
      enc2 = _keyStr.indexOf(input.charAt(i++));
      enc3 = _keyStr.indexOf(input.charAt(i++));
      enc4 = _keyStr.indexOf(input.charAt(i++));
      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;
      output = output + String.fromCharCode(chr1);
      if (enc3 != 64) {
        output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
        output = output + String.fromCharCode(chr3);
      }
    }
    output = this.utf8_decode(output);
    return output;
  },
  utf8_decode(utftext) {
    var string = "";
    var i = 0;
    var c, c2, c3;

    while (i < utftext.length) {
      c = utftext.charCodeAt(i);
      if (c < 128) {
        string += String.fromCharCode(c);
        i++;
      } else if (c > 191 && c < 224) {
        c2 = utftext.charCodeAt(i + 1);
        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
        i += 2;
      } else {
        c2 = utftext.charCodeAt(i + 1);
        c3 = utftext.charCodeAt(i + 2);
        string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
        i += 3;
      }
    }
    return string;
  },
  // 去奖励记录
  QueueRecord() {
    wx.navigateTo({
      url: "/packageB/member/enterprise/QueueRecord/QueueRecord",
    });
  },
});
