<template>
	<view class="patientInfoSure">
		<uni-nav-bar dark :fixed="true" :border="false" background-color="#002648" status-bar left-icon="left"
			left-text="返回" title="患者信息确认" @clickLeft="back">
			<block slot="right">
				<logout></logout>
			</block>
		</uni-nav-bar>
		<headInfo ref="headInfo"></headInfo>
		<view class="content">
			<view class="content-text">填写表单</view>
			<view class="buttons">
				<text @click="formButtonClick('生命体征录入')">生命体征录入</text>
				<text @click="formButtonClick('屈光手术安全核查表')">屈光手术安全核查表</text>
				<text @click="formButtonClick('屈光手术护理记录单')">屈光手术护理记录单</text>
			</view>
			<view class="notext" @click="back">暂不填写</view>
		</view>
		<!-- -4:等待签到，-3:等待验光/等待谈话，-2:等待验光，-1:等待谈话 -->
		<view class="buttons-check" @click="signHandle" v-if="patientInfo.status === -4" :disabled="isSignDisabled">
			签到
		</view>
		<!-- 核查 -->
		<!-- 0:等待呼叫，1:呼叫中，2:术前准备，3:手术中，4、手术完成 -->
		<view class="buttons-check" @click="sureStartHandle" v-if="patientInfo.status === 1 ||
		 patientInfo.status === 2 || patientInfo.status === 3 ">核查完成</view>
		<!-- <view class="finish" v-if="patientInfo.status >=4">手术已完成</view> -->
		<!-- <h5WebView></h5WebView> -->
	</view>
</template>

