<template>
	<view class="main-box">
		<movable-area ref="contentArea" class="content-window">
			<thirdlucky-uml-entity
				v-for="(table, index) in tables"
				:x="table.x"
				:y="table.y"
				:key="table.key"
				:entityName="table.entityName"
				:title="table.title"
				:columns="table.columns"
				:style="{ order: index }"
				@start="onStart"
				@end="onEnd"
			/>
		</movable-area>
		<movable-area ref="toolBar" class="tool-bar">
			<thirdlucky-uml-entity ref="entityTool" :x="offsetX" :y="offsetY" entityName="entity" title="实体" @start="onStartTool" @end="onEndTool" @change="onchange" />
		</movable-area>
		<view ref="propertyBar" class="property-bar">
			<thirdlucky-data-table ref="tableView" class="property-table" :formData="datas" :columns="schemas"></thirdlucky-data-table>
			<thirdlucky-data-form ref="propertyView" class="property-info" :formData="datas" :columns="columns" @confirm="onConfirm"></thirdlucky-data-form>
		</view>
	</view>
</template>

<script>
let defaultX = 20;
let defaultY = 20;

export default {
	data() {
		return {
			x: 0,
			y: 0,
			offsetX: defaultX,
			offsetY: defaultY,
			tables: [],
			columns: [
				{
					name: 'name',
					info: '列名',
					tips: '',
					type: 'text'
				},
				{
					name: 'type',
					info: '类型',
					tips: '',
					type: 'text'
				},
				{
					name: 'info',
					info: '描述',
					tips: '',
					type: 'text'
				}
			],
			schemas: [
				{
					name: 'name',
					info: '列名',
					tips: '',
					type: 'text'
				},
			],
			datas: {}
		};
	},
	methods: {
		onchange(e) {
			// console.log(e);
		},
		onConfirm(action, data) {},
		resetTool() {
			console.log('1.reset');
			if (this.offsetX == defaultX) {
				this.offsetX += 0.0000001;
			} else {
				this.offsetX = defaultX;
			}
		},
		onStart(key, event, x, y) {
			console.log(key + ':' + event + ':' + x + ':' + y);
		},
		onEnd(key, event, x, y) {
			console.log(key + ':' + event + ':' + x + ':' + y);
		},
		onStartTool(key, event, x, y) {
			console.log(key + ':' + event + ':' + x + ':' + y);
		},
		onEndTool(key, event, x, y) {
			console.log(key + ':' + event + ':' + x + ':' + y);
			this.addEntity(x, y);
		},
		addEntity(x, y) {
			let count = this.tables.length;
			let tool = this.$refs.toolBar;
			let w = tool.width;
			this.resetTool();

			let _x = x - w + defaultX;
			let _y = y + defaultY;

			if (_x >= 0) {
				let table = { x: _x, y: _y, title: '实体' + count, entityName: 'test' + count, columns: [] };
				this.tables.push(table);
			}
		}
	}
};
</script>

<style lang="scss">
$window-height: 93vh;
$border-width: 5rpx;
$control-width: 200rpx;
$control-height: 200rpx;

.main-box {
	display: flex;
	flex-direction: row;
	justify-content: space-between;
}
.tool-bar {
	order: 1;
	flex: 1;
	height: $window-height;
	background-color: white;
	border-style: solid;
	border-width: $border-width;
}
.content-window {
	order: 2;
	flex: 7;
	margin-left: -$border-width;
	height: $window-height;

	border-style: solid;
	border-width: $border-width;
	background-color: gray;

	display: flex;
	flex-direction: row;
	flex-wrap: wrap;
}
.property-bar {
	display: flex;
	flex-direction: column;
	justify-content:space-between;
	order: 3;
	flex: 2;
	margin-left: -$border-width;
	border-style: solid;
	border-width: $border-width;
	height: $window-height;
	background-color: white;
}
.property-table{
	order: 1;
	flex: 1;
}
.property-info{
	order: 2;
	flex: 1;
	position: absolute;
	bottom: -39rpx;
}

@keyframes example {
	0% {
		background-color: red;
		left: 0px;
		top: 0px;
	}
	25% {
		background-color: yellow;
		left: 200px;
		top: 0px;
	}
	50% {
		background-color: blue;
		left: 200px;
		top: 200px;
	}
	75% {
		background-color: green;
		left: 0px;
		top: 200px;
	}
	100% {
		background-color: red;
		left: 0px;
		top: 0px;
	}
}
</style>
