<!--
    
  Copyright (c) 2022 NIBIO <http://www.nibio.no/>. 
  
  This file is part of VIPSObservationApp.
  VIPSObservationApp is free software: you can redistribute it and/or modify
  it under the terms of the NIBIO Open Source License as published by 
  NIBIO, either version 1 of the License, or (at your option) any
  later version.
  
  VIPSObservationApp is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  NIBIO Open Source License for more details.
  
  You should have received a copy of the NIBIO Open Source License
  along with VIPSObservationApp.  If not, see <http://www.nibio.no/licenses/>.
    
  Author: Bhabesh Bhabani Mukhopadhyay
  Author: Tor-Einar Skog <tor-einar.skog@nibio.no>
  Dated : 19-Aug-2021
    
-->
<template>
	<div>
		<!-- <router-link v-show="iseditable" :to="{name:'Observation', params:{observationId:observation_Id}}">Back</router-link> -->
		<div v-if="isMounted">
			<FormSchema :schema="observationDataSchema" v-model="observationData" @change="change"></FormSchema>
		</div>

	</div>
</template>
<script>
	import CommonUtil from '@/components/CommonUtil'
	import FormSchema from '@formschema/native'
	export default {
		props: ['observationId', 'organismId', 'isEditable', 'schemaData'],
		components: {FormSchema},
		data() {
			return {
				isMounted: false,
				observation_Id: '',
				organism_id: '',
				iseditable: true,
				observationDataSchema: {},
				observationData: {}
			}
		},
		methods:
		{
			change() {
				this.$emit('updateQuntificationData', this.observationData);
			},
			initQuantification() {


				let pestList = JSON.parse(localStorage.getItem(CommonUtil.CONST_STORAGE_PEST_LIST));
				let pest = pestList.find(({organismId}) => organismId === this.organism_id);
				if(pest == null)
				{
					// TODO: i18n this!!
					pest = {
						observationDataSchema: "{\"$schema\":\"http://json-schema.org/draft-04/schema#\",\"type\":\"object\",\"title\":\" \",\"properties\":{\"number\":{\"title\":\"Antall\",\"type\":\"string\"},\"unit\":{\"title\":\"MÃ¥leenhet\",\"type\":\"string\"}}}"
					}
				}
				this.observationDataSchema = JSON.parse(pest.observationDataSchema);


			}
		},
		errorCaptured(err, vm, info) {
			console.log('err : ' + err);
			console.log('vm : ' + vm);
			console.log('info : ' + info);
		},
		mounted() {
			this.isMounted = true;
			this.observation_Id = (this.observationId) ? this.observationId : this.$route.params.observationId;
			this.organism_id = (this.organismId) ? this.organismId : this.$route.params.organismId;
			this.observationData = (this.schemaData) ? this.schemaData : (this.$route.params.schemaData) ? this.$route.params.schemaData : '';


			if (typeof (this.isEditable) === 'undefined') {
				this.iseditable = true;
			}
			else {
				this.iseditable = false;
			}

			this.initQuantification();
			//console.info(this.observationDataSchema);

		},

	}
</script>