<template>
	<Subtitle value="Patient" />
	<div v-if="(sensitive) && (!patient_info)" class="alert alert-danger text-center mb-3 shadow" role="alert">
		<Subtitle value="Restricted Record" />
		<h1>Warning: Restricted Record</h1>
		<p>This record is protected by the Privacy Act of 1974 and the Health Insurance Portability and Accountability Act of 1996. If you elect to proceed, you will be required to prove you have a need to know. Accessing this patient is tracked, and your station Security Officer will contact you for your justification.</p>
		<router-link class="btn btn-danger" :to="'/patient/' + patient_dfn + '?viewsensitive'">Proceed</router-link>
	</div>
	<template v-if="patient_info">
		<div class="card mb-3 shadow">
			<div class="card-header">{{patient_info.name}} <span :title="patient_info.pid">{{patient_info.pid.slice(-4)}}</span> #{{patient_dfn}}</div>
			<div class="card-body row" style="font-family: monospace;">
				<div class="col" v-if="patient_info.dob"><strong>DOB:</strong> {{strptime_vista(patient_info.dob).toLocaleDateString('sv-SE')}}</div>
				<div class="col" v-if="patient_info.age"><strong>Age:</strong> {{patient_info.age}}</div>
				<div class="col" v-if="patient_info.sex"><strong>Sex:</strong> {{patient_info.sex}}</div>
				<div class="col" v-if="patient_info.sc_percentage"><strong>SC%:</strong> {{patient_info.sc_percentage}}</div>
				<div class="col" v-if="patient_info.type"><strong>Type:</strong> {{patient_info.type}}</div>
				<div class="col" v-if="patient_info.ward"><strong>Ward:</strong> {{patient_info.ward}}</div>
				<div class="col" v-if="patient_info.room_bed"><strong>Room/bed:</strong> {{patient_info.room_bed}}</div>
			</div>
		</div>
		<router-view :client="client" :sensitive="sensitive" :patient_dfn="patient_dfn" :patient_info="patient_info"></router-view>
	</template>
</template>

<script>
	import { strptime_vista } from './util.mjs';

	import Subtitle from './Subtitle.vue';

	export default {
		components: {
			Subtitle
		},
		props: {
			client: Object
		},
		data() {
			return {
				sensitive: false,
				patient_dfn: null,
				patient_info: null
			};
		},
		methods: {
			strptime_vista,
			async loadinfo(dfn, viewsensitive) {
				this.patient_dfn = dfn;
				this.sensitive = await this.client.ORWPT_SELCHK(dfn);
				this.patient_info = (this.sensitive) && (!viewsensitive) ? null : await this.client.ORWPT16_ID_INFO(dfn);
			}
		},
		async mounted() {
			if(this.$route.params.id.startsWith('$')) {
				var id = this.$route.params.id.substring(1);
				if(id.length == 9) {
					var patient = await this.client.ORWPT_FULLSSN(id);
					this.$router.replace('/patient/' + patient[0].dfn);
				} else if(id.length == 5) {
					var name = this.$route.query.name.toUpperCase();
					var patient = await this.client.ORWPT_LAST5(id);
					for(var i = 0; i < patient.length; ++i) if(name == patient[i].name) {
						this.$router.replace('/patient/' + patient[i].dfn);
						break;
					}
				}
			} else this.loadinfo(this.$route.params.id, this.$route.query.hasOwnProperty('viewsensitive'));
		},
		async beforeRouteUpdate(to, from, next) {
			this.loadinfo(to.params.id, to.query.hasOwnProperty('viewsensitive'));
			next();
		}
	};
</script>
