<template>
	<view>
		<view class="main-box">
			<uni-popup ref="popupDelete" type="dialog">
				<uni-popup-dialog title="删除确认" message="成功消息" :duration="2000" :before-close="true" @close="deleteCancel" @confirm="deleteConfirm"></uni-popup-dialog>
			</uni-popup>
			<uni-fab ref="entityFab" class="menuStyle" :content="menuContent" @trigger="menuClick"></uni-fab>
			<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.name"
					:title="table.title"
					:columns="table.columns"
					:style="{ order: index }"
					@start="onStart"
					@end="onEnd"
				/>
			</movable-area>
			<movable-area ref="toolBar" class="tool-bar">
				<view class="title-bar">工具栏</view>
				<thirdlucky-uml-entity ref="entityTool" :x="offsetX" :y="offsetY" entityName="entity" title="实体" @start="onStartTool" @end="onEndTool" />
			</movable-area>
			<view ref="propertyBar" class="property-bar">
				<view class="title-bar">实体信息</view>
				<view class="entity_info">
					<uni-forms-item class="form-item" label="名称"><uni-easyinput :focus="focus.entityName" v-model="_entityName" placeholder="请输入表名" /></uni-forms-item>
					<uni-forms-item class="form-item" label="标题"><uni-easyinput v-model="_entityTitle" placeholder="请输入标题" /></uni-forms-item>
					<uni-forms-item class="form-item" label="描述"><uni-easyinput v-model="_entityInfo" placeholder="请输入描述" /></uni-forms-item>
				</view>
				<scroll-view class="property-table" scroll-y="true">
					<thirdlucky-data-table ref="tableView" actions="+-" border stripe :datas="_columns" :columns="schemas" @selected="onSelected"></thirdlucky-data-table>
				</scroll-view>
				<view class="property-info">
					<thirdlucky-data-form ref="propertyView" :formData="column" :columns="columnSchemas" @confirm="onConfirm" @changed="onChanged"></thirdlucky-data-form>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
let defaultX = 20;
let defaultY = 40;
let defaultTable = { key: 'default' };

