<template>
	<view class="conte">
		<view class="header">
			<view class="tabs" :class="tabsActive==index?'activeClass':''" 
				v-for="(item, index) in tabs" @click="tabsClick(index)">
				{{item.name}}
			</view>
		</view>
		<view class="height-list"></view>
		<view v-if="tabsActive==0 && infoData" class="info">
			<view class="info-list no-center"><view class="info-title">申请单号：</view>{{infoData.applyCode}}</view>
			<view class="info-list"><view class="info-title">状态：</view>{{infoData.status.name}}</view>
			<view class="info-list"><view class="info-title">科室名称：</view>{{infoData.patientDTO.department.dept}}</view>
			<view class="info-list no-center"><view class="info-title">患者姓名：</view>{{infoData.patientName}}({{infoData.patientDTO.bedNum}}床)</view>
			<view class="info-list no-center"><view class="info-title">住院号：</view>{{infoData.patientDTO.patientCode}}</view>
			<view class="info-list no-center"><view class="info-title">年龄：</view>{{infoData.patientDTO.age}}</view>
			<view class="info-list"><view class="info-title">性别：</view>{{infoData.patientDTO.gender && infoData.patientDTO.gender.name || '-'}}</view>
			<view class="info-list no-center">
				<view class="info-title">检查项目：</view>
				<view class="info-content">
					{{examineName.join('、')}}
				</view>
			</view>
			<view class="info-list"><view class="info-title">标本类型：</view>{{infoData.specimenType.name}}</view>
			<view class="info-list no-center"><view class="info-title">标本数量：</view>{{infoData.specimenNum}}</view>
			<view class="info-list no-center">
				<view class="info-title">部位：</view>
				<view class="info-content">
					{{infoData.takePart}}
				</view>
			</view>
			<view class="info-list"><view class="info-title">送检医生：</view>{{infoData.surgeryDoctorDTO.name}}</view>
			<view class="info-list no-center"><view class="info-title">离体时间：</view>{{yyTimeFilter(infoData.inVitroTime)}}</view>
			<view class="info-list no-center"><view class="info-title">固定时间：</view>{{yyTimeFilter(infoData.fixationTime)}}</view>
			<view class="info-list no-center">
				<view class="info-title">诊断：</view>
				<view class="info-content">
					{{infoData.diagnose}}
				</view>
			</view>
			<view class="info-list no-center">
				<view class="info-title">病历摘要：</view>
				<view class="info-content">
					{{infoData.medicalRecords}}
				</view>
			</view>
			<view class="info-list no-center">
				<view class="info-title">手术方案：</view>
				<view class="info-content">
					{{infoData.surgicalPlan}}
				</view>
			</view>
		</view>
		<view v-if="tabsActive==1" class="list-body">
			<view class="list" v-for="(item, index) in list" :key="index">
				<view class="df-list">
					<view class="list-title">标本名称：</view>{{item.specimenName}}
				</view>
				<view class="df-list">
					<view class="list-title">条码号：</view>{{item.specimenCode}}
				</view>
				<view class="list-content"></view>
			</view>
		</view>
		<view v-if="tabsActive==2 && stepsList.length" class="info-log">
			<uni-section type="line" padding>
				<uni-steps :options="stepsList" active-color="#49B856" :active="stepsActive" direction="column" />
			</uni-section>
		</view>
		<view class="btn-view">
			<button class="back-btn" @click="back">返回</button>
		</view>
		<!-- PDA扫描 -->
		<scanner></scanner>
	</view>
</template>

