<template>
  <view class="main">
		<uni-nav-bar dark :fixed="true" background-color="#18C7B0" status-bar left-icon="left" height="62.5rpx" width="750rpx"
			title="处方详情" @clickLeft="handleBack"
		/>
		
		<scroll-view class="content" scroll-y="true">
			<view class="header">
				<view class="ti">
					<view class="left">
						<view class="baracode">
							<w-barcode :options="preseObj"></w-barcode>
							<view class="barnum">{{v.preNo}}</view>
						</view>
					</view>
					<view class="mid">
						<!-- <text class="title">{{v.preName}}</text>	 -->
						<text class="title">中药处方笺</text>
					</view>
					<view class="rig"><text class="title">{{v.type=='1'?"中药处方":v.type=='1'?"中药制剂":""}}</text></view>
				</view>
				<view class="patiinfo">
					<view class="r1">
						<view class="row"><span class="we">姓名：</span>{{v.name}}</view>
						<view class="row"><span class="we">性别：</span>{{v.sex}}</view>
						<view class="row"><span class="we">年龄：</span>{{v.age}}岁</view>
						<view class="row"><span class="we">电话：</span>{{v.contactNumber}}</view>
						<!-- 20231222隐藏，因为处方状态后端返回state数据不对 -->
						<!-- <view class="row"><span class="we">当前处方状态：</span>{{v.state=="1"?"配药":v.state=="2"?"煎药":v.state=="3"?"发药":v.state=="4"?"配送":""}}</view> -->
					</view>
					<view class="r2">
						<view class="row"><span class="we">就诊科室：</span>{{v.department}}</view>
						<!-- <view class="row"><span class="we">临床诊断：</span>{{v.disName}}—{{v.symName}}</view> -->
						<view class="row"><span class="we">临床诊断：</span>{{v.disName}}</view>
						<view class="row"><span class="we">开方时间：</span>{{v.prescriptionTime}}</view>
					</view>
					<view class="r3"><span class="we">联系地址：</span>{{v.address}}</view>
				</view>
			</view>
			<view class="rp">
				<view class="title">
					<text class="text">Rp:</text>
				</view>
				<view class="lists">
					<view class="item"  v-for="(x, i) in v.rp" :key="i">
						<view class="le tag-view">
							<view style="width: 24rpx;font-size: 22rpx;">{{x.yphjh}}</view>
							<view style="width: 170rpx;margin-left: 10rpx;">{{x.matName}}</view>
							<view style="width: 50rpx;font-size: 22rpx;">{{x.matDose+x.matUnitName}}</view>
						</view>
						<view class="rig">
							<view class="up"> {{x.matUsageName}}</view>
							<!-- <view class="do"> {{x.sign}}</view> -->
						</view>
					</view>
				</view>
				<view class="footer">
					<view class="js">
						剂数：{{v.number}}
						
					</view>
					
					<view class="js">{{v.frequency}}</view>
					<view class="freque">{{v.medicationTime}}</view>
				</view>
				<view class="rp-tips">{{v.entrust}}</view>
			</view>
			<view class="amountarea">
				<view class="amount">
					<view class="r1">
						<view class="pijw">药品金额：{{v.prescriptionSum}}</view>
					</view>
					<view class="r2">
						<view class="jyf">煎药费：{{v.daijianCost}}</view>
						<view class="psf">配送费：{{v.distributionCost}}</view>
					</view>
					<view class="r3">
						总金额：{{v.prescriptionTotalSum}}
						
					</view>
				</view>
				<view class="service">
					<view class="kf"><span class="we">开方：</span>{{v.operateList[0].operater || ''}}</view>
					<view class="kf"><span class="we">审核：</span>{{v.operateList[1].operater || ''}}</view>
					<view class="kf"><span class="we">调配：</span>{{v.operateList[2].operater || ''}}</view>
					<view class="kf"><span class="we">复核：</span>{{v.operateList[3].operater || ''}}</view>
					<!-- <view class="kf"><span class="we">浸泡：</span>{{v.operateList[4].operater || ''}}</view>
					<view class="kf"><span class="we">煎煮：</span>{{v.operateList[5].operater || ''}}</view>
					<view class="kf"><span class="we">打包：</span>{{v.operateList[6].operater || ''}}</view> -->
					<view class="kf"><span class="we">发药：</span>{{v.operateList[7].operater || ''}}</view>
				</view>
			</view>
			<view class="comment">
				<view class="z">注：</view>
				<text>{{v.entrust1111}}</text>
			</view>
		</scroll-view>
		<view class="submit">
			<button type="primary" class="bu" @click="dialogConfirm">打印</button>
		</view>
		<view class="popup">
			<uni-popup ref="alertDialog" type="dialog">
				<uni-popup-dialog type="center" cancelText="不打印" confirmText="打印" title="打印标签" @confirm="dialogConfirm" @close="dialogClose">
					<view class="cotent" id="printMe">
						<view class="title">{{v.yljgName+'(中医院)' || '某中医院取药标签打印(门诊)'}}</view>
						<view class="body">
							<view class="lef">
								<view class="baracode">
									<w-barcode class="cav" :options="preseObj"></w-barcode>
									<view class="barnum">{{v.preNo}}</view>
								</view>
							</view>
							<view class="rig">
								<view class="hz">患者：<view class="bold">{{v.name}}，{{v.sex}}，{{v.age}}岁</view></view>
								<view class="hz">出生日期：<view class="bold">{{v.birth}}</view></view>
							</view>
						</view>
					</view>
				</uni-popup-dialog>
			</uni-popup>
		</view>
		<view class="popup">
			<uni-popup ref="printerDialog" type="dialog">
				<uni-popup-dialog type="center" cancelText="取消" confirmText="确认" title="设置打印机" @confirm="connectThenPrint" @close="cancelConfig">
					<view class="printconfig">
						<view style="font-size: 20rpx;">使用须知</view>
						<view style="font-size: 16rpx;color:brown;margin-bottom: 20rpx;">*使用前先连接wifi打印机</view>
						<uni-easyinput class="input" confirm-type="done" v-model="printInfo.ip" placeholder="请输入打印机ip地址" @confirm="connectThenPrint"></uni-easyinput>
					</view>
				</uni-popup-dialog>
			</uni-popup>
			<uni-popup ref="alertMessage" type="message">
				<uni-popup-message type="warn" message="您有一个新的处方" :duration="2000"></uni-popup-message>
			</uni-popup>
		</view>
  </view>