let idColumn = { name: 'id', type: 'bigint', info: '主键' };
let descriptionColumn = { name: 'description', type: 'text', info: '描述' };
let passwordColumn = { name: 'password', type: 'password', info: '密码' };
export default {
	data() {
		return {
			selected: 0,
			config: {
				followName: true
			},
			x: 0,
			y: 0,
			menuContent: [{ text: '删除' }, { text: '读取' }],
			offsetX: defaultX,
			offsetY: defaultY,
			tables: [],
			columnSchemas: [
				{
					name: 'name',
					info: '列名',
					tips: '',
					type: 'text'
				},
				{
					name: 'type',
					info: '类型',
					tips: '',
					type: 'enum',
					// values: ['text', 'number', 'date']
					values: [
						{ text: '文本', value: 'text' },
						{ text: '长整型', value: 'bigint' },
						{ text: '整型', value: 'int' },
						{ text: '浮点', value: 'double' },
						{ text: '日期', value: 'date' },
						{ text: '时间', value: 'time' },
						{ text: '开关', value: 'bool' }
					]
				},
				{
					name: 'info',
					info: '描述',
					tips: '',
					type: 'text'
				}
			],
			schemas: [
				{
					name: 'name',
					info: '列名',
					tips: '',
					type: 'text'
				}
			],
			table: defaultTable,
			column: {},
			focus: {
				entityName: false,
				columnName: false
			}
		};
	},

	computed: {
		_entityName: {
			get() {
				return this.table.name;
			},
			set(val) {
				this.table.name = val;
			}
		},
		_entityTitle: {
			get() {
				return this.table.title;
			},
			set(val) {
				this.table.title = val;
			}
		},
		_entityInfo: {
			get() {
				return this.table.info;
			},
			set(val) {
				this.table.info = val;
			}
		},
		_columns: {
			get() {
				if (this.table != 'undefined') {
					return this.table.columns;
				} else {
					return null;
				}
			}
		}
	},
	methods: {
		delteEntity() {
			if (this.table != null || this.table != defaulTable) {
				let index = this.tables.indexOf(this.table);
				this.tables.splice(index, 1);
				this.table = defaultTable;
			}
			this.$refs.entityFab.close();
		},
		loadEnetities() {
			this.api.getEntity('', this.entitySeccess);
		},
		entitySeccess(res) {
			console.log('实体是', res);
		},
		menuClick(e) {
			switch (e.index) {
				case 0:
					this.delteEntity();
					break;
				default:
					this.loadEnetities();
					break;
			}
		},
		deleteColumn(col) {
			let columns = this.table.columns;
			let index = columns.indexOf(col);
			columns.splice(index, 1);
			if (columns.length == 0) {
				this.refreshColumns();
			}
		},
		addColumn(col) {
			let propertyView = this.$refs.propertyView;
			propertyView.focusItem(0);
		},
		refreshColumns() {
			let columns = this.table.columns;
			if (columns != null && columns.length > 0) {
				let column = columns[0];
				this.onSelected(0, 'select', column);
			} else {
				this.onSelected(-1, 'select', {});
			}
		},
		onSelected(rindex, action, column) {
			this.column = column;
			switch (action) {
				case 'edit':
					break;
				case 'delete':
					this.deleteColumn(column);
					break;
				case 'select':
					break;
				case 'add':
					this.addColumn(column);
					break;
			}
		},
		followName(name, val) {
			let column = this.types.forName(val);
			if (column != null) {
				this.types.copyWithoutName(column, this.column);
			}
		},
		onChanged(name, newVal, oldVal) {
			if (this.config.followName) {
				this.followName(name, newVal);
			}
		},
		onConfirm(action, data) {
			console.log('保存');
		},
		resetTool() {
			if (this.offsetX == defaultX) {
				this.offsetX += 0.0000001;
			} else {
				this.offsetX = defaultX;
			}
		},
		onStart(key, event, x, y, table) {
			this.focus.entityName = false;
		},
		onEnd(key, event, x, y, table) {
			this.showEntity(key);
		},
		onStartTool(key, event, x, y, table) {
			this.focus.entityName = false;
		},
		onEndTool(key, event, x, y, table) {
			this.addEntity(x, y);
		},
		addEntity(x, y) {
			let tool = this.$refs.toolBar;
			let w = tool.width;
			let _x = x - w + defaultX;
			let _y = y + defaultY;

			if (_x >= 0) {
				let number = this.getNewNumber();
				let table = this.createTableWithNumber(_x, _y, number);
				this.tables.push(table);
				this.showEntity(table.name);
			}
			this.resetTool();
		},
		showEntity(key) {
			let table = this.tables.find(t => t.name == key);
			if (this.table != table) {
				this.table = table;
				this.refreshColumns();
				this.focus.entityName = true;
			}
		},
		getNewNumber() {
			let number = this.tables.length;
			while (true) {
				let name = 'entity' + number;
				let i = this.tables.findIndex(tab => {
					return tab.name == name;
				});
				if (i < 0) {
					break;
				}
				number++;
			}
			return number;
		},
		createTableWithNumber(x, y, number) {
			let title = '实体' + number;
			let name = 'entity' + number;
			return { x: x, y: y, index: number, title: title, name: name, info: '', columns: [this.tools.clone(idColumn)] };
		}
	}
};
</script>

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

$tool-bar-index: 100;
$content-bar-index: 0;
$property-bar-index: 200;
$menu-index: 201;

$title-bar-backcolor: #2d8cf0;
$property-bar-back-color: white;

.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;
	z-index: $tool-bar-index;
}
.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;
	z-index: $content-bar-index;
}
.property-bar {
	order: 3;
	flex: 2;
	border-style: solid;
	border-width: $border-width;
	margin-left: -$border-width;
	height: $window-height;
	background-color: $property-bar-back-color;
	z-index: $property-bar-index;
}
.menuStyle {
	z-index: $menu-index;
}
.title-bar {
	width: 100%;
	background-color: $title-bar-backcolor;
	color: white;
}
.property-table {
	order: 1;
	height: 38%;
}
.property-info {
	order: 2;
	flex: 1;
	position: absolute;
	bottom: -30rpx;
}
.table-info {
	display: flex;
	flex-direction: row;
}
.table-info-label {
	font-size: 0.95em;
	bottom: 0;
}
.entity_info {
	padding: 20rpx 15rpx 0rpx 10rpx;
	border-width: 1rpx;
}
</style>
