<template>
	<view class="bg">
		<view class="naming_result_bg" :style="{backgroundImage: `url(${$baseUrl}aiNaming/bg.jpg)`}">
			<view class="page-top">
				<image @tap="backHandler" class="btnback" :src="$baseUrl+'aiNaming/back_btn.png'"></image>
				<view class="page_title">星妈起名</view>
			</view>
			<view class="box">
				<view class="name_box" v-for="(item, index) in nameList" :key="index">
					<view class="name_con">
						<view class="name_py" :style="{'fontSize': item.pinyin.length>15?'20rpx':'30rpx'}">
							{{item.pinyin}}
						</view>
						<view class="name_txt" :style="{'fontSize': item.name.length>7?'30rpx':'54rpx'}">
							{{item.name}}
						</view>
					</view>
					<view class="jt_box" @click="onJtClick(index)">
						<image class="jt"
							:src="jt_type[index] === 0 ? `${$baseUrl}aiNaming/jx_icon_1.png` : `${$baseUrl}aiNaming/jx_icon_2.png`"
							mode="aspectFit" />
						<view class="jt_txt">解析</view>
					</view>

					<view class="name_desc" v-if="jt_type[index] == 1">
						<view class="meaning">{{item.meaning}}</view>
						<view class="line"></view>
						<view class="words_0">{{item.words?.[0]}}</view>
						<view class="words_1">{{item.words?.[1]}}</view>

						<view class="btn_box">

							<button open-type="share" class="share_btn">
								<image :src="`${$baseUrl}aiNaming/share_btn.png`" mode="aspectFit" class="share_icon" />
							</button>
							<!-- <view class="share_btn" :style="{backgroundImage: `url(${$baseUrl}aiNaming/share_btn.png)`}"
								@click="shareAppMessage">
							</view> -->
							<view class="regenerate_btn1"
								:style="{backgroundImage: `url(${$baseUrl}aiNaming/cxsc_btn.png)`}" @click="onAiNaming">
							</view>
						</view>
					</view>
				</view>

				<view class="regenerate_btn" :style="{backgroundImage: `url(${$baseUrl}aiNaming/cxsc_btn.png)`}"
					@click="onAiNaming"></view>
			</view>

		</view>
		<CustomLoading :show="globalStore.isShowLoading" :text="'起名中...'" :imgPath="$baseUrl+'aiNaming/loading_icon.png'">
		</CustomLoading>
	</view>
</template>
<script setup>
	import {
		ref,
		reactive,
		computed,
		onMounted,
		getCurrentInstance
	} from 'vue';
	import {
		jump,
		JumpType,
		throttleTap
	} from "@/utils/index.js";
	import {
		aiNaming
	} from '../../api/naming.js';
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import CustomLoading from '../../components/CustomLoading.vue';
	import {
		useGlobalStore
	} from "@/stores/global.js";

	const globalStore = useGlobalStore();
	
	const {
		proxy
	} = getCurrentInstance();
	const $baseUrl = proxy.$baseUrl;
	const jt_type = ref([0, 0, 0]);

	const param = ref();
	const nameList = ref(null);

	onLoad((options) => {
		param.value = JSON.parse(decodeURIComponent(options.param))
		nameList.value = JSON.parse(decodeURIComponent(options.nameList))
		console.log('接收到的参数:', param.value)
	});

	onMounted(() => {
		uni.hideLoading();
	});

	//生成or重新生成名字
	const onAiNaming = throttleTap(async () => {
		// uni.showLoading({
		// 	title: '生成中...',
		// 	mask: true // 是否显示透明蒙层（防止触摸穿透）
		// })
		globalStore.setIsShowLoading(true);
		let d = await aiNaming(param.value);
		// uni.hideLoading();
		globalStore.setIsShowLoading(false);
		if (d) {
			nameList.value = d?.data;
			console.log('重新生成:', nameList.value)
			jt_type.value = [0, 0, 0]
		}
	}, 1000)

	//点击箭头
	const onJtClick = (index) => {
		if (jt_type.value[index]) {
			jt_type.value[index] = 0;
		} else {
			jt_type.value[index] = 1;
		}
	}

	// 首页组件逻辑
	const backHandler = () => {
		uni.navigateBack();
	}
