<template>
	<view>
		<!-- left-icon="scan" -->
		<uni-nav-bar class="uniNavBar" dark :fixed="true" :border="false" background-color="#002648" status-bar
			title="患者列表" @clickLeft="scanClick">
			<block slot="right">
				<logout></logout>
			</block>
		</uni-nav-bar>
		<view class="patientList">
			<view class="search">
				<input :value="params.searchValue" class="uni-input" placeholder="请输入PID" @input="searchInput" />
			</view>
			<scroll-view scroll-y="true" @scrolltolower="handleTolower" class="list">
				<view class="head">
					<view class="PID-head">PID</view>
					<view class="name">姓名</view>
					<view class="doctor-name" @click="handleOrder('SU.REAL_NAME')">
						<text>主刀医生</text>
						<view class="doctor-icon">
							<uni-icons type="top" size="10"></uni-icons>
							<uni-icons type="bottom" size="10"></uni-icons>
						</view>
					</view>
					<view class="shoushu-name" @click="handleOrder('EO.OPERA_NAME')">
						<text>手术名称</text>
						<view class="shoushu-icon">
							<uni-icons type="top" size="10"></uni-icons>
							<uni-icons type="bottom" size="10"></uni-icons>
						</view>
					</view>
				</view>
				<template v-if="patientList.length>0">
					<view class="li" v-for="(item,index) in patientList" :key="index" @click="patientItemClick(item)">
						<view class="PID-li">{{item.patientId}}</view>
						<!-- {{item.patientName}} -->
						 <!---4:等待签到，-3:等待验光/等待谈话，-2:等待验光，-1:等待谈话，0:等待呼叫，1:呼叫中，2:术前准备，3:手术中，4、手术完成 -->
						<view class="PID-li">{{item.patientName.length >3 ? item.patientName.substr(0,3)+'...' : item.patientName}}: {{item.status}}</view>
						<view class="PID-li">{{item.mainDoctorName ? item.mainDoctorName : '-'}}</view>
						<view class="PID-li">{{item.operaNameList ? 
						(item.operaNameList.length >23 ? item.operaNameList.substr(0,23)+'...' : item.operaNameList): '-'}}</view>
					</view>
				</template>
			</scroll-view>
		</view>
		<scan></scan>
		<scan1></scan1>
	</view>
</template>

