<script setup lang="ts" name="Group">
import apis from "@a"
import message from "@@/utils/message"
import dayjs from "dayjs"

const tableRef = ref()
const modalRef = ref()
const opts = reactive<any>({
	columns: [
		{ field: "id", name: "ID", width: 100, visible: false, isSort: false, tooltip: true },
		{ field: "parentId", name: "父ID", width: "auto", isSort: false, visible: true },
		{ field: "name", name: "分组名称", width: "auto", isSort: true, visible: true },
		{ field: "description", name: "分组描述", width: "auto", isSort: false, visible: true },
		{ field: "createdAt", name: "创建时间", width: 185, isSort: true, visible: true },
		{ field: "actions", name: `操作`, width: 150 }
	],
	queryParams: {
		name: undefined,
		description: undefined,
		dateRange: []
	},
	searchFormItems: [
		{
			field: "name",
			label: "分组名称",
			class: "w-100",
			component: "I",
			listeners: {
				keyup: (e: any) => {
					if (e.code == "Enter") {
						handleQuery()
					}
				}
			}
		},
		{
			field: "description",
			label: "分组描述",
			class: "w-100",
			component: "I",
			listeners: {
				keyup: (e: any) => {
					if (e.code == "Enter") {
						handleQuery()
					}
				}
			}
		},
		{
			field: "dateRange",
			label: "创建时间",
			class: "w-100",
			component: "D",
			placeholder: "请选择创建时间",
			props: {
				type: "daterange",
				valueFormat: "YYYY-MM-DD",
				rangeSeparator: "-",
				startPlaceholder: "开始日期",
				endPlaceholder: "结束日期"
			},
			listeners: {
				change: (v: any) => {
					queryParams.value.dateRange = v
				}
			}
		}
	] as any,
	permission: "iot:group",
	handleFuns: {},
	customBtns: [],
	tableListFun: apis.iot.groupApi.listGroup,
	getEntityFun: apis.iot.groupApi.getGroup,
	deleteEntityFun: apis.iot.groupApi.delGroup,
	formItems: [
		{
			field: "parentId",
			label: "父ID",
			class: "w-100",
			component: "I",
			required: true
		},
		{
			field: "name",
			label: "分组名称",
			class: "w-100",
			component: "I",
			required: true
		},
		{
			field: "description",
			label: "分组描述",
			class: "w-100",
			component: "I",
			required: true
		}
	] as any,
	resetForm: () => {
		form.value = emptyFormData.value
	},
	emptyFormData: {
		id: undefined,
		parentId: undefined,
		name: undefined,
		description: undefined
	}
})
const { queryParams, emptyFormData } = toRefs(opts)
const form = ref<any>(emptyFormData.value)

/** 修改按钮操作 */
function handleUpdate(row: any) {
	tableRef.value.defaultHandleFuns.handleUpdate("", row)
}
/** 删除按钮操作 */
function handleDelete(rows: any[]) {
	tableRef.value.defaultHandleFuns.handleDelete("", rows)
}
/** 提交按钮 */
function submitForm() {
	if (form.value.id != undefined) {
		apis.iot.groupApi.updateGroup(form.value).then(() => {
			message.msgSuccess("修改成功")
			handleQuery()
		})
	} else {
		apis.iot.groupApi.addGroup(form.value).then(() => {
			message.msgSuccess("新增成功")
			handleQuery()
		})
	}
}
/** 查询按钮 */
function handleQuery() {
	addDateRange(queryParams.value, queryParams.value.dateRange)
	tableRef.value?.search()
}
/** 查询重置按钮 */
function resetQuery() {
	queryParams.value.dateRange = []
	addDateRange(queryParams.value, queryParams.value.dateRange)
}

function init() {}

onMounted(init)
</script>

<template>
	<div class="app-container">
		<VbDataTable
			ref="tableRef"
			:handle-perm="opts.permission"
			:handle-funs="opts.handleFuns"
			:search-form-items="opts.searchFormItems"
			:columns="opts.columns"
			:custom-btns="opts.customBtns"
			:remote-fun="opts.tableListFun"
			:get-entity-fun="opts.getEntityFun"
			:delete-entity-fun="opts.deleteEntityFun"
			sortField="createdAt"
			sort-order="desc"
			:modal="modalRef"
			:reset-form-fun="opts.resetForm"
			v-model:form-data="form"
			:query-params="queryParams"
			:check-multiple="true"
			:has-checkbox="true"
			:reset-search-form-fun="resetQuery"
			:custom-search-fun="handleQuery">
			<template #createdAt="{ row }">
				<span>{{ dayjs(row.createdAt).format("YYYY-MM-DD HH:mm:ss") }}</span>
			</template>
			<template #actions="{ row }">
				<vb-tooltip content="修改" placement="top">
					<el-button
						link
						type="primary"
						@click="handleUpdate(row)"
						v-hasPermission="'iot:group:edit'">
						<template #icon>
							<VbIcon icon-name="notepad-edit" icon-type="duotone" class="fs-3"></VbIcon>
						</template>
					</el-button>
				</vb-tooltip>
				<vb-tooltip content="删除" placement="top">
					<el-button
						link
						type="primary"
						@click="handleDelete([row])"
						v-hasPermission="'iot:group:remove'">
						<template #icon>
							<VbIcon icon-name="trash-square" icon-type="duotone" class="fs-3"></VbIcon>
						</template>
					</el-button>
				</vb-tooltip>
			</template>
		</VbDataTable>
		<VbModal
			v-model:modal="modalRef"
			:title="opts.modalTitle"
			:form-items="opts.formItems as any"
			:form-data="form"
			@confirm="submitForm"
			append-to-body></VbModal>
	</div>
</template>
