// packageE/video_question/watch/watch.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    vid: "",
    videoTapPlay: false,
    loadOne: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        vid: options.id,
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadOne = false;
    this.getData();
    this.getinformation();
  },

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  currtime(e) {
    let cur = e.detail.currentTime.toFixed(1);
    let dur = e.detail.duration.toFixed(1);
    var that = this;
    console.log(cur, dur);
    console.log(Math.floor((cur / dur) * 100));
    let timestem = (cur / dur) * 100;
    if (timestem >= that.data.percentage) {
      if (!that.data.loadOne) {
        that.data.loadOne = true;
        let urlStr = app.getNetAddresss(
          "plugin.agent-qa.api.video.watch-video"
        );
        app._postNetWork({
          url: urlStr,
          data: {
            video_id: that.data.vid,
          },
          success: (resdata) => {
            var res = resdata.data;
            if (res.result == 1) {
              return;
            } else {
              wx.showToast({
                title: res.msg,
                icon: "none",
              });
            }
          },
          fail: (res) => {
            wx.showToast({
              title: res.msg,
              icon: "none",
            });
          },
        });
      }
    }
  },
  getinformation() {
    try {
      const res = wx.getSystemInfoSync();
      let windowHeight = res.windowHeight;
      // let windowWidth = res.windowWidth;
      this.data.screeheight = windowHeight;
    } catch (e) {
      // Do something when catch error
    }
  },
  tapPlay(e) {
    console.log(e);
    var that = this;
    const query = wx.createSelectorQuery();
    query.select("#pic").boundingClientRect();
    query.exec(function (res) {
      console.log(res[0].height);
      let screeheight = that.data.screeheight;
      if (res[0] && res[0].height) {
        let imgheight = res[0].height;
        console.log(imgheight > Math.ceil(screeheight * (7 / 8)));
        if (imgheight > Math.ceil(screeheight * (7 / 8))) {
          that.setData({
            minStyle: false,
          });
        } else {
          that.setData({
            minStyle: true,
          });
        }
      } else {
        that.setData({
          minStyle: true,
        });
      }
    });
    this.setData({
      videoTapPlay: true,
    });
  },
  getData() {
    var that = this;
    let urlStr = app.getNetAddresss("plugin.agent-qa.api.video.get-info");
    app._postNetWork({
      url: urlStr,
      data: {
        video_id: that.data.vid,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            videoSrc: res.data.url,
            fmimg: res.data.image,
            percentage: res.data.percentage,
          });
          wx.setNavigationBarTitle({
            title: res.data.title,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
        }
      },
      fail: (res) => {
        wx.showToast({
          title: res.msg,
          icon: "none",
        });
      },
    });
  },
});