</template>

<script>
  export default {
    name: ''
  }
</script>

<script setup>
import { onMounted, ref, getCurrentInstance } from 'vue'
import { selectOrderDetail } from "@/static/js/request.js"


import { onLoad } from '@dcloudio/uni-app'
import debounce from 'lodash.debounce'

const isPrint = ref(true)
const initPrint =ref(uni.getStorageSync('initPrint'))

const {proxy} = getCurrentInstance()

// #ifdef APP-PLUS 
const plug= uni.requireNativePlugin('Html5app-Gprinter');
// #endif

const preseId = ref(11220822134401)
const preseObj = ref({
		code: '',
		color:['#000'], 
		bgColor: '#FFFFFF', 
		width: 200, 
		height: 50 
})
const imageStyles =ref({
				width:112.5,
				height:112.5,
				border:{
					color:"#DDDDDD",
					width:1,
					style:'solid',
					radius:'4px'
				}
			})

const depolyImage = ref([])
const barOpations = ref({
	height: 80,
	displayValue: false
})

const v=ref({
	preNo:'',
	preMzZy:'',
	pharmacistsremarks:'药师备注',
	presType:'',
	name:'',//'李某',
	sex:'',
	age:0,
	contactNumber:'',
	preName:'',
	state: '',
	department:'',
	bed_no:'床位号',
	disName:'',
	symName:'',
	prescriptionTime:'',
	address:'',
	printTime:'',
	rp:[],
	number:0,
	isBehalf:'',
	pies_tips:'',
	dosageForm:'',
	prescriptionUsage:'',
	concentration:'',
	frequency:'',
	medicationTime:'',
	entrust:'',
	
	prescriptionSum: '',
	daijianCost:'',
	distributionCost:'',
	prescriptionTotleSum: '',
	
	yljgName:'',
	operateList:[
		{py:'kf',title:'开方',operater:''},
		{py:'sh',title:'审核',operater:''},
		{py:'tp',title:'调配',operater:''},
		{py:'fh',title:'复核',operater:''},
		{py:'jp',title:'浸泡',operater:''},
		{py:'jz',title:'煎煮',operater:''},
		{py:'db',title:'打包',operater:''},
		{py:'fy',title:'发药',operater:''},
	],
		
	entrust1111:'1、本处方当日有效\n  2、取药时请您当面核对药品名称、规格、数量 \n  3、延长处方用量时间原因：慢性病 其他老年病 外地 其他'

})
onMounted(() => {

})

