// packageE/shortvideo/shortvideo.js
var app = getApp();
Component({
  properties: {
    datas: {
      type: null,
    },
    component_id: {
      type: null,
    },
    page_id: {
      type: null,
    },
    index: {
      type: null,
    },
    U_tabcontrol: {
      type: null,
    },
    bottomShowBolIndex: {
      type: null,
    },
    plugin_active: {
      // 会员中心商品加载
      type: null,
    },
    store_alone_temp: {
      // 门店独立判断
      type: null,
    }
  },
  // 私有数据，可用于模板渲染
  data: {
    emptyImage: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/image.png",
    clientWidth: "375",
    language: "",

    list: [],
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
  },

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {
      let language = wx.getStorageSync("langIndex");
      this.setData({
        language: language.en
      });
    },
    moved() {},
    detached() {},
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {},
  // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {
    if (
      Number(this.data.datas.get_number) > 0 &&
      this.data.datas.get_style == 1
    ) {
      this.getData(this.data.datas.list);
    } else {
      this.setData({
        isLoadMore: false,
      });
    }
    this.setData({
      clientWidth: wx.getSystemInfoSync().windowWidth,
    });
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },
  methods: {
    // 视频
    getData(data) {
      this.setData({
        page: 1,
        isLoadMore: true,
        total_page: data.last_page,
      });

      if (this.data.total_page <= 1) {
        this.setData({
          isLoadMore: false,
        });
      }

      if (data.data.length > this.data.datas.get_number) {
        data.data.splice(
          this.data.datas.get_number,
          data.data.length - this.data.datas.get_number
        );
        this.setData({
          isLoadMore: false,
        });
      }

      this.setData({
        list: data.data,
      });
    },
    // 获取更多数据
    getMoreData() {
      let that = this;
      that.setData({
        isLoadMore: false,
      });
      if (that.data.page >= that.data.total_page) {
        return;
      } else {
        that.setData({
          page: that.data.page + 1,
        });
        let urlStr = app.getNetAddresss("home-page.get-decorate-page");
        urlStr += "&page=" + that.data.page;
        urlStr += "&decorate_id=" + that.data.page_id;
        urlStr += "&component_id=" + that.data.component_id;

        if (this.data.U_tabcontrol) {
          urlStr += "&component_key=U_tabcontrol";
          urlStr +=
            "&component_info=" + JSON.stringify({
              list_key: this.data.index
            });
        } else {
          urlStr += "&component_key=U_shortvideo";
        }

        app._getNetWork({
          url: urlStr,
          success: (resdata) => {
            var res = resdata.data;
            if (res.result == 1) {
              let data = [];
              if (this.data.U_tabcontrol) {
                data = that.data.list.concat(res.data[0].remote_data.list.data);
              } else {
                data = that.data.list.concat(res.data.data);
              }

              if (that.data.page < that.data.total_page) {
                that.setData({
                  isLoadMore: true,
                });
              }

              if (data.length > this.data.datas.get_number) {
                data.splice(
                  this.data.datas.get_number,
                  data.length - this.data.datas.get_number
                );
                this.setData({
                  isLoadMore: false,
                });
              }

              this.setData({
                list: data,
              });
            } else {
              that.setData({
                page: that.data.page - 1,
                isLoadMore: false,
              });
            }
          },
          fail: function (res) {
            console.log(res);
          },
        });
      }
    },
    // 会员中心用的
    loadMoreMemberData() {
      let that = this;
      that.setData({
        isLoadMore: false,
      });
      if (that.data.page >= that.data.total_page) {
        return;
      } else {
        that.setData({
          page: that.data.page + 1,
        });
        let urlStr = app.getNetAddresss("plugin.video-share.frontend.video.getList");
        if (this.data.store_alone_temp == 1) {
          // 门店独立
          urlStr = app.getNetAddresss("plugin.store-alone-temp.frontend.member.nav-data");
          urlStr += '&code=' + that.data.plugin_active;
        }
        urlStr += "&page=" + that.data.page;

        app._getNetWork({
          url: urlStr,
          success: (resdata) => {
            var res = resdata.data;
            if (res.result == 1) {
              let data = [];
              data = that.data.list.concat(res.data.list.data);

              if (that.data.page < that.data.total_page) {
                that.setData({
                  isLoadMore: true,
                });
              }

              if (data.length > this.data.datas.get_number) {
                data.splice(
                  this.data.datas.get_number,
                  data.length - this.data.datas.get_number
                );
                this.setData({
                  isLoadMore: false,
                });
              }
              that.triggerEvent("setLoadMore", {
                isLoadMore: that.data.isLoadMore,
              });
              this.setData({
                list: data,
              });
            } else {
              that.setData({
                page: that.data.page - 1,
                isLoadMore: false,
              });
            }
          },
          fail: function (res) {
            console.log(res);
          },
        });
      }
    },
  },
});