<template>
	<view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title='音色'></cus-header>
		<!-- <div class="top adfacjb" v-if="voice">
			<div class="tl">
				<image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/07/04/a09113ef-34fe-4105-bc4e-84ab7351ffc8.gif"></image>
				<text>{{voice.name||''}}</text>
			</div>
			<div class="tr adfac">
				<div class="tr_btn qx" @tap="cancel">取消</div>
				<div class="tr_btn qr" @tap="confirm">确认</div>
			</div>
		</div> -->
		<template v-if="list.length">
			<div class="list">
				<div class="item adfacjb" :class="{'active':index===idx}" v-for="(item,index) in list" :key="index" @tap.prevent="selectVoice(item,index)">
					<div class="il adfac">
						<image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/07/04/6a79f5d7-2d41-4405-97e2-e93876797ec8.png" v-if="item.sex==1"></image>
						<image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/07/04/c4eaec06-7de9-4f1c-b76d-ead9a4a38373.png" v-else-if="item.sex==2"></image>
						<text>{{item.name}}</text>
					</div>
					<div class="ir">
						<image @tap.stop="playOnOff(0,item,index)" src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/07/04/fbfe67d5-a69b-4bb1-81a5-83d19403b88e.png" v-if="item.play"></image>
						<image @tap.stop="playOnOff(1,item,index)" src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/07/04/9ef5fc85-05b2-4681-b37e-47cf843f5b6c.png" v-else></image>
					</div>
				</div>
			</div>
		</template>
		<template v-else>
			<page-empty :height="'calc(100vh - 160rpx)'"></page-empty>
		</template>
	</view>
</template>

<script>
	import pageEmpty from '@/components/pageEmpty/index.vue'
	export default {
		components:{pageEmpty},
		data(){
			return {
				audioCtx: null,
				idx:'',
				list:[],
				voice:null,
			}
		},
		onLoad() {
			this.getModelVoiceList();
		},
		mounted() {
			this.audioCtx = uni.createInnerAudioContext()	
		},
		methods:{
			getModelVoiceList(){
				this.$api.get(`/ttsVoice`,{
					ttsModelId:'TTS_EdgeTTS',
					page:1,
					limit:-1
				}).then(res=>{
					if(res.data.code!==0) return this.$showToast(res.data.msg)
					this.list = res.data.data.list;
					this.list.forEach((l,i)=>{
						if(l.name.indexOf('男声')>-1) l.sex=1
						else if(l.name.indexOf('女声')>-1) l.sex=2
						this.$set(this.list[i],'play',false);
					})
				})
			},
			selectVoice(item,index){
				this.voice = item;
				this.idx = index;
				this.confirm();
			},
			playOnOff(type,item,index){
				let idx = this.list.findIndex(l=>l.play);
				if(idx!==index) {
					this.audioCtx.pause();
					this.audioCtx.src = '';
				}
				
				this.voice = item;
				this.audioCtx.src = item.voiceDemo;
				this.list.forEach((l,i)=>{
					this.$set(this.list[i],'play',false);
				})
				this.$set(this.list[index],'play',type);
				uni.pageScrollTo({ scrollTop: 0, duration: 100});
				
				if(type==1) this.audioCtx.play();
				else if(type===0) this.audioCtx.pause();
			},
			cancel(){
				this.voice = null;
				this.list.forEach((l,i)=>{
					this.$set(this.list[i],'play',false);
				})
			},
			confirm() {
				if(!this.voice) return this.$showToast('请选择音色');
				this.getOpenerEventChannel().emit('selectVoice',this.voice)
				uni.navigateBack();
			}
		},
		beforeDestroy() {
			if (this.audioCtx) {
				this.audioCtx.destroy();
				this.audioCtx = null;
			}
		}
	}
</script>

<style scoped lang="less">
	.page{
		width: 100%;
		padding: 0 30rpx 140rpx;
		background: #FFFFFF;
		box-sizing: border-box;
		
		.top{
			margin-top: 32rpx;
			border-radius: 16rpx;
			border: 1rpx solid #EEEEEE;
			padding: 16rpx 24rpx;
			.tl{
				image{
					width: 40rpx;
					height: 22rpx;
				}
				text{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #111111;
					line-height: 40rpx;
					margin-left: 9rpx;
				}
			}
			.tr{
				.tr_btn{
					width: 104rpx;
					height: 56rpx;
					border-radius: 14rpx;
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #111111;
					line-height: 56rpx;
					text-align: center;
					margin-left: 20rpx;
					&.qx{
						border: 1rpx solid #D1D5DB;
					}
					&.qr{
						border: 1rpx solid #D9F159;
						background: #D9F159;
					}
				}
			}
		}
		
		.list{
			margin-top: 35rpx;
			.item{
				box-shadow: inset 0rpx -1rpx 0rpx 0rpx #E2E2E2;
				padding: 24rpx 10rpx 24rpx 0;
				width: 100%;
				box-sizing: border-box;
				.il{
					image{
						width: 88rpx;
						height: 88rpx;
					}
					text{
						font-family: PingFang-SC, PingFang-SC;
						font-weight: bold;
						font-size: 32rpx;
						color: #111111;
						line-height: 32rpx;
						margin-left: 30rpx;
					}
				}
				.ir{
					image{
						width: 52rpx;
						height: 52rpx;
					}
				}
			}
		}
	}
</style>