var app = getApp();
Component({
  properties: {
    params: {
      type: null,
    },
  },
  data: {
    active: 1,
    status: "",
    announceList: "",
    broadcastlist: "",
    apiType: "recommend",
    playBackData: "",
    playBackShow: false,
    allLivelistData: [],
    allLivelistData_activitys1: [],
    allLivelistData_activitys2: [],
    all_colOneHeight: 0,
    all_colTwoHeight: 0,
    //more
    isLoadMore_all: true,
    page_all: 1,
    total_page_all: 0,
    recommendData_activitys1: [],
    recommendData_activitys2: [],
    broadcastlist_activitys1: [],
    broadcastlist_activitys2: [],
    announceList_activitys1: [],
    announceList_activitys2: [],
    playBackData_activitys1: [],
    playBackData_activitys2: [],
    recommendData: [],
    re_colOneHeight: 0,
    re_colTwoHeight: 0,
    br_colOneHeight: 0,
    br_colTwoHeight: 0,
    an_colOneHeight: 0,
    an_colTwoHeight: 0,
    pl_colOneHeight: 0,
    pl_colTwoHeight: 0,
    RecommendBol: true,
    allLiveBol: true,
    broadcastlistBol: true,
    announceListBol: true,
    playBackDataBol: true,
    //more
    isLoadMore_br: true,
    page_br: 1,
    total_page_br: 0,
    //more
    isLoadMore_an: true,
    page_an: 1,
    total_page_an: 0,
    //more
    isLoadMore_pl: true,
    page_pl: 1,
    total_page_pl: 0,
    showNumber: 10,
    notload_all: true,
    notload_br: true,
    notload_an: true,
    notload_pl: true,
  }, // 私有数据，可用于模板渲染

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {},
    moved() {},
    detached() {},
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {}, // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {},

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },
  methods: {
    goliveList() {
      wx.navigateTo({
        url: "/packageD/directSeeding/liveList/liveList",
      });
    },
    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,
        });
      }
    },
    getMorePdData_all() {
      const that = this;
      let urlStr = app.getNetAddresss(
        "plugin.room.frontend.live-list.get-all-live-list"
      );
      that.setData({
        isLoadMore_all: false, // 防止多次请求分页数据
      });
      if (this.data.page_all >= this.data.total_page_all) {
        return;
      } else {
        this.data.page_all = this.data.page_all + 1;
        app._postNetWork({
          url: urlStr,
          showToastIn: false,
          data: {
            page: this.data.page_all,
            page_size: this.data.showNumber,
          },
          success: function (resdata) {
            var res = resdata.data;
            if (res.result == 1) {
              that.setData({
                isLoadMore_all: true,
                allLivelistData: that.data.allLivelistData.concat(
                  res.data.data
                ),
              });
              if (that.data.total_page_all == that.data.page_all) {
                that.setData({
                  notload_all: false,
                });
              }
            } else {
              that.setData({
                page_all: that.data.page_all - 1,
                isLoadMore_all: false,
              });
              return;
            }
          },
          fail: function (res) {
            console.log(res.msg);
          },
        });
      }
    },
    getMorePdData_br() {
      const that = this;
      let urlStr = app.getNetAddresss(
        "plugin.room.frontend.live-list.get-live-list"
      );
      that.setData({
        isLoadMore_br: false, // 防止多次请求分页数据
      });
      if (this.data.page_br >= this.data.total_page_br) {
        return;
      } else {
        this.data.page_br = this.data.page_br + 1;
        app._postNetWork({
          url: urlStr,
          showToastIn: false,
          data: {
            status: 3,
            page: this.data.page_br,
            page_size: this.data.showNumber,
          },
          success: function (resdata) {
            var res = resdata.data;
            if (res.result == 1) {
              that.setData({
                isLoadMore_br: true,
                broadcastlist: that.data.broadcastlist.concat(res.data.data),
              });
              if (that.data.total_page_br == that.data.page_br) {
                that.setData({
                  notload_br: false,
                });
              }
            } else {
              that.setData({
                page_br: that.data.page_br - 1,
                isLoadMore_br: false,
              });
              return;
            }
          },
          fail: function (res) {
            console.log(res.msg);
          },
        });
      }
    },
    getMorePdData_an() {
      const that = this;
      let urlStr = app.getNetAddresss(
        "plugin.room.frontend.live-list.get-live-list"
      );
      that.setData({
        isLoadMore_an: false, // 防止多次请求分页数据
      });
      if (this.data.page_an >= this.data.total_page_an) {
        return;
      } else {
        this.data.page_an = this.data.page_an + 1;
        app._postNetWork({
          url: urlStr,
          showToastIn: false,
          data: {
            status: 2,
            page: this.data.page_an,
            page_size: this.data.showNumber,
          },
          success: function (resdata) {
            var res = resdata.data;
            if (res.result == 1) {
              that.setData({
                isLoadMore_an: true,
                announceList: that.data.announceList.concat(res.data.data),
              });
              if (that.data.total_page_an == that.data.page_an) {
                that.setData({
                  notload_an: false,
                });
              }
            } else {
              that.setData({
                page_an: that.data.page_an - 1,
                isLoadMore_an: false,
              });
              return;
            }
          },
          fail: function (res) {
            console.log(res.msg);
          },
        });
      }
    },
    getMorePdData_pl() {
      const that = this;
      let urlStr = app.getNetAddresss(
        "plugin.room.frontend.live-list.play-back"
      );
      that.setData({
        isLoadMore_pl: false, // 防止多次请求分页数据
      });
      if (this.data.page_pl >= this.data.total_page_pl) {
        return;
      } else {
        this.data.page_pl = this.data.page_pl + 1;
        app._postNetWork({
          url: urlStr,
          showToastIn: false,
          data: {
            page: this.data.page_pl,
            page_size: this.data.showNumber,
          },
          success: function (resdata) {
            var res = resdata.data;
            if (res.result == 1) {
              that.setData({
                isLoadMore_pl: true,
                playBackData: that.data.playBackData.concat(res.data.data),
              });
              if (that.data.total_page_pl == that.data.page_pl) {
                that.setData({
                  notload_pl: false,
                });
              }
            } else {
              that.setData({
                page_pl: that.data.page_pl - 1,
                isLoadMore_pl: false,
              });
              return;
            }
          },
          fail: function (res) {
            console.log(res.msg);
          },
        });
      }
    },
    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,
        },
      });
    },
    /**
     * 图片加载函数
     * 图片加载完成后，根据图片的高度，依次往第一列和第二列中推入数据
     */
    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 == "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,
        });
      }
    },
    //跳转直播预告
    goforeshow(e) {
      let id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: "/packageD/directSeeding/foreshow/foreshow?id=" + id,
      });
    },
    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,
      });
    },
    //跳转直播页
    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,
        });
      }
    },
    swichTabTItem(e) {
      let index = e.detail.index;
      this.setData({
        active: index,
      });
      if (index == 0 && this.data.allLiveBol) {
        this.data.apiType = "AllLivelist";
        this.getAllLivelist();
        this.data.allLiveBol = false;
      } else if (index == 1 && this.data.RecommendBol) {
        this.data.apiType = "recommend";
        this.getRecommend();
        this.data.RecommendBol = false;
      } else if (index == 2 && this.data.broadcastlistBol) {
        this.data.apiType = "liveList";
        this.data.status = 3;
        this.getLiveList(3);
        this.data.broadcastlistBol = false;
      } else if (index == 3 && this.data.announceListBol) {
        this.data.apiType = "liveList";
        this.data.status = 2;
        this.getLiveList(2);
        this.data.announceListBol = false;
      } else if (index == 4 && this.data.playBackDataBol) {
        this.data.apiType = "playBack";
        this.getplayBack();
        this.data.playBackDataBol = false;
      }
    },
    getplayBack() {
      let that = this;
      let urlStr = app.getNetAddresss(
        "plugin.room.frontend.live-list.play-back"
      );
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: {
          page: 1,
          page_size: this.data.showNumber,
        },
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              playBackData: res.data.data,
              total_page_pl: res.data.last_page,
            });
            if (that.data.total_page_pl == that.data.page_pl) {
              that.setData({
                notload_pl: false,
              });
            }
          } 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,
          page_size: this.data.showNumber,
        },
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            if (type == 2) {
              that.setData({
                total_page_an: res.data.last_page,
                announceList: res.data.data,
              });
              if (that.data.total_page_an == that.data.page_an) {
                that.setData({
                  notload_an: false,
                });
              }
            } else if (type == 3) {
              that.setData({
                total_page_br: res.data.last_page,
                broadcastlist: res.data.data,
              });
              if (that.data.total_page_br == that.data.page_br) {
                that.setData({
                  notload_br: false,
                });
              }
            }
          } 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: {
          page_size: this.data.showNumber,
        },
        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
            // })
            console.log(res.msg);
          }
        },
        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_size: this.data.showNumber,
        },
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              allLivelistData: res.data.data,
              total_page_all: res.data.last_page,
            });
            if (that.data.total_page_all == that.data.page_all) {
              that.setData({
                notload_all: false,
              });
            }
          } else {
            wx.showToast({
              icon: "none",
              title: res.msg,
              duration: 1500,
            });
          }
        },
        fail: function (res) {
          console.log(res.msg);
        },
      });
    },
    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,
            });
            if (!that.data.playBackShow) {
              that.setData({
                active: 1,
              });
              that.data.apiType = "recommend";
              that.getRecommend();
              that.data.RecommendBol = false;
            }
          } else {
            // wx.showToast({
            // 	icon: 'none',
            // 	title: res.msg,
            // 	duration: 1500
            // })
            console.log(res.msg);
          }
        },
        fail: function (res) {
          console.log(res.msg);
        },
      });
    },
    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,
        });
      }
    },
  },
  observers: {
    params: function () {
      this.setData({
        active: 1,
        status: "",
        announceList: "",
        broadcastlist: "",
        apiType: "recommend",
        playBackData: "",
        playBackShow: false,
        recommendData_activitys1: [],
        recommendData_activitys2: [],
        broadcastlist_activitys1: [],
        broadcastlist_activitys2: [],
        announceList_activitys1: [],
        announceList_activitys2: [],
        playBackData_activitys1: [],
        playBackData_activitys2: [],
        recommendData: [],
        re_colOneHeight: 0,
        re_colTwoHeight: 0,
        br_colOneHeight: 0,
        br_colTwoHeight: 0,
        an_colOneHeight: 0,
        an_colTwoHeight: 0,
        pl_colOneHeight: 0,
        pl_colTwoHeight: 0,
        RecommendBol: true,
        allLiveBol: true,
        broadcastlistBol: true,
        announceListBol: true,
        playBackDataBol: true,
        //more
        isLoadMore_br: true,
        page_br: 1,
        total_page_br: 0,
        //more
        isLoadMore_an: true,
        page_an: 1,
        total_page_an: 0,
        //more
        isLoadMore_pl: true,
        page_pl: 1,
        total_page_pl: 0,
        showNumber: 10,
        allLivelistData: [],
        allLivelistData_activitys1: [],
        allLivelistData_activitys2: [],
        all_colOneHeight: 0,
        all_colTwoHeight: 0,
        //more
        isLoadMore_all: true,
        page_all: 1,
        total_page_all: 0,
        notload_all: true,
        notload_br: true,
        notload_an: true,
        notload_pl: true,
      });
      let showNumber = this.data.params.number;
      if (showNumber) {
        this.data.showNumber = showNumber;
      }
      if (this.data.params.type && this.data.params.type == 0) {
        this.setData({
          active: 0,
        });
        this.data.apiType = "AllLivelist";
        this.getAllLivelist();
        this.data.allLiveBol = false;
      } else if (this.data.params.type && this.data.params.type == 1) {
        this.setData({
          active: 1,
        });
        this.data.apiType = "recommend";
        this.getRecommend();
        this.data.RecommendBol = false;
      } else if (this.data.params.type && this.data.params.type == 2) {
        this.setData({
          active: 2,
        });
        this.data.apiType = "liveList";
        this.data.status = 3;
        this.getLiveList(3);
        this.data.broadcastlistBol = false;
      } else if (this.data.params.type && this.data.params.type == 3) {
        this.setData({
          active: 3,
        });
        this.data.apiType = "liveList";
        this.data.status = 2;
        this.getLiveList(2);
        this.data.announceListBol = false;
      } else if (this.data.params.type && this.data.params.type == 4) {
        this.setData({
          active: 4,
        });
        this.data.apiType = "playBack";
        this.getplayBack();
        this.data.playBackDataBol = false;
      } else {
        this.setData({
          active: 1,
        });
        this.data.apiType = "recommend";
        this.getRecommend();
        this.data.RecommendBol = false;
      }
      this.getPlayBackShow();
    },
  },
});
