<template>
	<view class="operaBackCheck" :style="{background:title !== '屈光手术安全核查表' ? '#002648' : '#fff'}">
		<template v-if="title !== '屈光手术安全核查表'">
			<uni-nav-bar dark :fixed="true" :border="false" background-color="#002648" status-bar left-icon="left"
				title="患者离开手术室前" @clickLeft="back">
			</uni-nav-bar>
		</template>
		<view class="operaBackCheck-content">
			<view class="checkComplete" v-if="title !== '屈光手术安全核查表'">
				<icon type="success" size="40" />
				<view class="checkComplete-text-one">核查完成</view>
				<view class="checkComplete-text-two">核查完成后请填写相应表单</view>
			</view>
			<view class="patientinfo-father">
				<view class="patientInfo flex-1">
					<text class="patient-text">
						<text>PID：</text>
						{{formListValue.patientId ? formListValue.patientId : '-'}}
					</text>
					<text class="patient-text">
						<text>姓名：</text>
						{{formListValue.patientName ? formListValue.patientName : '-'}}
					</text>
					<text class="patient-text">
						<text>性别：</text>
						{{formListValue.patientSex ? formListValue.patientSex : '-'}}
					</text>
				</view>
				<view class="patientInfo flex-2 margin-top-10 ">
					<text class="font-w">手术方式：</text>
					<view>
						<view v-show="formListValue.nsOuOperaBieM">OU：{{formListValue.nsOuOperaBieM}}</view>
						<view v-show="formListValue.nsOdOperaBieM">OD：{{formListValue.nsOdOperaBieM}}</view>
						<view v-show="formListValue.nsOsOperaBieM">OS：{{formListValue.nsOsOperaBieM}}</view>
					</view>
				</view>
			</view>
			<view class="operaBackCheck-form">
				<view class="form-nameAgeSure form">
					<text>姓名/性别/年龄/PID等正确:</text>
					<radio-group @change="radioChange('backXm',$event)" class="radio-group">
						<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in radioList" :key="index">
							<view>
								<radio :value="item.name" :checked="item.name === formListValue.backXm"
									color="#1989FA" style="transform:scale(0.8)" />
							</view>
							<view>{{item.name}}</view>
						</label>
					</radio-group>
				</view>
				<view class="form-surgicalMethod form">
					<text>实际手术方式确认:</text>
					<radio-group @change="radioChange('backSsfs',$event)" class="radio-group">
						<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in radioList" :key="index">
							<view>
								<radio :value="item.name" :checked="item.name === formListValue.backSsfs"
									color="#1989FA" style="transform:scale(0.8)" />
							</view>
							<view>{{item.name}}</view>
						</label>
					</radio-group>
				</view>
				<view class="form-medicationCheck form">
					<text>术中用药核查:</text>
					<radio-group @change="radioChange('backYyhc',$event)" class="radio-group">
						<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in radioList" :key="index">
							<view>
								<radio :value="item.name" :checked="item.name === formListValue.backYyhc"
									color="#1989FA" style="transform:scale(0.8)" />
							</view>
							<view>{{item.name}}</view>
						</label>
					</radio-group>
				</view>
				<view class="form-inventorySure form">
					<text>手术用物清点正确:</text>
					<radio-group @change="radioChange('backSsywqd',$event)" class="radio-group">
						<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in radioList" :key="index">
							<view>
								<radio :value="item.name" :checked="item.name === formListValue.backSsywqd"
									color="#1989FA" style="transform:scale(0.8)" />
							</view>
							<view>{{item.name}}</view>
						</label>
					</radio-group>
				</view>
				<view class="form-skinComplete form">
					<text>皮肤是否完整:</text>
					<radio-group @change="radioChange('backPfsfwz',$event)" class="radio-group">
						<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in radioList" :key="index">
							<view>
								<radio :value="item.name" :checked="item.name === formListValue.backPfsfwz"
									color="#1989FA" style="transform:scale(0.8)" />
							</view>
							<view>{{item.name}}</view>
						</label>
					</radio-group>
				</view>
				<view class="form-Zlbdj form">
					<text>治疗绷带镜确认:</text>
					<radio-group @change="radioChange('backZlbdj',$event)" class="radio-group">
						<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in radioList" :key="index">
							<view>
								<radio :value="item.name" :checked="item.name === formListValue.backZlbdj"
									color="#1989FA" style="transform:scale(0.8)" />
							</view>
							<view>{{item.name}}</view>
						</label>
					</radio-group>
				</view>
				<view class="form-other-father">
					<view class="form-other form">
						<text>其它</text>
						<radio-group @change="radioChange('backQtOrder',$event)" class="radio-group">
							<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in radioList"
								:key="index">
								<view>
									<radio :value="item.name" :checked="item.name === formListValue.backQtOrder"
										color="#1989FA" style="transform:scale(0.8)" />
								</view>
								<view>{{item.name}}</view>
							</label>
						</radio-group>
					</view>
					<input v-if="formListValue.backQtOrder==='是'" type="text" :value="formListValue.backQtInput" placeholder="请输内容"
						@input="inputHandle('backQtInput',$event)" />
				</view>
				<view class="form-other-father">
					<view class="form-other form">
						<text>患者去向:</text>
						<radio-group @change="radioChange('backHzqxOrder',$event)" class="radio-group">
							<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in patientWhereList"
								:key="index">
								<view>
									<radio :value="item.name" :checked="item.name === formListValue.backHzqxOrder"
										color="#1989FA" style="transform:scale(0.8)" />
								</view>
								<view>{{item.name}}</view>
							</label>
						</radio-group>
					</view>
					<input v-if="formListValue.backHzqxOrder==='其它'" type="text" :value="formListValue.backHzqxInput" placeholder="请输入内容"
						@input="inputHandle('backHzqxInput',$event)" />
				</view>
			</view>
			<view class="content-sign">
				<view class="operaDoctorSign operaSign">
					<text>手术医生签字:</text>
					<view class="sign" :style="{ background: formListValue.backYsSign ? '#fff' : '#E8F5FF'}">
						<text class="sign-text" v-if="!formListValue.backYsSign" @click="getYsSign">获取签字</text>
						<view class="image-father" v-if="formListValue.backYsSign">
							<icon type="clear" size="14" class="image-clear"
								@click="imageClearClick('backYsSign')" />
							<image v-if="formListValue.backYsSign" mode="widthFix" :src="formListValue.backYsSign"
								alt="" class="img-class">
						</view>
					</view>
				</view>
				<view class="zhuShouSign operaSign">
					<text>技师签字:</text>
					<view class="sign" :style="{ background: formListValue.backJsSign ? '#fff' : '#E8F5FF'}">
						<text class="sign-text" v-if="!formListValue.backJsSign" @click="geLoginSign('backJsSign')">获取签字</text>
						<view class="image-father" v-if="formListValue.backJsSign">
							<icon type="clear" size="14" class="image-clear"
								@click="imageClearClick('backJsSign')" />
							<image v-if="formListValue.backJsSign" mode="widthFix" :src="formListValue.backJsSign"
								alt="" class="img-class">
						</view>
					</view>
				</view>
				<view class="nurseSign operaSign">
					<text>手术室护士签字:</text>
					<view class="sign" :style="{ background: formListValue.backHsSign ? '#fff' : '#E8F5FF'}">
						<text class="sign-text" v-if="!formListValue.backHsSign" @click="geLoginSign('backHsSign')">获取签字</text>
						<view class="image-father" v-if="formListValue.backHsSign">
							<icon type="clear" size="14" class="image-clear"
								@click="imageClearClick('backHsSign')" />
							<image v-if="formListValue.backHsSign" mode="widthFix" :src="formListValue.backHsSign"
								alt="" class="img-class">
						</view>
					</view>
				</view>
			</view>
			<view class="form-time form">
				<text>时间:</text>
				<text style="color: #1c76fd;">{{formListValue.backTime ? formListValue.backTime : '空'}}</text>
			</view>
		</view>
		<view class="button-father" v-if="title !== '屈光手术安全核查表'">
			<view class="buttons buttonsOne" @click="jumpHandle">跳过</view>
			<view class="buttons buttonsTwo" @click="sureHandle">保存</view>
		</view>

	</view>