</script>

<script>
	export default {
		setup() {
			const {
				proxy
			} = getCurrentInstance()
			const $baseUrl = proxy.$baseUrl
			// 返回供模板使用的数据/方法
			return {
				$baseUrl // 可以在 export default 的其他选项中通过 this.$baseUrl 访问
			}
		},

		onShareAppMessage() {
			// 通过 this 访问 setup() 返回的变量
			return {
				title: '好名字是给宝宝独一无二的礼物',
				path: `/pages/naming/naming?back_btn='hide'`,
				imageUrl: this.$baseUrl + 'aiNaming/single.png'
			}
		}
	}
</script>
<style lang="scss">
	.bg {
		width: 100vw;
		min-height: 100vh;
		height: auto;
		background-color: #f6f8fa;
	}

	.naming_result_bg {
		width: 100vw;
		min-height: 100vh;
		background-size: 100% 100%;
	}

	.box {
		height: auto;
		margin-top: 80rpx;
		padding-left: 32rpx;
		padding-right: 32rpx;
		padding-bottom: 100rpx;


		.name_box {
			padding: 100rpx 32rpx 100rpx 32rpx;
			height: auto;
			margin-bottom: 50rpx;
			background: #fff;
			border-radius: 28rpx;

			.name_con {
				width: 480rpx;
				margin-left: 0rpx;
				letter-spacing: 10rpx;

				.name_py {
					// background: #20ffff;
					width: 480rpx;
					text-align: center;
					word-break: break-all;
					/* 所有字符都可换行 */
				}

				.name_txt {
					// background: #fc0107;
					margin-top: 5rpx;
					width: 480rpx;
					text-align: center;
					word-break: break-all;
					/* 所有字符都可换行 */
				}
			}

			.jt_box {
				width: 94rpx;
				margin-left: 480rpx;
				margin-top: -80rpx;
				color: #6f6d67;
				font-size: 28rpx;
				text-align: center;

				.jt {
					width: 34rpx;
					height: 34rpx;
				}

				.jt_txt {
					width: 100%;
				}
			}

			.name_desc {
				width: 100%;
				margin-top: 100rpx;
				color: #1d1e25;

				.meaning {
					font-size: 26rpx;
					text-align: left;
					letter-spacing: 1rpx;
					line-height: 1.5;
					opacity: 0.8;
				}

				.line {
					width: 620rpx;
					height: 2rpx;
					background-color: #999;
					opacity: 0.1;
					margin-top: 30rpx;
					margin-bottom: 50rpx;
				}

				.words_0 {
					font-size: 28rpx;
					text-align: left;
					opacity: 0.9;
				}

				.words_1 {
					font-size: 28rpx;
					text-align: left;
					margin-top: 30rpx;
					opacity: 0.9;
				}

				.btn_box {
					width: 100%;
					display: flex;
					margin-top: 120rpx;
					margin-bottom: -60rpx;

					.share_btn {
						// width: 142rpx;
						// height: 55rpx;
						// background-size: 100% 100%;
						// margin-left: 260rpx;
						padding: 0;
						margin: 0;
						border: none;
						background: none;
						line-height: 1;
					}

					/* 去除微信按钮默认边框 */
					.share_btn::after {
						border: none !important;
					}

					/* 图片样式 */
					.share_icon {
						width: 142rpx;
						height: 55rpx;
						display: block;
						margin-left: 230rpx;
					}

					.regenerate_btn1 {
						width: 199rpx;
						height: 55rpx;
						background-size: 100% 100%;
						margin-left: 40rpx;
					}
				}
			}
		}

		.regenerate_btn {
			width: 199rpx;
			height: 55rpx;
			background-size: 100% 100%;
			margin-left: 490rpx;
		}

	}

	.page-top {
		width: 100%;
	}

	.btnback {
		width: 20rpx;
		height: 32rpx;
		margin-top: 110rpx;
		margin-left: 48rpx;
	}

	.page_title {
		width: 100%;
		font-size: 34rpx;
		font-weight: 500;
		text-align: center;
		color: #1d1e25;
		margin-top: -46rpx;
		margin-left: 0rpx;
	}
</style>