<template>
	<Subtitle value="Patient" />
	<div v-if="(sensitive) && (!viewsensitive)" 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>
		<button class="btn btn-danger" @click="viewsensitive = true">Proceed</button>
	</div>
	<template v-if="patient_info">
		<Submenu :value="menu" />
		<div class="card mb-3 shadow">
			<div v-if="sensitive" class="card-header alert-danger d-flex justify-content-between align-items-center">
				<span>{{patient_info.name}} <span :title="patient_info.pid">{{patient_info.pid.slice(-4)}}</span> #{{patient_dfn}}</span>
				<button class="btn-close" @click="viewsensitive = false"></button>
			</div>
			<div v-else 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 cookie from './cookie.mjs';
	import { strptime_vista } from './util.mjs';

	import Subtitle from './Subtitle.vue';
	import Submenu from './Submenu.vue';

	export default {
		components: {
			Subtitle, Submenu
		},
		props: {
			client: Object
		},
		data() {
			return {
				viewsensitive: false,
				sensitive: false,
				patient_dfn: null,
				patient_info: null
			};
		},
		computed: {
			menu() {
				return this.patient_info ? {
					name: this.patient_info.name,
					items: [
						{ name: 'Patient', href: '/patient/' + this.patient_dfn },
						{ name: 'Visits', href: '/patient/' + this.patient_dfn + '/visits' },
						{ name: 'Orders', href: '/patient/' + this.patient_dfn + '/orders' },
						{ name: 'Reports', href: '/patient/' + this.patient_dfn + '/reports' },
						{ name: 'Documents', href: '/patient/' + this.patient_dfn + '/document' },
						{ name: 'Consults', href: '/patient/' + this.patient_dfn + '/consult' },
					]
				} : null;
			}
		},
		methods: {
			strptime_vista
		},
		watch: {
			'$route.params.id': {
				async handler(value) {
					if(value.startsWith('$')) {
						var id = value.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.sensitive = await this.client.ORWPT_SELCHK(value);
						this.patient_dfn = value;
						var viewsensitive = cookie.get('viewsensitive');
						this.viewsensitive = viewsensitive ? viewsensitive.split(',').indexOf(value) >= 0 : false;
					}
				}, immediate: true
			},
			viewsensitive(value) {
				var viewsensitive = cookie.get('viewsensitive'), viewsensitive = viewsensitive !== null ? viewsensitive.split(',') : [], idx = viewsensitive.indexOf(this.patient_dfn);
				if(value) {
					if(idx < 0) {
						viewsensitive.push(this.patient_dfn);
						cookie.set('viewsensitive', viewsensitive.join(','));
					}
				} else {
					if(idx >= 0) {
						viewsensitive.splice(idx, 1);
						cookie.set('viewsensitive', viewsensitive.join(','));
					}
				}
			}
		},
		created() {
			this.$watch(
				() => (this.client, this.patient_dfn, this.sensitive, this.viewsensitive, {}),
				async function() {
					if(this.client) {
						if(this.patient_dfn) this.patient_info = (this.sensitive) && (!this.viewsensitive) ? null : await this.client.ORWPT16_ID_INFO(this.patient_dfn);
						else this.patient_info = null;
					}
				},
				{ immediate: true }
			);
		}
	};
</script>