onLoad(async(options) => {
	console.log('options', options)
	const userId = uni.getStorageSync('userId')
	const params = {
		token: uni.getStorageSync('token'),
		data: {
			id: options.id || '',
			preNo: options.preId || '',
			depolyStatus: options.depolyStatus || ''
		}
	}
	console.log("params--------->>>",params)
	const {data:res} = await selectOrderDetail(params)
	
	if (res.code === 200) {
		console.log("res>>>",res)
		v.value.prescriptionTime = res.data.prescriptionTime,
		v.value.name = res.data.name,
		v.value.sex = res.data.sex,
		v.value.age = res.data.age,
		v.value.contactNumber = res.data.contactNumber
		v.value.preName = res.data.preName
		v.value.address = res.data.address
		v.value.concentration = res.data.concentration
		v.value.frequency = res.data.frequency
		v.value.medicationTime = res.data.medicationTime
		
		v.value.disName = res.data.disName,
		v.value.symName = res.data.symName,
		v.value.preNo = res.data.preNo,
		
		preseObj.value.code = v.value.preNo
		v.value.preMzZy = res.data.preMzZy
		v.value.pharmacistsremarks = res.data.pharmacistsremarks
		v.value.department = res.data.department,
		v.value.bed_no = res.data.bedNo
		v.value.dosageForm = res.data.dosageForm,
		v.value.prescriptionUsage = res.data.prescriptionusage,
		
		v.value.number = res.data.number,
		v.value.isBehalf = res.data.isBehalf,
		
		v.value.prescriptionSum = res.data.prescriptionSum,
		v.value.prescriptionTotalSum = res.data.prescriptionTotalSum,
		v.value.daijianCost = res.data.daijianCost,
		v.value.distributionCost = res.data.distributionCost,
		v.value.prescriptionTotleSum = res.data.prescriptionTotleSum,
		v.value.birth = res.data.patientBirthday || ''
		v.value.printTime = res.data.printTime
		v.value.entrust = res.data.entrust,
		v.value.rp = res.data.detailList
		v.value.yljgName = res.data.yljgName || ''
	 
		v.value.state = res.data.state,
		console.log('》》》》》res.data',res.data)
		switch (res.data.state) {
			case '1':
				v.value.state = '配药'
				break
			case '2':
				v.value.state = '煎药'
				break
			case '3':
				v.value.state = '发药'
				break
			case '4':
				v.value.state = '配送'
				break
			case '5':
				v.value.state = '已分配'
				break
			case '6':
				v.value.state = '已传送'
				break
			default:
				v.value.state = '配药'
		}
		
		v.value.operateList[0].operater = res.data.doctor || ''
		// v.value.operateList[1].operater = res.data.checkBy || ''
		// v.value.operateList[2].operater = res.data.tiaopeiName || ''
		// 循环operateList
		const length = res.data.operateList.length
		if(length > 0){
			res.data.operateList.forEach((item)=>{
				switch(item.type){
					case '1':
						v.value.operateList[1].operater = item.createBy
						break
					case '2':
						v.value.operateList[2].operater = item.createBy
						break
					case '3':
						v.value.operateList[3].operater = item.createBy
						break
					case '16':
						v.value.operateList[4].operater = item.createBy
						break
					case '5':
						v.value.operateList[6].operater = item.createBy
						break
					case '6':
						v.value.operateList[7].operater = item.createBy
						break
					defalut:console.log('处方签res.data.operateList数据异常',item.type)
				}
			})
			console.log('item>>>>>>',v.value.operateList)
		}
	}
})


