<template>
  <!-- 底部信息流广告 -->
  <ad v-if="showBottomAd" :unit-id="adConfig.adUnitId" ad-type="feed"></ad>
  
  <!-- 激励视频弹窗 -->
  <view v-if="showRewardAd">
    <video-ad :unit-id="adConfig.adUnitId" @close="onAdClose"></video-ad>
  </view>
</template>

<script>
export default {
  props: {
    chapterId: Number,
    isVip: Boolean
  },
  data() {
    return {
      adConfig: {},
      showBottomAd: false,
      showRewardAd: false,
      chapterCount: 0
    }
  },
  watch: {
    chapterId() {
      this.loadAdConfig();
      this.chapterCount++;
      
      // 每5章触发激励广告
      if (!this.isVip && this.chapterCount % 5 === 0) {
        this.triggerRewardAd();
      }
    }
  },
  methods: {
    async loadAdConfig() {
      // 获取广告配置
      const res = await this.$http.get(`/ad/config/${this.chapterId}`);
      this.adConfig = res.data;
      
      // 展示底部广告（非VIP）
      this.showBottomAd = !this.isVip && this.adConfig.bottomAdEnabled;
    },
    
    triggerRewardAd() {
      if (!this.isVip && this.adConfig.rewardAdEnabled) {
        this.showRewardAd = true;
        
        // 发送广告计数
        this.$http.post('/ad/count', {
          userId: this.$store.state.user.id,
          chapterId: this.chapterId,
          platform: this.$platform
        });
      }
    },
    
    onAdClose() {
      this.showRewardAd = false;
    }
  }
}
</script>