<template>
	<view class="content">
		<view class="inner">
			<!-- 选择问诊人 -->
			<view class="chose-people">
				<view class="title-box">
					<text class="title">选择问诊人</text>
					<view class="add-box">
						<image src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/add26.png" mode=""></image>
						<text @click="addPeople">添加</text>
					</view>
				</view>
				<view class="peop-list">
					<view v-for="(item,index) in patient" :key="index" :class="patientId == item.patientId?'item active':'item'" @click="chosePatient(item)">
						<view class="name">{{ item.patientName }}</view>
						<view class="info">
							<text class="sex" v-if="item.gender==1">男</text>
							<text class="sex" v-if="item.gender==2">女</text>
							<text class="ege">{{utils.getAge(item.birthday)}}岁</text>
						</view>
						<!-- 选中的对号角标 -->
						<image v-if="patientId == item.patientId" class="active-img" src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/sel_right50.png" mode=""></image>
					</view>
				</view>
			</view>
			<!-- 病情描述 -->
			<view class="desc">
				<textarea 
					v-model="remark"
					placeholder="请描述您的病情..."
					placeholder-style="color:#999999;font-size:30rpx;"
				/>
				<!-- 上传图片描述 -->
				<view class="upload-des">
					<view class="label-box">
						<text class="black">上传疾病部位照片</text>
						<text class="gray">(选填)</text>
					</view>
					<view class="num-box">
						<text class="black">{{ imgList.length }}</text>
						<text class="gray">/3</text>
					</view>
				</view>
				<!-- 多图上传 -->
				<view class="upload-box">
					<!-- 上传的图片列表 -->
					<view class="img-item" v-for="(item,index) in imgList" :key="index" @tap="viewImage" :data-url="imgList[index]">
						<image class="img" :src="item" mode="aspectFill"></image>
						<image class="close-img" src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/close32.png" mode="" @tap.stop="delImg" :data-index="index"></image>
					</view>
					<!-- 上传图片按钮 -->
					<view class="uoload-btn" @tap="chooseImage" v-if="imgList.length<3">
						<image src="https://jnlzjk-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/photo.png" mode=""></image>
						<text>上传照片</text>
					</view>
				</view>
			</view>
			
		</view>
		<view class="btn-box">
			<view class="btn" @click="submitOrder()">发布问诊</view>
		</view>
	</view>
</template>

