<template>
	<div class="quality-lexicon-container layout-pd">
		<el-card shadow="never">
			<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent>
				<el-form-item label="违禁词" prop="Name">
					<el-input v-model="state.queryParams.Name" placeholder="请输入违禁词" clearable @keyup.enter="queryList" class="keyword-input" />
				</el-form-item>
				<el-form-item label="违禁词分类" prop="Classify">
					<el-select v-model="state.queryParams.Classify" placeholder="请选择违禁词分类">
						<el-option v-for="item in prohibitedClassify" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
					</el-select>
				</el-form-item>
				<el-form-item label="违禁词属性" prop="Type">
					<el-select v-model="state.queryParams.Type" placeholder="请选择违禁词属性">
						<el-option v-for="item in prohibitedType" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
					<el-button @click="resetQuery(ruleFormRef)" v-waves class="default-button"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card shadow="never">
			<ProTable
				ref="proTableRef"
				:columns="columns"
				:data="state.tableData"
				@updateTable="queryList"
				:loading="state.loading"
				:total="state.total"
				v-model:page-index="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
			>
				<!-- 表格 header 按钮 -->
				<template #tableHeader="scope">
					<el-button type="primary" @click="onLexiconAdd" v-auth="'quality:lexicon:add'"> <SvgIcon name="ele-Plus" class="mr5" />新增 </el-button>
					<el-button type="primary" @click="onLexiconDelete" v-auth="'quality:lexicon:delete'" :disabled="!scope.isSelected">
						<SvgIcon name="ele-Delete" class="mr5" />删除
					</el-button>
				</template>
				<!-- 表格操作 -->
				<template #operation="{ row }">
					<el-button link type="primary" @click="onLexiconEdit(row)" v-auth="'quality:lexicon:edit'" title="编辑违禁词"> 编辑 </el-button>
				</template>
			</ProTable>
		</el-card>
		<!--  违禁词新增  -->
		<lexicon-add ref="lexiconAddRef" @updateList="queryList" :prohibitedClassify="prohibitedClassify" :prohibitedType="prohibitedType" />
		<!--  违禁词编辑  -->
		<lexicon-edit ref="lexiconEditRef" @updateList="queryList" :prohibitedClassify="prohibitedClassify" :prohibitedType="prohibitedType" />
	</div>
</template>

<script lang="tsx" setup name="qualityLexicon">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElButton, ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { auth } from '@/utils/authFunction';
import { lexiconBaseData, lexiconDelete, lexiconList } from '@/api/quality/lexicon';

// 引入组件
const LexiconAdd = defineAsyncComponent(() => import('@/views/quality/lexicon/components/Lexicon-add.vue')); // 违禁词新增
const LexiconEdit = defineAsyncComponent(() => import('@/views/quality/lexicon/components/Lexicon-edit.vue')); // 违禁词编辑

const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ type: 'selection', fixed: 'left', width: 55 },
	{ prop: 'name', label: '违禁词', width: 300 },
	{ prop: 'classify', label: '违禁词分类', width: 130 },
	{ prop: 'type', label: '违禁词属性' },
	{ prop: 'creatorName', label: '创建人' },
	{
		prop: 'creationTime',
		label: '创建时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.creationTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'lastModificationName', label: '更新人' },
	{
		prop: 'lastModificationTime',
		label: '更新时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.lastModificationTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'operation', label: '操作', fixed: 'right', width: 120, align: 'center' },
]);
// 定义变量内容
const state = reactive({
	loading: false, // 加载状态
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 10,
		Classify: null,
		Type: null,
		Name: null,
	},
	total: 0, // 总条数
	tableData: [], // 表格数据
});
const ruleFormRef = ref<RefType>(null); // 表单ref
const prohibitedClassify = ref<EmptyArrayType>([]); // 违禁词分类
const prohibitedType = ref<EmptyArrayType>([]); // 违禁词属性
const getBaseData = async () => {
	try {
		const res = await lexiconBaseData();
		prohibitedClassify.value = res.result?.prohibitedClassify ?? [];
		prohibitedType.value = res.result?.prohibitedType ?? [];
	} catch (error) {
		console.log(error);
	}
};
// 获取参数列表
const queryList = () => {
	state.loading = true;
	lexiconList(state.queryParams)
		.then((res) => {
			state.loading = false;
			state.tableData = res.result.items ?? [];
			state.total = res.result.total ?? 0;
		})
		.finally(() => {
			state.loading = false;
		});
};
// 重置表单
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 新增意见
const lexiconAddRef = ref<RefType>(); // 意见新增
const onLexiconAdd = () => {
	lexiconAddRef.value.openDialog();
};
// 修改意见
const lexiconEditRef = ref<RefType>(); // 修改意见
const onLexiconEdit = (row: any) => {
	lexiconEditRef.value.openDialog(row);
};
// 删除参数
const onLexiconDelete = () => {
	const names = proTableRef.value.selectedList.map((item: any) => item.name).join('、');
	const ids = proTableRef.value.selectedList.map((item: any) => item.id);
	ElMessageBox.confirm(`您确定要删除：【${names}】违禁词，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			lexiconDelete({ ids }).then(() => {
				ElMessage.success('操作成功');
				queryList();
			});
		})
		.catch(() => {});
};
// 页面加载时
onMounted(() => {
	getBaseData();
	queryList();
});
</script>

<style lang="scss" scoped>
.quality-lexicon-container {
}
</style>
