<template>
	<cl-view-group ref="ViewGroup">
		<template #left>
			<source-list @refresh="refresh" />
		</template>

		<template #right>
			<cl-crud ref="Crud">
				<cl-row>
					<!-- 刷新按钮 -->
					<cl-refresh-btn />
					<!-- 新增按钮 -->
					<cl-add-btn />
					<!-- 删除按钮 -->
					<cl-multi-delete-btn />
					<cl-flex1 />
					<!-- 关键字搜索 -->
					<cl-search-key />
				</cl-row>

				<cl-row>
					<!-- 数据表格 -->
					<cl-table ref="Table" />
				</cl-row>

				<cl-row>
					<cl-flex1 />
					<!-- 分页控件 -->
					<cl-pagination />
				</cl-row>

				<!-- 新增、编辑 -->
				<cl-upsert ref="Upsert" />
			</cl-crud>
		</template>
	</cl-view-group>
</template>

<script lang="ts" name="base-sys-datatable" setup>
import { useCrud, useTable, useUpsert } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { useViewGroup } from "../../hooks";
import SourceList from "./components/source-list.vue";

const { service } = useCool();

const { ViewGroup } = useViewGroup({
	title: "数据表列表"
});

// cl-upsert
const Upsert = useUpsert({
	items: [
		{ prop: "name", label: "表名称", required: true, component: { name: "el-input" } },
		{
			prop: "description",
			label: "表描述",
			required: true,
			component: { name: "el-input" }
		}
		// { prop: "sourceId", label: "数据源ID", component: { name: "el-input" } }
	],
	onSubmit(data, { next }) {
		next({
			...data,
			sourceId: ViewGroup.value?.selected?.id
		});
	}
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		// { prop: "id", label: "ID" },
		{ prop: "name", label: "表名称" },
		{ prop: "description", label: "表描述" },
		{ prop: "dataSourceName", label: "数据源" },
		{ prop: "createTime", label: "创建时间", sortable: "desc", width: 160 },
		{ prop: "updateTime", label: "更新时间", sortable: "custom", width: 160 },
		{ type: "op", buttons: ["edit", "delete"] }
	]
});

// cl-crud
const Crud = useCrud(
	{
		service: service.base.sys.datatable
	},
	(app) => {
		app.refresh();
	}
);
// 刷新列表
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
</script>
