<template>
	<div class="snapshot-config-area-user-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<vxe-grid v-bind="gridOptions" ref="gridRef" @checkbox-all="selectAllChangeEvent" @checkbox-change="selectChangeEvent">
				<template #form>
					<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent :disabled="gridOptions.loading">
						<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="PhoneNumber">
							<el-input
								v-model="state.queryParams.PhoneNumber"
								placeholder="请填写联系方式"
								clearable
								@keyup.enter="handleQuery"
								class="keyword-input"
							/>
						</el-form-item>
						<el-form-item label="所属区县" prop="SystemAreaName">
							<el-input
								v-model="state.queryParams.SystemAreaName"
								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>
				</template>
				<template #toolbar_buttons>
					<el-button type="primary" @click="onAdd" v-auth="'snapshot:config:areaUser:add'">
						<SvgIcon name="ele-Plus" class="mr5" />新增人员
					</el-button>
					<el-button type="danger" @click="onDelete" v-auth="'snapshot:config:areaUser:delete'" :disabled="isChecked" :loading="state.loading"
						><SvgIcon name="ele-Delete" class="mr5" />删除<span v-if="checkTable.length">({{ checkTable.length }})</span>
					</el-button>
					<el-button type="primary" @click="onImport" v-auth="'snapshot:config:areaUser:import'">
						<SvgIcon name="ele-Upload" class="mr5" />导入
					</el-button>
				</template>
				<template #action="{ row }">
					<el-button link type="primary" @click="onEdit(row)" v-auth="'snapshot:config:areaUser:edit'" title="编辑人员"> 编辑 </el-button>
				</template>
				<template #pager>
					<pagination
						@pagination="queryList"
						:total="state.total"
						v-model:current-page="state.queryParams.PageIndex"
						v-model:page-size="state.queryParams.PageSize"
						:disabled="state.loading"
					/>
				</template>
			</vxe-grid>
		</div>
		<!--  新增用户  -->
		<user-add ref="userAddRef" @updateList="queryList" />
		<!--  编辑用户  -->
		<user-edit ref="userEditRef" @updateList="queryList" />
		<import-user ref="importUserRef" @updateList="queryList" />
	</div>
</template>

<script lang="tsx" setup name="snapshotConfigAreaUser">
import { computed, defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { delAreaUser, getAreaUserList } from '@/api/snapshot/config';

// 引入组件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const UserAdd = defineAsyncComponent(() => import('@/views/snapshot/config/areaUser/components/User-add.vue')); // 新增用户
const UserEdit = defineAsyncComponent(() => import('@/views/snapshot/config/areaUser/components/User-edit.vue')); // 编辑用户
const ImportUser = defineAsyncComponent(() => import('@/views/snapshot/config/areaUser/components/Import-user.vue')); // 导入

// 定义变量内容
const state = reactive<any>({
	loading: false,
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 20,
		Name: null, // 姓名
		PhoneNumber: null, // 联系方式
		SystemAreaName: null, // 所属区县
	},
	total: 0, // 总条数
});

const gridOptions = reactive<any>({
	loading: false,
	border: true,
	showOverflow: true,
	columnConfig: {
		resizable: true,
	},
	scrollY: {
		enabled: true,
		gt: 100,
	},
	toolbarConfig: {
		zoom: true,
		custom: true,
		refresh: {
			queryMethod: () => {
				handleQuery();
			},
		},
		slots: {
			buttons: 'toolbar_buttons',
		},
	},
	customConfig: {
		storage: true,
	},
	id: 'snapshotConfigAreaUser',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [
		{ type: 'checkbox', width: 50, align: 'center' },
		{
			field: 'name',
			title: '姓名',
		},
		{
			field: 'phoneNumber',
			title: '联系方式',
		},
		{
			field: 'genderTxt',
			title: '性别',
		},
		{
			field: 'systemAreaName',
			title: '所属区县',
		},
		{
			field: 'street',
			title: '所属镇/街',
			minWidth: 200,
		},
		{ title: '操作', width: 90, fixed: 'right', align: 'center', slots: { default: 'action' } },
	],
	data: [],
});
const ruleFormRef = ref<any>(null); // 表单ref
/** 搜索按钮操作 节流操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 获取参数列表
const queryList = () => {
	state.loading = true;
	gridOptions.loading = true;
	getAreaUserList(state.queryParams)
		.then((res) => {
			state.loading = false;
			gridOptions.data = res.result.items ?? [];
			state.total = res.result.total ?? 0;
			gridOptions.loading = false;
			gridRef.value.clearCheckboxRow();
			checkTable.value = [];
		})
		.finally(() => {
			state.loading = false;
			gridOptions.loading = false;
			gridRef.value.clearCheckboxRow();
			checkTable.value = [];
		});
};
// 重置表单
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 新增
const userAddRef = ref<RefType>();
const onAdd = () => {
	userAddRef.value.openDialog();
};
// 编辑
const userEditRef = ref<RefType>();
const onEdit = (row: any) => {
	userEditRef.value.openDialog(row.id);
};
const checkTable = ref<EmptyArrayType>([]);
const gridRef = ref<RefType>();
const selectAllChangeEvent = ({ checked }) => {
	if (gridRef.value) {
		const records = gridRef.value.getCheckboxRecords();
		checkTable.value = records;
		console.log(checked ? '所有勾选事件' : '所有取消事件', records);
	}
};

const selectChangeEvent = ({ checked }) => {
	if (gridRef.value) {
		const records = gridRef.value.getCheckboxRecords();
		checkTable.value = records;
		console.log(checked ? '勾选事件' : '取消事件', records);
	}
};
const isChecked = computed(() => {
	return !Boolean(checkTable.value.length);
});
// 删除
const onDelete = () => {
	const ids = checkTable.value.map((item: any) => item.id);
	ElMessageBox.confirm(`您确定删除选选中的从业人员吗？，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			state.loading = true;
			delAreaUser(ids)
				.then(() => {
					ElMessage.success('删除成功');
					queryList();
				})
				.catch(() => {
					state.loading = false;
				});
		})
		.catch(() => {
			state.loading = false;
		});
};
// 导入
const importUserRef = ref<RefType>();
const onImport = () => {
	importUserRef.value.openDialog();
};
// 页面加载时
onMounted(() => {
	queryList();
});
</script>
