// packageD/directSeeding/anchor_assistant/anchor_assistant.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    aideList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.geTaideList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  geTaideList() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.room.frontend.anchor-aide.get-list"
    );
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        page: this.data.page,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          let roomData = res.data.data;
          if (roomData && roomData.length > 0) {
            let nowTime = new Date().getTime();
            for (let i = 0; i < roomData.length; i++) {
              let live_time = roomData[i].live_time;
              let beginTime = new Date(live_time).getTime() * 1000;
              roomData[i].day = "00";
              roomData[i].hou = "00";
              roomData[i].min = "00";
              roomData[i].sec = "00";
              roomData[i].bol = true;
              roomData[i].launch_date = that.formatDate(roomData[i].live_time);
              if (nowTime > beginTime) {
                roomData[i].stop = true;
              } else {
                that._countDown(i, roomData[i].live_time);
                roomData[i].stop = false;
              }
              let back_open = roomData[i].back_open;
              if (
                Object.prototype.toString.call(back_open) == "[object Boolean]"
              ) {
                roomData[i].show = true;
              } else {
                roomData[i].show = false;
              }
            }
          } else {
            roomData = [];
          }

          that.setData({
            aideList: roomData,
            total_page: res.data.last_page,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  //小于10的格式化函数
  _timeFormat(param) {
    //小于10的格式化函数
    return param < 10 ? "0" + param : param;
  },
  _countDown: function (index, endTime_1) {
    let newTime = new Date().getTime();
    let endTime = new Date(endTime_1).getTime() * 1000;
    // let obj = null;
    if (endTime - newTime > 0) {
      let time = (endTime - newTime) / 1000;
      // 获取天、时、分、秒
      let day = parseInt(time / (60 * 60 * 24));
      let hou = parseInt((time % (60 * 60 * 24)) / 3600);
      let min = parseInt(((time % (60 * 60 * 24)) % 3600) / 60);
      let sec = parseInt(((time % (60 * 60 * 24)) % 3600) % 60);
      this.setData({
        ["aideList[" + index + "].day"]: this._timeFormat(day),
        ["aideList[" + index + "].hou"]: this._timeFormat(hou),
        ["aideList[" + index + "].min"]: this._timeFormat(min),
        ["aideList[" + index + "].sec"]: this._timeFormat(sec),
      });
    } else {
      //活动已结束，全部设置为'00'
      this.setData({
        ["aideList[" + index + "].day"]: "00",
        ["aideList[" + index + "].hou"]: "00",
        ["aideList[" + index + "].min"]: "00",
        ["aideList[" + index + "].sec"]: "00",
        ["aideList[" + index + "].stop"]: false,
      });
    }
    if (endTime - newTime < 0) {
      this.setData({
        ["aideList[" + index + "].day"]: "00",
        ["aideList[" + index + "].hou"]: "00",
        ["aideList[" + index + "].min"]: "00",
        ["aideList[" + index + "].sec"]: "00",
        ["aideList[" + index + "].stop"]: false,
      });
    } else {
      this.data.aideList[index].clearTime = setTimeout(
        this._countDown.bind(this, index, endTime_1),
        1000
      );
    }
  },
  formatDate(timestamp) {
    var now = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var date = now.getDate();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    return (
      year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second
    );
  },
  //获取更多数据
  getMoreData() {
    const that = this;
    let urlStr = app.getNetAddresss(
      "plugin.room.frontend.anchor-aide.get-list"
    );
    that.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: {
          page: this.data.page,
        },
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              isLoadMore: true,
            });
            let aideList = that.data.aideList.concat(res.data.data);
            let nowTime = new Date().getTime();
            for (let i = 0; i < aideList.length; i++) {
              if (!aideList[i].bol) {
                let live_time = aideList[i].live_time;
                let beginTime = new Date(live_time).getTime() * 1000;
                aideList[i].day = "00";
                aideList[i].hou = "00";
                aideList[i].min = "00";
                aideList[i].sec = "00";
                aideList[i].bol = true;
                aideList[i].launch_date = that.formatDate(
                  aideList[i].live_time
                );
                if (nowTime > beginTime) {
                  aideList[i].stop = true;
                } else {
                  that._countDown(i, aideList[i].live_time);
                  aideList[i].stop = false;
                }
              }
            }
            that.setData({
              aideList: aideList,
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false,
            });
            return;
          }
        },
        fail: function (res) {
          console.log(res.msg);
        },
      });
    }
  },
  notapbtn() {},
  permissionsBtn(e) {
    let roomid = e.currentTarget.dataset.roomid;
    this.getWatch(roomid);
  },
  permissionsShowCloseBtn() {
    this.setData({
      permissionsShow: false,
    });
  },
  getWatch(roomid) {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.room.frontend.anchor-aide.purview");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        id: roomid,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            WatchList: res.data,
            permissionsShow: true,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  onChange(e) {
    let roomid = e.currentTarget.dataset.roomid;
    let index = e.currentTarget.dataset.index;
    if (this.data.aideList[index].back_open) {
      this.closeBack(index, roomid);
    } else {
      this.openBack(index, roomid);
    }
  },
  openBack(index, roomid) {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.room.frontend.anchor-aide.open-back"
    );
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        id: roomid,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            icon: "none",
            title: "成功打开回放",
            duration: 1500,
          });
          that.setData({
            ["aideList[" + index + "].back_open"]: true,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  closeBack(index, roomid) {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.room.frontend.anchor-aide.close-back"
    );
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        id: roomid,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            icon: "none",
            title: "成功关闭回放",
            duration: 1500,
          });
          that.setData({
            ["aideList[" + index + "].back_open"]: false,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

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

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    for (let i = 0; i < this.data.aideList.length; i++) {
      if (this.data.aideList[i].clearTime) {
        clearTimeout(this.data.aideList[i].clearTime);
      }
    }
  },

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  goLiveAssistant(e) {
    let roomid = e.currentTarget.dataset.roomid;
    let status = e.currentTarget.dataset.status;
    let ban = e.currentTarget.dataset.ban;
    if (ban == 1) {
      wx.showToast({
        icon: "none",
        title: "该直播间已禁用",
        duration: 1500,
      });
      return;
    }
    if (status == 3 || status == 4) {
      wx.navigateTo({
        url:
          "/packageD/directSeeding/helperLiveRoom/helperLiveRoom?roomid=" +
          roomid,
      });
    } else if (status == 1) {
      wx.showToast({
        icon: "none",
        title: "待审核",
        duration: 1500,
      });
    } else if (status == 2) {
      wx.showToast({
        icon: "none",
        title: "等待直播",
        duration: 1500,
      });
    }
  },
});