<script>
	import {
		get,
		post
	} from "../../http/http.js";
	import scanner from "../../components/scanner/scanner.vue";
	export default {
		data() {
			return {
				tabs: [
					{name:'申请单'},
					{name:'标本信息'},
					{name:'历史记录'},
				],
				tabsActive:0,
				infoData:null,
				list:[],
				idx: 0,
				userInfo:uni.getStorageSync('userData').user,
				hosId: uni.getStorageSync('userData').user.currentHospital.id,
				stepsList: [],
				stepsActive:0,
				SMFlag:true,
			}
		},
		components: {
			scanner
		},
		methods: {
			back(){
				uni.navigateTo({
				  url: `/pages/specimenPort/specimenPort?backType=detail`
				});
			},
			// 时间格式化
			yyTimeFilter(timestamp) {
			  var date = new Date(timestamp); // 时间戳为毫秒级别
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				var day = date.getDate();
				var hours = date.getHours();
				var minutes = date.getMinutes();
				var seconds = date.getSeconds();
			 
				// 格式化月份、日期、小时、分钟、秒
				month = month < 10 ? '0' + month : month;
				day = day < 10 ? '0' + day : day;
				hours = hours < 10 ? '0' + hours : hours;
				minutes = minutes < 10 ? '0' + minutes : minutes;
				seconds = seconds < 10 ? '0' + seconds : seconds;
			 
				return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
			},
			// 切换tabs
			tabsClick(index) {
				this.idx = 0;
				this.tabsActive = index;
			},
			// 详情数据
			getDetail(detailId) {
				uni.showLoading({
					title: "加载中",
					mask: true,
				});
				get(`/data/fetchData/pathologyForm/${detailId}`).then(res => {
					uni.hideLoading();
					this.infoData = res.data
					this.examineName = res.data.pathologyInspectDTOS.map(i=>i.inspectProject.name)
					this.stepsList = res.data.formLogDTOS.map(i=>{
						if(i.handoverUserDto){
							return{
								title:i.operationType.name+' '+ i.handoverUserDto.name,
								desc:this.yyTimeFilter(i.createTime) +' '+ i.operationUserDto.name
							}
						}else{
							return{
								title:i.operationType.name,
								desc:this.yyTimeFilter(i.createTime) +' '+ i.operationUserDto.name
							}
						}
					})
					this.stepsActive = this.stepsList.length-1
					this.list = res.data.pathologySpecimenDTOList
				});
			},
			// PDA扫描
			padChange(scannerCode){
				scannerCode = scannerCode.replace('\n','')
				if (!this.SMFlag) {
				  return;
				}
				this.SMFlag = false;
				uni.showLoading({
					title: "加载中",
					mask: true,
				});
				post("/pathology/scanCode", {
					barcode:scannerCode,
					platform:"app"
				}).then((res) => {
					this.SMFlag = true
					uni.hideLoading();
					if(res.status==200){
						let data = res.data[0]
						let item = null
						let str = null
						if(scannerCode.indexOf(':')!=-1){
							str = scannerCode.split(':')
							item = data.pathologySpecimenDTOList.find(i=>i.specimenCode == str[1])
						}else{
							item = data.pathologySpecimenDTOList.find(i=>i.specimenCode == scannerCode)
						}
						// let item = data.pathologySpecimenDTOList.find(i=>i.specimenCode == scannerCode)
						if(!item.fixationTime && data.pathologyFormType==0){
							if(data.status.value==2 || data.status.value==3 ||
							data.status.value==4){
								uni.navigateTo({
								  url: `/pages/specimenPort/scan?data=${JSON.stringify(data)}&type=${res.type}`
								});
							}else{
								uni.showToast({
									title: '查询到标本申请单信息',
									duration: 1000,
									icon:'none'
								});
								setTimeout(_=>{
									uni.navigateTo({
									  url: `/pages/specimenPort/detail?detailId=${data.id}`
									});
								},800)
							}
						}else if(item.fixationTime && data.pathologyFormType==0){
							uni.showToast({
								title: '查询到标本申请单信息',
								duration: 1000,
								icon:'none'
							});
							setTimeout(_=>{
								uni.navigateTo({
									url: `/pages/specimenPort/detail?detailId=${data.id}`
								});
							},800)
						}else{
							uni.showToast({
								title: '该申请单类型不是病理检查申请单',
								duration: 1000,
								icon:'none'
							});
						}
					}else{
						uni.showModal({
						  title: "提示",
						  content: `${res.msg} \n 扫描内容：${scannerCode}`,
							confirmColor:'#49b856',
						  success: (res) => {
						    if (res.confirm) {
						      console.log("用户点击确定");
						    } else if (res.cancel) {
						      console.log("用户点击取消");
						    }
						  },
						});
					}
				})
			},
		},
		onBackPress(e) {
			// backbutton：物理按键返回
			if (e.from === 'backbutton') {
				this.back()
				return true 
			}
		},
		onShow() {
			let that = this
			// #ifdef APP-PLUS
			uni.$off('scan') // 每次进来先 移除全局自定义事件监听器
			uni.$on('scan', function(data) {
				that.padChange(data)
			})
			// #endif
		},
		onLoad(options) {
			this.getDetail(options.detailId)
		}
	}
