<template>
	<el-dialog v-model="state.dialogVisible" draggable title="查看标签记录" ref="dialogRef" width="60%" append-to-body>
		<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent>
			<el-form-item label="市民标签" prop="Label">
				<el-input v-model="state.queryParams.Label" placeholder="市民标签" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="记录人" prop="CreatorName">
				<el-input v-model="state.queryParams.CreatorName" placeholder="记录人名称" clearable @keyup.enter="handleQuery" />
			</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>
		<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"
			max-height="500px"
		>
		</ProTable>
	</el-dialog>
</template>

<script setup lang="tsx" name="businessCitizenTagsRecord">
import { reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { citizenLabelList } from '@/api/auxiliary/citizen';
import { formatDate } from '@/utils/formatTime';

const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ prop: 'label', label: '市民标签' },
	{ prop: 'phone', label: '市民' },
	{ prop: 'creatorName', label: '记录人' },
	{
		prop: 'creationTime',
		label: '创建时间',
		width: 170,
		render: (scope: any) => {
			return <span>{formatDate(scope.row.creationTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
]);
// 定义变量内容
const state = reactive({
	dialogVisible: false, // 弹窗显示隐藏
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 10, // 每页条数
		Keyword: null, // 关键字
	},
	tableData: [], // 表格数据
	total: 0, // 总条数
	loading: false, // 加载状态
	citizen: {}, //市民信息
});
const ruleFormRef = ref<RefType>(); // 表单ref
// 打开弹窗
const openDialog = (row: any) => {
	state.citizen = row;
	queryList();
	state.dialogVisible = true;
};
const dialogRef = ref<RefType>();
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	handleQuery();
};
/** 获取历史工单 */
const queryList = () => {
	state.loading = true;
	let request = {
		...state.queryParams,
		CitizenId: state.citizen.id,
	};
	citizenLabelList(request)
		.then((response: any) => {
			state.tableData = response?.result.items ?? [];
			state.total = response?.result.total;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