</template>

<script>
	import Signature from '@/components/sin-signature/sin-signature.vue'
	import {
		formatDate
	} from '@/utils/util.js'
	export default {
		data() {
			return {
				formListValue: {
					formName:'屈光手术安全核查表',
					patientName:'',
					patientId:'',
					patientSex:'',
					patientAge:'',
					// 拟施手术名称
					nsOuOperaName: '',
					nsOdOperaName: '',
					nsOsOperaName: '',
					// 拟施手术别名
					nsOuOperaBieM: '',
					nsOdOperaBieM: '',
					nsOsOperaBieM: '',
					// 确认患者姓名
					backXm: '是',
					// 手术方式
					backSsfs: '是',
					// 用药核查
					backYyhc: '是',
					// 手术用物清点正确
					backSsywqd: '是',
					// 皮肤是否完整
					backPfsfwz: '是',
					// 治疗绷带镜确认
					backZlbdj: '是',
					// 其它order
					backQtOrder: '否',
					// 其它输入
					backQtInput: '',
					// 患者去向
					backHzqxOrder: '离院',
					backHzqxInput:'',
					// 医生签字
					backYsSign: '',
					// 技师签字
					backJsSign: '',
					// 护士签字
					backHsSign: '',
					// 时间
					backTime: ''
				},
				radioList: [{
					name: '是',
				}, {
					name: '否',
				}],
				patientWhereList: [{
					name: '离院'
				}, {
					name: '日间病房'
				}, {
					name: '其它'
				}],
				optionList: {}
			}
		},
		props: {
			title: {
				type: String,
				default: ''
			}
		},
		components: {
			Signature
		},
		onLoad(options) {
			console.log('operaBackCheck', options);
			this.optionList = options
			// 获取表单保存信息
			this.getInfo()
		},
		methods: {
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
			init(options) {
				console.log('init-operaBefore', options);
				this.optionList = options
				// 获取表单保存信息
				this.getInfo()
			},
			// 单选框改变时
			radioChange(text, val) {
				this.formListValue[text] = val.target.value
			},
			// 输入框改变时
			inputHandle(text, val) {
				this.formListValue[text] = val.target.value
			},
			// 跳过
			jumpHandle() {
				uni.navigateTo({
					url: '/pages/patientList/index'
				})
			},
			// 获取表单信息
			async getInfo() {
				const res = await this.$baseAPI.request(this.$portAdress.getSafetyCheckBackInfo, {
					operaId: this.optionList.operaId,
					patientIdNumber: this.optionList.patientIdNumber,
				})
				if (res.code === 0) {
					Object.keys(res.data).forEach((item) => {
						// console.log(res.data)
						// 如果data中没有定义这个字段，就自动加进去
						this.formListValue[item] || typeof this.formListValue[item] =='boolean'? '' : this.formListValue[item] = ''
						// 如果不为空就赋值上去
						if ((res.data[item] && res.data[item] !== 'false' && res.data[item] !== 'true') ||
							typeof res.data[item] === 'number') {
							this.formListValue[item] = res.data[item]
						}
						if (res.data[item] && typeof res.data[item] !== 'number') {
							res.data[item].includes('[') || res.data[item] === 'false' || res.data[item] ===
								'true' ? this.formListValue[item] = JSON.parse(res.data[item]) : ''
						}
					})
				} else {
					uni.showToast({
						icon: 'none',
						title: res.msg
					})
				}
				setTimeout(() => {
					// 获取手术状态时间
					this.getOperaStatusTime()
				}, 500)
			},
			// 获取手术状态时间
			async getOperaStatusTime() {
				const res = await this.$baseAPI.request(this.$portAdress.getOperaStatusTime, {
					operaId: this.optionList.operaId,
				})
				if (res.code === 0) {
					res.data.forEach(item => {
						console.log('时间', item);
						if (item.status === 4) {
							this.formListValue.backTime = item.signDate
						}
					})
				} else {
					uni.showToast({
						icon: 'none',
						title: res.msg
					})
				}
			},
			// 获取主刀医生签字
			async getYsSign() {
				const res = await this.$baseAPI.request(this.$portAdress.getMainDoctorSign, {
					operaPatientId: this.optionList.operaId
				})
				if (res.code === 0) {
					this.formListValue.backYsSign = res.data.signImgBase
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
			// 获取登录签名
			geLoginSign(text) {
				let userInfo = uni.getStorageSync('userInfo') ? JSON.parse(uni.getStorageSync('userInfo')):''
				console.log(userInfo);
				this.formListValue[text] = userInfo.signImgBase
			},
			// 删除登录签名
			imageClearClick(textOne) {
				this.formListValue[textOne] = ''
			},
			// 保存
			async sureHandle() {
				const res = await this.$baseAPI.request(this.$portAdress.pda, {
					checkContent: JSON.stringify(this.formListValue),
					checkName: '屈光手术安全核查表',
					checkType: '患者离开手术室前',
					operaId: this.optionList.operaId,
					source:'single' // 标记是单表保存到数据还是整表保存的数据
				}, 'post')
				if (res.code === 0) {
					uni.redirectTo({
						url: '/pages/patientList/index'
					})
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			}
		}
	}
</script>

<style lang="less" scoped>
	.operaBackCheck {
		background-color: #002648;
		width: 100vw;
		.patientinfo-father {
			background-color: #e8f5ff;
			padding: 10px;
		}
		.patientInfo {
			.patient-text {
				padding-right: 20rpx;
		
				text {
					font-weight: 700;
				}
			}
		}
		.operaBackCheck-content {
			background-color: #fff;
			border-top-left-radius: 40rpx;
			border-top-right-radius: 40rpx;
			padding: 0 30rpx;
		}

		.checkComplete {
			text-align: center;
			padding: 50rpx 0;

			.checkComplete-text-one {
				font-weight: 700;
				margin-top: 20rpx;
				font-size: 36rpx;
			}

			.checkComplete-text-two {
				padding: 20rpx 0;
				color: #999999;
				font-size: 34rpx;
			}
		}

		.form {
			padding: 20rpx 0;
			background-color: #fff;
			border-bottom: 1rpx solid #F2F2F2;
			display: flex;
			justify-content: space-between;

			.uni-list-cell {
				display: flex;
				margin-left: 32rpx;
			}

			.radio-group {
				display: flex;
				flex-wrap: wrap;
			}
		}

		.form-other-father {
			border-bottom: 1rpx solid #F2F2F2;
		}

		.form-other {
			margin-bottom: 20rpx;
			border-bottom: none;
		}

		.content-sign {
			background-color: #fff;
			.img-class {
				width: 80px;
			}
			.operaSign {
				padding: 20rpx 0;
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-bottom: 1rpx solid #F2F2F2;
			}

			.sign {
				color: #aeb8bf;
				font-size: 48rpx;
				width: 240rpx;
				height: 96rpx;
				line-height: 96rpx;
				text-align: center;
				border: 1px dashed #6EB1FF;
				border-radius: 12px;
				position: relative;
			}
			.image-father {
				.image-clear {
					position: absolute;
					right: 4px;
					top: -2px;
				}
			}
			.sign-text,
			.signature {
				position: absolute;
				left: 0;
				bottom: 0;
				width: 240rpx;
				height: 96rpx;
				line-height: 96rpx;
				text-align: center;
			}
		}

		.button-father {
			background-color: #fff;
			display: flex;
			padding: 50rpx 0;
		}

		.buttonsOne {
			background-color: #fff;
			color: #4f4f4f;
			border: 1px solid #e7e7e7;
		}

		.buttonsTwo {
			background-color: #1c76fd;
			border: 1px solid #1c76fd;
			color: #fff;
		}

		.buttons {
			width: 45%;
			height: 88rpx;
			line-height: 88rpx;
			text-align: center;
			margin: 0 auto;
			border-radius: 12rpx;
		}
	}
</style>
<style lang="less">
	.operaBackCheck {
		.signature-wrap .img-wrap {
			width: 240rpx !important;
			height: 96rpx !important;
			line-height: 96rpx !important;
		}

		.signature-wrap .img-wrap,
		.signature-wrap .img-wrap image {
			width: 240rpx;
			height: 96rpx;
			line-height: 96rpx;
		}
	}
</style>
