<template>
	<div class="sys-workforce-staff-container layout-pd">
		<el-card shadow="never">
			<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent>
				<el-form-item label="关键词" prop="Keyword">
					<el-input v-model="state.queryParams.Keyword" 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>
		</el-card>
		<el-card shadow="never">
			<ProTable
				ref="proTableRef"
				:columns="columns"
				:data="state.tableData"
				@updateTable="queryList"
				:loading="state.loading"
				:total="state.total"
				v-model:page-index="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
			>
				<template #tableHeader="scope">
					<el-button type="primary" @click="onAdd" v-auth="'system:workforce:staff:add'" :loading="state.loading"
						><SvgIcon name="ele-Plus" class="mr5" />选择人员
					</el-button>
					<el-button
						type="danger"
						@click="onDeleteMultiple"
						v-auth="'system:workforce:staff:deleteMultiple'"
						:disabled="!scope.isSelected"
						:loading="state.loading"
						><SvgIcon name="ele-Delete" class="mr5" />删除
					</el-button>
				</template>
				<!-- 表格操作 -->
				<template #operation="{ row }">
					<el-button link type="danger" @click="onDelete(row)" title="删除人员" v-auth="'system:workforce:staff:delete'"> 删除 </el-button>
				</template>
			</ProTable>
		</el-card>
		<!-- 选择用户 -->
		<select-user ref="selectUserRef" @updateList="queryList" />
	</div>
</template>

<script lang="tsx" setup name="systemWorkforceStaff">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { ElMessage, ElMessageBox } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { deleteWorkforce, getWorkforceList } from '@/api/system/workforce';

// 引入组件
const SelectUser = defineAsyncComponent(() => import('@/views/system/workforce/components/Select-user.vue')); // 选择用户

const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ type: 'selection', fixed: 'left', width: 55 },
	{ prop: 'userName', label: '姓名' },
	{
		prop: 'creationTime',
		label: '创建时间',
		width: 170,
		render: (scope: any) => {
			return <span>{formatDate(scope.row.creationTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'operation', label: '操作', fixed: 'right', width: 100, align: 'center' },
]);
// 定义变量内容
const state = reactive({
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 10, // 每页条数
		Keyword: null, // 关键字
	},
	tableData: [], // 列表数据
	loading: false, // 加载
	total: 0, // 总条数
});
const ruleFormRef = ref<FormInstance>(); // 表单ref
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 人员列表 */
const queryList = async () => {
	state.loading = true;
	try {
		const response = await getWorkforceList(state.queryParams);
		state.tableData = response.result?.items ?? [];
		state.total = response.result?.total ?? 0;
		state.loading = false;
	} catch (e) {
		state.loading = false;
		console.log(e);
	}
};

/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 选择人员
const selectUserRef = ref<RefType>();
const onAdd = () => {
	selectUserRef.value.openDialog();
};
// 删除人员
const onDelete = (row: any) => {
	ElMessageBox.confirm(`确定删除【${row.userName}】该人员？`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
	})
		.then(() => {
			deleteWorkforce({ ids: [row.id] }).then(() => {
				queryList();
				ElMessage.success('删除成功');
			});
		})
		.catch(() => {
			console.log('取消删除');
		});
};
const onDeleteMultiple = () => {
	const ids = proTableRef.value.selectedList.map((item: any) => item.id);
	ElMessageBox.confirm(`确定删除选中的${proTableRef.value.selectedList.length}个人员？`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
	})
		.then(() => {
			deleteWorkforce({ ids }).then(() => {
				queryList();
				ElMessage.success('删除成功');
			});
		})
		.catch(() => {
			console.log('取消删除');
		});
};
onMounted(() => {
	queryList();
});
</script>
