<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="PhoneNumber">
				<el-input v-model="state.queryParams.PhoneNumber" placeholder="请输入市民姓名" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="联系电话" prop="Label">
				<el-input v-model="state.queryParams.Label" 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"
			:loading="state.loading"
			:total="state.total"
			v-model:page-index="state.queryParams.PageIndex"
			v-model:page-size="state.queryParams.PageSize"
			:tool-button="false"
      @updateTable="queryList"
		>
		</ProTable>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="closeDialog" class="default-button">取 消</el-button>
				<el-button type="primary" @click="onSubmit" :loading="loading" :disabled="!canChoose">确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="tsx">
import { computed, nextTick, onMounted, reactive, ref } from "vue";
import { ElButton, ElInput, ElMessage, FormInstance } from 'element-plus';
import { citizenList } from '@/api/auxiliary/citizen';

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

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

const ruleFormRef = ref<RefType>(null); // 表单ref
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 获取列表
const queryList = () => {
	/*state.loading = true;
	citizenList(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 proTableRef = ref<RefType>(); // 表格ref
const canChoose = computed(() => {
	return proTableRef.value?.selectedList.length;
});
// 表格配置项
const columns = ref<any[]>([
	{ type: 'selection', fixed: 'left', width: 55, align: 'center' },
	{ prop: 'name', label: '市民姓名' },
	{ prop: 'phoneNumber', label: '联系电话' },
]);

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