<template>
	<view class="remark-page">
		<view class="navbox">
			<view class="status_bar"></view>
			<uni-nav-bar
			left-icon="left"
			title="设置备注和分组"
			@clickLeft="goBack"
		
			color="#8B2316"
			
			:border="false"
			backgroundColor="#eee"
		/>
		</view>
		<view class="contentbox">
			<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="row" v-if="nickname">
			<view class="left">申请消息为：我是{{ nickname }}</view>
			<view class="right" @click="fillNickname">
				填入
			</view>
		</view>
        <view class="form-block" @click="goGroup">
			<view class="label"  @click.stop="goGroup">分组</view>
			<view class="iptbox readonly">
				<view class="input readonly-input"   :class="{'ph':!groupInfo.name}"   @click.stop="goGroup">{{groupInfo.name || '通过分组给患者分类'}}</view>
				<uni-icons type="right" size="20" color="#666" @click.stop="goGroup"></uni-icons>
			</view>
		</view>
		<view class="form-block">
			<view class="label">描述</view>
			<view class="textarea-wrap">
				<textarea class="textarea" v-model.trim="note" :placeholder="groupInfo.note || '补充患者关键信息，方便随访患者'" placeholder-class="ph" auto-height maxlength="100" :adjust-position="false"/>
				<view class="textarea-count">已输入{{ noteLength }}/100</view>
			</view>
		</view>
		</view>

		<view class="bottom-bar">
			<button class="save-btn" :class="{ disabled: isRemarkEmpty }" :disabled="isRemarkEmpty" @click="updateNicknameNote">完成</button>
		</view>
	</view>
</template>

<script setup>
	import { ref, computed } 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 noteLength = computed(() => (note.value || '').length)
	const isRemarkEmpty = computed(() => !(remark.value || '').trim())
	const nickname = ref('')
	const tempName=ref('')
    
	const goBack = () => {
		uni.navigateBack()
	}
	const goGroup = () => {
		navTo({
			url: `/pages_app/groupManage/groupManage?uuid=${patientUuid.value}`
		})
	}
	
	onLoad((options) => {
		patientUuid.value = options.uuid || ''
		if(options.nickname){
			nickname.value = options.nickname
		}
	})
	
	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 || remark.value
				note.value = res.group?.note || ''
				groupName.value = res.group?.name || ''
			}
		})
	}
	const updateNicknameNote = async() => {
		if (isRemarkEmpty.value) return
		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: 'none' })
		setTimeout(() => goBack(), 700)
	}
	const fillNickname = () => {
		remark.value = nickname.value
	}
</script>

<style lang="scss" scoped>
.row{
	margin-top: -20rpx;
	display: flex;
	background: #fff;
	padding:0 30rpx;
	align-items: center;
	justify-content: space-between;
	.left{
		font-size: 28rpx;
		color: #666;
	}
	.right{
		font-size: 32rpx;
		color: #3ec7c0;
	}
}
.contentbox{
	top: calc(var(--status-bar-height) + 44px);
	position: fixed;
	left: 0;
	right: 0;
	bottom: 140rpx;
	overflow-y: scroll;
	z-index: 1;
}
	.remark-page{
		height: 100vh;
		background: #f7f7f7;
		
	}
	.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: #333;
			}
			&.ph{
				color: #808080;
			}
		}
		.textarea{
			background: #f8f8f8;
			border-radius: 12rpx;
			padding: 24rpx 24rpx 58rpx;
			font-size: 28rpx;
			color: #333;
			min-height: 250rpx;
			box-sizing: border-box;
			width: 100%;
			line-height: 1.6;
		}
		.textarea-wrap{
			position: relative;
		}
		.textarea-count{
			position: absolute;
			right: 20rpx;
			bottom: 16rpx;
			font-size: 24rpx;
			color: #999;
		}
		.ph{
			color: #bfbfbf;
		}
	}
	.bottom-bar{
		position: fixed;
		left: 0; right: 0; bottom: 0;
		background: #ffffff;
		border-top: 1rpx solid #f0f0f0;
		padding: 20rpx 24rpx 20rpx;
		.save-btn{
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: center;
			height: 92rpx;
			background: #3ec7c0;
			color: #fff;
			border: none;
			border-radius: 12rpx;
			font-size: 32rpx;
			&.disabled{
				background: #d9d9d9;
				color: #ffffff;
			}
		}
	}
</style>
