<template>
	<el-dialog v-model="state.dialogVisible" draggable title="选择市民" @close="close" destroy-on-close append-to-body>
		<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="handleQuery" class="keyword-input" />
			</el-form-item>
			<el-form-item label="" prop="PhoneNum">
				<el-input v-model="state.queryParams.PhoneNum" 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-table
			border
			:loading="state.loading"
			:data="state.tableData"
			:column-config="{ resizable: true }"
			:row-config="{ isCurrent: true, isHover: true, height: 30, useKey: true }"
			ref="tableRef"
			show-overflow
			:scrollY="{ enabled: true, gt: 100 }"
			showHeaderOverflow
			@checkbox-all="selectAllChangeEvent"
			@checkbox-change="selectChangeEvent"
			max-height="500"
		>
			<vxe-column type="checkbox" width="60" align="center"></vxe-column>
			<vxe-column field="name" title="市民姓名"></vxe-column>
			<vxe-column field="phoneNum" title="联系电话"></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-table>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="closeDialog" class="default-button">取 消</el-button>
				<el-button type="primary" @click="onSubmit" :loading="loading" :disabled="isChecked">确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="tsx">
import { computed, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { externalCitizenList } from '@/api/auxiliary/externalCitizen';
import { formatDate } from '@/utils/formatTime';
import { auth } from '@/utils/authFunction';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['selectCitizen']);

// 定义变量内容
const state = reactive<any>({
	dialogVisible: false,
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 10,
		Name: null,
		PhoneNum: null,
	},
	tableData: [],
});

const ruleFormRef = ref<RefType>(null); // 表单ref
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 获取列表
const queryList = () => {
	state.loading = true;
	externalCitizenList(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();
};

let loading = ref<Boolean>(false); // 加载状态
// 打开弹窗
const openDialog = async () => {
	try {
		queryList();
		state.dialogVisible = true;
	} catch (error) {
		console.log(error);
	}
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	ruleFormRef.value?.resetFields();
	ruleFormRef.value?.resetFields();
};
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 onSubmit = () => {
	emit('selectCitizen', checkTable.value);
	closeDialog();
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
