<template>
	<view class="wordPronItem">
		<text style="font-size: 20rpx;color:#4c50e7; position: absolute;top:10rpx;left:20rpx">{{type==1?'复习':""}}</text>
		<view class="num">
			{{cur}}/{{total}}
		</view>
		<view class="meaning">
			{{item.WordWrite}}
		</view>
		<view class="Japanese">
			{{item.WordContent}}
		</view>
		<view class="desc">
			<view class="attr">
				{{item.WordType}}
			</view>
			<view class="hiragana">
				[{{item.ChineseMean}}]
			</view>
		</view>
		<view class="listen" @click="play(item.FileUrl)">
			<template v-if="!isLoading">
				<i class="icon_audio_default" v-if="!isPlay"></i>
				<i class="icon_audio_playing" v-else></i>
			</template>
			<template v-else>
				<van-loading type="spinner" color="#7b7f80" />
			</template>
		</view>
		<view class="listenText">
			播放读音
		</view>
	</view>
</template>

<script>
	import {
		ref,
		reactive,
		toRefs,
		toRef,
		getCurrentInstance,
		watch,
		computed,
		onMounted,
		provide,
	} from "vue";
	const innerAudioContext = uni.createInnerAudioContext();
	innerAudioContext.autoplay = false;
	export default {
		props: {
			type: {
				type: Number,
				default: 0
			},
			cur: {
				type: Number,
				default: 0
			},
			total: {
				type: Number,
				default: 0
			},
			item: {
				type: Object,
				default: () => {
					return {}
				}
			},
		},
		setup(props) {
			let data = reactive({
				isPlay: false,
				isLoading: false
			})
			let methods = {
				play(src) {
					if(src){
						innerAudioContext.src=encodeURI(src);
					}else{
						innerAudioContext.src =
							`https://fanyi.baidu.com/gettts?lan=jp&text=${encodeURIComponent(props.item.WordContent)}&spd=3&source=web`
					}
					innerAudioContext.play();
					if(!data.isPlay){
						data.isLoading=true;
					}
					innerAudioContext.onCanplay(()=>{
						data.isLoading=false;
						data.isPlay = true;
					})
					innerAudioContext.onEnded((e)=>{
						data.isPlay = false;
						data.isLoading=false;
					})
				},
				stopVoice(){
					data.isPlay=false;
					innerAudioContext.stop();
					innerAudioContext.offCanplay(()=>{
						
					})
				}
			}
			return {
				...toRefs(data),
				...methods
			};
		}
	}
</script>

<style scoped>
	.icon_audio_default {
		background: transparent url('https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Static/educationStu/iconloop.png') no-repeat 0 0;
		width: 18px;
		height: 25px;
		vertical-align: middle;
		display: inline-block;
		-webkit-background-size: 54px 25px;
		background-size: 54px 25px;
		background-position: -36px center;
	}

	.icon_audio_playing {
		background: transparent url('https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Static/educationStu/iconloop.png') no-repeat 0 0;
		width: 18px;
		height: 25px;
		vertical-align: middle;
		display: inline-block;
		-webkit-background-size: 54px 25px;
		background-size: 54px 25px;
		-webkit-animation: audio_playing 1s infinite;
		background-position: 0px center;
	}

	@-webkit-keyframes audio_playing {
		30% {
			background-position: 0px center;
		}

		31% {
			background-position: -18px center;
		}

		61% {
			background-position: -18px center;
		}

		61.5% {
			background-position: -36px center;
		}

		100% {
			background-position: -36px center;
		}
	}

	.wordPronItem {
		width: 640rpx;
		min-height: 990rpx;
		box-shadow: 0rpx 15rpx 80rpx 0rpx #E9E9E9;
		border-radius: 24rpx;
		margin: 100rpx auto 0;
		position: relative;
		overflow: hidden;
	}

	.wordPronItem .num {
		font-size: 26rpx;
		font-family: PingFang SC;
		font-weight: 700;
		color: #999999;
		padding: 50rpx 0 110rpx 0;
		text-align: center;
	}

	.meaning {
		font-size: 28rpx;
		font-weight: 500;
		color: #111111;
		margin-bottom: 80rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.Japanese {
		font-size: 60rpx;
		font-weight: bold;
		color: #111111;
		display: flex;
		justify-content: center;
		align-items: center;
		margin-bottom: 60rpx; 
		padding: 0 20rpx;
	}

	.wordPronItem .desc {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		justify-content: center;
		margin-bottom: 200rpx;
		padding: 0 20rpx;
		box-sizing: border-box;
	}

	.wordPronItem .attr {
		background-color: #E8E9EA;
		border-radius: 6rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		box-sizing: border-box;
		padding: 5rpx 10rpx;
		line-height: 30rpx;
		margin-right: 15rpx;
		font-size: 24rpx;
		font-weight: 500;
		color: #666666;
	}

	.wordPronItem .hiragana {
		font-size: 28rpx;
		font-weight: 500;
		color: #999999;
	}

	.listen {
		width: 170rpx;
		height: 170rpx;
		background-color: #E4E5FB;
		border-radius: 50%;
		margin: 0 auto 30rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.listenIcon {
		width: 55rpx;
		height: 55rpx;
	}

	.listenText {
		font-size: 22rpx;
		font-weight: 500;
		color: #B8B8B8;
		text-align: center;
	}
</style>
