// packageD/businessActivity/businessActivity.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    Data: {},
    showAll: false, //内容是否全部展示
    time: "",
    Day: "", //剩余天
    Hour: "", //剩余小时
    Min: "", //剩余分钟
    Sec: "", //剩余秒
    stop: false, //判断活动是否结束
    songStop: true, //判断停止还是播放
    button: 0, //是否开启悬浮按钮
    button_name: "", //悬浮按钮名称
    goods_id: "", //商品id
    is_store: 0, //联盟商家是否开启
    wheels: [], //免费详情数据
    start: "", //活动开始时间
    end: "", //活动结束时间
    commission_amount: [], //排行榜数据
    timeShow: 0, //是否开启倒计时
    stores: [], //返回的联盟商家数据
    business: [], //组装后的联盟商家数据
    link: "",
    readyState: 0, //音频是否加载完成
    clientWidths: "",
    member_count: "", //参与人数
    shareInfo: {},
    isSetShare: 0, //是否开启了自定义分享 ，0||关闭
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id,
    });
    this.initData();
    this.getData();
  },
  linkTo() {
    if (app._isTextEmpty(this.data.link1)) {
      wx.navigateTo({
        url: "/packageA/member/storeApply/storeApply",
      });
    }
  },
  initData() {
    this.setData({
      Data: {},
      showAll: false, //内容是否全部展示
      time: "",
      Day: "", //剩余天
      Hour: "", //剩余小时
      Min: "", //剩余分钟
      Sec: "", //剩余秒
      stop: false, //判断活动是否结束
      songStop: true, //判断停止还是播放
      button: 0, //是否开启悬浮按钮
      button_name: "", //悬浮按钮名称
      goods_id: "", //商品id
      is_store: 0, //联盟商家是否开启
      wheels: [], //免费详情数据
      start: "", //活动开始时间
      end: "", //活动结束时间
      commission_amount: [], //排行榜数据
      timeShow: 0, //是否开启倒计时
      stores: [], //返回的联盟商家数据
      business: [], //组装后的联盟商家数据
      link: "",
      readyState: 0,
      clientWidths: "",
      member_count: "",
      shareInfo: {},
      isSetShare: 0,
    });
  },
  play(e) {
    if (e.currentTarget.dataset.info == "plays") {
      this.innerAudioContext.pause();
      this.setData({
        songStop: true,
      });
    } else {
      this.innerAudioContext.play();
      this.setData({
        songStop: false,
      });
    }
  },
  showAlltap(e) {
    this.setData({
      showAll: !this.data.showAll,
    });
  },
  //活动倒计时
  countdown() {
    // 当前时间戳
    const now = Date.parse(new Date()) / 1000;
    if (this.data.end - now <= 0) {
      this.stop = true;
      return;
    }
    // 相差的秒数
    const msec = this.data.end - now;
    // 计算的时间
    let day = parseInt(msec / 60 / 60 / 24);

    let hr = parseInt((msec / 60 / 60) % 24);
    let min = parseInt((msec / 60) % 60);
    let sec = parseInt(msec % 60);
    // 个位数前补零
    hr = hr > 9 ? hr : "0" + hr;
    min = min > 9 ? min : "0" + min;
    sec = sec > 9 ? sec : "0" + sec;
    // 控制台打印
    // this.time=`${day}天 ${hr}小时 ${min}分钟 ${sec}秒`
    // (this.Day = day), (this.Hour = hr), (this.Min = min), (this.Sec = sec);
    this.setData({
      Day: day,
      Hour: hr,
      Min: min,
      Sec: sec,
    });
    // 一秒后递归
    setTimeout(() => {
      this.countdown();
    }, 1000);
  },
  toGoods(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/packageA/detail_v2/detail_v2?id=" + id,
    });
  },
  toPage(e) {
    let val = e.currentTarget.dataset.val;
    if (!app._isTextEmpty(val.store_name)) {
      wx.navigateTo({
        url: "/packageC/o2o/o2oStore/o2oStore?store_id=" + val.id,
      });
    } else if (!app._isTextEmpty(val.hotel_name)) {
      wx.navigateTo({
        url: "/packageC/hotel/HotelHome/HotelHome?id=" + val.id,
      });
    }
  },
  gobannerlink(e) {
    try {
      wx.navigateTo({
        url: this.data.link2,
      });
    } catch (err) {
      console.log(err);
    }
  },
  gomiandan(e) {
    let link = e.currentTarget.dataset.info.link;
    if (app._isTextEmpty(link)) {
      return;
    } else {
      try {
        wx.navigateTo({
          url: link,
        });
      } catch (err) {
        console.log(err);
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getData() {
    let urlStr = app.getNetAddresss(
      "plugin.business-activity.frontend.controllers.activity.getActivityById"
    );
    let that = this;
    app._getNetWork({
      data: {
        activity_id: 2,
      },
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          if (res.data.activity.audio_link) {
            that.autoPlay();
            that.innerAudioContext.src = res.data.activity.audio_link;
            that.innerAudioContext.play();
            that.setData({
              songStop: false,
            });
          }
          for (let i = 0; i < res.data.wheels.length; i++) {
            if (app._isTextEmpty(res.data.wheels[i].has_one_store)) {
              res.data.wheels[i].showStore = false;
            } else {
              res.data.wheels[i].showStore = true;
            }
          }
          that.setData({
            Data: res.data.activity,
            button: res.data.activity.button,
            button_name: res.data.activity.button_name,
            member_count: res.data.member_count,
            goods_id: res.data.goods_id,
            wheels: res.data.wheels,
            is_store_button: res.data.is_store_button,
            stores: res.data.stores.concat(res.data.hotels),
            commission_amount: res.data.commission_amount,
            start: res.data.activity.countdown_time[0],
            end: res.data.activity.countdown_time[1],
            timeShow: that.showTime() ? res.data.activity.countdown : 0,
            isSetShare: res.data.share_status,
            shareInfo: res.data.share_data,
            // 广告链接
            link1: res.data.activity.miniLink1,
            // banner链接
            link2: res.data.activity.miniLink2,
            miandanPto: true,
          });
          console.log(that.data.wheels);
          wx.setNavigationBarTitle({
            title: res.data.activity.name,
          });
          that.countdown();
          that.StoreArr();
          console.log(that.data.business);
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  //组装返回的联盟商家数据
  StoreArr() {
    let n = Math.ceil(this.data.stores.length / 10);
    let arr = [];
    for (let i = 1; i <= n; i++) {
      arr.push(this.data.stores.slice((i - 1) * 10, i * 10));
      // this.business.push(arr);
    }
    this.setData({
      business: this.data.business.concat(arr),
    });
    console.log(this.data.business);
  },
  showTime() {
    let nowTime = new Date().getTime() / 1000;
    let beginTime = this.data.start;
    console.log(nowTime >= beginTime);
    if (nowTime >= beginTime) {
      return true;
    } else {
      return false;
    }
  },
  autoPlay() {
    this.innerAudioContext = wx.createInnerAudioContext();
    this.innerAudioContext.onCanplay(() => {});
    this.innerAudioContext.onPlay(() => {
      console.log("开始播放");
      console.log(this.innerAudioContext.duration);
    });
  },
});
