<template>
	<view class="remark-page">
		<uni-nav-bar
			left-icon="left"
			title="设置备注和分组"
			@clickLeft="goBack"
			fixed
			color="#8B2316"
			height="140rpx"
			:border="false"
			backgroundColor="#eee"
		/>
		<view class="form-block">
			<view class="label">备注</view>
			<input class="input" v-model.trim="remark" :placeholder="groupInfo.nickname || '给患者添加备注名'" placeholder-class="ph" maxlength="20"/>
		</view>
        <view class="form-block" @click="goGroup">
			<view class="label">分组</view>
			<view class="iptbox readonly">
				<input class="input readonly-input" v-model.trim="groupName" :placeholder="groupInfo.name || '通过分组给患者分类'" placeholder-class="ph" maxlength="20" readonly disabled/>
				<uni-icons type="right" size="20" color="#666"></uni-icons>
			</view>
		</view>
		<view class="form-block">
			<view class="label">描述</view>
			<textarea class="textarea" v-model.trim="note" :placeholder="groupInfo.note || '补充患者关键信息，方便随访患者'" placeholder-class="ph" auto-height maxlength="140"/>
		</view>

		<view class="bottom-bar">
			<button class="save-btn" @click="updateNicknameNote">保存</button>
		</view>
	</view>
</template>

<script setup>
	import { ref } from 'vue'
	import api from '@/api/api.js'
	import navTo from '@/utils/navTo.js'
	import { onLoad, onShow } from '@dcloudio/uni-app'
	const remark = ref('')
	const note = ref('')
	const groupName = ref('')
	const patientUuid = ref('')
	const patientCardData = ref({})
	const groupInfo = ref({})
    
	const goBack = () => {
		uni.navigateBack()
	}
	const goGroup = () => {
		navTo({
			url: `/pages_app/groupManage/groupManage?uuid=${patientUuid.value}`
		})
	}
	
	onLoad((options) => {
		patientUuid.value = options.uuid || ''
	})
	
	onShow(() => {
		patientCard()
	})
	const patientCard = () =>{
		api.patientCard({
			patient_uuid: patientUuid.value
		}).then(res => {
			if(res.code == 200){
				patientCardData.value = res
				groupInfo.value = res.group || {}
				// 初始化表单数据
				remark.value = res.group?.nickname || ''
				note.value = res.group?.note || ''
				groupName.value = res.group?.name || ''
			}
		})
	}
	const updateNicknameNote = async() => {
		const res = await api.updateNicknameNote({
			patient_uuid: patientUuid.value,
			nickname: remark.value,
			note: note.value,
		})
		if(res.code == 200){
			uni.showToast({ title: '保存成功', icon: 'none' })
			setTimeout(() => goBack(), 700)
		}
	}

	const saveRemark = () => {
		if (!remark.value) {
			uni.showToast({ title: '请输入备注名', icon: 'none' })
			return
		}
		// TODO: 调用保存接口
		uni.showToast({ title: '保存成功', icon: 'success' })
		setTimeout(() => goBack(), 700)
	}
</script>

<style lang="scss" scoped>
	.remark-page{
		min-height: 100vh;
		background: #f7f7f7;
		padding-bottom: 140rpx;
	}
	.form-block{
        .iptbox{
            background: #f8f8f8;
            display: flex;
            align-items: center;
            &.readonly {
                background: #f5f5f5;
                opacity: 0.8;
            }
        }
		
		background: #fff;
		padding: 24rpx 30rpx 30rpx;
		.label{
			font-size: 28rpx;
			color: #666;
			margin-bottom: 16rpx;
		}
		.input{
            flex:1;
			background: #f8f8f8;
			border-radius: 12rpx;
			padding: 24rpx;
			font-size: 30rpx;
			color: #333;
			&.readonly-input {
				background: transparent;
				color: #999;
			}
		}
		.textarea{
			background: #f8f8f8;
			border-radius: 12rpx;
			padding: 24rpx;
			font-size: 28rpx;
			color: #333;
			min-height: 180rpx;
			line-height: 1.6;
		}
		.ph{
			color: #bfbfbf;
		}
	}
	.bottom-bar{
		position: fixed;
		left: 0; right: 0; bottom: 0;
		background: #ffffff;
		border-top: 1rpx solid #f0f0f0;
		padding: 20rpx 24rpx env(safe-area-inset-bottom);
		.save-btn{
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: center;
			height: 92rpx;
			background: #8B2316;
			color: #fff;
			border: none;
			border-radius: 12rpx;
			font-size: 32rpx;
		}
	}
</style>
