<template>
	<view class="main-box">
		<movable-area ref="contentArea" class="content-window">
			<thirdlucky-uml-entity
				v-for="(table, index) in tables"
				:ref="table.key"
				: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" />
		</movable-area>
		<!-- 		<uni-popup ref="popup" type="top">
			<thirdlucky-data-form ref="tableView" class="entity-info" :columns="columns" @confirm="onConfirm"></thirdlucky-data-form>
		</uni-popup> -->
		<view ref="propertyBar" class="property-bar">
			<!-- <scroll-view style="background-color: #a1a1a1;height: 10%;" scroll-y="true">
				<text>123</text><br/>
				<text>123</text><br/>
				<text>123</text><br/>
				<text>123</text><br/>
			</scroll-view>
			<scroll-view style="background-color: #aaaaff;height: 50%;">123</scroll-view> -->
			<scroll-view class="property-table" scroll-y="true">
				<thirdlucky-data-table ref="tableView" border stripe :datas="datas" :columns="schemas" @selected="onSelected"></thirdlucky-data-table>
			</scroll-view>
			<view class="property-info"><thirdlucky-data-form ref="propertyView" :columns="columns" @confirm="onConfirm"></thirdlucky-data-form></view>
		</view>
	</view>
</template>

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

export default {
	data() {
		return {
			currentEntityKey: '',
			currentColumnKey: '',
			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: [
				{ name: '1', info: 'test1' },
				{ name: '2', info: 'test2' },
				{ name: '3', info: 'test3' },
				{ name: '4', info: 'test4' },
				{ name: '5', info: 'test5' },
				{ name: '6', info: 'test6' },
				{ name: '7', info: 'test7' },
				{ name: '8', info: 'test8' }
			]
		};
	},
	computed: {
		tableDats() {
			return datas.map(function(data) {
				data.name;
			});
		}
	},
	methods: {
		onSelected(rindex, id, data) {
			console.log('选中' + rindex);
			console.log(data);
		},
		onConfirm(action, data) {},
		resetTool() {
			// console.log('1.reset:OFFSET=' + this.offsetX);
			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 title = '实体' + count;
				let key = 'entity' + count;
				let table = { x: _x, y: _y, title: title, entityName: key, 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: end;
	order: 3;
	flex: 2;
	margin-left: -$border-width;
	border-style: solid;
	border-width: $border-width;
	height: $window-height;
	background-color: white;
}
// .entity-info {
// 	order: 1;
// 	flex: 1;
// 	position: absolute;
// 	// top: 0;
// }
.property-table {
	order: 1;
	// flex: 1;
	height: 60%;
}
.property-info {
	order: 2;
	flex: 1;
	position: absolute;
	bottom: -39rpx;
}
</style>
