<template>
	<div class="snapshot-config-safe-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="SafetyTypeId">
							<el-select v-model="state.queryParams.SafetyTypeId" class="w100" placeholder="请选择审批状态" clearable @change="handleQuery">
								<el-option v-for="item in statusOptions" :key="item.id" :label="item.name" :value="item.id" />
							</el-select>
						</el-form-item>
						<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>
							<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:safe:add'">
						<SvgIcon name="ele-Plus" class="mr5" />新增安全志愿者
					</el-button>
					<el-button type="danger" @click="onDelete" v-auth="'snapshot:config:safe:delete'" :disabled="isChecked" :loading="state.loading"
						><SvgIcon name="ele-Delete" class="mr5" />删除<span v-if="checkTable.length">({{ checkTable.length }})</span>
					</el-button>
				</template>
				<template #action="{ row }">
					<el-button link type="danger" @click="onDeleteSingle(row)" v-auth="'snapshot:config:safe:delete'"> 删除 </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>
		<safe-add ref="safeAddRef" @updateList="queryList" />
	</div>
</template>

<script lang="tsx" setup name="snapshotConfigSafe">
import { computed, defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { baseSafetyVolunteer, delSafetyVolunteer, getSafetyVolunteerList, getSafetyVolunteerListExport } from '@/api/snapshot/config';
import Other from '@/utils/other';

// 引入组件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const SafeAdd = defineAsyncComponent(() => import('@/views/snapshot/config/safe/components/Safe-add.vue')); // 新增

// 定义变量内容
const state = reactive<any>({
	loading: false,
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 20,
		IndustryName: null, // 行业类型
		Name: null, // 关联人员
		PhoneNumber: null, // 电话号码
		SafetyTypeId:null, // 志愿者类型
	},
	total: 0, // 总条数
});
const requestParams = ref<EmptyObjectType>({});
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',
		},
		tools: [{ toolRender: { name: 'exportCurrent' } }, { toolRender: { name: 'exportAll' } }],
	},
	params: {
		exportMethod: getSafetyVolunteerListExport,
		exportParams: requestParams,
	},
	customConfig: {
		storage: true,
	},
	id: 'snapshotConfigSafe',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [
		{ type: 'checkbox', width: 50, align: 'center' },
		{
			field: 'citizenName',
			title: '关联人员',
		},
		{
			field: 'phoneNumber',
			title: '电话号码',
		},
		{
			field: 'safetyTypeName',
			title: '志愿者类型',
		},
		{ 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;
	requestParams.value = Other.deepClone(state.queryParams);
	getSafetyVolunteerList(requestParams.value)
		.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 safeAddRef = ref<RefType>();
const onAdd = () => {
	safeAddRef.value.openDialog();
};
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) => {
		return {
			citizenId: item.citizenId,
			safetyTypeId: item.safetyTypeId,
		};
	});
	ElMessageBox.confirm(`您确定删除选选中的志愿者名单吗？，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			state.loading = true;
			delSafetyVolunteer({
				items: ids,
			})
				.then(() => {
					ElMessage.success('删除成功');
					queryList();
				})
				.catch(() => {
					state.loading = false;
				});
		})
		.catch(() => {
			state.loading = false;
		});
};
// 删除单个
const onDeleteSingle = (row: any) => {
	ElMessageBox.confirm(`您确定要删除选中的安全志愿者，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			state.loading = true;
			delSafetyVolunteer({
				items: [
					{
						citizenId: row.citizenId,
						safetyTypeId: row.safetyTypeId,
					},
				],
			})
				.then(() => {
					ElMessage.success('操作成功');
					queryList();
				})
				.catch(() => {
					state.loading = false;
				});
		})
		.catch(() => {
			state.loading = false;
		});
};
// 查询基础数据
const statusOptions = ref<EmptyArrayType>([]);
const getStatusOptions = () => {
	// 获取基础数据
	baseSafetyVolunteer().then((res) => {
		statusOptions.value = res.result.safetyTypes ?? [];
	});
};
// 页面加载时
onMounted(() => {
	queryList();
	getStatusOptions();
});
</script>
