// blog.js   emlog博客小程序作者QQ：809666838，AIR娱乐网www.airzyw.com制作，请保留该版权！！
import util from '../../utils/util.js';
let app = getApp();
var interstitialAd = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 0,
    data: [],
      imgUrls: [
      'http://png.hkiii.cn/blog/1.png',
      'http://png.hkiii.cn/blog/2.png',
      'http://png.hkiii.cn/blog/3.png'
    ],
    indicatorDots: true, //是否显示面板指示点
    autoplay: true, //是否自动播放
    interval: 3000, //停留时间间隔
    duration: 1000, //播放时长
    previousMargin: '50px', //前边距，可用于露出前一项的一小部分，接受 px 和 rpx 值
    nextMargin: '50px', //后边距，可用于露出后一项的一小部分，接受 px 和 rpx 值
    circular: true, //是否采用衔接滑动
    currentSwiperIndex: 0, //swiper当前索引,
    wordsData: [],
    backTopValue: false,
    nickname:'',
    excerpt: '',
  },
 
  swiperBindchange(e) {
    this.setData({
      currentSwiperIndex: e.detail.current
    })
  },
  
  goSortLogs: function (event) {
    console.log(event)
    var sid = event.currentTarget.dataset.sid;
    var sortname = event.currentTarget.dataset.sortname;
    wx.navigateTo({
      url: '../sorts-log/blog?sid=' + sid + '&sortname=' + sortname
    });
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getWords();
    this.getData();
    this.chaping();
  },

  getWords: function() {
    let that = this
    util.getWords(function(data) {
      console.log("qqqqqqqqqqqqqqqqqqqq")
      console.log(data)
      that.setData({
        wordsData: data
      })
    }); 
  },

  showblogInfo: function (event) {
    console.log(event)
    var gid = event.currentTarget.dataset.gid
    wx.navigateTo({
      url: '../blog-info/blog-info?gid=' + gid
    })
    qq.vibrateShort({
                    
     })
     this.cha();
  },
    chaping:function(){
     if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
       adUnitId: '8cc1f855e9b7438f99f2c57c5f72cd5a'  //这里是广告id
 })
      interstitialAd.onLoad(() => {
        console.log('onLoad event emit')
      })
      interstitialAd.onError((err) => {
        console.log('onError event emit', err)
      })
      interstitialAd.onClose((res) => {
        console.log('onClose event emit', res)
      })
    }
  },
   cha:function(){
 if (interstitialAd) {
            interstitialAd.show().catch((err) => {
              console.error(err)
            })
          }
  },
  getData: function (fromStart) {
    let isEnd = this.data.isend
    if (isEnd && !fromStart) return false
    var page = fromStart ? 1 : this.data.page + 1
    let that = this
    let oldData = this.data.data
    app.getSetting(setting => {
      console.log('setting', setting);
      util.getArticle(page, 0, function (data) {
        that.setData({
          page: page,
          data: fromStart ? data : oldData.concat(data),
          isend: data.length < setting.index_lognum // 判断每页文章条数，小于这条数说明是最后一页
        })

        wx.stopPullDownRefresh()
      });
    });
  },
  onReachBottom: function () {
    this.getData();
  },

  onPullDownRefresh: function () {
    this.getData(1);
  },
  onShareAppMessage: function (res) {         
    return {
      title: '分享一个超好玩的程序！',
      imageUrl: 'https://api.ixiaowai.cn/api/api.php', // 图片 URL
      path: ''
    }
  },
  // 监听滚动条坐标
  onPageScroll: function (e) {
    //console.log(e)
    var that = this
    var scrollTop = e.scrollTop
    var backTopValue = scrollTop > 500 ? true : false
    that.setData({
      backTopValue: backTopValue
    })
  },
 //盒子广告
 backTop: function () {
  qq.pageScrollTo({
  scrollTop: 0,
  duration: 300
})
  qq.showToast({
  title: '开火箭咯！',
  icon: 'cancel',
  duration: 2000
  })
},

newApp: function () {
  qq.navigateToMiniProgram({
  appId: '1109732502',
  extraData: {
    foo: 'bar'
  },
  envVersion: 'release',
  success(res) {
    // 打开成功
  }
})
},

jumpTo: function () {
  qq.navigateToMiniProgram({
  appId: '1109684282',
  extraData: {
    foo: 'bar'
  },
  envVersion: 'release',
  success(res) {
    // 打开成功
  }
})
},
})