<script>
	import logout from '@/components/logout.vue'
	import headInfo from '@/components/headInfo.vue'
	import h5WebView from '@/components/h5WebView.vue'
	export default {
		components: {
			logout,
			headInfo,
			h5WebView
		},
		data() {
			return {
				scanCodeData: {},
				patientInfo: {},
				isSignDisabled: false,
			}
		},
		onLoad(options) {
			this.scanCodeData = options
			// 获取患者信息
			this.getPatientInfo()
		},
		mounted() {
			this.$refs.headInfo.getPatientInfo(this.scanCodeData)
		},
		methods: {
			// 获取患者信息
			async getPatientInfo() {
				// -4:等待签到，-3:等待验光/等待谈话，-2:等待验光，-1:等待谈话，0:等待呼叫，1:呼叫中，2:术前准备，3:手术中，4、手术完成
				const res = await this.$baseAPI.request(`${this.$portAdress.pda}/${this.scanCodeData.patientID}`)
				if (res.code === 0) {
					console.log('res.data.status',res.data.status);
					this.patientInfo = res.data
					res.data.status === -3 || res.data.status === -2 || res.data.status === -1 || res.data.status === 0 || res.data.status === 4 ? setTimeout(() => {
						uni.showToast({
							icon: 'none',
							title: res.data.status >= -3 && res.data.status < 0 ? '此患者还未验光或谈话' : (res
								.data.status === 0 ? '此患者还未叫号' :
								(res.data.status >= 4 ?
									'此患者手术已完成' : ''))
						})
					}, 500) : ''
				} else {
					uni.showToast({
						icon: 'none',
						title: res.msg
					})
				}
			},
			// 返回
			back() {
				uni.navigateTo({
					url: '/pages/patientList/index'
				})
			},
			// 表单填写入口
			formButtonClick(text) {
				if (text === '生命体征录入') {
					uni.navigateTo({
						url: `/pages/life/index?patientId=${this.patientInfo.patientId}&patientIdNumber=${this.patientInfo.patientIdNumber}&operaId=${this.patientInfo.operaId}`
					})
				} else if (text.includes('手术安全核查表')) {
					uni.navigateTo({
						url: `/pages/operaSafetyCheck/index?patientId=${this.patientInfo.patientId}&patientIdNumber=${this.patientInfo.patientIdNumber}&operaId=${this.patientInfo.operaId}&flag=all`
					})
				} else if (text.includes('手术护理记录单')) {
					uni.navigateTo({
						url: `/pages/operaNurseRecord/index?patientId=${this.patientInfo.patientId}&patientIdNumber=${this.patientInfo.patientIdNumber}&operaId=${this.patientInfo.operaId}`
					})
				}
			},
			// 签到 
			// -4:等待签到，-3:等待验光/等待谈话，-2:等待验光，-1:等待谈话
			async signHandle() {
				this.isSignDisabled = true
				let newStatus = this.patientInfo.status + 1
				const res = await this.$baseAPI.request(this.$portAdress.updataOperaStatus, {
					operaId: this.patientInfo.operaId,
					status: newStatus
				}, 'post')
				if (res.code === 0) {
					uni.showToast({
						icon: 'none',
						title: '签到成功'
					})
					if (newStatus === -3) {
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/patientList/index'
							})
						}, 200)
					}
				} else {
					this.isSignDisabled = false
					uni.showToast({
						icon: 'none',
						title: res.msg
					})
				}
			},
			// 核查 
			// 0:等待呼叫，1:呼叫中，2:术前准备，3:手术中，4、手术完成
			async sureStartHandle() {
				let newStatus = this.patientInfo.status + 1
				const res = await this.$baseAPI.request(this.$portAdress.updataOperaStatus, {
					operaId: this.patientInfo.operaId,
					status: newStatus
				}, 'post')
				if (res.code === 0) {
					// 1、第二次扫码：pda扫码确认患者信息----传2，跳转回到患者列表--状态变为术前准备
					// 即刻跳转到生命体征页面，填写生命体征，点击保存，跳转回到患者列表     
					if (newStatus === 2) {
						uni.navigateTo({
							url: `/pages/life/index?patientId=${this.patientInfo.patientId}&patientIdNumber=${this.patientInfo.patientIdNumber}&operaId=${this.patientInfo.operaId}`
						})
					}
					// 第三次扫码：pda扫码确认患者信息---点核查完成传3，状态变为“手术中”
					// 即刻跳转到屈光手术安全核查表“麻醉实施前”和手术开始前”两个表单合二为一的页面，填写完成后，点击保存，跳转回到患者列表
					console.log('-------------------');
					if (newStatus === 3) {
						uni.navigateTo({
							url: `/pages/operaSafetyCheck/index?patientId=${this.patientInfo.patientId}&patientIdNumber=${this.patientInfo.patientIdNumber}&operaId=${this.patientInfo.operaId}&flag=two`
						})
					}
					// 第四次扫码：pda扫码确认患者信息---点核查完成传4
					// 即刻跳转到安全核查表中的第三块“患者离开手术室”表单，填写完成后，点击保存，跳转回到患者列表
					if (newStatus === 4) {
						uni.navigateTo({
							url: `/pages/operaSafetyCheck/index?patientId=${this.patientInfo.patientId}&patientIdNumber=${this.patientInfo.patientIdNumber}&operaId=${this.patientInfo.operaId}&flag=one`
						})
					}
				} else {
					uni.showToast({
						icon: 'none',
						title: res.msg
					})
				}
			},
		},
	}
</script>

<style lang="less" scoped>
	.patientInfoSure {
		height: 100vh;
		overflow: hidden;

		.content {
			padding: 20rpx;
			background-color: #fff;

			.content-text {
				text-align: center;
				font-weight: 700;
				font-size: 40rpx;
			}

			.buttons {
				margin-top: 20rpx;

				text {
					border: 1px solid #ccc;
					display: block;
					padding: 20rpx 0;
					text-align: center;
					border-radius: 20rpx;
					width: 80vw;
					margin: 0 auto;
					margin-bottom: 16rpx;
				}
			}

			.notext {
				color: red;
				text-align: center;
				margin-top: 30rpx;
			}
		}

		.buttons-check {
			width: 90%;
			height: 88rpx;
			line-height: 88rpx;
			background-color: #1e79ff;
			color: #fff;
			text-align: center;
			position: fixed;
			bottom: 10rpx;
			left: 50%;
			border-radius: 20rpx;
			transform: translateX(-50%);
		}

		.finish,
		.start {
			position: fixed;
			bottom: 5px;
			left: 50%;
			width: 120px;
			height: 40px;
			line-height: 40px;
			text-align: center;
			color: #fff;
			border-radius: 10rpx;
			transform: translateX(-50%);
		}

		.finish {
			background-color: red;
		}

		.start {
			background-color: #1e79ff;
		}
	}
</style>