<template>
  <div class="list-wrap scroll-touch">
    <wsh-list-item  
      v-for="(item, index) in newsLists" 
      :key = 'index' :itemData = 'item' />
    <!-- <div class="list-wrap-ad">
      <ad unit-id="adunit-7496c51cbc82905d"></ad>
    </div> -->
    <div class="list-footer">
      <p v-if="moreStatus"> 没有更多数据</p>
    </div>
  </div>
</template>

<script>
import {get} from '@/utils/request'

import ListItem from './list-item'
export default {
  props: ['channelId'],
  data () {
    return {
      tips: '请稍后',
      show: true,
      animated: true,
      newsLists: [],
      page: 1,
      limit: 40,
      moreStatus: false,
      interstitialAd: null,
      rewardedVideoAd: null,
      onlyPlay: true
    }
  },
  onLoad () {
    // 加载视频广告
    if (wx.createRewardedVideoAd) {
      this.rewardedVideoAd = wx.createRewardedVideoAd({
        adUnitId: 'adunit-de36057cf600716b'
      })
      this.rewardedVideoAd.onLoad(() => {
        console.log('onLoad event emit')
      })
      this.rewardedVideoAd.onError((err) => {
        console.log('onError event emit', err)
      })
      this.rewardedVideoAd.onClose((res) => {
        console.log('onClose event emit', res)
      })
    }
    // 加载插屏广告
    if (wx.createInterstitialAd) {
      this.interstitialAd = wx.createInterstitialAd({ adUnitId: 'adunit-5ecedc2dc28ef414' })
      this.interstitialAd.onLoad(() => {
        console.log('onLoad event emit')
        console.log('插屏 广告加载成功')
      })
      this.interstitialAd.onError((err) => {
        console.log('onError event emit', err)
      })
      this.interstitialAd.onClose((res) => {
        console.log('onClose event emit', res)
      })
    }
  },
  components: {
    'wsh-list-item': ListItem
  },
  methods: {
    async getListDate (init) {
      if (init) {
        this.page = 1
        this.moreStatus = false
      }
      this.page++
      // wx.showNavigationBarLoading()
      const params = {
        page: this.page,
        limit: this.limit,
        channel_id: this.channelId,
      }
      // if (this.page !== 1) {
      //   wx.showToast({
      //     title: '加载中...',
      //     mask: true,
      //     icon: 'loading'
      //   })
      // }
      console.log('params', params)
      const newsData = await get('/v1/info/web-list', params)
      console.log('newDate', newsData)
      if (newsData.list.length < this.limit & this.page > 1) {
        this.moreStatus = true
      }
      if (init) {
        wx.stopPullDownRefresh() // 手动停止下拉刷新
        this.newsLists = newsData.list
      } else {
        wx.hideNavigationBarLoading() // 关闭下拉刷新加载
        if (newsData.list) {
          this.newsLists = this.newsLists.concat(newsData.list)
          this.onlyPlay = true
        } else {
        }
      }
    }
  },
  onPullDownRefresh () {
    wx.stopPullDownRefresh();
    // console.log('观看视频获取最新资讯')
    var that = this
    wx.showModal({
      title: '提示',
      content: '观看视频获取最新资讯',
      success (res) {
        if (res.confirm) {
          // console.log('用户点击确定')
          that.rewardedVideoAd.show()
            .then(() => console.log('激励视频 广告显示'))
            .catch(() => {
              that.rewardedVideoAd.load()
                .then(() => that.rewardedVideoAd.show())
                .catch(err => {
                  console.log('激励视频 广告显示失败', err)
                })
            })
          that.rewardedVideoAd.onClose(res => {
            // 用户点击了【关闭广告】按钮
            if (res && res.isEnded) {
              // 正常播放结束，可以下发游戏奖励
              wx.showToast({
                title: '更多最新资讯尽请期待',
                icon: 'none',
                duration: 2000
              });
            } else {
              // 播放中途退出，不下发游戏奖励
            }
          })
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
    // wx.startPullDownRefresh()
  },
  onReachBottom () {
    // if (!this.moreStatus) {
    //   // 没有更多了
    //   return false
    // }
    this.interstitialAd.show().catch((err) => {
      console.error(err)
    })
    this.interstitialAd.onClose(res => {
      console.log('插屏 广告关闭')
    })
    if (this.onlyPlay) {
      this.onlyPlay = false
      this.getListDate()
    }
    // console.log('上拉加载', this.page)
  },
  mounted () {
    this.getListDate(true)
  },
  created () {
  }
}
</script>

<style lang="scss" scoped>
.scroll-touch {
	-webkit-overflow-scrolling: touch;
}
.list-wrap {
  //  border-top: 1px solid #ebebeb;
  height: 100%;
  .list-wrap-ad {
    width: 100%;
  }
}
.list-footer {
  display: none;
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: center;
}
</style>

