// packageE/goodsrush/goodsrush.js
var app = getApp();
Component({
  properties: {
    datas: {
      type: null,
    },
    component_id: {
      type: null,
    },
    page_id: {
      type: null
    },
    bottomShowBolIndex: {
      type: null,
    },
    index: {
      type: null,
    },
    U_tabcontrol: {
      type: null,
    },
    bottomShowBol: {
      type: null,
    },
    isLast: {
      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",
    list: [],

    language: "",
    page: 1,
    total_page: 0,
    isLoadMore: true,
    allLoaded: false, // 全部数据已经加载完成
  },

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

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {},
  // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {
    if (this.data.datas.list) {
      this.setData({
        total_page: this.data.datas.list.last_page,
        page: this.data.datas.list.current_page,
        per_page: this.data.datas.list.per_page,
        list: this.data.datas.list.data.slice(0, this.data.per_page)
      });
      if (this.data.total_page <= this.data.page) {
        this.setData({
          isLoadMore: false,
          allLoaded: true,
        });
      }
    }
    this.setTime();
    // this.datas.get_number 限制数量
    if (this.data.datas.get_number && this.data.datas.get_number <= this.data.per_page) {
      this.setData({
        isLoadMore: false,
        allLoaded: true,
        list: this.data.datas.list.data.slice(0, this.data.datas.get_number)
      });
    }

    this.setData({
      clientWidth: wx.getSystemInfoSync().windowWidth,
    });
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },
  methods: {
    setTime() {
      let nowTime = new Date().getTime();
      for (let i = 0; i < this.data.list.length; i++) {
        if (this.data.list[i].start_time) {
          // let start_time = this.data.list[i].start_time.replace(/-/g, '/');
          // // .replace(/-/g, '/')兼容iOS和安卓   否则在iOS上会变成NaN
          // let beginTime = new Date(start_time).getTime();
          let start_time = this.data.list[i].start_time;
          let beginTime = start_time * 1000;
          this.setData({
            ["list[" + i + "].day"]: "00",
            ["list[" + i + "].hou"]: "00",
            ["list[" + i + "].min"]: "00",
            ["list[" + i + "].sec"]: "00",
          });
          if (nowTime > beginTime) {
            this._countDown(i, this.data.list[i].end_time);
            this.setData({
              ["list[" + i + "].begShow"]: true,
            });
          } else {
            this._countDown(i, this.data.list[i].start_time);
            this.setData({
              ["list[" + i + "].begShow"]: false,
            });
          }
        }
      }
    },
    //倒计时
    _countDown: function (index, endTime_1) {
      let newTime = new Date().getTime();
      // let endTime =  endTime_1 ? new Date(endTime_1.replace(/-/g, '/')).getTime() : 0;
      let endTime = endTime_1 ? endTime_1 * 1000 : 0;
      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({
          ["list[" + index + "].day"]: this._timeFormat(day),
          ["list[" + index + "].hou"]: this._timeFormat(hou),
          ["list[" + index + "].min"]: this._timeFormat(min),
          ["list[" + index + "].sec"]: this._timeFormat(sec),
        });
      } else {
        //活动已结束，全部设置为'00'
        this.setData({
          ["list[" + index + "].day"]: "00",
          ["list[" + index + "].hou"]: "00",
          ["list[" + index + "].min"]: "00",
          ["list[" + index + "].sec"]: "00",
        });
      }
      if (endTime - newTime < 0) {
        this.setData({
          ["list[" + index + "].day"]: "00",
          ["list[" + index + "].hou"]: "00",
          ["list[" + index + "].min"]: "00",
          ["list[" + index + "].sec"]: "00",
        });
      } else {
        setTimeout(this._countDown.bind(this, index, endTime_1), 1000);
      }
    },
    //小于10的格式化函数
    _timeFormat(param) {
      //小于10的格式化函数
      return param < 10 ? "0" + param : param;
    },
    gotoDetail(e) {
      let id = e.currentTarget.dataset.item.id;
      if (e.currentTarget.dataset.item.plugin_id == 78) {
        wx.navigateTo({
          url: '/packageA/detail_v2/detail_v2?id=' + id + '&md=1'
        });
      } else {
        wx.navigateTo({
          url: "/packageA/detail_v2/detail_v2?id=" + id,
        });
      }

    },
    // 获取更多数据
    getMoreData() {
      let that = this;
      if (this.data.page >= this.data.total_page) {
        this.setData({
          allLoaded: true
        });
        return;
      }
      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_goodsrush";
      }
      app._getNetWork({
        url: urlStr,
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            if (this.data.U_tabcontrol) {
              that.setData({
                page: res.data[0].remote_data.list.current_page,
                list: this.data.list.concat(res.data[0].remote_data.list.data),
              });
            } else {
              that.setData({
                page: res.data.current_page,
                list: this.data.list.concat(res.data.data),
              });
            }
            this.setTime();

            if (that.data.page < that.data.total_page) {
              that.setData({
                isLoadMore: true,
              });
            } else {
              that.setData({
                allLoaded: true,
              });
              return;
            }
            if (this.data.datas.get_number && this.data.list.length > this.data.datas.get_number) {
              this.setData({
                isLoadMore: false,
                allLoaded: true,
                list: this.data.list.slice(0, this.data.datas.get_number)
              });
            }
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false,
              allLoaded: true,
            });
          }
        },
        fail: function (res) {
          // console.log(res);
        },
      });
    },
    // 会员中心用的
    loadMoreMemberData() {
      let that = this;
      that.setData({
        isLoadMore: false
      });
      if (that.data.datas.current_page >= that.data.datas.last_page) {
        return;
      } else {
        that.data.datas.current_page += 1;
        let urlStr = app.getNetAddresss('member.member.plugin-data');
        if (this.data.store_alone_temp == 1) {
          // 门店独立
          urlStr = app.getNetAddresss("plugin.store-alone-temp.frontend.member.nav-data");
        }
        urlStr += '&page=' + that.data.datas.current_page;
        urlStr += '&code=' + that.data.plugin_active;
        app._getNetWork({
          url: urlStr,
          success: (resdata) => {
            var res = resdata.data;
            if (res.result == 1) {
              that.setData({
                list: this.data.list.concat(res.data.data.data),
              });
              this.setTime();
              if (that.data.datas.current_page < that.data.datas.last_page) {
                that.setData({
                  isLoadMore: true
                });
              } else {
                that.setData({
                  allLoaded: true
                });
              }
              that.triggerEvent("setLoadMore", {
                isLoadMore: that.data.isLoadMore,
              });
            } else {
              that.setData({
                page: that.data.datas.current_page - 1,
                isLoadMore: false,
                allLoaded: true
              });
            }
          },
          fail: function (res) {
            console.log(res);
          }
        });
      }
    },
  },
  observers: {
    bottomShowBol: function (flag) {
      if (flag && this.data.bottomShowBolIndex == this.data.index && this.data.datas.sort_style.choose_icon != 5 && this.data.isLoadMore) {
        this.getMoreData();
      }
    },
  },
});