
<template>
	<view class="" style="font-size: 0;position: relative;">
		<BackButton />
		<image v-if="agentInfo"  @click="toWebView" class="bannerImg" mode="aspectFill" :src="showImg(agentInfo.posterUrl)"> </image>
		
	</view>
</template>

<script>
	import BackButton from "@/components/BackButton.vue";
	export default{
		components: {
		  BackButton,
		},
		data(){
			return{
				img:'',
				agentId:'',
				agentInfo:null
			}
		},
		onLoad(e) {
			this.agentId = e.id
			this.getInfo()
		},
		onUnload() {
		  console.log("页面卸载，停止音频");
		  this.stopCurrentAudio();
		},
		onHide() {
		  console.log("页面隐藏，停止音频");
		  this.stopCurrentAudio();
		},
		methods:{
			stopCurrentAudio() {
			  try {
			    const app = getApp();
			    // 检查是否有背景音乐实例
			    if (app.globalData.bgMusic) {
			      app.globalData.bgMusic.stop();
			      app.globalData.bgMusic.destroy();
			      app.globalData.bgMusic = null;
			    }
			    // 恢复初始音乐
			    if (app.globalData.initMusicSrc) {
			      app.updateMusicSrc(app.globalData.initMusicSrc);
			      app.initBackgroundMusic();
			      uni.$bgMusic.play(); // 播放音乐
			    }
			    console.log("音频已停止并恢复初始音乐");
			  } catch (error) {
			    console.error("停止音频时出错:", error);
			  }
			},
			getInfo(){
				if(this.agentId==0){
					this.Post(
					  {
					  },
					  "/framework/agent/"+this.agentId,
					  "DES"
					).then((res) => {
					  if (res.code == 200) {
					   this.agentInfo = res.data
					   const app = getApp();
					   // 先停止当前音频
					   if (app.globalData.bgMusic) {
					     app.globalData.bgMusic.stop();
					   }
					   // 播放新的音频
					   app.updateMusicSrc(this.agentInfo.voiceUrl);
					   app.initBackgroundMusic(); // 初始化背景音乐
					   uni.$bgMusic.play(); // 播放音乐
					  } else {
					    uni.showToast({
					      title: res.msg,
					      icon: "none",
					    });
					  }
					});
				}else{
					
					this.Post(
					  {
					  },
					  "/framework/benefitPackage/detail/"+this.agentId,
					  "DES"
					).then((res) => {
					  if (res.code == 200) {
					   this.agentInfo = res.data.agent
					   const app = getApp();
					   // 先停止当前音频
					   if (app.globalData.bgMusic) {
					     app.globalData.bgMusic.stop();
					   }
					   // 播放新的音频
					   app.updateMusicSrc(this.agentInfo.voiceUrl);
					   app.initBackgroundMusic(); // 初始化背景音乐
					   uni.$bgMusic.play(); // 播放音乐
					  } else {
					    uni.showToast({
					      title: res.msg,
					      icon: "none",
					    });
					  }
					});
				}
				
			},
			toWebView() {
				if(this.agentInfo.agentId==0){
					uni.navigateTo({
						url: "/pages/agent/index?id="+this.agentInfo.agentId
					});
				}else{
					if(this.agentInfo.agentStatus){
						uni.navigateTo({
							url: "/pages/agent/index?id="+this.agentInfo.agentId
						});
					}else{
						uni.redirectTo({
							url: "/subPackages/equityGoods/detail?id="+this.agentId
						});
						// uni.showToast({
						//   	title: '请先购买当前商品',
						//   	icon: 'none'
						//   })
					}
				}
				
				
			},
		}
	}
</script>

<style lang="scss" scoped>
	.bannerImg{
		width: 100vw;
		height: 100vh;
	}
</style>