<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="columns"
				:style="{ order: index }"
				@start="onStart"
				@end="onEnd"
				out-of-bounds="false"
			/>
		</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-form :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: 'id',
					info: '唯一标识',
					tips: '字段名',
					type: 'text'
				},
				{
					name: 'name',
					info: '名称',
					tips: '请输入名称',
					type: 'text'
				},
				{
					name: 'pass',
					info: '密码',
					tips: '填入密码',
					type: 'password'
				},
				{
					name: 'level',
					info: '级别',
					tips: '级别',
					type: 'slider'
				},
				{
					name: 'salary',
					info: '工资',
					tips: '工资',
					type: 'digit'
				},
				{
					name: 'date',
					info: '日期',
					tips: '日期',
					type: 'date'
				},
				{
					name: 'datetime',
					info: '时间',
					tips: '事件',
					type: 'datetime'
				},
				{
					name: 'deleted',
					info: '是否删除',
					tips: '是否删除',
					type: 'switch'
				}
			],
			datas: {}
		};
	},
	methods: {
		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 {
	order: 3;
	flex: 2;
	margin-left: -$border-width;
	height: $window-height;
	background-color: white;
	border-style: solid;
	border-width: $border-width;
}

@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>
