<template>
	<view class="container">
		<view class="header" v-if="title">
			<text class="title">{{ title }}</text>
		</view>
		<textarea
			class="textarea"
			v-model="text"
			:placeholder="placeholder"
			:maxlength="500"
			show-confirm-bar
			auto-focus
		/>
		<view class="footer">
			<button class="btn cancel" @click="cancel">取消</button>
			<button class="btn save" @click="save">保存</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				key: 'bio',
				text: '',
				placeholder: '请输入内容',
				title: ''
			}
		},
		onLoad(options) {
			this.key = options.key || 'bio'
			const value = options.value || ''
			this.text = value ? decodeURIComponent(value) : ''
			this.placeholder = options.placeholder ? decodeURIComponent(options.placeholder) : '请输入内容'
			this.title = options.title ? decodeURIComponent(options.title) : '编辑文本'
		},
		methods: {
			cancel() {
				uni.navigateBack()
			},
			save() {
				// 保存到临时存储，编辑资料页会在 onShow 时读取
				const trimmedText = this.text.trim()
				uni.setStorageSync(`temp_text_${this.key}`, trimmedText)
				uni.showToast({
					title: '已保存',
					icon: 'success',
					duration: 1000
				})
				setTimeout(() => {
					uni.navigateBack()
				}, 500)
			}
		}
	}
</script>

<style scoped>
	.container {
		display: flex;
		flex-direction: column;
		height: 100vh;
		background-color: #FFFFFF;
	}
	.header {
		padding: 30rpx;
		border-bottom: 1rpx solid #F0F0F0;
	}
	.title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333333;
	}
	.textarea {
		flex: 1;
		width: 100%;
		padding: 30rpx;
		box-sizing: border-box;
		background: #fff;
		font-size: 30rpx;
		color: #333;
		line-height: 1.6;
	}
	.footer {
		display: flex;
		justify-content: space-between;
		gap: 20rpx;
		padding: 30rpx;
		border-top: 1rpx solid #F0F0F0;
		background-color: #FFFFFF;
	}
	.btn {
		flex: 1;
		height: 88rpx;
		border-radius: 44rpx;
		font-size: 30rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.btn::after {
		border: none;
	}
	.cancel {
		background: #f5f5f5;
		color: #666;
		border: none;
	}
	.save {
		background: #4FC3F7;
		color: #fff;
		border: none;
	}
</style>


