<template>
	<s-layout
		title="话术测试"
		navbar="inner"
		:bgStyle="state.page"
		:navbarStyle="state.navigationBar"
		showBackButton
	>
	<view v-if="state.isTest" class="test_tips call_test_tips">将在30秒内拨电话给您<br>请耐心等待……</view>
	<view v-else class="test_tips">
		点击下方按钮<br>
		AI智能体将拨打带有话术内容的电话给您<br>
		请您注意接听<br>
	</view>
	<button v-if="!(state.isTest&&!state.isNext)" class="submit_btn" :loading="state.loading" @click="submitForm">{{state.isNext?'下一步':'接听测试电话'}}</button>
	</s-layout>
</template>

<script setup>
	import { reactive, computed } from 'vue'
	import { onLoad } from '@dcloudio/uni-app'
	import taskApi from '@/common/api/member/task'
	import sheep from '@/common'
	const state = reactive({
		navigationBar: {},
		page: {
			backgroundColor: '#FFFFFF',
			backgroundImage: '/static/homeTopBg.png'
		},
		isTest: false,
		isNext: false,
		loading: false
	})
	const taskFormInfo = computed(() => sheep.$store('task').taskFormInfo)
	onLoad((options) => {
	  if(options.isTest){
		  submitForm()
	  }
	})
	async function submitForm(){
		if(state.isTest){
			sheep.$router.go('/pages/task/speechConfirm')
			state.isTest = false
			return
		}
		// if(taskFormInfo.value.callPlatform==='YI_WISE'){
			state.isNext = false
			state.isTest = true
			state.loading =  true
			const { code, data } = await taskApi.addTest({
				typeId:taskFormInfo.value.typeId,
				taskName:taskFormInfo.value.name
			})
			if (code === 1) {
				sheep.$store('task').updateTaskFormInfo({
					callUserTestId:data.id
				})
				state.isTest = true
				setTimeout(()=>{
					state.isNext = true
				},15*1000)
			}else{
				state.isTest = false
			}
			state.loading =  false
		// }else{
		// 	sheep.$router.go('/pages/public/webview', {
		// 	  url:encodeURIComponent(`/webphone.html?ws=wss&ip=test.webphone.noai.top&port=1360&extnumber=${sheep.$store('user').userInfo.phone}`),
		// 	  webphone:true
		// 	})
		// }
	}
</script>

<style lang="scss" scoped>
	.test_tips{
		font-weight: 400;
		font-size: 32rpx;
		color: #222222;
		line-height: 80rpx;
		text-align: center;
		padding-top: 344rpx;
	}
	.call_test_tips{
		padding-top: 344rpx;
	}
	.submit_btn{
		width: 630rpx;
		height: 104rpx;
		line-height: 104rpx;
		background: #35E89A;
		border-radius: 40rpx;
		font-weight: bold;
		font-size: 32rpx;
		color: #222222;
		text-align: center;
		position: fixed;
		left: calc(50%);
		transform: translateX(-50%);
		bottom: 40rpx;
	}
</style>