<template>
	<view class="container">
		<scroll-view class="content" scroll-y>
			<!-- 表单标题 -->
			<view class="form-header">
				<view class="form-title">{{ formData.title }}</view>
				<view class="form-tips">
					<view class="tip-item">请您根据患者真实情况选择并填写</view>
					<view class="tip-item">我们承诺对您及患者所提供的所有信息严格保密</view>
				</view>
			</view>
			
			<!-- 表单内容 -->
			<view class="form-section">
				<!-- 患者姓名 -->
				<view class="form-item">
					<view class="form-label">
						<text class="required">*</text>
						<text>1、患者姓名</text>
					</view>
					<input 
						class="form-input" 
						v-model="formData.patientName" 
						placeholder="请输入患者姓名"
					/>
				</view>
				
				<!-- 患者性别 -->
				<view class="form-item">
					<view class="form-label">
						<text class="required">*</text>
						<text>2、患者性别</text>
					</view>
					<radio-group @change="onGenderChange" class="radio-group">
						<label class="radio-item">
							<radio value="male" :checked="formData.patientGender === 'male'" color="#388BFF" />
							<text>男</text>
						</label>
						<label class="radio-item">
							<radio value="female" :checked="formData.patientGender === 'female'" color="#388BFF" />
							<text>女</text>
						</label>
					</radio-group>
				</view>
				
				<!-- 患者年龄 -->
				<view class="form-item">
					<view class="form-label">
						<text class="required">*</text>
						<text>3、患者年龄</text>
					</view>
					<input 
						class="form-input" 
						v-model="formData.patientAge" 
						type="number"
						placeholder="请输入患者年龄 (岁)"
					/>
				</view>
				
				<!-- 处方日期 -->
				<view class="form-item">
					<view class="form-label">
						<text class="required">*</text>
						<text>4、处方日期</text>
					</view>
					<picker mode="date" :value="formData.prescriptionDate" @change="onDateChange">
						<view class="form-input picker-input" :class="{ placeholder: !formData.prescriptionDate }">
							{{ formData.prescriptionDate || '请选择处方日期' }}
							<text class="calendar-icon">📅</text>
						</view>
					</picker>
				</view>
				
				<!-- 心脏病类型 -->
				<view class="form-item">
					<view class="form-label">
						<text class="required">*</text>
						<text>5、您被诊断的心脏病类型 (多选)</text>
					</view>
					<checkbox-group @change="onDiseaseTypeChange" class="checkbox-group">
						<label class="checkbox-item">
							<checkbox value="coronary" :checked="formData.diseaseTypes.includes('coronary')" color="#388BFF" />
							<text>冠心病</text>
						</label>
						<label class="checkbox-item">
							<checkbox value="heartFailure" :checked="formData.diseaseTypes.includes('heartFailure')" color="#388BFF" />
							<text>心力衰竭</text>
						</label>
						<label class="checkbox-item">
							<checkbox value="arrhythmia" :checked="formData.diseaseTypes.includes('arrhythmia')" color="#388BFF" />
							<text>心律失常</text>
						</label>
					</checkbox-group>
				</view>
				
				<!-- 图片上传 -->
				<view class="form-item">
					<view class="form-label">
						<text class="required">*</text>
						<text>6、图片上传</text>
					</view>
					<view class="upload-section">
						<view class="upload-item" v-for="(image, index) in formData.images" :key="index">
							<image class="uploaded-image" :src="image" mode="aspectFill"></image>
							<view class="delete-btn" @click="removeImage(index)">×</view>
						</view>
						<view class="upload-item upload-placeholder" @click="chooseImage" v-if="formData.images.length < 2">
							<text class="camera-icon">📷</text>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		
		<!-- 提交按钮 -->
		<view class="submit-btn" @click="handleSubmit">提交</view>
	</view>
</template>

