// packageH/Advertising/advertisingPlayPage/advertisingPlayPage.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Loadeddata: false, //是否视频元数据已加载完成
    myVideo: null,
    isPlaying: false,
    ADDetails: {}, //广告详情数据
    duration: null,
    countdown: null,
    list: [],
    showReward: false,
    redpack_amount: "",
    advertising_id: 0,
    isMuted: false, //是否静音
    page: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.advertising_id = this.options.aid;
    this.data.page = this.options.page;
    this.setData({
      showReward: false
    });
    this.getData();
  },

  changeMuted() {
    this.setData({
      isMuted: !this.data.isMuted
    });
  },
  clickADVideo() {
    this.setData({
      isPlaying: true
    });
  },
  getData() {
    let json = {};
    if (this.options.id && this.options.id != 0) {
      json.put_in_type = this.options.tag == "video" ? "2" : "1"; //投放类型:1-直播间，2-短视频
      json.put_in_id = this.options.id; //投放短视频/直播间ID
    }
    json.page = this.options.page;
    this.page = this.options.page || 1;
    let urlStr = app.getNetAddresss('plugin.ad-serving.frontend.advertising.index.getList');
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          let list = res.data.list.data;
          for (let i = 0; i < list.length; i++) {
            if (list[i].id == this.data.advertising_id) {
              list.splice(0, i);
              this.setData({
                list: list
              });
              break;
            }
          }
          this.current_page = res.data.list.current_page;
          this.last_page = res.data.list.last_page;
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
          if (app._isTextEmpty(res.data)) {
            wx.navigateBack({
              delta: 1
            });
          }
        }
        this.getADDetails();
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  getADDetails() {
    let urlStr = app.getNetAddresss('plugin.ad-serving.frontend.advertising.play.getAdvertisingVideoData');
    app._postNetWork({
      url: urlStr,
      data: {
        advertising_id: this.data.advertising_id
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            ADDetails: res.data.has_one_advertising_apply
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
          if (res.data.status && res.data.status == 1) {
            // 1--视频，直播删除，下播 ，2--广告超时，下架
            //跳转回视频、直播
            wx.navigateBack({
              delta: 2
            });
          } else {
            this.nextADData();
            //下一个广告
          }
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  nextADData() {
    this.data.list.splice(0, 1);
    if (this.data.list.length == 0) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '广告已到底',
        success(res) {
          if (res.confirm) {
            wx.navigateBack({
              delta: 1
            });
          }
        }
      });
      return;
    }

    this.setData({
      list: this.data.list,
      advertising_id: this.data.list[0].id,
      redpack_amount: '',
      showReward: false
    });
    this.getADDetails();
  },
  cloneADs() {
    try {
      wx.navigateBack({
        delta: 1
      });
    } catch (error) {
      wx.reLaunch({
        url: '/packageG/index/index',
      });
    }
  },
  addAdsertisingBrowse(action_type) {
    // 播放量，点击进去就记一次，不管有没有看完，分享量，分享出去算一次，不论有没有人去点击
    let urlStr = app.getNetAddresss('plugin.ad-serving.frontend.advertising.play.addAdsertisingBrowse');
    app._postNetWork({
      url: urlStr,
      data: {
        advertising_id: this.data.advertising_id,
        action_type: action_type
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result != 1) {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }

      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  bindloadedmetadata(e) {
    this.setData({
      duration: Math.ceil(e.detail.duration),
      countdown: Math.ceil(e.detail.duration),
      Loadeddata: true
    });
  },
  bindtimeupdate(e) {
    this.setData({
      countdown: this.data.duration - Math.ceil(e.detail.currentTime)
    });
  },
  bindended(e) {
    this.redpackRewardAction();
  },
  bindpause(e) {
    this.setData({
      isPlaying: false
    });
  },
  bindplay() {
    this.setData({
      isPlaying: true,
      Loadeddata: false
    });
    this.addAdsertisingBrowse(1);
  },
  redpackRewardAction() {
    //播放结束，领取红包
    let urlStr = app.getNetAddresss('plugin.ad-serving.frontend.advertising.play.redpackRewardAction');
    app._postNetWork({
      url: urlStr,
      data: {
        advertising_id: this.data.advertising_id
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            redpack_amount: res.data.redpack_amount,
            showReward: true
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
          this.nextADData(); //下一个广告
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  getMore() {
    let json = {};
    if (this.options.id && this.options.id != 0) {
      json.put_in_type = this.options.tag == "video" ? "2" : "1"; //投放类型:1-直播间，2-短视频
      json.put_in_id = this.options.id; //投放短视频/直播间ID
    }
    json.page = this.data.page;
    let urlStr = app.getNetAddresss("plugin.ad-serving.frontend.advertising.index.getList");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            list: that.data.info.concat(res.data.list.data),
            current_page: res.data.current_page,
            last_page: res.data.last_page
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.current_page >= this.data.last_page) {
      return;
    } else {
      let pages = this.data.page + 1;
      this.setData({
        page: pages
      });
      this.getMore();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var value = wx.getStorageSync("yz_uid");
    var mid = "";
    if (value) {
      mid = value;
    }
    return {
      title: "广告播放",
      path: "/packageH/Advertising/advertisingPlayPage/advertisingPlayPage?tag=video&id=" + this.options.id + "&page=" + this.options.page + "&aid=" + this.options.aid + "&mid=" + mid,
    };
  }
});