const connectThenPrint = ()=>{
	const ipReg = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
	if(ipReg.test(printInfo.value.ip)){
		if(RegExp.$1<256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4<256){
			uni.$showMsg('打印机连接成功！', 500)
			plug.connectIP({"ip":printInfo.value.ip,"port":printInfo.value.port},ret=>{
				const res = JSON.parse(JSON.stringify(ret))
				console.log('connectPrint>>res',res.code)
				if(res.code==3){
					uni.setStorageSync('hasPrinter', '1')
					uni.setStorageSync('printerIp', printInfo.value.ip)
					proxy.$refs.printerDialog.close()
						printerTsc()
					if(uni.getStorageSync('hasPrinter') == '1'){
						deploy()
					}
				} else{
					uni.$showMsg('打印机连接失败，请重试！', 500)
				}
			});
		} else{
			uni.$showMsg('ip地址有误，请重试！', 2000)
		}
	}
}

const handleBack =()=>{
	uni.navigateBack({
		delta: 1
	})
}


const dialogConfirm = debounce(async()=>{
	
	if(uni.getStorageSync('hasPrinter') == '1'){
		printerTsc()
		
		// const tmp  = await deploy()
		uni.redirectTo({
			url:"/pages/index/index"
		})
	} else {
		proxy.$refs.printerDialog.open('center')
	}
},500)


const printerTsc = ()=>{
	
	let data=[];  
	let line={};  
	
	line={};
	line.width=80; 
	line.height=50; 
	line.gap=2; 
	line.page=1; 
	line.direction=0, 
	data.push(line); 
	
	let filePath=plus.io.convertLocalFileSystemURL("static/OpenSans-Bold.ttf")
	
	
	line={};
	line.customText=v.value.yljgName+'(中药房)';
	line.x=120;
	line.y=10;
	line.fontSize=14;
	line.fontPath='';
	data.push(line); 
	
	// 患者姓名性别出生年月
	line={};
	line.customText=v.value.name+', '+v.value.sex+', '+v.value.birth;
	line.x=10;
	line.y=70;
	line.fontSize=10;
	line.fontPath='';
	data.push(line); 
	
	// 门诊住院
	line={};
	line.customText=v.value.preMzZy==1 ? '门诊' : '住院';
	line.x=500;
	line.y=70;
	line.fontSize=10;
	line.fontPath='';
	data.push(line); 
	
	// 条形码
	line={};
	line.barcode=v.value.preNo;
	line.x=15; 
	line.y=110; 
	line.type="CODE128"; 
	line.height=115; 
	line.width=180;
	line.readable=true; 
	line.rotation=0;  
	data.push(line);
	
	line={};
	line.customText=v.value.preNo;
	line.x=15;
	line.y=225;
	line.fontSize=8;
	line.fontPath='';
	data.push(line); 

	// 手机号
	line={};
	line.customText='手机号:'+v.value.contactNumber;
	line.x=250;
	line.y=110;
	line.fontSize=10;
	line.fontPath='';
	data.push(line); 
	
	// 处方用法
	line={};
	line.customText='处方用法:'+v.value.prescriptionUsage;
	line.x=250;
	line.y=150;
	line.fontSize=10;
	line.fontPath='';
	data.push(line); 
	
	
	line={};
	line.customText='剂数:'+v.value.number;
	line.x=250;
	line.y=190;
	line.fontSize=10;
	line.fontPath='';
	data.push(line);
	
	line={};
	if (v.value.isBehalf=='0') {
		line.customText='是否代煎:否';
	} else{
		line.customText='是否代煎:是';
	}
	line.x=385;
	line.y=190;
	line.fontSize=10;
	line.fontPath='';
	data.push(line);
	
	// 科室病房
	line={};
	if(v.value.bed_no!=null){
		line.customText='科室/病房:'+v.value.department+v.value.bed_no;
	}else{
		line.customText='科室/病房:'+v.value.department
	}
	line.x=15;
	line.y=260;
	line.fontSize=10;
	line.fontPath='';
	data.push(line); 
	
	// 药师备注
	line={};
	if(v.value.pharmacistsremarks != null){
		line.customText='药师备注:'+v.value.pharmacistsremarks;
	}else{
		line.customText='药师备注:';
	}
	line.x=15;
	line.y=300;
	line.fontSize=10;
	line.fontPath='';
	data.push(line); 
	
	line={};
	line.customText='打印时间:'+v.value.printTime;
	line.x=145;
	line.y=345;
	line.fontSize=8;
	line.fontPath='';
	data.push(line); 
	
	// #ifdef APP-PLUS
	plug.printer({TSC:data}, (ret) => {
		const tip = JSON.stringify(ret);
	})
	// #endif

 }

