<template>
	<cl-view-group ref="ViewGroup">
		<template #left>
			<table-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-datafield" setup>
import { useCrud, useTable, useUpsert } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { useViewGroup } from "../../hooks";
import TableList from "./components/table-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: "type",
			label: "字段类型",
			component: {
				name: "el-radio-group",
				options: [
					{ label: "字符", value: 0 },
					{ label: "日期", value: 1, color: "#67C23A" },
					{ label: "数值", value: 2, color: "#E6A23C" },
					{ label: "空间数据", value: 3, color: "#F56C6C" }
				]
			},
			value: 0,
			required: true
		},
		{ prop: "length", label: "字段长度", component: { name: "el-input" } },
		{ prop: "unit", label: "字段单位", component: { name: "el-input" } },
		{
			prop: "isDisplay",
			label: "是否显示",
			required: true,
			component: {
				name: "el-radio-group",
				options: [
					{ label: "是", value: 1 },
					{ label: "否", value: 0 }
				]
			},
			value: 1
		},
		{
			prop: "sortCode",
			label: "排序",
			required: true
		}
	],
	onSubmit(data, { next }) {
		next({
			...data,
			tableId: ViewGroup.value?.selected?.id
		});
	}
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		// { prop: "id", label: "ID" },
		{ prop: "name", label: "字段名称" },
		{ prop: "description", label: "字段描述" },
		{
			prop: "type",
			label: "字段类型"
		},
		{ prop: "length", label: "字段长度" },
		{ prop: "unit", label: "字段单位" },
		{ prop: "isDisplay", label: "是否显示" },
		{
			prop: "sortCode",
			label: "排序"
		},
		{ prop: "tableName", 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.datafield
	},
	(app) => {
		app.refresh();
	}
);
// 刷新列表
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
</script>