</script>

<style lang="less" scoped>
	page {
		background: #F7F7F7;
	}

	.conte {
		.header {
			display: flex;
			position: fixed;
			width: 100%;
			background: #fff;
			z-index: 999;
			.tabs{
				height: 80rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				border-bottom: 1px solid #fff;
				flex: 1;
			}
		}
		.activeClass{
			border-bottom: 1px solid #49B856 !important;
			color: #49B856;
		}
		.info{
			padding: 0 20rpx;
			background: #fff;
			height: calc(100vh - 180rpx);
			overflow-y: auto;
			.info-list{
				padding: 14rpx 0;
				display: flex;
				align-items: center;
				font-size: 28rpx;
				color: #555555;
				.info-title{
					color: #000;
				}
				.info-content{
					flex: 1;
				}
			}
			.no-center{
				align-items: baseline;
			}
		}
		.height-list {
			height: 44px;
		}
		.list-body{
			height: calc(100vh - 180rpx);
			overflow-y: auto;
			.list {
				// margin-top: 20rpx;
				background: #fff;
				.list-content{
					height: 20rpx;
					background: #F7F7F7;
				}
				.df-list {
					display: flex;
					padding: 14rpx 20rpx;
					font-size: 28rpx;
					color: #555555;
					.list-title{
						color: #000;
					}
				}
			}
		}
		.info-log{
			background: #fff;
			height: calc(100vh - 80rpx);
		}
		.df {
			display: flex;
			height: 80rpx;
			align-items: center;
			padding: 0 20rpx;
			border-bottom: 1px solid #D2D2D2;
		}
		.execFilterMask{
		  .execFilter{
		    position: relative;
		    background-color: #fff;
		    height: calc(100vh - 80rpx);
		    
		    .execFilterHeader{
		      height: 70rpx;
		      display: flex;
		      justify-content: center;
		      align-items: center;
		      border-bottom: 2rpx solid #ccc;
		    }
		    
		    .execFilterBody{
		      .execFilterItem{
		        padding: 32rpx;
		        border-bottom: 2rpx dashed #ccc;
		        &:last-of-type{
		          border-bottom: none;
		        }
		        .execFilterItemHeader{}
		        .execFilterItemBody{
		          display: flex;
		          flex-wrap: wrap;
		          padding: 32rpx 0 0;
		          justify-content: space-between;
		          text-align: left;
		          
		          .execFilterItemBox{
		            width: 200rpx;
		            height: 80rpx;
		            text-align: center;
		            line-height: 80rpx;
		            background-color: #f6f6f6;
		            margin-bottom: 20rpx;
		            &.active{
		              color: #fff;
		              background-color: #49b856;
		            }
		          }
		          
		          .deptName {
		            height: 80rpx;
		            background-color: #f6f6f6;
		            padding: 0 20rpx;
								width: 100%;
		          }
		        }
		      }
		    }
		  }
		}
		.btn-view{
			width: 100%;
			bottom: 20rpx;
			position: fixed;
			.back-btn{
				background: #49B856;
				border-radius: 10rpx;
				color: #fff;
				font-size: 30rpx;
				text-align: center;
				line-height: 80rpx;
				width: 95%;
			}
		}
	}
</style>