</script>

<style lang="scss">
.main {
	display: flex;
	flex-direction:column;
	justify-content:flex-start;
	width: 750rpx;
	:deep(.uni-nav-bar-text){
		font-size: 20px;
	}
	:deep(.uni-navbar-btn-text){
		font-size: 16px;
	}
	.content{
		margin: 5rpx;
		background-color: #fff;
		flex-direction:column;
		align-items: center;
		height:1050rpx;
		.header{
			display: flex;
			flex-direction:column;
			margin:16rpx 16rpx 0;
			.ti{
				display: flex;
				flex-direction:row;
				justify-content: space-between;
				margin-top: 15.22rpx;
				.left{
					align-items: flex-start;
					.baracode{
						display: flex;
						flex-direction:column;
						.barnum{
							font-size: 15rpx;
							display: flex;
							justify-content:center;
							align-items: center;
							letter-spacing: 0.3em;
						}
					}
				}
				.mid{
					display: flex;
					margin-right: 160rpx;
					.title{
						font-size: 23.75rpx;
						font-weight: 800;
					}
				}
				.rig{
					display: flex;
					.title{
						font-size: 16rpx;
						width: 40rpx;
						font-weight: 300;
					}
				}
			}
			.patiinfo{
				display: flex;
				flex-direction:column;
				margin-top: 25.2rpx;
				padding-bottom: 21.45rpx;
				border-bottom: 1px solid #979797;
				.r1{
					display: flex;
					flex-direction:row;
					margin-bottom:15rpx;
					.row{
						font-size: 15rpx;
						font-weight: 400;
						margin-right: 30rpx;
					}
					.we{
						color:#999999;
					}
				}
				.r2{
					display: flex;
					flex-direction:row;
					margin-bottom:15rpx;
					.row{
						font-size: 15rpx;
							font-weight: 400;
							margin-right: 30rpx;
						}
						.we{
							color:#999999;
						}
				}
				.r3{
					display: flex;
					flex-direction:row;
					font-size: 15rpx;
					.we{
						color:#999999;
					}
				}
			}
		}
		.rp{
			display: flex;
			flex-direction:column;
			margin:22.39rpx 17.36rpx 0;
			border-bottom: 1px solid #979797;
			.title{
				font-size: 22rpx;
				margin-bottom: 10rpx;
			}
			.lists{
				display: flex;
				flex-wrap: wrap;
				flex-direction: row;
				justify-content:flex-start;
				align-content:flex-start;
				margin-left:15rpx;
				// background-color: #999999;
				// height: 545rpx;
				// 处方明细单个小框
				
				.item{
					display: flex;
					// justify-content:center;
					// align-items: center;
					width: 345rpx;
					margin: 0 1rpx 20rpx 0;
					// background-color: yellow;
					// position: relative;
					// 药品内容
					.le{
						display: flex;
						// font-size: 24rpx;
						// width: 500rpx;
						// word-wrap: break-word;
						// z-index: 100;
						// position: absolute;
						
					}
					.tag-view{
						display: flex;
						// justify-content:center;
						align-items: center;
						font-size: 25rpx;
						// height: 22rpx;
						// line-height: 22rpx;
						font-weight:500;
						// width: 250rpx;
						padding: 6rpx 6rpx;
					}
					.rig{
						display: flex;
						// flex-direction:column;
						align-items: center;
						margin-left: 2rpx;
						// width: 200rpx;
						// z-index: 200;
						// position: absolute;
						// left:120rpx;
						.up{
							// margin-bottom:8rpx;
							font-size: 14rpx;
							// height: 12.5rpx;
						}
						.do{
							
							font-size: 12rpx;
							height: 12.5rpx;
						}
					}
				}
			}
			.footer{
				margin: 0 10rpx 8rpx;
				display: flex;
				font-size: 22rpx;
				.js{
					display: flex;
					margin-right: 54.4rpx;
					.pies_tips{}
				}
				.freque{}
			}
			.rp-tips{
				display: flex;
				justify-content:flex-end;
				font-size: 15rpx;
				margin-right: 10rpx;
				font-size: 22rpx;
			}
		}
		.amountarea{
			display: flex;
			margin: 21.3rpx 19.38px 0;
			padding-bottom: 20.94rpx;
			border-bottom: 1px solid #979797;
			.amount{
				display: flex;
				flex-direction:column;
				.r1{
					border: 1px solid #979797;
					font-size: 15rpx;
					width: 287.5rpx;
					display: flex;
					justify-content:center;
					align-items: center;
					height: 33.75rpx;
				}
				.r2{
					display: flex;
					justify-content:center;
					align-items: center;
					font-size: 15rpx;
					height: 33.75rpx;
					width: 287.5rpx;
					border-left: 1px solid #979797;
					border-right: 1px solid #979797;
					.jyf{
						
						width: 143.93rpx;
						display: flex;
						justify-content:center;
						align-items: center;
					}
					.psf{
						border-left: 1px solid #979797;
						width: 143.93rpx;
						display: flex;
						justify-content:center;
						align-items: center;
					}
				}
				.r3{
					display: flex;
					justify-content:center;
					align-items: center;
					height: 33.75rpx;
					font-size: 15rpx;
					border: 1px solid #979797;
				}
			}
			.service{
				display: flex;
				flex-wrap: wrap;
				margin-left: 50rpx;
				.kf{
					font-size: 15rpx;
					margin-right: 60rpx;
					width:115rpx;
					.we{
						font-size: 15rpx;
						color:#999999;
					}
				}
			}
		}
		
		.comment{
			margin: 10rpx 16rpx 10rpx;
			font-size: 15rpx;
			display: flex;
		}
	}
	
	.submit{
		display: flex;
		margin-top: 8rpx;
		background-color: #fff;
		align-items: center;
		width: 750rpx;
		height: 80rpx;
		.bu{
			display: flex;
			justify-content:center;
			align-items: center;
			height: 60rpx;
			width: 671.25rpx;
			font-size: 17.5rpx;
			border-radius: 3.75rpx;
			background-color: #18c7b0;
			color:#fff;
		}
	}
	
	:deep(.uni-popup-dialog){
		width: 500rpx;
		display: flex;
		flex-direction: column;
		background-color: #ffffff;

		.uni-dialog-title-text{
			display: flex;
			font-size: 20rpx;
		}
		.uni-dialog-content{
			display: flex;
			margin: 10rpx 20rpx;
			background-color: #fff;
			box-shadow: 0 0 20rpx rgba(0, 0, 0, 0.15);
			.cotent{
				display: flex;
				flex-direction: column;
				width:400rpx;
				height: 112.5rpx;
				.title{
					font-size: 17.5rpx;
					font-weight: 500;
					margin-bottom: 10rpx;
					display: flex;
					align-items: center;
					justify-content:center;
				}
				.body{
					display: flex;
					justify-content:center;
					.lef{
						display: flex;
						flex-direction: column;
						width: 200rpx;
						justify-content:center;
						.baracode{
							display: flex;
							flex-direction:column;
							width: 200rpx;
							height: 54.38rpx;
							.cav{
								width:200rpx;
								
							}
							.barnum{
								width:200rpx;
								font-size: 10rpx;
								display: flex;
								color: #333333;
								justify-content:center;
								align-items: center;
								letter-spacing: 0.7em;
							}
						}
					}
					.rig{
						display: flex;
						flex-direction: column;
						width: 340px;
						margin-left: 13.75rpx;
						.hz{
							display: flex;
							margin-bottom: 10rpx;
							font-size: 15rpx;
						}
						.bold{
							font-weight: 500;
						}
					}
				}
			}
		}
		.uni-dialog-button-group{
			display: flex;
			font-size: 17.5rpx;
			.uni-dialog-button{
				display: flex;
				.uni-dialog-button-text{
				}
			}
			.uni-border-left{
				.uni-dialog-button-text{
				color: #18c7b0;
				}
			}
		}
	}
	
.printconfig{
	background-color: #fff;
	width: 500rpx;
	height: 100rpx;
	padding: 20rpx;
	.initPrint{
		.title{
			font-size: 20rpx;
		}
		.tips{
			font-size: 16rpx;
		}
		.input{
			width: 300rpx;
		}
	}
}
}


</style>
