// packageA/member/course/VoiceDetail/VoiceDetail.js
var app = getApp();
let innerAudioContext = null;
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    stop1: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/stop1.png",
    play1: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/play1.png",
    bfstatus: false,
    currentTime: "",
    slmin: "",
    valueSlmin: 0,
    audio_length: 0,

    article_id: '',
    dataInfo: {
      audio_link: ''
    },
    // innerAudioContext: null,
    isPlay: false,
    // 是否有权限看
    isShow: 1,
    codeUrl: ''
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    innerAudioContext = wx.createInnerAudioContext();
    if (options.article_id) {
      this.setData({
        article_id: options.article_id
      });
    }
    this.getData();
    this.getQrCode();
  },

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

  },

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

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

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

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

  },

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

  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  audioPlay() {
    if(this.data.isPlay) {
      this.data.isPlay = false;
      this.innerAudioContext.pause();
    }else {
      this.data.isPlay = true;
      this.innerAudioContext.play();
    }
  },

  //获取数据
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.article.api.article.get-article");
    urlStr += '&article_id=' + this.data.article_id;
    urlStr += '&share_uid=';
    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          res.data.audio_link = decodeURIComponent(res.data.audio_link);
          that.setData({
            dataInfo: res.data,
          });
          innerAudioContext.src = encodeURI(this.data.dataInfo.audio_link);

          // this.innerAudioContext = wx.createAudioContext("myAudio");
          // this.innerAudioContext.setSrc(decodeURIComponent(res.data.audio_link))
          wx.setNavigationBarTitle({
            title: that.data.dataInfo.title ? that.data.dataInfo.title : ''
          });
        } else {
          wx.navigateBack({
            delta: 1
          });
          that.setData({
            isShow: 0
          });
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  tapbf(e) {
    if(this.data.bfstatus) {
      innerAudioContext.pause();
    }else {
      innerAudioContext.src = encodeURI(this.data.dataInfo.audio_link);
      innerAudioContext.play();
    }
    this.setData({
      bfstatus: !this.data.bfstatus,
    });
  },
  init() {
    innerAudioContext.onCanplay(() => {});
    innerAudioContext.onPlay(() => {
      console.log("开始播放");
      console.log(innerAudioContext.duration);
      this.setData({
        currentTime: this.realFormatSecond(innerAudioContext.duration),
        slmin: "0:00:00",
      });
    });
    innerAudioContext.onTimeUpdate(() => {
      setTimeout(() => {
        let smun = (innerAudioContext.currentTime / innerAudioContext.duration) * 100;
        var valueSlmin = parseInt(smun);
        // console.log(smun);
        this.setData({
          currentTime: this.realFormatSecond(innerAudioContext.duration),
          slmin: this.realFormatSecond(innerAudioContext.currentTime),
          valueSlmin: valueSlmin,
          audio_length: innerAudioContext.duration,
        });
      }, 1000);
    });
    innerAudioContext.onError((res) => {
      wx.showToast({
        title: "播放错误",
        icon: "none",
      });
    });
  },
  sildein(e) {
    var setvalue = e.detail.value / 100;
    var long = this.data.audio_length * setvalue;
    console.log(long, setvalue);
    innerAudioContext.seek(long);
  },
  realFormatSecond(second) {
    var secondType = typeof second;

    if (secondType === "number" || secondType === "string") {
      second = parseInt(second);

      var hours = Math.floor(second / 3600);
      second = second - hours * 3600;
      var mimute = Math.floor(second / 60);
      second = second - mimute * 60;

      return hours + ":" + ("0" + mimute).slice(-2) + ":" + ("0" + second).slice(-2);
    } else {
      return "0:00:00";
    }
  },
  getQrCode() {
    let that = this;
    let urlStr = app.getNetAddresss("member.member.getArticleQr");
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            codeUrl: res.data
          });
        } else {
          console.log(res.msg);
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
});