<script>
import { submitCaseCollection } from '@/api-js/medicationSurvey'
export default {
	data() {
		return {
			statusBarHeight: uni.getSystemInfoSync().statusBarHeight + 'px',
			activityId: '',
			formData: {
				title: '心脏相关疾病病例征集表',
				patientName: '',
				patientGender: 'male',
				patientAge: '',
				prescriptionDate: '',
				diseaseTypes: ['coronary', 'heartFailure'],
				images: []
			}
		}
	},
	onLoad(options) {
		if (options.activityId) {
			this.activityId = options.activityId
		}
	},
	methods: {
		goBack() {
			uni.navigateBack()
		},
		onDateChange(e) {
			this.formData.prescriptionDate = e.detail.value
		},
		onGenderChange(e) {
			this.formData.patientGender = e.detail.value
		},
		onDiseaseTypeChange(e) {
			this.formData.diseaseTypes = e.detail.value
		},
		chooseImage() {
			uni.chooseImage({
				count: 2 - this.formData.images.length,
				sizeType: ['compressed'],
				sourceType: ['album', 'camera'],
				success: (res) => {
					this.formData.images = [...this.formData.images, ...res.tempFilePaths]
				}
			})
		},
		removeImage(index) {
			this.formData.images.splice(index, 1)
		},
		async handleSubmit() {
			// 表单验证
			if (!this.formData.patientName) {
				uni.showToast({
					icon: 'none',
					title: '请输入患者姓名'
				})
				return
			}
			if (!this.formData.patientAge) {
				uni.showToast({
					icon: 'none',
					title: '请输入患者年龄'
				})
				return
			}
			if (!this.formData.prescriptionDate) {
				uni.showToast({
					icon: 'none',
					title: '请选择处方日期'
				})
				return
			}
			if (this.formData.diseaseTypes.length === 0) {
				uni.showToast({
					icon: 'none',
					title: '请至少选择一种心脏病类型'
				})
				return
			}
			if (this.formData.images.length === 0) {
				uni.showToast({
					icon: 'none',
					title: '请至少上传一张图片'
				})
				return
			}
			
			try {
				uni.showLoading({ title: '提交中...' })
				const res = await submitCaseCollection({
					activityId: this.activityId,
					...this.formData
				})
				uni.hideLoading()
				if (res.code === 200) {
					uni.showToast({
						icon: 'success',
						title: '提交成功'
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)
				} else {
					uni.showToast({
						icon: 'none',
						title: res.msg || '提交失败'
					})
				}
			} catch (e) {
				uni.hideLoading()
				uni.showToast({
					icon: 'none',
					title: '提交失败'
				})
			}
		}
	}
}
</script>
<style lang="stylus">
	checkbox .wx-checkbox-input{
		border-radius:0 !important;
	}
	checkbox .wx-checkbox-input.wx-checkbox-input-checked {
		border-radius:0 !important;
	}
</style>
<style lang="scss" scoped>
.container {
	min-height: 100vh;
	background: linear-gradient(180deg, #E6F3FF 0%, #FFFFFF 100%);
	display: flex;
	flex-direction: column;
}
.status-bar {
	width: 100%;
	background: transparent;
}

.header {
	position: relative;
	height: 88rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	background: transparent;
	
	.back-btn {
		position: absolute;
		left: 24rpx;
		width: 40rpx;
		height: 40rpx;
		
		image {
			width: 100%;
			height: 100%;
		}
	}
	
	.title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
	}
	
	.header-right {
		position: absolute;
		right: 24rpx;
		display: flex;
		align-items: center;
		gap: 16rpx;
		
		.more-icon {
			font-size: 32rpx;
			color: #333;
		}
	}
}

.content {
	flex: 1;
	padding: 24rpx;
}

.form-header {
	margin-bottom: 32rpx;
	
	.form-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 16rpx;
	}
	
	.form-tips {
		.tip-item {
			font-size: 24rpx;
			color: #666;
			margin-bottom: 8rpx;
		}
	}
}

.form-section {
	background: #fff;
	border-radius: 16rpx;
	padding: 24rpx;
}

.form-item {
	margin-bottom: 32rpx;
	
	&:last-child {
		margin-bottom: 0;
	}
	
	.form-label {
		display: flex;
		align-items: center;
		font-size: 28rpx;
		color: #333;
		margin-bottom: 16rpx;
		
		.required {
			color: #FF5030;
			margin-right: 4rpx;
		}
	}
	
	.form-input {
		width: 100%;
		height: 80rpx;
		padding: 0 24rpx;
		background: #f5f5f5;
		border-radius: 8rpx;
		font-size: 28rpx;
		color: #333;
		
		&.placeholder {
			color: #999;
		}
		
		&.picker-input {
			display: flex;
			align-items: center;
			justify-content: space-between;
			
			.calendar-icon {
				font-size: 32rpx;
			}
		}
	}
	
	.radio-group,
	.checkbox-group {
		display: flex;
		gap: 48rpx;
		
		.radio-item,
		.checkbox-item {
			display: flex;
			align-items: center;
			gap: 16rpx;
			font-size: 28rpx;
			color: #333;
		}
	}
	
	.upload-section {
		display: flex;
		gap: 16rpx;
		flex-wrap: wrap;
		
		.upload-item {
			width: 200rpx;
			height: 200rpx;
			border-radius: 8rpx;
			overflow: hidden;
			position: relative;
			
			.uploaded-image {
				width: 100%;
				height: 100%;
			}
			
			.delete-btn {
				position: absolute;
				top: 8rpx;
				right: 8rpx;
				width: 40rpx;
				height: 40rpx;
				background: rgba(0, 0, 0, 0.5);
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 32rpx;
				color: #fff;
			}
			
			&.upload-placeholder {
				background: #f5f5f5;
				display: flex;
				align-items: center;
				justify-content: center;
				
				.camera-icon {
					font-size: 60rpx;
				}
			}
		}
	}
}

.submit-btn {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	height: 88rpx;
	background: #388BFF;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 32rpx;
	color: #fff;
	font-weight: 500;
	z-index: 100;
}
</style>


