// pages/account/course/read/detail.js
const util = require("../../../../utils/util.js")
Page({
  data: {
    id:'',
    playerId:'',
    data: { title: "", content: "", readerNum: 0, myRead:0},
    alertStatus: false,//弹窗
    alert:{},
    showBack: false,
  },
  onLoad: function (options) {
    this.setData({id:options.id, playerId:options.playerId})
    let that = this;
    util.ajax({
      func: "v2/course/read/detail",
      data: {"id":that.data.id,playerId:that.data.playerId}
    }, function (res) {
      if (res.code == 0) {
        res.data.content = res.data.content.replace(/section/g, "div");
        res.data.content = res.data.content.replace(/<img/g, "<img style='max-width:100%'");
          that.setData({data:res.data});
      }else{
        util.showTips(res.reason);
      }
    });

  },
  read() {
    let that = this;
    util.ajax({
      func: "v2/course/read/confirm",
      method:"POST",
      data: { id: that.data.id, playerId: that.data.playerId},
      contentType: 'application/x-www-form-urlencoded'
    }, function (res) {
      if (res.code == 0) {
        let alert={};
        if (res.data.status==1){//计算积分
            alert= {icon: 'read-ok',title: '恭喜您,又进步一点',score: res.data.score}
        }else{//无效
            alert = { icon: res.data.overtime == 0 ? 'read-ok': 'time-out', title: '恭喜您,又进步一点', content: res.data.message, status: 1}
        }
        that.setData({alertStatus: true,alert});
      }else{
        util.showTips(res.reason);
      }
    });
  },
  close(){
    this.data.data.readerNum = this.data.data.readerNum*1 + 1
    this.data.data.myRead=1;
    this.setData({ data: this.data.data})
    
  },
  toTop() {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 500
    })
  },
  onPageScroll(e) {
    var showBack = this.data.showBack;
    if (e.scrollTop > 400) {
      if (!showBack) {
        this.setData({ showBack: true })
      }
    } else {
      if (showBack) {
        this.setData({ showBack: false })
      }
    }
  }
  
})