// packageE/goodsrush/goodsrush.js
const app = getApp();
Component({
  properties: {
    datas: {
      type: null,
    },
    component_id: {
      type: null,
    },
    page_id: {
      type: null
    },
    index: {
      type: null,
    },
    U_tabcontrol: {
      type: null,
    },
    bottomShowBol: {
      type: null,
    },
    bottomShowBolIndex: {
      type: null,
    },
  },
  // 私有数据，可用于模板渲染
  data: {
    emptyImage: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/image.png",
    clientWidth: "375",
    show_list: [],

    language: "",
    showMore: true,

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

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

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {},
  // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {
    if (this.data.datas.content_list.length > 0 && this.data.datas.content_list[0] instanceof Object) {
      this.setData({
        show_list: this.handle_list(this.data.datas.content_list),
      });
    } else {
      this.setData({
        show_list: this.data.datas.content_list,
      });
    }
    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,
        alreadyHandleArray: this.data.datas.list.data,
        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.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,
    });
    this.setData({
      showList: true,
    });
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },
  methods: {
    setTime() {
      let nowTime = new Date().getTime();
      let info = this.data.datas.list.data;
      for (let i = 0; i < info.length; i++) {
        let start_time = info[i].start_time;
        let beginTime = start_time * 1000;
        this.setData({
          ["datas.list.data[" + i + "].day"]: "00",
          ["datas.list.data[" + i + "].hou"]: "00",
          ["datas.list.data[" + i + "].min"]: "00",
          ["datas.list.data[" + i + "].sec"]: "00",
        });
        if (nowTime > beginTime) {
          this.setData({
            ["datas.list.data[" + i + "].begShow"]: true,
          });
        } else {
          this._countDown(i, info[i].start_time);
          this.setData({
            ["datas.list.data[" + i + "].begShow"]: false,
          });
        }
      }
    },
    tohistoryGroup(e) {
      let group_id = e.target.dataset.id || e.currentTarget.dataset.id;
      wx.navigateTo({
        url: `/packageF/others/historyGroup/historyGroup?group_id=${group_id}`,
      });
    },
    handle_list(list) {
      let handleDatas = [];
      if (list) {
        list.map((item, i) => {
          handleDatas[i] = item.value;
        });
      }
      return handleDatas;
    },
    //倒计时
    _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({
          ["datas.list.data[" + index + "].day"]: this._timeFormat(day),
          ["datas.list.data[" + index + "].hou"]: this._timeFormat(hou),
          ["datas.list.data[" + index + "].min"]: this._timeFormat(min),
          ["datas.list.data[" + index + "].sec"]: this._timeFormat(sec),
        });
      } else {
        //活动已结束，全部设置为'00'
        this.setData({
          ["datas.list.data[" + index + "].day"]: "00",
          ["datas.list.data[" + index + "].hou"]: "00",
          ["datas.list.data[" + index + "].min"]: "00",
          ["datas.list.data[" + index + "].sec"]: "00",
        });
      }
      if (endTime - newTime < 0) {
        this.setData({
          ["datas.list.data[" + index + "].day"]: "00",
          ["datas.list.data[" + index + "].hou"]: "00",
          ["datas.list.data[" + index + "].min"]: "00",
          ["datas.list.data[" + 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 item = e.target.dataset.item || e.currentTarget.dataset.item;
      if (item.store_id) {
        //门店拼团
        wx.navigateTo({
          url: "/packageA/detail_v2/detail_v2?id=" + item.id + "/" + item.store_id + "&name=group",
        });
        return;
      }

      wx.navigateTo({
        url: "/packageA/detail_v2/detail_v2?id=" + item.id + "&name=group",
      });
    },
    // 获取更多数据
    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_goodsgroup";
      }
      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,
                alreadyHandleArray: this.data.alreadyHandleArray.concat(res.data[0].remote_data.list.data),
              });
            } else {
              that.setData({
                page: res.data.remote_data.list.current_page,
                alreadyHandleArray: this.data.alreadyHandleArray.concat(res.data.remote_data.list.data),
              });
            }

            that.setData({
              'datas.list.data': this.data.alreadyHandleArray,
              arrIndex: this.data.arrIndex + 1
            });
            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);
        },
      });
    },
  },
  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();
      }
    },
  },
});