<script>
	import logout from '@/components/logout.vue'
	import scan from '@/components/scan.vue';
	import scan1 from '@/components/scan1.vue';
	export default {
		data() {
			return {
				params: {
					searchValue: '',
					page: 1, // 当前页码
					limit: 10, // 每页数
					total: 0, // 总条数
				},
				// 是否还有下一页
				hasMore: true,
				patientList: [],
				searchTimeFlag: null,
				patientID: '',
				patientlistTIme:null,
				orderField:'',
				order:'',
			}
		},
		components: {
			scan,
			scan1,
			logout,
		},
		onShow() {
			let that = this
			uni.$off('scan') // 每次进来先 移除全局自定义事件监听器
			uni.$on('scan', (data) => {
				//扫码成功后的回调，你可以写自己的逻辑代码在这里
				this.patientID = data.code
				// 二维码内容:患者id
				console.log('扫码 结果：', data.code);
				data.code ? this.getPatientInfo() : ''
			})
			
			uni.$off('scan1') // 每次进来先 移除全局自定义事件监听器
			uni.$on('scan1', (data) => {
				//扫码成功后的回调，你可以写自己的逻辑代码在这里
				this.patientID = data.code
				// 二维码内容:operaId手术id
				console.log('扫码 结果：', data.code);
				data.code ? this.getPatientInfo() : ''
			})
		},
		onLoad() {
			this.getPatientList()
			this.patientlistTIme = setInterval(()=>{
				this.getPatientList()
			},3000)
		},
		onUnload() {
			var that = this;
			clearInterval(that.patientlistTIme);
			console.log('onHide')
			that.patientlistTIme = null;
		},
		onHide() {
			var that = this;
			clearInterval(that.patientlistTIme);
			console.log('onHide')
			that.patientlistTIme = null;
		},
		onPullDownRefresh() {
			console.log('上拉刷新');
			this.getPatientList()
			uni.stopPullDownRefresh() // 停止下拉刷新
		},
		onReachBottom() {
			console.log('页面触底了哦！')
		},
		methods: {
			async getPatientInfo() {
				console.log('this.patientID',this.patientID);
				// 改为：-4:等待签到，-3:等待验光/等待谈话，-2:等待验光，-1:等待谈话，0:等待呼叫，1:呼叫中，2:术前准备，3:等待手术，4:手术中，5、手术完成
				const res = await this.$baseAPI.request(`${this.$portAdress.pda}/${this.patientID}`)
				console.log(res);
				if (res.code === 0) {
					uni.navigateTo({
						url: `/pages/patientInfoSure/index?patientID=${res.data.patientId}`
					})
					clearInterval(this.patientlistTIme)
					// if(res.data.status==0) {
					// 	uni.showToast({
					// 		icon: 'none',
					// 		title: '此患者还未呼叫'
					// 	})
					// } else if(res.data.status==2) {
					// 	uni.showToast({
					// 		icon: 'none',
					// 		title: '请先进行生命体征录入'
					// 	})
					// } else if(res.data.status==5) {
					// 	uni.showToast({
					// 		icon: 'none',
					// 		title: '此患者手术已结束'
					// 	})
					// } else if(res.data.status==1 || res.data.status==3 || res.data.status===4){

					// }
				} else {
					uni.showToast({
						icon: 'none',
						title: res.msg
					})
				}
			},
			scanClick() {
				uni.scanCode({
					onlyFromCamera: true,
					success: (res) => {
						console.log('扫码成功', res)
						// uni.showLoading({
						// 	title: '获取患者信息中'
						// })
					}
				});
			},
			// 点击当前患者跳转
			patientItemClick(item) {
				console.log('点击当前患者跳转',item.patientId);
				uni.navigateTo({
					url: `/pages/patientInfoSure/index?patientID=${item.patientId}`
				})
				clearInterval(this.patientlistTIme)
			},
			// 点击标题排序
			handleOrder(orderTitle) {
				this.orderField = orderTitle
				this.order = this.order == 'asc' ? 'desc' : 'asc'
				this.getPatientList()
			},
			// 获取患者列表
			async getPatientList() {
				console.log(123);
				const res = await this.$baseAPI.request(`${this.$portAdress.todayOperaList}`, {
					patientId: this.params.searchValue,
					patientName: '',
					orderField:this.orderField,  // 主刀排序: SU.REAL_NAME | 手术名称排序: EO.OPERA_NAME
					order:this.order, // 排序类型: desc 倒叙 | asc 顺序
				})
				console.log(res);
				if (res.code === 0) {
					this.patientList = res.data
					console.log(this.patientList);
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
			// 搜索
			searchInput(e) {
				console.log(e);
				this.params.searchValue = e.target.value
				// 设置阈值
				clearTimeout(this.searchTimeFlag)
				this.searchTimeFlag = setTimeout(() => {
					this.getPatientList()
				}, 1000)

			},
			handleTolower() {
				console.log('scroll');
				// if (this.hasMore) {
				// 	this.params.page++
				// } else {
				// 	// 弹框提示用户
				// 	console.log(没有数据了);
				// }
			},
		}
	}
</script>

<style lang="less" scoped>
	.patientList {
		.search {
			background: #002648;
			padding: 20rpx 0;
			position: fixed;
			width: 100vw;
			top: 128rpx;
			left: 0;
			z-index: 999;

			.uni-input {
				background-color: #fff;
				height: 60rpx;
				line-height: 60rpx;
				width: 90%;
				border-radius: 10rpx;
				padding: 8rpx 8rpx 8rpx 20rpx;
				margin: 0 auto;
			}
		}

		.list {
			margin-top: 200rpx;
			height: calc(100vh - 200rpx);
		}

		.head,
		.li {
			display: flex;
			justify-content: space-between;
			height: 90rpx;
			line-height: 90rpx;
		}

		.head {
			position: fixed;
			z-index: 999;
			width: 100vw;
			top: 238rpx;
			left: 0;
			background: linear-gradient(180deg, #EEF8FF 0%, #CDEDFF 100%);
			
			view {
				width: 22%;
				text-align: center;
				color: #002648;
				font-weight: 700;
			}
			view:nth-child(3) {
				width: 27%;
				// text-align: left;
			}
			view:nth-child(4) {
				width: 34%;
				// text-align: left;
			}
			.doctor-name,.shoushu-name {
				display: flex;
				align-items: center;
				justify-content: center;
				height: 90rpx;
				line-height: 90rpx;
				
			}
			.doctor-icon,.shoushu-icon {
				display: flex;
				flex-direction: column;
				justify-items: center;
				align-items: center;
				height: 90rpx;
				line-height: 40rpx;
				uni-icons {
					// height: 18rpx;
					// line-height: 18rpx;
				}
			}
		}

		.li {
			view {
				width: 22%;
				text-align: center;
			}
			view:nth-child(3) {
				width: 23%;
			}
			view:nth-child(4) {
				width: 34%;
				text-align: center;
				font-size: 25rpx;
				line-height: 40rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}

		.li:nth-child(odd) {
			background-color: #ddf2ff;
		}
	}
</style>
