<template>
	<div class="auxiliary-order-lexicon-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent>
				<el-form-item label="关键词" prop="Tag">
					<el-input v-model="state.queryParams.Tag" placeholder="请填写关键词" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</el-form-item>
				<el-form-item label="分类" prop="Classify">
					<el-select v-model="state.queryParams.Classify" placeholder="请选择分类" @change="handleQuery">
						<el-option v-for="item in orderWordClassify" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
					</el-select>
				</el-form-item>
				<el-form-item label="近义词" prop="Synonym">
					<el-input v-model="state.queryParams.Synonym" placeholder="请填写近义词" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
					<el-button @click="resetQuery(ruleFormRef)" class="default-button"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
				</el-form-item>
			</el-form>
			<vxe-toolbar
				ref="toolbarRef"
				:loading="state.loading"
				custom
				:refresh="{
					queryMethod: handleQuery,
				}"
			>
				<template #buttons>
					<el-button type="primary" @click="addParameter" v-auth="'auxiliary:orderLexicon:add'">
						<SvgIcon name="ele-Plus" class="mr5" />新增工单词库
					</el-button>
					<el-button type="danger" @click="businessTagRemove" v-auth="'auxiliary:orderLexicon:delete'" :disabled="isChecked">
						<SvgIcon name="ele-Delete" class="mr5" />删除
					</el-button>
				</template>
			</vxe-toolbar>
			<div style="overflow: hidden; width: 100%; height: 100%; flex: 1">
				<vxe-table
					border
					:loading="state.loading"
					:data="state.tableData"
					:column-config="{ resizable: true, useKey: true }"
					:row-config="{ isCurrent: true, isHover: true, useKey: true, height: 30 }"
					ref="tableRef"
					height="auto"
					auto-resize
					show-overflow
					:scrollY="{ enabled: true, gt: 100 }"
					id="auxiliaryOrderLexicon"
					:custom-config="{ storage: true }"
					@checkbox-all="selectAllChangeEvent"
					@checkbox-change="selectChangeEvent"
				>
					<vxe-column type="checkbox" width="60" align="center"></vxe-column>
					<vxe-column field="classify" title="词性分类" min-width="150"></vxe-column>
					<vxe-column field="tag" title="关键词" min-width="150"></vxe-column>
					<vxe-column field="synonym" title="同义词" min-width="150"></vxe-column>
					<vxe-column field="isEnable" title="是否启用" min-width="150">
						<template #default="{ row }">
							{{ row.isEnable === 1 ? '启用' : '禁用' }}
						</template>
					</vxe-column>
					<vxe-column field="creatorName" title="创建人" min-width="120"></vxe-column>
					<vxe-column field="creatorOrgName" title="创建部门" min-width="140"></vxe-column>
					<vxe-column field="creationTime" title="创建时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="lastModificationName" title="更新人" min-width="120"></vxe-column>
					<vxe-column field="lastModificationTime" title="更新时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.lastModificationTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column title="操作" fixed="right" width="90" align="center">
						<template #default="{ row }">
							<el-button link type="primary" @click="updateLexicon(row)" v-auth="'auxiliary:orderLexicon:edit'" title="编辑工单词库">
								编辑
							</el-button>
						</template>
					</vxe-column>
				</vxe-table>
			</div>
			<pagination
				@pagination="queryList"
				:total="state.total"
				v-model:current-page="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
				:disabled="state.loading"
			/>
		</div>
		<!--  新增工单词库  -->
		<order-lexicon-add ref="orderLexiconAddRef" @updateList="queryList" :orderWordClassify="orderWordClassify" />
		<!--  编辑工单词库  -->
		<order-lexicon-edit ref="orderLexiconEditRef" @updateList="queryList" :orderWordClassify="orderWordClassify" />
	</div>
</template>

<script lang="tsx" setup name="auxiliaryOrderLexicon">
import { computed, defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { orderLexiconBaseData, orderLexiconDelete, orderLexiconList } from '@/api/auxiliary/orderLexicon';
import { formatDate } from '@/utils/formatTime';
// 引入组件
const OrderLexiconAdd = defineAsyncComponent(() => import('@/views/auxiliary/orderLexicon/components/Order-lexicon-add.vue')); // 新增工单词库
const OrderLexiconEdit = defineAsyncComponent(() => import('@/views/auxiliary/orderLexicon/components/Order-lexicon-edit.vue')); // 编辑工单词库
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
const state = reactive({
	loading: false, // 加载状态
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 20,
		Tag: null, // 标签
		Classify: null, // 标签类型
		Synonym: null, // 近义词
	},
	total: 0, // 总条数
	tableData: [], // 表格数据
});
const ruleFormRef = ref<any>(null); // 表单ref
const orderWordClassify = ref<EmptyArrayType>(); // 标签类型
// 获取基础数据
const getBaseData = async () => {
	try {
		const res = await orderLexiconBaseData();
		orderWordClassify.value = res.result?.orderWordClassify ?? [];
	} catch (error) {
		console.log(error);
	}
};
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 获取列表
const queryList = () => {
	state.loading = true;
	orderLexiconList(state.queryParams)
		.then((res) => {
			state.loading = false;
			state.tableData = res.result.items ?? [];
			state.total = res.result.total ?? 0;
			tableRef.value.clearCheckboxRow();
			checkTable.value = [];
		})
		.finally(() => {
			state.loading = false;
			tableRef.value.clearCheckboxRow();
			checkTable.value = [];
		});
};
// 重置表单
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 新增词库
const orderLexiconAddRef = ref<RefType>();
const addParameter = () => {
	orderLexiconAddRef.value.openDialog();
};
// 修改参词库
const orderLexiconEditRef = ref<RefType>();
const updateLexicon = (row: any) => {
	orderLexiconEditRef.value.openDialog(row.id);
};
// 删除词库
const businessTagRemove = () => {
	const ids = checkTable.value.map((item: any) => item.id);
	ElMessageBox.confirm(`您确定要删除选中的工单词库是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			orderLexiconDelete({ ids }).then(() => {
				ElMessage.success('操作成功');
				queryList();
			});
		})
		.catch(() => {});
};
const tableRef = ref<RefType>();
const checkTable = ref<EmptyArrayType>([]);
const selectAllChangeEvent = ({ checked }) => {
	if (tableRef.value) {
		const records = tableRef.value.getCheckboxRecords();
		checkTable.value = records;
		console.log(checked ? '所有勾选事件' : '所有取消事件', records);
	}
};

const selectChangeEvent = ({ checked }) => {
	if (tableRef.value) {
		const records = tableRef.value.getCheckboxRecords();
		checkTable.value = records;
		console.log(checked ? '勾选事件' : '取消事件', records);
	}
};
const isChecked = computed(() => {
	return !Boolean(checkTable.value.length);
});
const toolbarRef = ref<RefType>();
// 页面加载时
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
	getBaseData();
});
</script>
