<template>
	<view class="container safe-area-inset-bottom">
		<!-- 处方 -->
		<view class="head">
			<view class="head_record_top">
				<view class="record">
					处方编号：{{infoList.prescription_no}}
				</view>
				<view class="record_state">
					{{lists.type==1 ? '普通':'常用'}}
				</view>
			</view>
			<!-- 医院 -->
			<view class="head_record">
				<image :src="lists.store.offical_seal||'/static/group/xkyard.png'" mode=""></image>
				<view class="record_yard">
					<view class="yard">
						{{lists.store.name}}
					</view>
					<view class="state">
						处方笺
					</view>
				</view>
			</view>
			<!-- 时间 -->
			<view class="head_record_time">
				<text style="text-align: right;">开具日期：{{infoList.created_at}}}</text>
			</view>
		</view>

		<!-- 信息 -->
		<view class="my_info">
			<view class="info">
				<view class="info_item">
					<view class="name">
						姓名：
						<text>{{infoList.patient.name}}</text>
					</view>
				</view>
				<view class="info_item">
					<view class="name">
						性别：
						<text>{{infoList.patient.sex%2==0?'女':'男'}}</text>
					</view>
				</view>
				<view class="info_item">
					<view class="name">
						年龄：
						<text>{{infoList.patient.age}}</text>
					</view>
				</view>
				<view class="info_item">
					<view class="name">
						类别：
						<text>{{infoList.category}}</text>
					</view>
				</view>
			</view>

			<view class="info">
				<view class="info_item">
					<view class="name">
						科室：
						<text>{{infoList.doctor.depart.name}}</text>
					</view>
				</view>
				<view class="info_item">
					<view class="name">
						电话：
						<text>{{infoList.patient.mobile}}</text>
					</view>
				</view>
			</view>

			<view class="info">
				<view class="info_item">
					<view class="names">
						诊断：
						<text>{{infoList.clinical_diagnose}}</text>
					</view>
				</view>
			</view>
			<view v-if="lists.online_tcm_print && lists.online_tcm_print.show" class="info">
				<view v-if="lists.online_tcm_print.tcm_syndrome" class="info_item">
					<view class="names">
						中医证候：
						<text>{{ lists.online_tcm_print.tcm_syndrome }}</text>
					</view>
				</view>
				<view v-if="lists.online_tcm_print.tcm_method" class="info_item">
					<view class="names">
						中医治法：
						<text>{{ lists.online_tcm_print.tcm_method }}</text>
					</view>
				</view>
				<view v-if="lists.online_tcm_print.tcm_disease" class="info_item">
					<view class="names">
						中医疾病：
						<text>{{ lists.online_tcm_print.tcm_disease }}</text>
					</view>
				</view>
			</view>
		</view>

		<view class="bg">
			<!-- 药品 -->
			<view class="my_medical">
				<view class="title">
					Rp
				</view>

				<block v-if="lists.prescription_type==1||lists.prescription_type==3">
					<view class="items" v-for="(item,index) in infoList.repice" :key="item.id">
						<!--  v-for="(it,index) in item.content" :key="it.id" -->
						<view class="name">
							<view class="_name" v-for="(it,index) in item.content" :key="it.id">
								<view class="text">
									<text>{{ it.name}}</text>
									<text class="_abbr" v-if="it.order">[{{useWay[it.order]}}]</text>
								</view>
								<text class="name_num">{{ it.number}}{{it.unit?it.unit.name:'g'}}</text>
							</view>
						</view>
						<view class="details">
							<view class="text">
								<text>用法：煎服，每天 {{item.consumption}} 次 ，</text>
								<!-- <text v-if="item.deployment==2">{{' 每次 '+item.volume+' ml '}} ， </text> -->
								<text> 共 {{item.dosage}} 剂 </text>
							</view>
              <view class="text">
                <text>包装方式：{{item.process_rule}}</text>
              </view>
						</view>
					</view>
				</block>


				<block v-if="lists.prescription_type==2||lists.prescription_type==5||lists.prescription_type==7">
					<view class="item" v-for="it in infoList.repice" :key="it._wxKey">
						<!--  v-for="(it,index) in item.content" :key="it.id" -->
						<view class="name" style="justify-content: space-between;">
							<view class="yp_name">
								<text>{{ (it.content && it.content.drug_name) || '' }} </text>
								<text
									v-if="it.content && it.content.specification"
									class="drug-spec">{{ it.content.specification }}</text>
							</view>
							<text>x{{ it.number}}</text>
						</view>
						<view class="details">
							<text>用法：{{ it.instruction }}</text>
						</view>
					</view>
				</block>

			</view>

			<!-- 医嘱 -->
			<view class="doctor_order">
				<view class="yz">
					<text class="order_info_left">医嘱:</text>
					<view class="order_info">
						<view class="info" v-for="(item ,index) in lists.doctor_order" :key="index">
							{{item}}
						</view>
					</view>
				</view>
				<view class="titles">
					处方开具已完毕
				</view>
			</view>

		</view>
		<!-- 医生 -->
		<view class="my_doctor">
			<view class="doctor_info">
				<view class="info">
					<text>医师</text>
					<!-- <text class="name">{{infoList.doctor.name || ''}}</text> -->
					<image :src="checkImageUrl(lists.doctor_sign_image)" mode="aspectFit"></image>
					<image v-if="lists.doctor_second_sign == 1" :src="checkImageUrl(lists.doctor_sign_image)" mode="aspectFit"></image>
				</view>
				<view class="info">
					<text>审核药师</text>
					<!-- <text class="name">{{rpList.name || ' '}}</text> -->
					<image :src="checkImageUrl(lists.Pharmacist_sign_image)" mode="aspectFit"></image>
				</view>
				<view class="info">
					<text>发药人</text>
					<image :src="checkImageUrl(lists.Pharmacist_sign_image)" mode="aspectFit"></image>
				</view>
				<view class="info">
					<text>核对人</text>
					<image :src="checkImageUrl(lists.Pharmacist_sign_image)" mode="aspectFit"></image>
				</view>
				<view class="info">
					<text>调配人</text>
					<image :src="checkImageUrl(lists.Pharmacist_sign_image)" mode="aspectFit"></image>
				</view>
			</view>

			<view class="price">
				价格 ￥{{infoList.total_pay_price}}
			</view>
		</view>


		<!-- 驳回原因 -->
		<u-modal v-model="isshow" :show-cancel-button="true" title="驳回原因" confirm-text="驳回" @confirm="confirm"
			@cancel="isshow=false">
			<view class="slot-content" style="padding: 20rpx;">
				<u-input v-model="reject_reason" placeholder="请输入驳回原因" type="text" :border="true" />
			</view>
		</u-modal>

		<!-- 提醒 -->
		<!-- <view class="umode">
			<u-modal v-model="isshow" :content="content" @confirm="confirm" @cancel="cancel"
				:content-style="{color:'#1E293B',fontWeight: '500',fontSize: '34rpx'}" :show-cancel-button="true"
				confirm-text="取消" cancel-text="确定" cancel-color="#F44336" confirm-color="#536DFE" :show-title="false">
			</u-modal>
		</view> -->


		<!-- 温馨提示 -->
		<view class="my_prompt">
			<view class="title">
				温馨提示：请遵医嘱服药！处方{{lists.valid_hours}}小时有效！
			</view>
		</view>


		<!-- 驳回原因 -->
		<u-modal v-model="isModal" :show-cancel-button="true" title="驳回原因" confirm-text="驳回" @confirm="confirm"
			@cancel="isModal=false">
			<view class="slot-content" style="padding: 20rpx;">
				<u-input v-model="reject_reason" placeholder="请输入驳回原因" type="text" :border="true" />
			</view>
		</u-modal>

		<!-- 状态   || infoList.status==2 未通过 -->
		<view class="footer" v-if="lists.status==0">
			<view class="cancle" @click="refuse">
				拒绝
			</view>
			<view class="next" @click="pass(1)">
				通过
			</view>
		</view>

		<view class="footer" v-if="lists.status==1||lists.status==2" @click="toInfo">
			<view class=""></view>
			<view class="cancle">
				处方溯源
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getPassed,
		prescripRecordDetail,
		getUseWay
	} from "@/api/all.js";
	import { withWxKey } from '@/utils/wxListKey.js'
	export default {
		data() {
			return {
				statusName: {
					0: '待审核',
					1: '已通过',
					2: '未通过',
					3: '待使用',
					4: '已使用',
					5: '未使用',
					6: '已失效',
					7: '已初审'
				},
				// status:flase,
				status: 1,
				// status: "", // 处方状态
				id: "",
				infoList: [],
				num: 1,
				rpList: [],
				time: "",
				price: "",
				show: false,
				sign_image: null,
				prescription_no: "",
				isshow: false,
				reject_reason: "",
				isModal: false,
				useWay: [],
				order_id: "",
				lists: [],
			}
		},
		onLoad(e) {
			// console.log('12')
			// console.log(e, "id");
			this.id = e.id
			this.prescription_no = e.prescription_no
			this.getInfo()
			getUseWay({
				store_id: uni.getStorageSync('store_id') || 11001,
			}).then((res) => {
				if (res.errcode == 0) {
					const useWay = ["无", ...(res.data.map(it => it.name))]
					uni.setStorageSync('useWay', JSON.stringify(useWay))
					this.useWay = useWay
				}
			})
		},
		methods: {
			refuse() {
				this.is_reject = true;
				this.isshow = true
			},
      checkImageUrl(url) {
        // 如果url包含http，则返回url，反之加上 data:image/jpeg;base64,
        return url.includes('http') ? url : 'data:image/jpeg;base64,' + url
      },
			confirm() {
				getPassed({
					prescription_no: this.prescription_no,
					status: 2,
					reject_reason: this.reject_reason,
					store_id: uni.getStorageSync('store_id') || '11001',
				}).then((res) => {
					if (res.errcode == 0) {
						this.show = false
						this.loading = true;
						this.isshow = false
						this.getInfo() && this.$go(1, 4)
						this.$toast(res.data[0]);
					} else {
						this.$toast(res.msg);
						this.loading = false
					}
				})
			},
			// 数量
			getnum(obj) {
				let list = obj
				let num = 0
				list.map((it) => {
					num += it.number
				})
				return num
			},
			normalizeRepice(list) {
				const normalized = (list || []).map((it) => {
					let content = it.content
					if (typeof content === 'string') {
						try {
							content = JSON.parse(content || '{}')
						} catch (e) {
							content = {}
						}
					}
					if (!content || typeof content !== 'object' || Array.isArray(content)) {
						content = Array.isArray(content) ? content : {}
					}
					return {
						...it,
						content,
					}
				})
				return withWxKey(normalized, 'id', 'rp')
			},
			getInfo() {
				prescripRecordDetail({
					store_id: uni.getStorageSync('store_id') || '11001',
					prescription_no: this.prescription_no
				}).then((res) => {
					// console.log(res, 'deta');

					if (res.errcode == 0) {
						const content = res.data.content
						content.repice = this.normalizeRepice(content.repice)
						this.infoList = content
						this.rpList = res.data.pharmacistInfo
						this.lists = res.data
						// console.log(this.infoList, 'info');
					}
				})
			},

			pass(e) {
				this.status = e
				getPassed({
					store_id: uni.getStorageSync('store_id') || '11001',
					prescription_no: this.prescription_no,
					status: this.status
				}).then((res) => {
					if (res.errcode == 0) {
						this.show = false
						this.$toast('审核成功')
						this.getInfo()
					} else {
						this.$toast(res.msg);
					}
				})
			},
			toInfo() {
				this.$go('../../subPackages/sub_pharmacist/pharmacist_traceability?prescription_id=' + this
					.id + '&prescription_no=' + this.prescription_no)
			},
			// // 确定
			// cancel() {
			// 	this.status = 2
			// 	getPassed({
			// 		store_id: uni.getStorageSync('store_id') || '11001',
			// 		prescription_no: this.prescription_no,
			// 		status: this.status
			// 	}).then((res) => {
			// 		// console.log(res, 'deta');
			// 		if (res.errcode == 0) {

			// 			this.getInfo()
			// 		} else {
			// 			this.$toast(res.msg);
			// 		}
			// 	})
			// },
			// // 暂不
			// confirm() {
			// 	this.isshow = false;
			// },
		},
		mounted() {
			this.getInfo()
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		width: 750rpx;
		min-height: 100vh;
		max-height: 100%;
		padding-bottom: env(safe-area-inset-bottom);

		.bg {
			min-height: calc(100vh - 800rpx);
		}

		.head {
			.head_record_top {
				width: 710rpx;
				margin: 14rpx auto;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.record {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #6C7380;
				}

				.record_state {
					width: 80rpx;
					height: 42rpx;
					line-height: 42rpx;
					text-align: center;
					font-size: 24rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #A7ABB0;
					border: 1rpx solid #C4C7CC;
				}

			}

			.head_record {
				display: flex;
				align-items: center;
				justify-content: center;
				position: relative;

				image {
					width: 165rpx;
					height: 165rpx;
					position: absolute;
					left: 40%;
					top: -28rpx;
				}

				.record_yard {
					text-align: center;

					.yard {
						font-size: 48rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #31353D;
						line-height: 56rpx;
					}

					.state {
						height: 44rpx;
						font-size: 32rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #31353D;
						line-height: 38rpx;
					}
				}
			}

			.head_record_time {
				text-align: right;
				margin-top: 20rpx;
				width: 750rpx;
				padding-right: 32rpx;
				height: 34rpx;
				font-size: 24rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #6C7380;
				line-height: 28rpx;
			}
		}

		// 信息
		.my_info {
			width: 710rpx;
			margin: 2rpx auto;
			border-top: 1rpx solid #31353D;
			border-bottom: 1rpx solid #31353D;
			display: flex;
			flex-direction: column;
			justify-content: space-around;
			padding: 2rpx 4rpx;

			.info {
				width: 710rpx;
				display: flex;
				align-items: center;

				.info_item {

					.names {
						display: flex;
						align-items: center;
						justify-content: space-around;
						font-size: 28rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #6C7380;

						text {
							flex: 1;
							display: block;
							font-size: 28rpx;
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #31353D;
							line-height: 40rpx;
							margin: 0 0rpx 0 8rpx;
						}
					}

					.name {
						display: flex;
						align-items: center;
						justify-content: space-around;
						font-size: 28rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #6C7380;


						text {
							display: block;
							font-size: 28rpx;
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #31353D;
							margin: 0 20rpx 0 2rpx;
						}
					}
				}
			}
		}

		// 药品
		.my_medical {
			width: 710rpx;
			margin: 6rpx auto;

			.title {
				height: 44rpx;
				font-size: 32rpx;
				font-family: PingFang SC-Semibold, PingFang SC;
				font-weight: 600;
				color: #31353D;
				line-height: 44rpx;
			}

			.items {
				width: 710rpx;
				padding: 1rpx 0;

				.name {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #31353D;
					display: flex;
					// justify-content: space-between;
					flex-wrap: wrap;

					._name {
						display: flex;
						justify-content: space-between;
						margin: 2rpx 80rpx 2rpx 0;

						.text {
							margin-right: 26rpx;
							display: flex;
							flex-direction: column;

							._abbr {
								color: #A7ABB0;
								font-size: 20rpx;
								margin-top: 2rpx;
							}
						}
					}
				}

				.details {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #6C7380;
					display: flex;
					flex-direction: column;
					justify-content: space-around;

					.text {

						text {
							word-spacing: 1rpx;
						}
					}
				}
			}

			.item {
				width: 710rpx;
				padding-top: 2rpx;

				.name {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #31353D;
					display: flex;
					justify-content: space-between;
					align-items: center;

					.drug-spec {
						font-size: 24rpx;
						color: #999;
						margin-left: 10rpx;
					}
				}

				.details {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #6C7380;
					display: flex;
					flex-direction: column;
					justify-content: space-around;
				}
			}

			.item:last-child {
				border: 0;
			}
		}

		// 医嘱
		.doctor_order {
			width: 710rpx;
			margin: 2rpx auto;

			.yz {
				display: flex;
				color: #6C7380;

				.order_info_left {
					margin-right: 3rpx;
				}

				.order_info {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					line-height: 40rpx;
					margin-bottom: 2rpx;

					.info {
						flex: 1;
					}
				}
			}

			.titles {
				width: 710rpx;
				height: 40rpx;
				font-size: 24rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #A7ABB0;
				line-height: 40rpx;
			}
		}

		// 医生
		.my_doctor {
			width: 710rpx;
			margin: 2rpx auto;
			border-top: 1rpx solid #31353D;
			border-bottom: 1rpx solid #31353D;
			padding: 2rpx 4rpx;

			.doctor_info {
				width: 710rpx;
				display: flex;
				align-items: center;
				margin-bottom: 2rpx;
				align-items: center;
				flex-wrap: wrap;

				.info {
					min-width: 222rpx;
					max-width: 400rpx;
					margin: 5rpx 0 8rpx;

					text {
						font-size: 24rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #31353D;
					}

					image {
						width: 82rpx;
						height: 62rpx;
						vertical-align: middle;
					}

					.name {
						width: 106rpx;
						font-size: 32rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #31353D;

						border-bottom: 1rpx solid #000000;
						margin-left: 16rpx;
					}
				}

			}

			.price {
				width: 710rpx;
				height: 44rpx;
				font-size: 24rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #31353D;
				line-height: 44rpx;
			}
		}

		.umode {
			font-size: 34rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
		}

		// 温馨提示
		.my_prompt {
			width: 680rpx;
			margin: 0 auto 70rpx;
			padding: 20rpx 0rpx 40rpx 0rpx;

			.title {
				width: 680rpx;
				height: 40rpx;
				font-size: 28rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #A7ABB0;
				line-height: 40rpx;
				margin: 32rpx 0;
			}

			.prompt {
				width: 680rpx;

				text {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #A7ABB0;
				}

				ol {
					padding: 0 0 0 32rpx;

					li {
						font-size: 28rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #A7ABB0;
						margin: 4rpx 0;
					}
				}
			}
		}

		// 状态
		.footer {
			width: 750rpx;
			height: 90rpx;
			background-color: #fff;
			margin: 0rpx auto;
			position: fixed;
			bottom: env(safe-area-inset-bottom);
			left: 0;
			font-size: 28rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 40rpx;

			.cancle {
				display: flex;
				justify-content: center;
				align-items: center;
				margin-right: 20rpx;
				width: 308rpx;
				height: 76rpx;
				border-radius: 44rpx 44rpx 44rpx 44rpx;
				border: 2rpx solid #6ACDBB;
				color: #6ACDBB;
			}

			.next {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 308rpx;
				height: 76rpx;
				border-radius: 44rpx 44rpx 44rpx 44rpx;
				border: 2rpx solid #6ACDBB;
				color: #fff;
				background: #6ACDBB;
			}
		}

	}
</style>