<script>
	import {getPatientList} from '@/api/patient'
	import {uploadOSS} from '@/api/common.js'
	import {create} from '@/api/doctorOrder.js'
	export default {
		data() {
			return {
				patient:[],
				patientId:null,
				priceId:null,
				doctorId:null,
				remark:"",
				// 选择上传的图片
				imgList: [],
				photos:[],
				  
			};
		},
		onLoad(option) {
			this.doctorId=option.doctorId;
			this.priceId=option.priceId;
			this.getPatientList();
			uni.$on('refreshPatient', () => {
				this.getPatientList()
			})
		},
		methods:{
			getPatientList(){
			 	uni.showLoading({
			 		title:"正在加载中"
			 	})
			 	getPatientList().then(
			 		res => {
			 			uni.hideLoading()
			 			if(res.code==200){
			 				this.patient=res.data;
			 			}else{
			 				uni.showToast({
			 					icon:'none',
			 					title: "请求失败",
			 				});
			 			}
			 		},
			 		rej => {}
			 	);
			},
			// 选中问诊人
			chosePatient(item) {
				this.patientId = item.patientId
			},
			// 添加问诊人
			addPeople() {
				uni.navigateTo({
					url:"pages_user/user/addPatient?type=add"
				})
			},
			 
			// 选择上传的图片
			chooseImage() {
				var that = this;
				uni.showActionSheet({
					itemList: ['相册', '拍照'],
					success: function(res) {
						if (res.tapIndex == 0) {
							//从相册选择
							that.chooseImageFunction('album')
						} else if (res.tapIndex == 1) {
							//拍照
							that.chooseImageFunction('camera')
						}
					},
					fail: function(res) {
						
					}
				});
			},
			chooseImageFunction: function(type) {
				var that = this;
				uni.chooseImage({
					count: 3, // 默认9
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: [type],
					success: (res) => {
						uni.uploadFile({
							url: uni.getStorageSync('requestPath')+'/app/common/uploadOSS', //仅为示例，非真实的接口地址
							filePath: res.tempFilePaths[0],
							name: 'file',
							formData: {
								'user': 'test'  // 上传附带参数
							},
							success: (uploadFileRes) => {
								// 根据接口具体返回格式   赋值具体对应url
								if (this.imgList.length != 0) {
									this.imgList = this.imgList.concat(res.tempFilePaths)
								} else {
									this.imgList = res.tempFilePaths
									
								}
						 
								this.photos = this.photos.concat(JSON.parse(uploadFileRes.data).url)
							}
						});
						 
					}
				});
			},
			// 查看图片
			viewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			// 删除上传的图片
			delImg(e) {
				var that = this;
				uni.showModal({
					title: '提示',
					content: '确定要删除照片吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							that.imgList.splice(e.currentTarget.dataset.index, 1)
							this.photos.splice(e.currentTarget.dataset.index, 1);
						}
					},
				})
			},
			submitOrder() {
				if(this.remark==""){
					uni.showToast({
						icon:'none',
						title: '请输入描述信息',
					});
					return;
				}
				if(this.patientId==null){
					uni.showToast({
						icon:'none',
						title: '请选择患者',
					});
					return;
				}
				let data = {patientId:this.patientId,title:this.remark,doctorId:this.doctorId,priceId:this.priceId,imgs:this.photos.toString()};
				create(data).then(
					res => {
						if(res.code==200){
							setTimeout(function(){
								uni.redirectTo({
									url: './paymentOrder?orderId='+res.order.orderId
								})
							},200);
							return;
						}
						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{
			flex: 1;
			padding: 20upx;
			box-sizing: border-box;
			.chose-people{
				box-sizing: border-box;
				min-height: 247upx;
				background: #FFFFFF;
				border-radius: 16upx;
				padding: 40upx 30upx 30upx;
				.title-box{
					display: flex;
					align-items: center;
					justify-content: space-between;
					.title{
						font-size: 32upx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #111111;
					}
					.add-box{
						display: flex;
						align-items: center;
						image{
							width: 26upx;
							height: 26upx;
							margin-right: 11upx;
						}
						text{
							font-size: 28upx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #111111;
						}
					}
				}
				.peop-list{
					display: flex;
					flex-wrap: wrap;
					margin-top: 30upx;
					.item{
						box-sizing: border-box;
						padding: 18upx 30upx;
						width: 31.28%;
						height: 110upx;
						background: #F7F7F7;
						border-radius: 16upx;
						margin: 0 20upx 10upx 0;
						border: 2upx solid #F7F7F7;
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						&:nth-child(3n){
							margin-right: 0;
						}
						&.active{
							background: rgba(230, 250, 247, 0);
							border: 2upx solid #2BC7B9;
							position: relative;
						}
						.active-img{
							width: 50upx;
							height: 50upx;
							position: absolute;
							right: -2upx;
							bottom: -2upx;
						}
						.name{
							font-size: 30upx;
							line-height: 1;
							font-family: PingFang SC;
							font-weight: bold;
							color: #111111;
						}
						.info{
							display: flex;
							align-items: center;
							.sex,
							.ege{
								font-size: 26upx;
								font-family: PingFang SC;
								line-height: 1;
								font-weight: 500;
								color: #666666;
							}
							.ege{
								margin-left: 19upx;
							}
						}
					}
				}
			}
			.desc{
				box-sizing: border-box;
				height: 619upx;
				background: #FFFFFF;
				border-radius: 16upx;
				margin-top: 20upx;
				padding: 40upx 30upx;
				textarea{
					width: 100%;
					height: 280upx;
				}
			}
			.upload-des{
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding-top: 40upx;
				border-top: 1px solid #F0F0F0;
				.label-box{
					display: flex;
					align-items: center;
					.black{
						font-size: 28upx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #111111;
					}
					.gray{
						font-size: 28upx;
						font-family: PingFang SC;
						color: #999999;
					}
				}
				.num-box{
					display: flex;
					align-items: center;
					.black{
						font-size: 24upx;
						font-family: PingFang SC;
						color: #111111;
					}
					.gray{
						font-size: 24upx;
						font-family: PingFang SC;
						color: #999999;
					}
				}
			}
			.upload-box{
				display: flex;
				flex-wrap: wrap;
				padding-top: 30upx;
				.img-item{
					width: 160upx;
					height: 160upx;
					border-radius: 16upx;
					overflow: hidden;
					position: relative;
					margin-right: 20upx;
					.img{
						width: 100%;
						height: 100%;
					}
					.close-img{
						width: 32upx;
						height: 32upx;
						position: absolute;
						top: 4upx;
						right: 4upx;
					}
				}
				.uoload-btn{
					width: 160upx;
					height: 160upx;
					background: #F7F7F7;
					border-radius: 16upx;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					image{
						width: 40upx;
						height: 32upx;
						margin-bottom: 16upx;
					}
					text{
						font-size: 22upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #666666;
						line-height: 1;
					}
				}
			}
		}
		.btn-box{
			height: 121upx;
			background: #FFFFFF;
			display: flex;
			align-items: center;
			justify-content: center;
			.btn{
				width: 91.73%;
				height: 88upx;
				line-height: 88upx;
				font-size: 30upx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #FFFFFF;
				text-align: center;
				background: #2BC7B9;
				border-radius: 44upx;
			}
		}
	}
</style>
