<template>
	<view class="content">
			<view class="form-box">
				
				<view class="form-item">
					<text class="label">指标名称</text>
					<input type="text" :value="detail.indicatorName" class="form-input" disabled="disabled"/>
				</view>
				
				<view class="form-item">
					<text class="label">参考值范围</text>
					<input type="text" :value="detail.referenceMin+'-'+detail.referenceMax" class="form-input" disabled="disabled"/>
				</view>
				<view class="form-item">
					<text class="label">指标单位</text>
					<input type="text" :value="unitName" class="form-input" disabled="disabled"/>
				</view>
				<view class="form-item">
					<text class="label">检测值</text>
					<input type="digit" v-model="form.testValue" placeholder="请输入测量值" placeholder-class="form-input" />
				</view>
				
			</view>
		<view class="btn-box2">
			<view  class="sub-btn" @click="submit()">
				确认
			</view>
		</view>
	</view>
</template>

<script>
	import {getResultById,updateResult,addResult,getListAll,listEnabled,listByIndicator} from '@/api/companyUser.js'
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin], 
		data() {
			return {
				orderStatus:[
					{name:"电话记录",val:1},
					{name:"短信记录",val:2},
				],
				aIndex:0,
				cates:[],
				tags:[],
				str:null,
				end:null,
				showType:1,
				mescroll:null,
				indicatorName:null,
				unitName:null,
				// 上拉加载的配置
				upOption: {
					onScroll:true,
					use: true, // 是否启用上拉加载; 默认true
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 10, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						icon:'/static/images/no_data.png',
						tip: '暂无数据'
					}
				},
				// 列表数据
				form:null,
				detail:null
				
			}
		},
		onLoad(options) {
			this.type=options.type;
			if(this.type=='edit'){
				this.resultId=options.resultId;
				this.reportId=options.reportId
				this.getResultById();
				// this.listByIndicator()
			}
			// this.form.reportId=options.reportId
			// this.form.resultId=options.resultId
			this.getListAll()
			this.listEnabled()
			// uni.$on('refreshDoctorOrder', () => {
			// 	this.mescroll.resetUpScroll()
			// })
		},
		methods: {
			changeTag(index){
				this.aIndex=index
			},
			bindNameChange:function(e,list) {
				console.log(list[e.target.value],'--')
				this.indicatorName = list[e.target.value].indicatorName
				this.form.indicatorName = list[e.target.value].indicatorName
				this.form.indicatorId=list[e.target.value].indicatorId
				},
			bindUnitChange:function(e,list) {
				console.log(e.target.value)
				this.unitName = list[e.target.value].unitSymbol
				this.form.unitName = list[e.target.value].unitSymbol
				},
			radioClick(id) {
			        this.form.isAbnormal=id
			      },
				  getListAll(){
					  getListAll().then(
					  	res => {
					  		if(res.code==200){
					  			  this.tags=res.data;
					  		}else{
					  			uni.showToast({
					  				title: res.msg,
					  			});
					  		}
					  	},
					  	rej => {}
					  );
				  },
				  listEnabled() {
				  	listEnabled().then(
				  		res => {
				  			if (res.code == 200) {
				  				this.cates = res.data;
				  			} else {
				  				uni.showToast({
				  					icon: 'none',
				  					title: res.msg,
				  				});
				  			}
				  		},
				  		rej => {}
				  	);
				  },
			getResultById(){
				getResultById(this.resultId).then(
					res => {
						if(res.code==200){
							
							  this.form=res.data;
							  this.detail=res.indicator
							  const unit = this.tags.find(item => item.unitId == res.indicator.unitId);
							  // 安全地获取单位名称，避免空数组错误
							  this.unitName = unit ? unit.unitSymbol : '';
							  
						}else{
							uni.showToast({
								title: res.msg,
							});
						}
					},
					rej => {}
				);
			},
			listByIndicator(){
				listByIndicator(this.resultId).then(
					res => {
						if(res.code==200){
							  this.form=res.data;
							  
							  // this.unitName = this.tags[res.data.unitId].unitSymbol
							  // this.form.examDate=this.form.examDate.slice(0,5)
							  
						}else{
							uni.showToast({
								title: res.msg,
							});
						}
					},
					rej => {}
				);
			},
			submit(){
				// if(this.form.testValue==null){
				// 	uni.showToast({
				// 		icon:'none',
				// 		title: "检测值不能为空",
				// 	});
				// 	return;
				// }
				// if(this.form.indicatorName==null){
				// 	uni.showToast({
				// 		icon:'none',
				// 		title: "指标名称不能为空",
				// 	});
				// 	return;
				// }
				// if(this.form.unitName==null){
				// 	uni.showToast({
				// 		icon:'none',
				// 		title: "单位不能为空",
				// 	});
				// 	return;
				// }
				if(this.form.testValue==null){
					uni.showToast({
						icon:'none',
						title: "检测值不能为空",
					});
					return;
				}
				this.form.unitName=this.unitName
				// if(this.form.recordTime==null){
				// 	uni.showToast({
				// 		icon:'none',
				// 		title: "记录时间不能为空",
				// 	});
				// 	return;
				// }
				// if(this.form.mealDescription==null){
				// 	uni.showToast({
				// 		icon:'none',
				// 		title: "用餐情况不能为空",
				// 	});
				// 	return;
				// }
				if(this.type=="add"){
					this.addResult()
				}
				else if(this.type=="edit"){
					this.updateResult()
				}
				
			},
			updateResult(){
				updateResult(this.form).then(
					res => {
						if(res.code==200){
							 uni.showToast({
							 	icon:'success',
							 	title: "更新成功",
							 });
							 setTimeout(function() {
								  uni.$emit('refreshInformation');
								 uni.$emit('refreshBatchRecord');
								 uni.navigateBack({
									 delta: 1
								 })
							 }, 500);
						}else{
							uni.showToast({
								icon:'none',
								title: res.msg,
							});
						}
					},
					rej => {}
				);
			},
			addResult(){
				addResult(this.form).then(
					res => {
						if(res.code==200){
							 uni.showToast({
							 	icon:'success',
							 	title: "操作成功",
							 });
							 setTimeout(function() {
								 uni.$emit('refreshInformation');
								uni.$emit('refreshBatchRecord');
								 uni.navigateBack({
									 delta: 1
								 })
							 }, 500);
						}else{
							uni.showToast({
								icon:'none',
								title: res.msg,
							});
						}
					},
					rej => {}
				);
			},
			// tab切换
			orderStatusChange(item) {
				this.showType = item.val
				this.mescroll.resetUpScroll()
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*下拉刷新的回调 */
			downCallback(mescroll) {
				mescroll.resetUpScroll()
			},
			upCallback(page) {
				//联网加载数据
				var that = this;
				var data = {
					status:this.showType,
					page: page.num,
					pageSize: page.size
				};
				// getMyDoctorOrderList(data).then(res => {
				// 	if(res.code==200){
				// 		//设置列表数据
				// 		if (page.num == 1) {
				// 			that.dataList = res.data.list; 
							
				// 		} else {
				// 			that.dataList = that.dataList.concat(res.data.list);
							 
				// 		}
				// 		that.mescroll.endBySize(res.data.list.length, res.data.total);
						
				// 	}else{
				// 		uni.showToast({
				// 			icon:'none',
				// 			title: "请求失败",
				// 		});
				// 		that.dataList = null;
				// 		that.mescroll.endErr();
				// 	}
				// });
			},
			showDetail(item) {
				if(item.orderType==1){
					uni.navigateTo({
						url: '/pages_doctor/doctorOrderIM?orderId='+item.orderId
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.content{
		padding: 20upx;
		.form-box{
			// padding: 0 30upx;
			// background: #FFFFFF;
			// border-radius: 16upx;
			.form-item{
				padding: 30upx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				background: #FFFFFF;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				margin-bottom: 20rpx;
				// border-bottom: 1px solid #F1F1F1;
				&:last-child{
					margin-bottom:0;
				}
				.label{
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 32rpx;
					color: #222426;
					line-height: 34rpx;
					text-align: left;
				}
				input{
					text-align: right;
				}
				.to{
					color: #626468;
					margin: 0 20rpx;
				}
				.switch{
					width: 232rpx;
					height: 72rpx;
					background: #FFFFFF;
					border-radius: 36rpx 36rpx 36rpx 36rpx;
					border: 2rpx solid #008FD3;
					.item{
						width: 112rpx;
						height: 64rpx;
						line-height: 63rpx;
						font-weight: 400;
						font-size: 24rpx;
						color: #008FD3;
						text-align: center;
						&.active{
							color: #FFFFFF;
							background: #008FD3;
							border-radius: 32rpx 32rpx 32rpx 32rpx;
						}
					}
				}
				
				.form-input{
					font-size: 32upx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #999999;
					text-align: right;
				}
				.form-input2{
					width: 150rpx;
					// font-size: 32upx;
					// font-family: PingFang SC;
					// font-weight: 400;
					// color: #999999;
					// text-align:center;
				}
				.birth-picker {
					flex: 1;
					display: flex;
					align-items: center;
					
					.right-box{
						width: 100%;
						display: flex;
						align-items: center;
						.input-box{
							width: 470upx;
						}
						.arrow{
							width: 13upx;
							height: 23upx;
							margin-left: 20upx;
						}
					}
				}
			}
			.note{
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 24rpx;
				color: #898E91;
				text-align: left;
			}
		}
	
		.btn-box2{
			z-index: 9999;
			width: 100%;
			padding: 30upx;
			// position: fixed;
			bottom: 0;
			left: 0;
			box-sizing: border-box;
			// background: #FFFFFF;
			display: flex;
			align-items: center;
			justify-content: space-between;
			 padding-bottom: 40rpx;
			.sub-btn{
				flex:1;
				// width: 100%;
				height: 88upx;
				line-height: 88upx;
				text-align: center;
				font-size: 32upx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #FFFFFF;
				background: #008FD3;
				border-radius: 44upx;
				display: flex;
				align-items: center;
				 border: 2rpx solid #008FD3;
				justify-content: center;
				image{
					margin-right: 16rpx;
				}
			}
			.sub-btn2{
					flex:1;
					height: 88upx;
					line-height: 88upx;
					text-align: center;
					font-size: 32rpx;
					color: #008FD3;
					font-family: PingFang SC;
					font-weight: bold;
					background: #FFFFFF;
						 border-radius: 44rpx 44rpx 44rpx 44rpx;
						 border: 2rpx solid #008FD3;
						 margin-right: 20rpx;
			}
		}
	   .phone-list{
		   // padding: 30rpx;
		  
		   .item{
			   display: flex;
			   align-items: center;
			   justify-content: space-between;
			   background: #FFFFFF;
			   border-radius: 16rpx 16rpx 16rpx 16rpx;
			   // border-bottom: 1rpx solid #ECECEC;
			   padding: 20rpx;
			   margin-bottom: 20rpx;
			   &:last-child{
				    margin-bottom: 0;
					
			   }
			   .phone-name{
				   display: flex;
				   flex-direction: column;
				   align-items: flex-start;
				   // justify-content: flex-start;
				   // padding-left: 24rpx;
				       flex: 1;
				   .name{
				   				   font-family: PingFang SC, PingFang SC;
				   				   font-weight: 500;
				   				   font-size: 32rpx;
				   				   color: #222426;
				   				   text-align: left;
				   }
				   .type{
					   font-family: PingFang SC;
					   font-weight: 400;
					   font-size: 28rpx;
					   color: #626468;
					   text-align: right;
				   }
				   .time{
					   font-family: PingFang SC;
					   font-weight: 400;
					   font-size: 24rpx;
					   color: #898E91;
					   text-align: left;
					   margin-top: 14rpx;
				   }
			   }
			   
		   }
	   }
	   .sms-list{
		   .item{
			   padding: 30rpx;
			   background: #FFFFFF;
			   border-radius: 16rpx 16rpx 16rpx 16rpx;
			   margin-bottom: 20rpx;
			   .title{
				   font-family: PingFang SC;
				   font-weight: 500;
				   font-size: 32rpx;
				   color: #222426;
				   text-align: left;
			   }
			   .box{
				   display: flex;
				   align-items: center;
				   justify-content: space-between;
				   padding-top: 22rpx;
				   .time{
					   font-family: PingFang SC;
					   font-weight: 400;
					   font-size: 24rpx;
					   color: #898E91;
					   text-align: left;
				   }
				   .state{
					   font-family: PingFang SC;
					   font-weight: 400;
					   font-size: 24rpx;
					   color: #2CAE5C;
					   text-align: right;
				   }
			   }
		   }
	   }
		
	}
</style>
