<template>
	<view class="content">
		<!-- 顶部导航栏 -->
		<uni-nav-bar
			left-icon="left"
			title="添加随访计划"
			@clickLeft="goBack"
			fixed
			color="#8B2316"
			height="140rpx"
			:border="false"
			backgroundColor="#ffffff"
		>
			<template #right>
				<view class="nav-right" @click="submitPlan">
					<text class="submit-text">提交</text>
				</view>
			</template>
		</uni-nav-bar>

		<!-- 患者选择 -->
		<view class="form-section" @click="selectPatient">
			<view class="section-label"><text class="label-text">患者</text></view>
			<view class="section-content">
				<text class="content-text">{{ selectedPatient || '请选择患者' }}</text>
				<uni-icons type="right" size="20" color="#999"></uni-icons>
			</view>
		</view>

		<!-- 随访内容列表 -->
		<view class="title-bar">随访内容</view>
		<scroll-view class="visit-list" scroll-y scroll-with-animation :scroll-into-view="scrollIntoViewId">
			<view class="visit-item" v-for="(item, idx) in visits" :key="item.id" :id="`visit-${item.id}`">
				<view class="left">
					<uni-icons type="close" size="20" :color="idx===0 ? '#cfcfcf' : '#c0392b'" @click="removeVisit(idx)"></uni-icons>
				</view>
				<view class="mid">
					<text class="visit-text">{{ idx+1 }}.{{ defaultContent }}</text>
				</view>
				<view class="right" @click="pickDate(idx)">
					<text class="date-text">{{ item.date }}</text>
					<uni-icons type="right" size="20" color="#999"></uni-icons>
				</view>
			</view>
		</scroll-view>

		<!-- 添加随访日程按钮 -->
		<view class="add-row" @click="addVisit">
			<text class="plus">＋</text>
			<text class="add-text">添加随访日程</text>
		</view>

		<!-- 提醒开关 -->
		<view class="reminder-section">
			<view class="reminder-item" @click="toggleRemindMe">
				<text class="reminder-text">提醒我</text>
				<view class="checkbox" :class="{ active: remindMe }">
					<uni-icons v-if="remindMe" type="checkmarkempty" size="16" color="#ffffff"></uni-icons>
				</view>
			</view>
			<view class="reminder-item" @click="toggleRemindPatient">
				<text class="reminder-text">提醒患者</text>
				<view class="checkbox" :class="{ active: remindPatient }">
					<uni-icons v-if="remindPatient" type="checkmarkempty" size="16" color="#ffffff"></uni-icons>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { ref,nextTick } from 'vue';
	import { onShow } from "@dcloudio/uni-app";
	
	const selectedPatient = ref('');
	const defaultContent = '请近日来医院复诊、复查';
	const visits = ref([
		{ id: 1, date: formatDate(new Date()) },
		{ id: 2, date: formatDate(addMonths(new Date(), 6)) },
		{ id: 3, date: formatDate(addMonths(new Date(), 12)) },
		{ id: 4, date: formatDate(addMonths(new Date(), 24)) }
	]);
	const remindMe = ref(false);
	const remindPatient = ref(true);
	const scrollIntoViewId = ref('');
	
	function formatDate(d){
		const y=d.getFullYear();
		const m=`${d.getMonth()+1}`.padStart(2,'0');
		const day=`${d.getDate()}`.padStart(2,'0');
		const w=['日','一','二','三','四','五','六'][d.getDay()];
		return `${y}.${m}.${day}(星期${w})`;
	}
	function addMonths(d, m){
		const nd=new Date(d);
		nd.setMonth(nd.getMonth()+m);
		return nd;
	}
	
	const goBack=()=>uni.navigateBack();
	const submitPlan=()=>{
		uni.showToast({ title: '已提交', icon: 'success' });
		setTimeout(()=>uni.navigateBack(),1000);
	};
	const selectPatient=()=>{
		uni.showActionSheet({
			itemList:['张三','李四','王五','赵六'],
			success:(res)=>{ selectedPatient.value=['张三','李四','王五','赵六'][res.tapIndex]; }
		});
	};
	
	const addVisit=()=>{
		const id= Date.now();
		visits.value.push({ id, date: formatDate(new Date()) });
		// 下一帧滚动到新项
		nextTick(()=>{ scrollIntoViewId.value = `visit-${id}`; });
	};
	const removeVisit=(idx)=>{
		if(idx===0) return; // 第一条不可删
		visits.value.splice(idx,1);
	};
	const pickDate=(idx)=>{
		uni.showDatePicker({
			mode:'date',
			value:new Date().toISOString().split('T')[0],
			success:(res)=>{
				visits.value[idx].date = formatDate(new Date(res.value));
			}
		});
	};
	
	const toggleRemindMe=()=>{ remindMe.value=!remindMe.value; };
	const toggleRemindPatient=()=>{ remindPatient.value=!remindPatient.value; };
	
	onShow(()=>{});
</script>

<style lang="scss" scoped>
	.content {
		background-color: #f5f5f5;
		min-height: 100vh;
		
	}
	.nav-right { display: flex; align-items: center; }
	.submit-text { font-size: 32rpx; color: #8B2316; font-weight: 500; }

	.form-section {
		margin-top: 20rpx;
		display: flex; align-items: center; justify-content: space-between;
		padding: 30rpx; background: #ffffff; border-bottom: 1rpx solid #f0f0f0;
		.section-label { width: 160rpx; flex-shrink: 0; }
		.label-text { font-size: 32rpx; color: #8B2316; font-weight: 500; }
		.section-content { display: flex; align-items: center; }
		.content-text { font-size: 32rpx; color:#333; }
	}
	.title-bar { padding: 20rpx 30rpx; color:#8B2316; font-size: 32rpx; background:#ffffff; margin-top: 20rpx; border-top: 1rpx solid #f0f0f0; border-bottom: 1rpx solid #f0f0f0; }

	.visit-list {
		/* 让列表在可视区域内滚动 */
		height: 400rpx;
		background-color: #ffffff;
	}
	.visit-item { box-sizing:border-box;display:flex; align-items:center; padding: 26rpx 30rpx; border-bottom:1rpx solid #f0f0f0; }
	.visit-item .left { width: 60rpx; display:flex; justify-content:center; }
	.visit-item .mid { flex:1;  }
	.visit-text { font-size: 30rpx; color:#333; display: inline-block;white-space: nowrap;width:80%;overflow: hidden;}
	.visit-item .right {  flex:1;display:flex; align-items:center; }
	.date-text { display: inline-block;white-space: nowrap; font-size: 30rpx; color:#333; margin-right: 10rpx; }

	.add-row { display:flex; align-items:center; padding: 26rpx 30rpx; background:#ffffff; margin-top: 20rpx; }
	.plus { color:#8B2316; font-size: 36rpx; margin-right: 14rpx; }
	.add-text { color:#8B2316; font-size: 32rpx; }

	.reminder-section { background:#ffffff; margin-top: 20rpx; }
	.reminder-item { display:flex; align-items:center; justify-content:space-between; padding: 30rpx; border-bottom:1rpx solid #f0f0f0; }
	.reminder-item:last-child { border-bottom:none; }
	.reminder-text { font-size: 32rpx; color:#8B2316; font-weight: 500; }
	.checkbox { width: 40rpx; height: 40rpx; border-radius: 50%; border: 2rpx solid #cccccc; display:flex; align-items:center; justify-content:center; background:#ffffff; }
	.checkbox.active { background:#8B2316; border-color:#8B2316; }
</style>
