<template>
	<view class="content">
		<view class="inner">
			
			<view class="text-content">
				<view class="title">
					<text class="black">健康记录</text>
				</view>
				<textarea class="textArea" maxlength="200" @input="contentInput" placeholder="请填写健康记录,我们会匹配专业的医师为您回复"></textarea>
			</view>
			<view class="img-content">
				<view class="title">
					<text class="black">上传图片</text>
				</view>
				<view class="upload-img">
					<view class="img" v-for="(item,index) in imgList" :key="index"  >
						<image :src="item" mode="aspectFill"  @click="previewImage(index)"></image>
						<view class="del" @click="delImg(index)" >
							<image src="https://zkzh-2025.oss-cn-beijing.aliyuncs.com/orangeShop/del2.png"></image>
						</view>
					</view>
					<view class="chose-img" @tap="chooseImage(1)" v-if="imgList.length<4">
						<image src="https://zkzh-2025.oss-cn-beijing.aliyuncs.com/orangeShop/adds.png"></image>
					</view>
				</view>
			</view>
			 
		</view>
		<view class="btn-box">
			<view class="sub-btn" @click="submit()">提交记录</view>
		</view>
	</view>
</template>

<script>
	import {getDocDetails,addDocRecord} from '@/api/doc.js'
	export default {
		data() {
			return {
				imgList: [],  
				docId:null,
				form: {
					images:[],
					title:""
				}
			};
		},
		onLoad(option) {
			this.docId=option.docId;
		},
		methods:{
			contentInput(e) {
				this.form.title = e.detail.value
			},
			// 选择图片
			chooseImage(type) {
				var that=this;
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: (res) => {
						uni.uploadFile({
							url: uni.getStorageSync('requestPath')+'/app/common/uploadOSS', //仅为示例，非真实的接口地址
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: (res) => {
								that.imgList.push(JSON.parse( res.data).url)
							}
						});
					}
				});
			},
			//图片预览
			previewImage(index){
				//预览图片
				uni.previewImage({
					urls: this.imgList,
					current: this.imgList[index]
				});
			},
			delImg(index) {
				uni.showModal({
					title: '提示',
					content: '确定要删除这张图片吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(index, 1);
						}
					}
				})
			},
			submit(){
				var that=this;
				if(this.form.title==""){
					uni.showToast({
						icon:'none',
						title: "请输入健康记录内容"
					});
					return;
				}
				uni.showLoading({
					title:"正在处理中"
				})
				var data={
					docId:this.docId,
					title:this.form.title,
					images:this.imgList.toString()
				}
				addDocRecord(data).then(
				 	res => {
				 		if(res.code==200){
							uni.hideLoading()
				 			uni.showToast({
				 			 	icon:'success',
				 			 	title:res.msg,
				 			});
							uni.$emit('refreshDocRecord');
							setTimeout(function(){
								uni.navigateBack({
									delta:1
								})
							},200);
				 			
				 		}else{
				 			uni.showToast({
								icon:'none',
				 				title: res.msg,
				 			});
				 		}
				 	},
				 	rej => {}
				 );
			},
			 
		}
	}
</script>

<style lang="scss">
	page{
		height: 100%;
	}
	.content{
		height: 100%;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		.inner{
			height: calc(100% - 120upx);
			padding: 20upx;
			.text-content{
				box-sizing: border-box;
				min-height: 286upx;
				background: #FFFFFF;
				border-radius: 16upx;
				padding: 40upx 30upx 22upx;
				margin-top: 20upx;
				.title{
					display: flex;
					align-items: center;
					align-items: flex-end;
					.black{
						font-size: 32upx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #111111;
						line-height: 1;
					}
					.gray{
						font-size: 26upx;
						font-family: PingFang SC;
						color: #999999;
						line-height: 1;
						margin-left: 10upx;
					}
				}
				.textArea{
					margin: 30rpx 0rpx 0rpx 0rpx;
					width: 100%;
				}
				 
				
				 
			}
			.img-content{
				box-sizing: border-box;
				min-height: 286upx;
				background: #FFFFFF;
				border-radius: 16upx;
				padding: 40upx 30upx 22upx;
				margin-top: 20upx;
				margin-bottom: 20upx;
				.title{
					display: flex;
					align-items: center;
					align-items: flex-end;
					.black{
						font-size: 32upx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #111111;
						line-height: 1;
						margin-left: 10upx;
					}
					.gray{
						font-size: 28upx;
						font-family: PingFang SC;
						color: #999999;
						line-height: 1;
						margin-left: 10upx;
					}
				}
				.textArea{
					margin: 30rpx 0rpx 0rpx 0rpx;
					width: 100%;
				}
				.upload-img{
					margin: 30rpx 0rpx 0rpx 0rpx;
					width: 100%;
					display: flex;
					align-items: flex-start;
					.img{
						margin-right: 10rpx;
						width: 100rpx;
						height: 100rpx;
						position: relative;
						image{
							width: 100%;
							height: 100%;
						}
						.del{
							right:0rpx;
							top:0rpx;
							position: absolute;
							image{
								width: 30rpx;
								height:30rpx;
							}
						}
					}
					.chose-img{
						box-sizing: border-box;
						border-radius: 5rpx;
						border: 1px solid #eee;
						display: flex;
						align-items: center;
						justify-content: center;
						width: 100rpx;
						height: 100rpx;
						image{
							width: 50rpx;
							height: 50rpx;
						}
						
					}
				}
				
				 
			}
		}
		.btn-box{
			height: 120upx;
			padding: 0 30upx;
			display: flex;
			align-items: center;
			justify-content: center;
			background: #FFFFFF;
			.sub-btn{
				width: 100%;
				height: 88upx;
				line-height: 88upx;
				text-align: center;
				font-size: 30upx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #FFFFFF;
				background: #FC581C;
				border-radius: 44upx;
			}
		}
	}
</style>
