<template>
	<view class="content">
		<view class="top-box align-center justify-between">
			<view class="title">检查时间</view>
			<view class="align-center">
				<view class="time">{{date}}</view>
				<image src="/static/images/health/right_arrow_right_icon24.png" class="w48 h48"></image>
			</view>
		</view>
		<!-- <mescroll-body ref="mescrollRef"> -->
		<view class="phone-list">
			<view class="title-top align-center">
				<view class="left">记录指标</view>
				<!-- <view class="right align-center" @click="navgetTo()">
						<image src="/static/images/user/edit_add_icon12.png" class="w24 h24"></image>
						<text>编辑指标</text>
					</view> -->
			</view>
			<view v-for="(item,index) in dataList" :key="index" :class="item.testValue==null?'item edit':'item'">
				<view class="phone-name">
					<view class="name">{{item.indicatorName}}{{item.description&&item.description!==null?'('+item.description+')':''}}
					</view>
					<input type="digit" v-model="item.testValue" placeholder="请输入测量值" placeholder-class="form-input"/>
					<!-- <view v-if="item.testValue" class="num">{{item.testValue||''}}</view> -->
				</view>
				<view class="text">参考范围：{{item.refRange||''}} 单位：{{item.unitName||''}}</view>
			</view>
		</view>
		<!-- </mescroll-body> -->
		<view class="h160"></view>
		<view class="btn-box2">
			<!-- <view class="sub-btn2" @click="navgetTo('/pages_user/user/myFolder')">
				<image src="/static/images/user/nav_add_icon25.png" class="w48 h48"></image>
				<text>识别导入</text>
			</view> -->
			<view class="sub-btn" @click="submit()">
				<text>保存</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		listByReport,
		listByIndicator,
		addBatchAdd,
		deleteResult,
		listEnabled,
		updateBatch
	} 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
					},
				],
				date: null,
				showType: 1,
				mescroll: 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: '暂无数据'
					}
				},
				// 列表数据
				dataList: [
					// {
					// 	orderId: 1,
					// 	name: '大血小板比率(P-LCR)',
					// 	num: 13,
					// 	text: '参考值：15.9~43.47 %',
					// 	edit: 0
					// },
					// {
					// 	orderId: 2,
					// 	name: '血小板体积分布宽度(PDW)',
					// 	num: 11,
					// 	text: '参考范围：0-1.75 单位:U/L',
					// 	edit: 0
					// },
					// {
					// 	orderId: 3,
					// 	name: '血小板体积分布宽度(PDW)',
					// 	text: '参考范围：0-1.75 单位:U/L',
					// 	edit: 1
					// }
				],
			}
		},
		onLoad(options) {
			if (options.id) {
				this.reportId = options.id;
				this.listByReport()
			}
			this.date = options.date
			// uni.$on('refreshDoctorOrder', () => {
			// 	this.mescroll.resetUpScroll()
			// })
		},
		methods: {
			pay(item) {
				uni.navigateTo({
					url: '/pages_/doctor/paymentOrder?orderId=' + item.orderId
				})
			},
			navgetTo(url) {
				uni.navigateTo({
					url: '/pages_user/user/addResult?type=add&reportId=' + this.reportId
				})
			},
			listByReport() {
				listByReport(this.reportId).then(
					res => {
						if (res.code == 200) {
							this.dataList = res.data;

						} else {
							uni.showToast({
								title: res.msg,
							});
						}
					},
					rej => {}
				);
			},
			cancel(item, index) {
				var that = this;
				// let test = 'swipeAction' + index
				uni.showModal({
					title: '提示',
					content: '确定删除指标吗',
					success: function(res) {
						if (res.confirm) {
							// var data = {
							// 	orderId:item.orderId
							// };
							deleteResult(item.resultId).then(res => {
								if (res.code == 200) {
									uni.showToast({
										icon: 'success',
										title: '删除成功',
									});
									that.listByReport()
									// that.$refs[test][0].closeHandler()
									// that.mescroll.resetUpScroll()
								} else {
									uni.showToast({
										icon: 'none',
										title: res.msg,
									});
								}
							});
						} else if (res.cancel) {
							// that.$refs[test][0].closeHandler()
						}
					}
				});
			},
			submit() {
				// if(this.form.recordDate==null){
				// 	uni.showToast({
				// 		icon:'none',
				// 		title: "日期不能为空",
				// 	});
				// 	return;
				// }
				// if(this.form.recordTime==null){
				// 	uni.showToast({
				// 		icon:'none',
				// 		title: "记录时间不能为空",
				// 	});
				// 	return;
				// }
				// if(this.form.mealDescription==null){
				// 	uni.showToast({
				// 		icon:'none',
				// 		title: "用餐情况不能为空",
				// 	});
				// 	return;
				// }
				// this.formList.push(this.form)
				// this.addReport()
				const data = this.dataList.map(item => ({
					 reportId: this.reportId,
					 resultId: item.resultId,
					  abnormalType: item.abnormalType,
					  indicatorCateId: item.indicatorCateId,
					  indicatorId: item.indicatorId,
					  indicatorName: item.indicatorName,
					  isAbnormal: item.isAbnormal,
					  remarks: item.remarks,
					  testResult: item.testResult,
					  testValue: item.testValue
				}));
				this.updateReport(data)
				// if(this.type=="add"){
				// 	this.addReport()
				// }
				// else if(this.type=="edit"){
				// 	this.updateReport()
				// }

			},
			updateReport(data) {
				updateBatch(data).then(
					res => {
						if (res.code == 200) {
							uni.showToast({
								icon: 'success',
								title: "更新成功",
							});
							setTimeout(function() {
								uni.$emit('refreshInformation');
								uni.navigateBack({
									delta: 1
								})
							}, 500);
						} else {
							uni.showToast({
								icon: 'none',
								title: res.msg,
							});
						}
					},
					rej => {}
				);
			},
			addReport() {
				const list = this.dataList.filter(item => item.testValue !== null && item.testValue !== undefined)
				const data = list.map(item => ({
					reportId: this.reportId,
					resultId: item.resultId,
					 abnormalType: item.abnormalType,
					 indicatorCateId: item.indicatorCateId,
					 indicatorId: item.indicatorId,
					 indicatorName: item.indicatorName,
					 isAbnormal: item.isAbnormal,
					 remarks: item.remarks,
					 testResult: item.testResult,
					 testValue: item.testValue
				}));
				addBatchAdd(data).then(
					res => {
						if (res.code == 200) {
							uni.showToast({
								icon: 'success',
								title: "操作成功",
							});
							setTimeout(function() {
								uni.$emit('refreshInformation');
								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) {

				uni.navigateTo({
					url: '/pages_user/user/addResult?type=edit&id=' + item.resultId
				})
			}
		}
	}
</script>

<style lang="scss">
	.content {
		padding: 20upx;

		.top-box {
			padding: 30rpx;
			background: #FFFFFF;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			margin-bottom: 20rpx;

			.title {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 32rpx;
				color: #222426;
				// line-height: 34rpx;
			}

			.time {
				font-weight: 400;
				font-size: 32rpx;
				color: #222426;
				// line-height: 34rpx;
				text-align: right;

			}
		}

		.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;

			}

			.sub-btn2 {
				flex: 1;
				height: 88upx;
				line-height: 88upx;
				display: flex;
				align-items: center;
				justify-content: center;
				text-align: center;
				font-size: 32rpx;
				color: #fff;
				font-family: PingFang SC;
				font-weight: bold;
				background: #FF5030;
				border-radius: 44rpx 44rpx 44rpx 44rpx;
				margin-right: 20rpx;

				image {
					margin-right: 16rpx;
				}
			}
		}

		.phone-list {
			padding: 30rpx;
			background: #FFFFFF;
			border-radius: 16rpx 16rpx 16rpx 16rpx;

			.title-top {
				margin-bottom: 30rpx;

				.left {
					font-family: PingFang SC, PingFang SC;
					font-weight: 600;
					font-size: 32rpx;
					color: #222426;
					text-align: left;
				}

				.right {
					text {
						font-weight: 400;
						font-size: 24rpx;
						color: #008FD3;
						text-align: right;
						margin-left: 5rpx;
					}
				}
			}

			.item {
				display: flex;
				flex-direction: column;
				align-items: flex-start;
				justify-content: space-between;
				padding: 20rpx;
				margin-bottom: 20rpx;
				background: linear-gradient(360deg, #F0FAFF 0%, #DEF4FF 100%);
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				border: 2rpx solid rgba(0, 143, 211, 0.5);
				height: 158rpx;
				box-sizing: border-box;

				&:last-child {
					margin-bottom: 0;

				}

				&.edit {
					background: #FFF6EA;
					border-radius: 16rpx 16rpx 16rpx 16rpx;
					border: 2rpx solid #F3CDA7;
				}

				.phone-name {
					// height: 158rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					width: 100%;

					.name {
						font-family: PingFang SC, PingFang SC;
						font-weight: 600;
						font-size: 32rpx;
						color: #222426;
						text-align: left;
					}

					input {
						text-align: right;
					}

					.form-input {
						font-size: 32upx;
						font-family: PingFang SC;
						font-weight: 400;
						color: #999999;
						text-align: right;
					}

					.num {
						font-weight: 600;
						font-size: 32rpx;
					}
				}

				.text {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #898E91;
					text-align: left;
				}
			}
		}

		.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>