// packageD/directSeeding/liveClassification/liveClassification.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    myConcern: "",
    announceList: "",
    broadcastlist: "",
    allLivelistData: [],
    apiType: "recommend",
    playBackData: "",
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    active: 2,
    playBackShow: false,
    myConcern_activitys1: [],
    myConcern_activitys2: [],
    allLivelistData_activitys1: [],
    allLivelistData_activitys2: [],
    recommendData_activitys1: [],
    recommendData_activitys2: [],
    broadcastlist_activitys1: [],
    broadcastlist_activitys2: [],
    announceList_activitys1: [],
    announceList_activitys2: [],
    playBackData_activitys1: [],
    playBackData_activitys2: [],
    recommendData: [],
    live_search: "",
    my_colOneHeight: 0,
    my_colTwoHeight: 0,
    re_colOneHeight: 0,
    re_colTwoHeight: 0,
    br_colOneHeight: 0,
    br_colTwoHeight: 0,
    an_colOneHeight: 0,
    an_colTwoHeight: 0,
    pl_colOneHeight: 0,
    pl_colTwoHeight: 0,
    all_colOneHeight: 0,
    all_colTwoHeight: 0,
    cate_id: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id, 4444444444);
    if (options.id) {
      this.setData({
        cate_id: options.id,
      });
    }
    this.getRecommend();
    this.getBanner();
    this.getPlayBackShow();
  },
  getPlayBackShow() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.room.frontend.live-list.record");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {},
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            playBackShow: res.data.result == 1 ? true : false,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  getBanner() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.room.frontend.live-list.banner");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {},
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            liveBanner: res.data,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  getRecommend() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.room.frontend.live-list.get-live-recommend"
    );
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        keyword: this.data.live_search,
        cate_id: this.data.cate_id,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            recommendData: res.data,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  swichTabTItem(e) {
    let index = e.detail.index;
    this.setData({
      isLoadMore: true,
      page: 1,
      total_page: 0,
      live_search: "",
    });
    if (index == 0) {
      this.data.apiType = "myConcern";
      this.getMyConcern();
    } else if (index == 1) {
      this.data.apiType = "AllLivelist";
      this.getAllLivelist();
    } else if (index == 2) {
      this.data.apiType = "recommend";
      this.getRecommend();
    } else if (index == 3) {
      this.data.apiType = "liveList";
      this.data.status = 3;
      this.getLiveList(3);
    } else if (index == 4) {
      this.data.apiType = "liveList";
      this.data.status = 2;
      this.getLiveList(2);
    } else if (index == 5) {
      this.data.apiType = "playBack";
      this.getplayBack();
    }
  },
  getplayBack() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.room.frontend.live-list.play-back");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        page: 1,
        keyword: this.data.live_search,
        cate_id: this.data.cate_id,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            playBackData: res.data.data,
            total_page: res.data.last_page,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  getLiveList(type) {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.room.frontend.live-list.get-live-list"
    );
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        status: type,
        page: 1,
        keyword: this.data.live_search,
        cate_id: this.data.cate_id,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (type == 2) {
            that.setData({
              total_page: res.data.last_page,
              announceList: res.data.data,
            });
          } else if (type == 3) {
            that.setData({
              total_page: res.data.last_page,
              broadcastlist: res.data.data,
            });
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  getAllLivelist() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.room.frontend.live-list.get-all-live-list"
    );
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        page: 1,
        keyword: this.data.live_search,
        cate_id: this.data.cate_id,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            allLivelistData: res.data.data,
            total_page: res.data.last_page,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  getMyConcern() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.room.frontend.live-list.get-live-concern"
    );
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        page: 1,
        keyword: this.data.live_search,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            myConcern: res.data.data,
            total_page: res.data.last_page,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      if (this.data.apiType !== "recommend") {
        this._getMoreData();
      }
    } else {
      console.log("没有更多数据");
    }
  },
  //获取更多数据
  _getMoreData() {
    const that = this;
    let json = {};
    let api = "";
    let urlStr = "";
    that.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      if (this.data.apiType == "myConcern") {
        api = "plugin.room.frontend.live-list.get-live-concern";
        json = {
          page: this.data.page,
          cate_id: this.data.cate_id,
        };
      } else if (that.data.apiType == "AllLivelist") {
        api = "plugin.room.frontend.live-list.get-all-live-list";
        json = {
          page: this.data.page,
          cate_id: this.data.cate_id,
        };
      } else if (that.data.apiType == "playBack") {
        api = "plugin.room.frontend.live-list.play-back";
        json = {
          page: this.data.page,
          cate_id: this.data.cate_id,
        };
      }
      // else if (that.data.apiType == "recommend") {
      // }
      else {
        api = "plugin.room.frontend.live-list.get-live-list";
        json = {
          status: this.data.status,
          page: this.data.page,
          cate_id: this.data.cate_id,
        };
      }
      json.keyword = this.data.live_search;
      urlStr = app.getNetAddresss(api);
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: json,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            if (that.data.apiType == "myConcern") {
              that.setData({
                isLoadMore: true,
                myConcern: that.data.myConcern.concat(res.data.data),
              });
            } else if (that.data.apiType == "playBack") {
              that.setData({
                isLoadMore: true,
                playBackData: that.data.playBackData.concat(res.data.data),
              });
            } else if (that.data.apiType == "AllLivelist") {
              that.setData({
                isLoadMore: true,
                allLivelistData: that.data.allLivelistData.concat(
                  res.data.data
                ),
              });
            }
            // else if (that.data.apiType == "recommend") {
            // }
            else {
              if (that.data.status == 2) {
                that.setData({
                  isLoadMore: true,
                  announceList: that.data.announceList.concat(res.data.data),
                });
              } else if (that.data.status == 3) {
                that.setData({
                  isLoadMore: true,
                  broadcastlist: that.data.broadcastlist.concat(res.data.data),
                });
              }
            }
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false,
            });
            return;
          }
        },
        fail: function (res) {
          console.log(res.msg);
        },
      });
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  //跳转直播页
  golivePage(e) {
    let id = e.currentTarget.dataset.id;
    let status = e.currentTarget.dataset.status;
    let memberid = e.currentTarget.dataset.memberid;
    console.log(e);
    if (status == 1) {
      wx.navigateTo({
        url:
          "/packageD/directSeeding/livePage/livePage?id=" +
          id +
          "&playerType=live",
      });
    } else {
      wx.navigateTo({
        url: "/packageD/directSeeding/anchorDetail/anchorDetail?id=" + memberid,
      });
    }
  },
  gopage(e) {
    let status = e.currentTarget.dataset.status;
    let id = e.currentTarget.dataset.id;
    let backid = e.currentTarget.dataset.backid;
    if (status == 1) {
      wx.navigateTo({
        url:
          "/packageD/directSeeding/livePage/livePage?id=" +
          id +
          "&playerType=live",
      });
    } else if (status == 2) {
      wx.navigateTo({
        url:
          "/packageD/directSeeding/livePage/livePage?id=" +
          id +
          "&playerType=video" +
          "&backid=" +
          backid,
      });
    }
  },
  golistPage(e) {
    let status = e.currentTarget.dataset.status;
    let id = e.currentTarget.dataset.id;
    let backid = e.currentTarget.dataset.backid;
    if (status == 3) {
      wx.navigateTo({
        url:
          "/packageD/directSeeding/livePage/livePage?id=" +
          id +
          "&playerType=live",
      });
    } else if (status == 2) {
      wx.navigateTo({
        url: "/packageD/directSeeding/foreshow/foreshow?id=" + id,
      });
    } else if (status == 4) {
      wx.navigateTo({
        url:
          "/packageD/directSeeding/livePage/livePage?id=" +
          id +
          "&playerType=video" +
          "&backid=" +
          backid,
      });
    }
  },
  goPlayBackData(e) {
    let roomid = e.currentTarget.dataset.roomid;
    let backid = e.currentTarget.dataset.backid;
    wx.navigateTo({
      url:
        "/packageD/directSeeding/livePage/livePage?id=" +
        roomid +
        "&playerType=video" +
        "&backid=" +
        backid,
    });
  },
  //跳转直播预告
  goforeshow(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/packageD/directSeeding/foreshow/foreshow?id=" + id,
    });
  },
  /**
   * 图片加载函数
   * 图片加载完成后，根据图片的高度，依次往第一列和第二列中推入数据
   */
  onImageLoad(options) {
    let index = options.target.dataset.index;
    let seltype = options.target.dataset.seltype;
    let imageWidth = options.detail.width; // 图片宽度
    let imageHeight = options.detail.height; // 图片高度
    let activityObj = null;
    let scle = imageWidth / imageHeight;
    if (seltype == "myConcern") {
      let activitys = this.data.myConcern; // 获取所有的数据资源
      activityObj = activitys[index];
      let activitys1 = this.data.myConcern_activitys1;
      let activitys2 = this.data.myConcern_activitys2;
      if (this.data.my_colOneHeight <= this.data.my_colTwoHeight) {
        this.data.my_colOneHeight += 176 / scle + 60;
        activitys1.push(activityObj);
      } else {
        this.data.my_colTwoHeight += 176 / scle + 60;
        activitys2.push(activityObj);
      }
      this.setData({
        myConcern_activitys1: activitys1,
        myConcern_activitys2: activitys2,
      });
    } else if (seltype == "recommend") {
      let activitys = this.data.recommendData; // 获取所有的数据资源
      activityObj = activitys[index];
      let activitys1 = this.data.recommendData_activitys1;
      let activitys2 = this.data.recommendData_activitys2;
      if (this.data.re_colOneHeight <= this.data.re_colTwoHeight) {
        this.data.re_colOneHeight += 176 / scle + 60;
        activitys1.push(activityObj);
      } else {
        this.data.re_colTwoHeight += 176 / scle + 60;
        activitys2.push(activityObj);
      }
      this.setData({
        recommendData_activitys1: activitys1,
        recommendData_activitys2: activitys2,
      });
    } else if (seltype == "AllLivelist") {
      let activitys = this.data.allLivelistData; // 获取所有的数据资源
      activityObj = activitys[index];
      let activitys1 = this.data.allLivelistData_activitys1;
      let activitys2 = this.data.allLivelistData_activitys2;
      if (this.data.all_colOneHeight <= this.data.all_colTwoHeight) {
        this.data.all_colOneHeight += 176 / scle + 60;
        activitys1.push(activityObj);
      } else {
        this.data.all_colTwoHeight += 176 / scle + 60;
        activitys2.push(activityObj);
      }
      this.setData({
        allLivelistData_activitys1: activitys1,
        allLivelistData_activitys2: activitys2,
      });
    } else if (seltype == "liveList_1") {
      let activitys = this.data.broadcastlist; // 获取所有的数据资源
      activityObj = activitys[index];
      let activitys1 = this.data.broadcastlist_activitys1;
      let activitys2 = this.data.broadcastlist_activitys2;
      if (this.data.br_colOneHeight <= this.data.br_colTwoHeight) {
        this.data.br_colOneHeight += 176 / scle + 60;
        activitys1.push(activityObj);
      } else {
        this.data.br_colTwoHeight += 176 / scle + 60;
        activitys2.push(activityObj);
      }
      this.setData({
        broadcastlist_activitys1: activitys1,
        broadcastlist_activitys2: activitys2,
      });
    } else if (seltype == "liveList_2") {
      let activitys = this.data.announceList; // 获取所有的数据资源
      activityObj = activitys[index];
      let activitys1 = this.data.announceList_activitys1;
      let activitys2 = this.data.announceList_activitys2;
      if (this.data.an_colOneHeight <= this.data.an_colTwoHeight) {
        this.data.an_colOneHeight += 176 / scle + 60;
        activitys1.push(activityObj);
      } else {
        this.data.an_colTwoHeight += 176 / scle + 60;
        activitys2.push(activityObj);
      }
      this.setData({
        announceList_activitys1: activitys1,
        announceList_activitys2: activitys2,
      });
    } else if (seltype == "playBack") {
      let activitys = this.data.playBackData; // 获取所有的数据资源
      activityObj = activitys[index];
      let activitys1 = this.data.playBackData_activitys1;
      let activitys2 = this.data.playBackData_activitys2;
      if (this.data.pl_colOneHeight <= this.data.pl_colTwoHeight) {
        this.data.pl_colOneHeight += 176 / scle + 60;
        activitys1.push(activityObj);
      } else {
        this.data.pl_colTwoHeight += 176 / scle + 60;
        activitys2.push(activityObj);
      }
      this.setData({
        playBackData_activitys1: activitys1,
        playBackData_activitys2: activitys2,
      });
    }
  },
  onImageError(options) {
    let index = options.target.dataset.index;
    let seltype = options.target.dataset.seltype;
    // let currentId = options.currentTarget.id;
    console.log("图片加载发生错误");
    this.onImageLoad({
      target: {
        dataset: {
          index: index,
          seltype: seltype,
        },
      },
      detail: {
        width: 2340,
        height: 1080,
      },
    });
  },
  searchConfirm() {
    if (this.data.apiType == "myConcern") {
      this.data.my_colOneHeight = 0;
      this.data.my_colTwoHeight = 0;
      this.setData({
        myConcern: [],
        myConcern_activitys1: [],
        myConcern_activitys2: [],
      });
      this.getMyConcern();
    } else if (this.data.apiType == "recommend") {
      this.data.re_colOneHeight = 0;
      this.data.re_colTwoHeight = 0;
      this.setData({
        recommendData: [],
        recommendData_activitys1: [],
        recommendData_activitys2: [],
      });
      this.getRecommend();
    } else if (this.data.apiType == "liveList") {
      if (this.data.status == 3) {
        this.data.br_colOneHeight = 0;
        this.data.br_colTwoHeight = 0;
        this.setData({
          broadcastlist: [],
          broadcastlist_activitys1: [],
          broadcastlist_activitys2: [],
        });
        this.getLiveList(3);
      } else if (this.data.status == 2) {
        this.data.an_colOneHeight = 0;
        this.data.an_colTwoHeight = 0;
        this.setData({
          announceList: [],
          announceList_activitys1: [],
          announceList_activitys2: [],
        });
        this.getLiveList(2);
      }
    } else if (this.data.apiType == "playBack") {
      this.data.pl_colOneHeight = 0;
      this.data.pl_colTwoHeight = 0;
      this.setData({
        playBackData: [],
        playBackData_activitys1: [],
        playBackData_activitys2: [],
      });
      this.getplayBack();
    } else if (this.data.apiType == "AllLivelist") {
      this.data.all_colOneHeight = 0;
      this.data.all_colTwoHeight = 0;
      this.setData({
        allLivelistData: [],
        allLivelistData_activitys1: [],
        allLivelistData_activitys2: [],
      });
      this.getAllLivelist();
    }
  },
  searchbtn(e) {
    let val = e.detail.value;
    this.setData({
      live_search: val,
    });
  },
});
