// pages/classDetails/classDetails.js
import Toast from '../../dist/toast/toast';
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mediaId: 0,
    courseTitle:'',
    teacherName: '',
    teacherProfile: '',
    frontImage: '',
    courseComment: '',
    mediaCount: '',
    mediaList: [],
    videoHeight:'150',
    play:0,
    fufeiPage:0,
    playUrl:'',
    money:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      mediaId: options.mediaId
    })
    //获取详细信息
    this.classDetails()
  },

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

  },

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

  },

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

  },

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

  },

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

  },
  cliLearn(e){
    var that = this;
    let id = e.currentTarget.dataset['id'];
    wx.getStorage({
      key: 'config',
      success: function(res) {
        if(res){
          wx.request({
            url: app.url + 'ajax_get_play_media_detail.php',
            data: {
              mediaId: id,
              unionId: res.data.unionId
            },
            success: function (resp) {
              console.log(resp)
              if(resp.data.status=='OK'){
                if (resp.data.result.courseMediaDat.haveBuy && !resp.data.result.courseMediaDat.is_free || resp.data.result.courseMediaDat.is_free){
                  that.setData({
                    play:1,
                    playUrl:resp.data.result.courseMediaDat.media
                  })
                }else{
                  that.setData({
                    playUrl: resp.data.result.courseMediaDat.media,
                    fufeiPage:1,
                    money: resp.data.result.courseMediaDat.price
                  })
                }
              }
            }
          })
        }
      },
    })
    
    // console.log(index)
    // var playUrl = this.data.mediaList[index].media;
    // playUrl=playUrl.split('?')[0];
    // this.setData({
    //   playUrl:playUrl,
    //   play:1
    // })
  },
  // 查询课程详细
  classDetails(){
    var that = this;
    var config = wx.getStorageSync('config');
    wx: wx.request({
      url: app.url + 'ajax_get_course_detail.php',
      data: {
        unionId: config.unionId,
        mediaId:that.data.mediaId
      },
      header: { 'content-type': 'application/json' },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        console.log(res)
        if (res.statusCode == 200) {
          that.setData({
            courseTitle:res.data.result.courseTitle,
            teacherName: res.data.result.teacherName,
            teacherProfile: res.data.result.teacherProfile,
            frontImage: res.data.result.frontImage,
            courseComment: res.data.result.courseComment,
            mediaCount: res.data.result.mediaCount,
            mediaList: res.data.result.mediaList
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  videoplay(){
    if(this.data.show1){
      this.setData({
        fufeiPage:1
      })
      this.fufeilook()
    }else{
      this.setData({
        play: 1
      })
      this.classDerails();
      var query = wx.createSelectorQuery();
      var that = this;
      query.select('.video').boundingClientRect(function (rect) {
        var videoHeight = (rect.width * 9) / 16;
        that.setData({
          videoHeight: videoHeight
        })
      }).exec();
    }
  },
  backplay(){
    this.setData({
      play:0
    })
  },
  // 付费观看
  fufeilook(){
    var config = wx.getStorageSync('config');
    wx: wx.request({
      url: app.url + 'ajax_set_buy_media.php',
      data: {
        unionId: config.unionId,
        openId:1,
        courseId:1,
        mediaId:1
      },
      header: { 'content-type': 'application/json' },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        console.log(res)
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  backfufei(){
    this.setData({
      fufeiPage:0
    })
  },
  surefufei(){
    var that=this;
    wx.getStorage({
      key: 'config',
      success: function(res) {
        if(res){
          wx.request({
            url: app.url + 'ajax_buy_course_media.php',
            data: {
              unionId:res.data.unionId,
              openId:res.data.openId,
              mediaId: that.data.mediaId
            },
            success:function(resp){
              console.log(resp)
              if(resp.data.status=='OK'){
                var result = JSON.parse(resp.data.result);
                wx.requestPayment({
                  timeStamp: result.timeStamp,
                  nonceStr: result.nonceStr,
                  package: result.package,
                  signType: 'MD5',
                  paySign: result.paySign,
                  success(res) {
                    that.setData({
                      fufeiPage: 0,
                      play:1
                    })
                  },
                  fail(res) {
                    Toast.fail('支付失败');
                   }
                })
              }
            }
          })
        }
      },
    })
    
    // this.setData({
    //   fufeiPage: 0
    // })
  }
})