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

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

    topData: [], //置顶数据

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

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("第一次执行");
    this.data.circleId = options.circleId;
    this.getDetails();
    this.getListData();
    this.getTopData();
  },

  updateView() {
    this.getDetails();
    this.getListData();
  },

  gotoCircleMyRelease(evt) {
    console.log(evt);
    let item = evt.currentTarget.dataset.item;
    wx.navigateTo({
      url: "/packageH/circleCommunity/circleMyRelease/circleMyRelease?member_id=" + item.member_id + "&circleId=" + this.data.circleId,
    });
  },

  //获取置顶文章
  getTopData() {
    let { circleId } = this.data;
    let json = { circle_id: circleId, is_top: 1 };
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-invitation.get-circle-invitation-list");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        this.setData({
          topData: res.data.data,
        });
      },
    });
  },

  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,
    });
  },

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

  // openSheet(evt){
  // 	let item = evt.currentTarget.dataset.item;
  // 	let index = evt.currentTarget.dataset.index;
  // 	let c1="置顶",c2="推荐",c3="未加入可看",c4="删除";
  // 	if(item.is_top==1) c1="取消置顶";
  // 	if(item.is_recommend==1) c2="取消推荐";
  // 	if(item.is_show==1) c3="取消未加入可看";
  // 	wx.showActionSheet({
  // 		itemList: [c1, c2, c3, c4],
  // 		success :res=> {
  // 			if(res.tapIndex==3){
  // 				this.delCircle(item.id,index);
  // 			}else {
  // 				let arr = ['is_top','is_recommend','is_show'];
  // 				this.setCircleReview(arr[res.tapIndex],item.id,index);
  // 			}

  // 		},
  // 		fail (res) {
  // 		  console.log(res.errMsg)
  // 		}
  // 	})
  // },
  adminBtn(evt) {
    let item = evt.currentTarget.dataset.item;
    if (item.type == "del") {
      this.delCircle(item.id, item.index);
    } else {
      this.setCircleReview(item.type, item.id, item.index);
    }
    this.adminOperationshowClose();
  },
  managementBtn(evt) {
    let item = evt.currentTarget.dataset.item;
    let index = evt.currentTarget.dataset.index;
    let adminOperationInfo = [];
    if (this.data.circleInfo.is_master == 1) {
      adminOperationInfo = [
        {
          type: "is_top",
          id: item.id,
          index,
          img: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/micro_communities/detail_roof@2x.png",
          name: item.is_top == 1 ? "取消置顶" : "置顶",
        },
        {
          type: "is_recommend",
          id: item.id,
          index,
          img: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/micro_communities/detail_recommend@2x.png",
          name: item.is_recommend == 1 ? "取消推荐" : "推荐",
        },
        {
          type: "is_show",
          id: item.id,
          index,
          img: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/micro_communities/detail_invisible@2x.png",
          name: item.is_show == 1 ? "取消未加入可见" : "未加入可见",
        },
      ];
    }

    // if (this.data.detailsData.is_current_member == 1) {
    // 	adminOperationInfo.push({
    // 		type:'edit',
    // 		id:item.id,
    // 		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,
    });
  },

  //设置置顶/推荐/未加入可见
  setCircleReview(type, invitation_id, index) {
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-invitation.set-invitation-review");
    app._getNetWork({
      url: urlStr,
      data: { circle_id: this.data.circleId, invitation_id, invitation_type: type },
      success: (resdata) => {
        var res = resdata.data;
        app.tips(res.msg);
        if (res.result != 1) return;
        let status = 0;
        if (this.data.listData[index][type] == 0) status = 1;
        let target = "listData[" + index + "]." + type;
        this.setData({
          [target]: status,
        });
      },
    });
  },

  //删除帖子或视频
  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 });
      },
    });
  },

  followMember(evt) {
    let index = evt.currentTarget.dataset.index;
    let id = evt.currentTarget.dataset.id;
    let is_attention = this.data.listData[index].is_attention;
    let result;
    let urlStr;
    let json = { attention_member_id: id };
    if (is_attention == 0) {
      result = 1;
      urlStr = app.getNetAddresss("plugin.circle.frontend.circle-member.attention-member");
    } else if (is_attention == 1) {
      result = 0;
      urlStr = app.getNetAddresss("plugin.circle.frontend.circle-member.cancel-attention");
    }

    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        app.tips(res.msg);
        if (res.result != 1) return;
        this.data.listData.forEach((item, key) => {
          if (item.member_id == id) {
            let target = "listData[" + key + "].is_attention";
            this.setData({
              [target]: result,
            });
          }
        });
      },
    });
  },

  getDetails() {
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle.get-circle");
    app._postNetWork({
      url: urlStr,
      data: { circle_id: this.data.circleId },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        this.setData({
          circleInfo: res.data,
        });
        wx.setNavigationBarTitle({
          title: res.data.name,
        });
        console.log(res);
      },
    });
  },

  getListData() {
    this.initData();
    let { tabsIndex, circleId } = this.data;
    let json = { circle_id: circleId };
    if (tabsIndex == 0) {
      json.is_attention = 1;
    } else json.invitation_status = tabsIndex;

    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-invitation.get-circle-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-circle-invitation-list");
      let { tabsIndex, circleId, page } = this.data;
      let json = { circle_id: circleId, page };
      if (tabsIndex == 0) {
        json.is_attention = 1;
      } else json.invitation_status = tabsIndex;

      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;
          }
        },
      });
    }
  },

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

  addCircle() {
    let { circleInfo, circleId } = this.data;
    if (circleInfo.is_toll == 1) {
      // wx.navigateTo({
      //   url: '/packageH/circleCommunity/circlePayment/circlePayment?circleId='+circleId+"&money="+circleInfo.price,
      // });
      this.payAddCircle();
      return;
    }
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle.add-circle");
    app._postNetWork({
      url: urlStr,
      data: { circle_id: circleId },
      success: (resdata) => {
        var res = resdata.data;
        app.tips(res.msg);
        if (res.result != 1) return;
        this.getDetails();
      },
    });
  },
  payAddCircle() {
    let { circleId } = this.data;
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-pay.add-circle-order");
    app._postNetWork({
      url: urlStr,
      data: { circle_id: circleId },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        this.createOrder(res.data.goods_id);
        console.log(res);
      },
    });
  },
  createOrder(goods_id) {
    let { circleId } = this.data;
    let goods = [{ goods_id: goods_id, total: 1, option_id: 0 }];
    let urlStr = app.getNetAddresss("plugin.circle.api.create.index");
    app._postNetWork({
      url: urlStr,
      data: { circle_id: circleId, goods },
      success: (resdata) => {
        var res = resdata.data;
        app.tips(res.msg);
        if (res.result != 1) return;
        console.log(res);
        wx.navigateTo({
          url: "/packageD/buy/orderPay/orderPay?order_id=" + res.data.order_ids + "&status=2",
        });
      },
    });
  },

  gotoCircleSetting() {
    wx.navigateTo({
      url: "/packageH/circleCommunity/circleSetting/circleSetting?circleId=" + this.data.circleId,
    });
  },
  gotoCircleIntroduce() {
    wx.navigateTo({
      url: "/packageH/circleCommunity/circleIntroduce/circleIntroduce?circleId=" + this.data.circleId,
    });
  },
  gotoCircleSelect() {
    wx.navigateTo({
      url: "/packageH/circleCommunity/circleSelect/circleSelect?circleId=" + this.data.circleId,
    });
  },
  gotoTopicDetails(evt) {
    let item = evt.currentTarget.dataset.item;
    if (item.status == 1) {
      wx.navigateTo({
        url: "/packageH/circleCommunity/topicDetails/topicDetails?stickId=" + item.id,
      });
    } else if (item.status == 2) {
      let page = this.data.listData.findIndex((value, index, arr) => {
        return value.id === item.id;
      });
      page = Math.ceil((page + 1) / 15);
      wx.navigateTo({
        url: "/packageH/circleCommunity/videoDetails/videoDetails?vid=" + item.id + "&page=" + page + "&circleId=" + this.data.circleId,
      });
    }
  },

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: "/packageH/circleCommunity/circleDetails/circleDetails?circleId=" + this.data.circleId,
    };
  },
  onShareTimeline(){
    return{
      query:`circleId=${this.data.circleId}`
    };
  }
});
