<script>
export default {
	name: 'tile',
	data(){
		return {
			zIdx: 0,
			overFlow: 'visible',
		}
	},
	props: {
		tree: Object,
	},
	methods: {
		onImgClick(){
			this.$emit('tile-clicked', [this.tree]);
			//increase z-index and hide overflow during transition
			this.zIdx = 1;
			this.overFlow = 'hidden';
			setTimeout(() => {this.zIdx = 0; this.overFlow = 'visible'}, 300);
		},
		onInnerTileClicked(nodeList){
			this.$emit('tile-clicked', [...nodeList, this.tree]);
		},
		onHeaderClick(){
			this.$emit('header-clicked', [this.tree]);
			//increase z-index and hide overflow during transition
			this.zIdx = 1;
			this.overFlow = 'hidden';
			setTimeout(() => {this.zIdx = 0; this.overFlow = 'visible'}, 300);
		},
		onInnerHeaderClicked(nodeList){
			this.$emit('header-clicked', [...nodeList, this.tree]);
		}
	}
}
</script>

<template>
<div
	:style="{position: 'absolute', left: tree.x+'px', top: tree.y+'px',
		width: tree.w+'px', height: tree.h+'px', zIndex: zIdx, overflow: overFlow}"
	class="transition-[left,top,width,height] duration-300 ease-out border border-stone-900 bg-white">
	<div v-if="tree.children.length == 0"
		:style="{backgroundImage: 'url(\'/src/assets/' + tree.tolNode.name + '.jpg\')'}"
		class="hover:cursor-pointer w-full h-full bg-cover" @click="onImgClick"
		/>
	<div v-else>
		<div v-if="tree.headerSz > 0" :style="{height: tree.headerSz+'px'}"
			class="text-center hover:cursor-pointer bg-stone-300" @click="onHeaderClick">
			{{tree.tolNode.name}}
		</div>
		<div v-if="tree.sideArea"
			:style="{position: 'absolute', left: tree.sideArea.x+'px', top: tree.sideArea.y+'px',
				width: (tree.sideArea.w + (tree.sideArea.sweptLeft ? tree.sideArea.extraSz : 0))+'px',
				height: (tree.sideArea.h + (tree.sideArea.sweptLeft ? 0 : tree.sideArea.extraSz))+'px',
				borderWidth: tree.sideArea.w > 0 ? '1px' : '0',
				borderRightColor: (tree.sideArea.sweptLeft ? 'white' : 'currentColor'),
				borderBottomColor: (tree.sideArea.sweptLeft ? 'currentColor' : 'white')}"
			class="transition-[left,top,width,height] duration-300 ease-out border border-stone-900 bg-white">
			<tile v-for="child in tree.sideChildren" :key="'SIDE_' + child.tolNode.name" :tree="child"
				@tile-clicked="onInnerTileClicked" @header-clicked="onInnerHeaderClicked"
				></tile>
		</div>
		<tile v-for="child in tree.children" :key="child.tolNode.name" :tree="child"
			@tile-clicked="onInnerTileClicked" @header-clicked="onInnerHeaderClicked"
			></tile>
	</div>
</div>
</template>

