<template>
	<view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title='配置角色' bgColor="transparent"></cus-header>
		<div class="title">助手昵称</div>
		<div class="inp">
			<input v-model="agentDto.agentName" type="text" placeholder="请输入昵称">
		</div>
		<div class="title">角色模板</div>
		<div class="list">
			<div class="pre" v-for="(item,index) in list" :key="index" :class="{'active':midx===index}" @tap="changeModel(item,index)">
				<image src="@/static/selected.png" v-if="midx===index"></image>
				{{item.agentName}}
			</div>
		</div>
		<div class="title">角色介绍</div>
		<div class="intro">
			<u-parse :content="agentDto.systemPrompt"></u-parse>
		</div>
		<div class="title">角色音色</div>
		<div class="inp" @tap="show=true">
			<input type="text" placeholder="请选择音色" disabled v-if="!agentDto.ttsVoiceId">
			<text v-else>{{voiceText}}</text>
		</div>
		<div class="zt_btn" @tap="comfirmSure">确定</div>
		<u-picker :itemHeight="88" title="角色音色" :show="show" :columns="voiceList" keyName="name"
			@cancel="show=false" @confirm="confirm" :immediateChange="true" style="height: 500rpx;">
		</u-picker>
	</view>
</template>

<script>
	export default {
		data(){
			return {
				id:'',
				agentId:'',
				agentDto:null,
				list:[],
				midx:0,
				modelMap:new Map(),
				midx:0,
				voiceText:'',
				voiceList:[],
				show:false
			}
		},
		async onLoad(option) {
			this.id = option.id;
			this.agentId = option.agentId;
			await this.getDetail();
			await this.getAgentModelList()
			this.getModelVoiceList()
		},
		methods:{
			getDetail(){
				return new Promise((resolve,reject)=>{
					this.$api.get(`/agent/${this.id}`).then(res=>{
						if(res.data.code!==0) return this.$showToast(res.data.msg)
						this.agentDto = res.data.data;
							resolve();
					})
				})
			},
			getAgentModelList(){
				return new Promise((resolve,reject)=>{
					this.$api.get('/agent/template').then(res=>{
						if(res.data.code!==0) return this.$showToast(res.data.msg)
						this.list = res.data.data;
						let idx = this.list.findIndex(l=>l.systemPrompt===this.agentDto.systemPrompt);
						if(idx>-1){
							this.midx = idx;
						}
						if(this.list.length){
							let map = new Map();
							this.list.forEach(l=>{
								map.set(l.agentName,l)
							})
							this.modelMap = map;
						}
						resolve();
					})
				})
			},
			getModelVoiceList(){
				this.$api.get(`/models/${this.agentDto.ttsModelId}/voices`).then(res=>{
					if(res.data.code!==0) return this.$showToast(res.data.msg)
					this.voiceList = [res.data.data];
					this.voiceText = res.data.data.find(v=>v.id===this.agentDto.ttsVoiceId)?.name||'';
				})
			},
			confirm(e){
				this.show = false;
				this.agentDto.ttsVoiceId = e.value[0].id;
				this.voiceText = e.value[0].name;
			},
			changeModel(item,index){
				this.midx = index;
				this.agentDto = {...this.agentDto,...this.modelMap.get(item.agentName)};
				this.getModelVoiceList()
			},
			comfirmSure(){
				let dto = JSON.parse(JSON.stringify(this.agentDto));
				this.$api.put('/agent/'+this.agentId,dto).then(res=>{
					if(res.data.code!==0) return this.$showToast(res.data.msg)
					this.$showToast('配置成功');
					setTimeout(()=>{
						uni.reLaunch({
							url:'/pages/home'
						})
					},1500)
				})
			},
		}
	}
</script>

<style scoped lang="less">
	.page{
		background: linear-gradient( 180deg, #E0EEFF 0%, #F6FCFF 100%);
		padding: 0 30rpx 30rpx;
		box-sizing: border-box;
		
		.title{
			font-family: PingFang-SC, PingFang-SC;
			font-weight: bold;
			font-size: 32rpx;
			color: #111111;
			line-height: 32rpx;
			margin-top: 48rpx;
		}
		.inp{
			margin-top: 40rpx;
			background: #FFFFFF;
			border-radius: 16rpx;
			padding: 30rpx 24rpx;
			input{
				border: none;
			}
		}
		.list{
			margin-top: 20rpx;
			display: flex;
			flex-wrap: wrap;
			margin-left: -10rpx;
			.pre{
				margin-top: 20rpx;
				margin-left: 10rpx;
				width: calc(100% / 3 - 10rpx);
				height: 80rpx;
				background: #FFFFFF;
				border-radius: 16rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #333333;
				line-height: 80rpx;
				text-align: center;
				position: relative;
				&.active{
					background: #0066FE;
					font-weight: bold;
					color: #FFFFFF;
				}
				image{
					width: 48rpx;
					height: 48rpx;
					position: absolute;
					right: 0;
					bottom: 0;
					z-index: 22;
				}
			}
		}
		.intro{
			margin-top: 40rpx;
			background: #FFFFFF;
			border-radius: 16rpx;
			padding: 21rpx 24rpx 100rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #616161;
			line-height: 56rpx;
		}
		.zt_btn{
			margin-top: 96rpx;
		}
	}
</style>