// packageH/circleCommunity/circleMyRelease/circleMyRelease.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabsIndex: 0,

    autographShow: false,
    autographText: "",

    defaultBgImg: "https://dev3.yunzmall.com/attachment/image/aae1e9a875eab1b4c9bdc44f69a3c0d7.jpeg",
    member_id: 0,
    circleId: 0,
    circleInfo: {},

    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数
    listData: [],
    networkLoading: false,

    adminOperationshow: false, //操作框
    adminOperationInfo: [],
  },

  setAutographShow() {
    this.setData({
      autographShow: !this.data.autographShow,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.member_id && options.member_id != 0) {
      this.setData({
        member_id: options.member_id
      });
    }
    if (options.circleId && options.circleId != 0) {
      this.setData({
        circleId: options.circleId
      });
    }
    this.getDetails();
    this.getListData();
    this.getCircleSet();
  },

  getDetails() {
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-member.my-invitation");
    let json = {};
    if (this.data.member_id && this.data.member_id != 0) {
      json = {
        member_id: this.data.member_id
      };
    }
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        this.setData({
          circleInfo: res.data,
        });
        console.log(res);
      },
    });
  },

  subMainBtn() {
    this.editMemberInfo("autograph", this.data.autographText);
    this.setAutographShow();
  },

  editMemberInfo(key, value) {
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-member.edit-member-info");
    let json = {
      id: this.data.circleInfo.id
    };
    json[key] = value;
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        app.tips(res.msg);
        if (res.result != 1) return;
        let target = "circleInfo." + key;
        this.setData({
          [target]: value,
        });
      },
    });
  },

  editImage(evt) {
    let type = evt.currentTarget.dataset.type;
    this.upImage((res) => {
      if (type == "bg") {
        this.setData({
          "circleInfo.bg_img": res.data.img_url,
        });
        this.editMemberInfo("bg_img", res.data.img_url);
      }
    });
  },

  upImage(fn) {
    let urlStr = app.getNetAddresss("upload.uploadPic");
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: (res) => {
        const tempFilePaths = res.tempFilePaths;
        let photourl = tempFilePaths[0]; //e.tempFilePath
        wx.uploadFile({
          url: urlStr,
          filePath: photourl,
          name: "file",
          success(resdata) {
            var res = JSON.parse(resdata.data);
            if (res.result == 1) {
              fn(res);
            } else {
              app.tips(res.msg);
            }
          },
        });
      },
    });
  },


  //获取圈子设置
  getCircleSet() {
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle.get-set");
    app._getNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        wx.setNavigationBarTitle({
          title: res.data.plugin_name,
        });
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },

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

  setTabsIndex(evt) {
    let index = evt.currentTarget.dataset.index;
    this.setData({
      tabsIndex: index,
      networkLoading: false,
    });
    this.getListData();
  },

  initData() {
    this.setData({
      page: 1,
      total_page: 0,
      isLoadMore: true,
      networkLoading: false,
    });
  },

  getListData() {
    let {
      tabsIndex
    } = this.data;
    let json = {
      invitation_status: tabsIndex + 1
    };
    if (this.data.member_id && this.data.member_id != 0) {
      json.member_id = this.data.member_id;
      json.circle_id = this.data.circleId;
    }

    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-invitation.get-my-invitation-list");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        this.data.total_page = res.data.last_page;
        if (!this.data.total_page) {
          this.data.total_page = 0;
        }
        this.setData({
          listData: res.data.data,
          networkLoading: true,
        });
        console.log(res);
      },
    });
  },
  //加载更多数据
  _getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      // that.loading = true;
      return;
    } else {
      this.data.page += 1;
      let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-invitation.get-my-invitation-list");
      let {
        tabsIndex,
        circleId,
        page
      } = this.data;
      let json = {
        page,
        invitation_status: tabsIndex + 1
      };
      if (this.data.member_id && this.data.member_id != 0) {
        json.member_id = this.data.member_id;
        json.circle_id = circleId;
      }

      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          this.data.isLoadMore = true;
          if (res.result === 1) {
            let listData = this.data.listData.concat(res.data.data);
            this.setData({
              listData
            });
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        },
      });
    }
  },

  managementBtn(evt) {
    let item = evt.currentTarget.dataset.item;
    let index = evt.currentTarget.dataset.index;
    let adminOperationInfo = [];
    adminOperationInfo.push({
      type: "edit",
      id: item.id,
      index,
      img: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/micro_communities/detail_revise@2x.png",
      name: "修改",
    });

    adminOperationInfo.push({
      type: "del",
      id: item.id,
      index,
      img: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/micro_communities/detail_delect@2x.png",
      name: "删除",
    });

    this.setData({
      adminOperationInfo,
      adminOperationshow: true,
    });
  },

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

  gotoTopicDetails(evt) {
    let item = evt.currentTarget.dataset.item;
    wx.navigateTo({
      url: "/packageH/circleCommunity/topicDetails/topicDetails?stickId=" + item.id,
    });
  },

  gotoCircleDetails(evt) {
    let id = evt.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/packageH/circleCommunity/circleDetails/circleDetails?circleId=" + id,
    });
  },

  adminBtn(evt) {
    let item = evt.currentTarget.dataset.item;
    if (item.type == "del") {
      this.delCircle(item.id, item.index);
    } else if (item.type == "edit") {
      wx.navigateTo({
        url: "/packageH/circleCommunity/circleTopic/circleTopic?stickId=" + item.id,
      });
    }
    this.adminOperationshowClose();
  },

  adminOperationshowClose() {
    this.setData({
      adminOperationshow: false,
    });
  },

  //删除帖子或视频
  delCircle(invitation_id, index) {
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-invitation.del");
    app._getNetWork({
      url: urlStr,
      data: {
        circle_id: this.data.circleId,
        invitation_id
      },
      success: (resdata) => {
        var res = resdata.data;
        app.tips(res.msg);
        if (res.result != 1) return;
        let listData = this.data.listData;
        listData.splice(index, 1);
        this.setData({
          listData
        });
      },
    });
  },

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

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

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

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

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