<template>
	<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">
			<template #slot-parentId="{ scope }">
				<cl-tool-select v-model="scope.parentId" />
			</template>
		</cl-upsert>
	</cl-crud>
</template>

<script lang="ts" name="base-sys-tool" setup>
import { useCrud, useTable, useUpsert } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { deepTree } from "/@/cool/utils";

const { service } = useCool();

// cl-upsert
const Upsert = useUpsert({
	items: [
		{ prop: "name", label: "名称", required: true, component: { name: "el-input" } },
		{
			prop: "parentId",
			label: "上级节点",
			hook: "empty",
			component: { name: "slot-parentId" }
		},
		{ prop: "key", label: "key", required: true, component: { name: "el-input" } },
		{ prop: "component", label: "组件", required: true, component: { name: "el-input" } },
		{ prop: "autoDisable", label: "自动禁用", required: true, component: { name: "el-input" } },
		{ prop: "disableOther", label: "禁用其他", component: { name: "el-input" } },
		{ prop: "group", label: "组", component: { name: "el-input" } },
		{ prop: "visible", label: "可见", required: true, component: { name: "el-input" } },
		{ prop: "data", label: "数据", component: { name: "el-input" } },
		{ prop: "meta", label: "元", component: { name: "el-input" } },
		{
			prop: "orderNum",
			label: "排序号",
			component: {
				name: "el-input-number",
				props: {
					placeholder: "请填写排序号",
					min: 0,
					max: 99,
					"controls-position": "right"
				}
			}
		},
		{
			prop: "icon",
			label: "图标",
			component: {
				name: "el-input"
			}
		}
	]
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		// { prop: "id", label: "ID" },
		{ prop: "name", label: "名称" },
		{ prop: "key", label: "key" },
		{ prop: "component", label: "组件" },
		{ prop: "autoDisable", label: "自动禁用" },
		{ prop: "disableOther", label: "禁用其他" },
		{ prop: "group", label: "组" },
		{ prop: "visible", label: "可见" },
		{ prop: "data", label: "数据" },
		{ prop: "meta", 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.tool,
		onRefresh(_, { render }) {
			service.base.sys.tool.list().then((list) => {
				render(deepTree(list));
			});
		}
	},
	(app) => {
		app.refresh();
	}
);
</script>
