// packageC/micro_communities/microFanlist/microFanlist.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isShowTab: false,
    activeName: 0,
    attentionList: [],
    fanList: [],
    isdelete: false,
    avtiveUid: null, //当前点击移除的粉丝id
    avtiveIndex: -1,
    //more
    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      uid: options.uid,
      activeName: options.actived,
      active: options.actived == 2 ? 0 : 1,
      isShowTab: true
    });
    
    this.getList();
  },

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

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

  getList() {
    let that = this;
    let json = {};
    if (this.data.activeName == 1) {
      json.userType = 1; //粉丝
      json.page = 1;
      json.member = this.data.uid;
    } else {
      json.userType = 2; //关注
      json.page = 1;
      json.member = this.data.uid;
    }

    let urlStr = app.getNetAddresss(
      "plugin.micro-communities.api.user.getFollowFans"
    );
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        let res = resdata.data;
        if (res.result === 1) {
          that.setData({
            isLoadMore: true,
            total_page: res.data.last_page,
          });
          if (that.data.activeName == 1) {
            //粉丝
            that.setData({
              fanList: res.data.data,
            });
          } else {
            //关注
            that.setData({
              attentionList: res.data.data,
            });
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  cancelLook(e) {
    let uid = e.currentTarget.dataset.uid;
    // let index = e.currentTarget.dataset.index;
    let that = this;
    wx.showModal({
      title: "提示",
      content: "确定取消关注对方",
      success(res) {
        if (res.confirm) {
          let urlStr = app.getNetAddresss(
            "plugin.micro-communities.api.user.delFollow"
          );
          let dataJson = {
            uid: uid,
          };
          app._getNetWork({
            url: urlStr,
            data: dataJson,
            success: function (resdata) {
              let res = resdata.data;
              if (res.result === 1) {
                wx.showToast({
                  title: res.msg,
                  icon: "none",
                  duration: 1500,
                });
                // let list = that.data.attentionList.splice(index, 1);
                that.setData({
                  attentionList: that.data.attentionList,
                });
              } else {
                wx.showToast({
                  title: res.msg,
                  icon: "none",
                  duration: 1500,
                });
              }
            },
            fail: function (res) {
              console.log(res);
            },
          });
          console.log("用户点击确定");
        } else if (res.cancel) {
          console.log("用户点击取消");
        }
      },
    });
  },
  toMember(e){
    let uid = e.currentTarget.dataset.uid;
    wx.navigateTo({url: '/packageC/micro_communities/microhomepage/microhomepage?micuid=' + uid });
  },
  deleteFan() {
    let that = this;
    wx.showModal({
      title: "提示",
      content: "对方将不再关注你，且不会收到通知，你也不会被推荐给对方",
      success(res) {
        if (res.confirm) {
          let urlStr = app.getNetAddresss(
            "plugin.micro-communities.api.user.delFans"
          );
          let dataJson = {
            fid: that.data.avtiveUid,
          };
          app._getNetWork({
            url: urlStr,
            data: dataJson,
            success: function (resdata) {
              let res = resdata.data;
              if (res.result === 1) {
                wx.showToast({
                  title: res.msg,
                  icon: "none",
                  duration: 1500,
                });
                // let list = that.data.fanList.splice(that.data.avtiveIndex, 1);
                that.setData({
                  fanList: that.data.fanList,
                  isdelete: false,
                });
              } else {
                that.setData({
                  isdelete: false,
                });
                wx.showToast({
                  title: res.msg,
                  icon: "none",
                  duration: 1500,
                });
              }
            },
            fail: function (res) {
              console.log(res);
            },
          });
          console.log("用户点击确定");
        } else if (res.cancel) {
          console.log("用户点击取消");
        }
      },
    });
  },
  lookUser(e) {
    let uid = e.currentTarget.dataset.uid;
    let urlStr = app.getNetAddresss(
      "plugin.micro-communities.api.user.addFollow"
    );
    let dataJson = {
      user_id: uid,
    };
    app._getNetWork({
      url: urlStr,
      data: dataJson,
      success: function (resdata) {
        let res = resdata.data;
        if (res.result === 1) {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  showDelete(e) {
    let uid = e.currentTarget.dataset.uid;
    let index = e.currentTarget.dataset.index;
    this.setData({
      isdelete: true,
      avtiveUid: uid,
      avtiveIndex: index,
    });
  },
  handleClick(e) {
    let index = e.detail.index;
    // let title = e.detail.title;
    if (String(index) == "0") {
      this.data.activeName = 2;
    } else {
      this.data.activeName = 1;
    }
    this.setData({
      page: 1,
    });
    this.getList();
  },
  isCoverLayer() {
    this.setData({
      isdelete: false,
    });
  },
  getMoreData() {
    console.log(this.data.activeName);
    let that = this;
    that.setData({
      isLoadMore: false,
    });
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      that.setData({
        page: this.data.page + 1,
      });
      let json = [];
      if (that.data.activeName == 1) {
        json.userType = 1; //粉丝
        json.page = that.data.page;
        json.member = this.data.uid;
      } else {
        json.userType = 2; //关注
        json.page = that.data.page;
        json.member = this.data.uid;
      }
      let urlStr = app.getNetAddresss(
        "plugin.micro-communities.api.user.getFollowFans"
      );
      app._getNetWork({
        url: urlStr,
        data: json,
        success: function (resdata) {
          let res = resdata.data;
          that.setData({
            isLoadMore: true,
          });
          if (res.result === 1) {
            let myData = res.data.data;
            if (that.data.activeName == 1) {
              //粉丝
              that.setData({
                fanList: that.data.fanList.concat(myData),
              });
              console.log(that.data.fanList);
            } else {
              //关注
              that.setData({
                attentionList: that.data.attentionList.concat(myData),
              });
            }
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false,
            });
            wx.showToast({
              title: res.msg,
              icon: "none",
              duration: 1500,
            });
          }
        },
        fail: function (res) {
          console.log(res);
        },
      });
    }
  },

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log("没有更多了");
    }
  },

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