<template>
	<el-dialog title="新增企业专员" v-model="state.dialogVisible" draggable @close="close" destroy-on-close>
		<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent :disabled="state.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>
				<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" :loading="state.loading">
					<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="{ isHover: true, height: 30, useKey: true }"
			ref="tableRef"
			max-height="500px"
			show-overflow
			:scrollY="{ enabled: true, gt: 100 }"
			:checkbox-config="{ highlight: true }"
			@checkbox-all="selectAllChangeEvent"
			@checkbox-change="selectChangeEvent"
		>
			<vxe-column type="checkbox" fixed="left" width="50"></vxe-column>
			<vxe-column field="name" title="用户名称"></vxe-column>
      <vxe-column field="userName" title="账号"></vxe-column>
			<vxe-column field="phoneNo" title="手机号"></vxe-column>
		</vxe-table>
		<pagination
			@pagination="queryList"
			:total="state.total"
			v-model:current-page="state.queryParams.PageIndex"
			v-model:page-size="state.queryParams.PageSize"
			:disabled="state.loading"
		/>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="closeDialog" class="default-button">取 消</el-button>
				<el-button type="primary" @click="onSubmit" :loading="state.loading" :disabled="isChecked">确 定 </el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { computed, defineAsyncComponent, reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { batchAddEnterprises, getSelectUserList } from '@/api/system/enterprises';

const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

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

// 定义变量内容
const state = reactive<any>({
	dialogVisible: false, // 弹窗显示隐藏
	queryParams: {
		name: null,
		PageIndex: 1,
		PageSize: 10,
	},
	total: 0,
	ruleForm: {
		orderBy: 0,
		eventName: null, // 事件名称
		parentId: null, // 上级区域id
	},
	loading: false, // 确定按钮loading
	tableData: [],
});
// 打开弹窗
const parentId = ref('');
const openDialog = async (id: string) => {
	state.dialogVisible = true;
	if (id) parentId.value = id;
	queryList();
};
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
const ruleFormRef = ref<RefType>();
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 获取可选择的用户列表
const queryList = () => {
	state.loading = true;
	getSelectUserList(state.queryParams)
		.then((res) => {
			state.tableData = res.result.items ?? [];
			state.total = res.result.total ?? 0;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
};
const close = () => {
	parentId.value = '';
};
const checkTable = ref<EmptyArrayType>([]);
const tableRef = ref<RefType>();
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 !checkTable.value.length;
});
// 新增
const onSubmit = () => {
	state.loading = true;
	const result = checkTable.value.map((item) => {
		return {
			parentId: parentId.value,
			id: item.id,
		};
	});
	batchAddEnterprises(result)
		.then(() => {
			emit('updateList');
			closeDialog(); // 关闭弹窗
			ElMessage.success('操作成功');
			state.loading = false;
		})
		.catch(() => {
			emit('updateList');
			state.loading = false;
			closeDialog();
		});
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
