<template>
	<el-dialog title="导入表" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')">
		<pi-table ref="tableRef" :api="api.tools.gen_table.select" @selection-change="selectionChange"
				  :page-size="10" hide-do>
			<template #do>
				<el-input v-model="search.table_name" placeholder="表名称" clearable style="width: 200px;"
						  @keydown.enter="upsearch"></el-input>
				<el-input v-model="search.table_comment" placeholder="表描述" clearable style="width: 200px;"
						  @keydown.enter="upsearch"></el-input>
				<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
			</template>
			<el-table-column type="selection" width="50"></el-table-column>
			<el-table-column label="表名称" prop="table_name"></el-table-column>
			<el-table-column label="表描述" prop="table_comment"></el-table-column>
			<el-table-column label="创建时间" prop="create_time"></el-table-column>
			<el-table-column label="更新时间" prop="update_time"></el-table-column>
		</pi-table>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" :disabled="selection.length === 0" :loading="isSaveing" @click="submit()">生 成</el-button>
		</template>
	</el-dialog>
</template>

<script setup>
import {getCurrentInstance, ref} from 'vue'
import api from "@/api/index.js"

defineExpose({
	open
})
const emit = defineEmits(['success', 'closed'])
const {proxy} = getCurrentInstance()
const tableRef = ref(null)

let visible = ref(false)
let isSaveing = ref(false)
let selection = ref([])
let search = ref({
	table_name: null,
	table_comment: null
})

function open() {
	visible.value = true
}

async function submit() {
	isSaveing.value = true;
	const res = await api.tools.gen_table.build({names: selection.value.map(i => i.table_name)});
	isSaveing.value = false;
	emit('success')
	visible.value = false;
	proxy.$message.success(res.msg)
}

//表格选择后回调事件
function selectionChange(e) {
	selection.value = e;
}

//搜索
function upsearch() {
	tableRef.value.upData(search.value)
}
</script>
