<template>
	<pi-table ref="tableRef" :apiObj="api.tools.gen_table.list" @selection-change="selectionChange">
		<template #do>
			<el-button v-auth="'gen_table:add'" type="primary" icon="el-icon-plus" @click="add"></el-button>
			<el-button v-auth="'gen_table:edit'" type="success" icon="el-icon-edit" @click="edit()"
					   :disabled="selection.length!==1"></el-button>
			<el-button v-auth="'gen_table:del'" type="danger" plain icon="el-icon-delete" :disabled="selection.length===0"
					   @click="batch_del"></el-button>
		</template>
		<template #search>
			<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_id" width="80"></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="module_name"></el-table-column>
		<el-table-column label="控制器名称" prop="controller_name"></el-table-column>
		<el-table-column label="备注" prop="remark"></el-table-column>
		<el-table-column label="创建时间" prop="create_time"></el-table-column>
		<el-table-column label="更新时间" prop="update_time"></el-table-column>
		<el-table-column label="操作" fixed="right" align="right" width="120">
			<template #default="scope">
				<el-button-group>
					<el-button text type="primary" size="small" @click="show(scope.row, scope.$index)">预览</el-button>
					<el-button v-auth="'gen_table:edit'" text type="success" size="small"
							   @click="edit(scope.row, scope.$index)">编辑
					</el-button>
					<el-popconfirm title="确定删除吗？" @confirm="del(scope.row, scope.$index)">
						<template #reference>
							<el-button v-auth="'gen_table:del'" text type="danger" size="small">删除</el-button>
						</template>
					</el-popconfirm>
					<el-popconfirm title="确定删除吗？" @confirm="del(scope.row, scope.$index)">
						<template #reference>
							<el-button v-auth="'gen_table:sync'" text type="warning" size="small">同步</el-button>
						</template>
					</el-popconfirm>
				</el-button-group>
			</template>
		</el-table-column>
	</pi-table>
	<save-dialog v-if="dialogShow" ref="dialogRef" @success="tableRef.refresh()"
				 @closed="dialogShow=false"></save-dialog>
</template>

<script setup>
import saveDialog from './save'
import api from "@/api/index";
import {getCurrentInstance, nextTick, ref} from "vue";
import router from "@/router/index"

defineOptions({
	name: "toolsGen"
})

const {proxy} = getCurrentInstance()
const tableRef = ref(null)
const dialogRef = ref(null)

let dialogShow = ref(false)
let selection = ref([])
let search = ref({
	crontab_name: null,
	enable: null
})

//添加
function add() {
	dialogShow.value = true
	nextTick(() => {
		dialogRef.value.open()
	})
}

//编辑
async function edit(row) {
	router.push({
		path: "/tools/gen/edit",
		query: {
			id: row?.table_id ?? selection.value[0]?.table_id
		}
	})
}

//查看
async function show(row) {
	dialogShow.value = true
	nextTick(() => {
		dialogRef.value.open('show', row)
	})
}

function show_log(row) {
	router.push({
		path: "/monitor/crontab_log",
		query: {
			crontab_id: row?.crontab_id
		}
	})
}

//删除
async function del(row) {
	const loading = proxy.$loading();
	const res = await api.tools.gen_table.del({ids: [row.table_id]});
	tableRef.value.refresh()
	loading.close();
	proxy.$message.success(res.msg)
}

//批量删除
async function batch_del() {
	proxy.$confirm(`确定删除选中的 ${selection.value.length} 项吗？如果删除项中含有子集将会被一并删除`, '提示', {
		type: 'warning'
	}).then(async () => {
		const loading = proxy.$loading();
		const res = await api.tools.gen_table.del({ids: selection.value.map(item => item.table_id)});
		tableRef.value.refresh()
		loading.close();
		proxy.$message.success(res.msg)
	})
}

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

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