<template>
	<view class="content">
		<view class="top-fixed">
			<view class="date-box">
				<view class="text">体检日期</view>
				<!-- <picker class="birth-picker" mode="date">
					<view class="right-box"> -->
						<view class="input-box">
							<input type="text" :value="form.examDate" placeholder="请选择日期" placeholder-class="form-input" disabled="disabled" />
						</view>
						<!-- <image class="w48 h48" src="@/static/images/health/right_arrow_right_icon24.png"></image> -->
					<!-- </view> -->
				</picker>
			</view>
			<!-- tab切换 -->
			
			<view class="pub-tab-box">
				<!-- <scroll-view   scroll-x="true" >
					<view class="inner">
						<view v-for="(item,index) in cateOptions" :key="index" :class="cateId == item.dictValue?'item active':'item'" @click="choseCate(item)">
							{{ item.dictLabel }}
						</view>
					</view>
				</scroll-view> -->
				<scroll-view scroll-x="true" >
				<view class="tab-inner" v-if="type=='edit'">
					<view v-for="(item,index) in orderStatus" :key="item.cateId"
						:class="item.cateId == showType?'item active':'item'" @click="choseCate(item)">
						<view class="text">
							{{item.cateName}}
						</view>
					</view>
				</view>
				<view class="tab-inner" v-else>
					<text class="fs32rpx ml30">身体信息</text>
				</view>
				</scroll-view>
				
			</view>
			
		</view>

		
		<view class="m-list" v-if="showType==10000">
			<view class="form-box">
				<view class="form-item">
					<text class="label">身高</text>
					<input maxlength="10" class="input-width" type="digit" v-model="form.height" placeholder="请输入身高"
						placeholder-class="form-input" />
					<text class="unit">厘米 (CM)</text>
				</view>
				<view class="form-item">
					<text class="label">体重</text>
					<input maxlength="10" class="input-width" type="digit" v-model="form.weight" placeholder="请输入体重"
						placeholder-class="form-input" />
					<text class="unit">千克 (KG)</text>
				</view>
				<view class="form-item justify-between">
					<view class="align-start">
						<text class="label">BMI</text>
						<input maxlength="10" class="input-width" type="digit" v-model="form.bmi"
							placeholder="请输入BMI" placeholder-class="form-input" />
					</view>
					<image src="/static/images/user/help_icon24.png" class="w32 h32"></image>
				</view>
				<view class="form-item">
					<text class="label">臀围</text>
					<input maxlength="10" class="input-width" type="digit" v-model="form.chestCircumference" placeholder="请输入臀围"
						placeholder-class="form-input" />
					<text class="unit">厘米 (CM)</text>
				</view>
				<view class="form-item">
					<text class="label">腰围</text>
					<input maxlength="10" class="input-width" type="digit" v-model="form.waistCircumference" placeholder="请输入腰围"
						placeholder-class="form-input" />
					<text class="unit">厘米 (CM)</text>
				</view>
			</view>
			</view>
			<view class="index-body" v-if="showType!==10000">
				<mescroll-body  top="196upx"  ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
				<view class="search-cont">
					<view class="inner2">
						<image class="icon-search" src="/static/images/search.png" mode=""></image>
						<input type="text" v-model="keyword" placeholder="请输入想要搜索的指标" confirm-type="search" @confirm="doSearch" placeholder-style="font-size:28rpx;color:#BBBBBB;font-family: PingFang SC;" />
					</view>
				</view>
				<view class="title-box">
					<view class="left-title">指标信息({{num}}项)</view>
					<view class="right align-center" @click="pageTo()">
						<image class="w24 h24" src="@/static/images/company/edit_add_icon.png"></image>
						<text>编辑指标项</text>
					</view>
				</view>
				
					<view class="sms-list">
						<u-swipe-action>
						<view class="form-swipe" v-for="(item,index) in dataList" :key="index">
							<u-swipe-action-item :options="options" @click="cancel(item,index)" :name="index"
								:ref="'swipeAction' + index">
						<view class="item" @click.stop="showDetail(item)">
							<view class="item-box">
								<view class="left">
									<view class="align-center">
										<view class="title">{{item.indicatorName}}</view>
										<!-- <view class="unit" v-if="item.unitName!==null">（{{item.unitName}}）</view> -->
									</view>
									<view class="align-center">
										<view class="num">{{item.testValue||''}}</view>
										<view v-if="item.testValue!==null">
											<view :class="item.isAbnormal==0?'tag2':'tag'">{{item.isAbnormal==0?'正常':'异常'}}</view>
										</view>
										
									</view>
									<view class="reference">
										参考值范围：{{item.refRange||''}}  单位：{{item.unitName||''}}
									</view>
								</view>
							
							<image src="/static/images/health/right_arrow_right_icon24.png" class="w48 h48"></image>
							</view>
						</view>
						</u-swipe-action-item>
						</view>
						</u-swipe-action>
					</view>
					<view class="h150"></view>
				</mescroll-body>
				
			
			</view>
			
		<!-- </mescroll-body> -->
		<view class="btn-box2">
			<view v-if="showType==10000" class="sub-btn" @click="submit()">
				<!-- <image src="/static/images/company/make_phone_call_icon.png" class="w48 h48"></image> -->
				<text>保存</text>
			</view>
			<view v-if="showType!==10000" class="sub-btn" @click="navgetTo()">
				<!-- <image src="/static/images/company/send_SMS_icon.png" class="w48 h48"></image> -->
				<text>批量编辑</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {getByUserAndDate,addReport,updateReport,getReportById,
	listByIndicator,getResult,deleteResult,getAllCateByReportId, listByReport
	} from '@/api/companyUser.js'
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				examDate:this.utils.timeFormat(new Date(),'yyyy-mm-dd'),
				num:null,
				type:null,
				keyword:'',
				orderStatus: [
					// {
					// 	cateName: "身体信息",
					// 	cateId: 10000
					// },
					// {
					// 	cateName: "指标检查",
					// 	cateId: 10001
					// },
					// {
					// 	name: "查体",
					// 	val: 2
					// },
					// {
					// 	name: "肝脏功能",
					// 	val: 3
					// },
				],
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#FF5030'
					}
				}],
				showType: 10000,
				userId:null,
				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:'https://user.test.ylrztop.com/images/empty_icon.png',
						tip: '暂无数据'
					}
				},
				indicatorCateId:'',
				form:{
						"bmi": null,
						"chestCircumference":null,
						"examDate":null,
						"height": null,
						// "reportId": 0,
						"reportStatus": 1,
						"userId": null,
						"waistCircumference":null,
						"weight": null,
				},
				// 列表数据
				dataList: [],
			}
		},
		onLoad(options) {
			this.type=options.type === 'edit' ? 'edit' : 'add';
			this.reportId=options.id;
			if(this.type=='edit'){
				this.getReportById();
				this.getAllCateByReportId()
				// this.getResult()
			}
			uni.$on('refreshInformation', () => {
				this.showType=10001
				this.indicatorCateId=''
				this.getAllCateByReportId()
				this.mescroll.resetUpScroll()
			})
			// var user=JSON.parse(uni.getStorageSync('userInfo'))
			// this.form.userId=user.userId
			this.form.userId=options.userId
			this.form.companyUserId=options.companyUserId
			this.form.examDate=this.examDate
			// this.getByUserAndDate()
		},
		methods: {
			pay(item) {
				uni.navigateTo({
					url: '/pages_/doctor/paymentOrder?orderId=' + item.orderId
				})
			},
			navgetTo(url) {
				uni.navigateTo({
					url: '/pages_user/user/batchRecord?id='+this.reportId+'&date='+this.form.examDate
				})
			},
			pageTo(url) {
				uni.navigateTo({
					url: '/pages_user/user/editIndicator?id='+this.reportId
				})
			},
			bindDateChange: function(e) {
				this.form.examDate = e.target.value
			},
			// tab切换
			choseCate(item) {
				this.showType = item.cateId
				// console.log(this.showType,item.cateId,'---')
				if(item.cateId!==10000&&item.cateId!==10001){
					this.indicatorCateId=item.cateId
				    // this.mescroll.resetUpScroll()
				}else{
					this.indicatorCateId=''
					// this.mescroll.resetUpScroll()
				}
				// 关键修复：仅当 mescroll-body 渲染（showType!==10000）且 mescroll 已初始化时，才重置滚动
				  if (this.showType!== 10000) {
				      // 延迟 100ms 执行，等 tab 切换渲染完成
				      setTimeout(() => {
				        this.$nextTick(() => {
				          if (this.mescroll && this.$refs.mescrollRef) {
				            this.mescroll.resetUpScroll();
				          }
				        });
				      }, 100);
				    }
				
			},
			getAllCateByReportId(){
				var data={
					 reportId:this.reportId,
				}
				var list=[{
						cateName: "身体信息",
						cateId: 10000
					},
					{
						cateName: "指标检查",
						cateId: 10001
					}]
				getAllCateByReportId(data).then(
					res => {
						if(res.code==200){
							this.orderStatus.splice(0, this.orderStatus.length, ...list.concat(res.data));
						}else{
							uni.showToast({
								title: res.msg,
							});
						}
					},
					rej => {}
				);
			},
			doSearch(){
				this.mescroll.resetUpScroll()
			},
			mescrollInit(mescroll) {
					this.mescroll = mescroll;
					// this.mescroll.resetUpScroll()
			},
			/*下拉刷新的回调 */
			downCallback(mescroll) {
				mescroll.resetUpScroll()
			},
			upCallback(page) {
				//联网加载数据
				var that = this;
				var data = {
					indicatorName:this.keyword,
					indicatorCateId:this.indicatorCateId,
					// indicatorId:this.showType,
					reportId:this.reportId ||'',
					pageNum: page.num,
					pageSize: page.size
				};
				getResult(data).then(res => {
					if (res.code == 200) {
						//设置列表数据
						if (page.num == 1) {
							that.dataList = res.data.list;
							this.num=res.data.total||0
			
						} 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();
					}
				});
			},
			listByReport(){
				listByReport(this.reportId).then(
					res => {
						if(res.code==200){
							  this.dataList=res.data;
							  this.num=res.data.length;
							  // this.form.examDate=this.form.examDate.slice(0,5)
							  
						}else{
							uni.showToast({
								title: res.msg,
							});
						}
					},
					rej => {}
				);
			},
			getReportById(){
				getReportById(this.reportId).then(
					res => {
						if(res.code==200){
							  this.form=res.data;
							  // this.form.examDate=this.form.examDate.slice(0,5)
							  
						}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.$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;
				// }
				if(this.type=="add"){
					this.addReport()
				}
				else if(this.type=="edit"){
					this.updateReport()
				}
				
			},
			updateReport(){
				updateReport(this.form).then(
					res => {
						if(res.code==200){
							 uni.showToast({
							 	icon:'success',
							 	title: "更新成功",
							 });
							 setTimeout(function() {
								 uni.$emit('refreshReportList');
								 uni.navigateBack({
									 delta: 1
								 })
							 }, 500);
						}else{
							uni.showToast({
								icon:'none',
								title: res.msg,
							});
						}
					},
					rej => {}
				);
			},
			addReport(){
				addReport(this.form).then(
					res => {
						if(res.code==200){
							 uni.showToast({
							 	icon:'success',
							 	title: "操作成功",
							 });
							 setTimeout(function() {
								uni.$emit('refreshReportList');
								 uni.navigateBack({
									 delta: 1
								 })
							 }, 500);
						}else{
							uni.showToast({
								icon:'none',
								title: res.msg,
							});
						}
					},
					rej => {}
				);
			},
			showDetail(item) {
			
					uni.navigateTo({
						url: '/pages_user/user/addResult?type=edit&resultId=' + item.resultId+'&reportId='+item.reportId
					})
			}
		}
	}
</script>

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

		.top-fixed {
			width: 100%;
			position: fixed;
			top: 0;
			left: 0;
			z-index: 999;
		}
        
		.date-box {
			background: #fff;
			padding: 30rpx;
			border-bottom: 1rpx solid #ECECEC;
			display: flex;
			align-items: center;
			justify-content: space-between;

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

			.time {
				font-weight: 400;
				font-size: 32rpx;
				color: #222426;
				line-height: 34rpx;
				text-align: right;
			}
			.input-box{
				text-align: right;
			}
		}
		.search-cont{
			// padding: 16upx 30upx;
			.inner2{
				box-sizing: border-box;
				width: 100%;
				height: 72upx;
				background: #fff;
				border-radius: 36upx;
				display: flex;
				align-items: center;
				padding: 0 30upx;
				.icon-search{
					width: 28upx;
					height: 28upx;
					margin-right: 20upx;
				}
				input{
					height: 60upx;
					line-height: 60upx;
					flex: 1;
				}
			}
		}
        .m-list{
			margin-top: 196upx;
		}
		.pub-tab-box {
			box-sizing: border-box;
			width: 100%;
			// padding: 0 40upx;
			background-color: #FFFFFF;

			.tab-inner {
				height: 88upx;
				line-height: 88upx;
				display: flex;
				align-items: center;
				// justify-content: space-between;
				// overflow-x: auto;
				position: relative;
			}

			.item {
				// flex: 1;
				 /* 添加过渡动画 */
				    transition: all 0.2s ease; /* 状态变化时平滑过渡 */
				font-size: 28upx;
				white-space: nowrap;
				line-height: 1;
				font-family: PingFang SC;
				font-weight: 500;
				color: #626468;
				// margin-right: 60upx;
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 0 30rpx;

				&:last-child {
					margin-right: 0;
				}

				&.active {
					font-weight: bold;
					color: #222426;

					&::after {
						content: "";
						width: 48rpx;
						height: 8rpx;
						background: linear-gradient(120deg, #31A1FE 0%, #008FD3 100%);
						position: absolute;
						bottom: 0;
						border-radius: 6upx 6upx 0upx 0;
					}
				}

				.text {
					position: relative;
					z-index: 1;
				}

				.tab-bg {
					width: 72upx;
					height: 28upx;
					position: absolute;
					top: 17upx;
					left: 50%;
					transform: translateX(-36upx);
					z-index: -1;
				}
			}
		}

		.btn-box2 {
			z-index: 9999;
			width: 100%;
			padding: 30upx;
			position: fixed;
			bottom: 0;
			left: 0;
			box-sizing: border-box;
			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: #008FD3;
				border-radius: 44upx;
				display: flex;
				align-items: center;
				justify-content: center;

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

		.form-box {
			padding: 0 30upx;
			background: #FFFFFF;
			border-radius: 16upx;
			margin-bottom: 20upx;

			.form-title {
				font-family: PingFang SC;
				font-weight: 600;
				font-size: 40rpx;
				color: #222426;
				text-align: left;
				padding: 30rpx 0;
				display: block;
			}

			.form-item {
				padding: 30upx 0;
				display: flex;
				align-items: flex-start;
				border-bottom: 1px solid #F1F1F1;

				&:last-child {
					border-bottom: none;
				}

				.label {
					width: 150upx;
					text-align: left;
					font-size: 32upx;
					line-height: 44upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #222222;
					flex-shrink: 0;
				}

				input {
					text-align: left;
				}

				.unit {
					width: 150upx;
					text-align: right;
					font-size: 32upx;
					line-height: 44upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #222222;
					flex-shrink: 0;
				}

				.form-input {
					font-size: 30upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #999999;
					text-align: left;
				}

				.form-item-tag {
					margin-right: 16rpx;

					.tag {
						border-radius: 8rpx 8rpx 8rpx 8rpx;
						border: 2rpx solid #ECECEC;
						display: flex;
						align-items: center;
						padding: 24rpx;
						font-family: PingFang SC;
						font-weight: 400;
						font-size: 24rpx;

						&.active {
							background: #F0FAFF;
							border: 2rpx solid #008FD3;
							color: #008FD3;
						}
					}



				}
			}
		}
       .index-body{
		   .title-box {
		   	display: flex;
		   	align-items: center;
		   	justify-content: space-between;
		   	padding: 30rpx 0;
		   
		   	.left-title {
		   		font-family: PingFang SC, PingFang SC;
		   		font-weight: 600;
		   		font-size: 32rpx;
		   		color: #222426;
		   		text-align: left;
		   	}
		   
		   	.right {
		   		width: 216rpx;
		   		height: 64rpx;
		   		font-weight: 400;
		   		font-size: 24rpx;
		   		color: #626468;
		   		justify-content: center;
		   		background: #FFFFFF;
		   		border-radius: 32rpx 32rpx 32rpx 32rpx;
		          text{
		   		   margin-left:5rpx ;
		   	   }
		   	}
		   }
	   }
		.sms-list {
			border-radius: 16rpx 16rpx 16rpx 16rpx;
            overflow: hidden;
			.item {
				padding: 0rpx 20rpx;
background: #FFFFFF;
border-bottom: 1rpx solid #ECECEC;
				.item-box {
					display: flex;
					align-items: flex-start;
					justify-content: space-between;
					
					
    height: 150rpx;
    padding: 30rpx 0;
					.left {
						flex: 1;
						display: flex;
						flex-direction: column;
						align-items: flex-start;
						justify-content: space-between;
						height: 100%;
						

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

						.unit {
							font-weight: 400;
							font-size: 28rpx;
							color: #898E91;
							text-align: left;
						}

						.num {
							font-family: PingFang SC, PingFang SC;
							font-weight: 600;
							font-size: 32rpx;
							color: #FF5030;
							text-align: left;
							margin-right: 16rpx;
						}

.tag2 {
							width: 58rpx;
							height: 30rpx;
							line-height: 30rpx;
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							font-size: 21rpx;
							color: #52D087;
							text-align: center;
							border-radius: 4rpx 4rpx 4rpx 4rpx;
							border: 1rpx solid #B0EAC9;
						}
						.tag {
							width: 58rpx;
							height: 30rpx;
							line-height: 30rpx;
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							font-size: 21rpx;
							color: #FF4B33;
							text-align: center;
							border-radius: 4rpx 4rpx 4rpx 4rpx;
							border: 1rpx solid #FFA599;
						}

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

				} 
				&:last-child .item-box{
								  border-bottom: 0;
								 }
			}

		}

	}
</style>