<template>
	<div class="sys-workforce-number-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:number:add'" :loading="state.loading"
						><SvgIcon name="ele-Plus" class="mr5" />新增班次
					</el-button>
					<el-button
						type="danger"
						@click="onDeleteMultiple"
						v-auth="'system:workforce:number:deleteMultiple'"
						:disabled="!scope.isSelected"
						:loading="state.loading"
						><SvgIcon name="ele-Delete" class="mr5" />删除
					</el-button>
				</template>
				<!-- 表格操作 -->
				<template #operation="{ row }">
					<el-button link type="primary" @click="onEdit(row)" title="编辑班次" v-auth="'system:workforce:number:edit'"> 编辑 </el-button>
					<el-button link type="danger" @click="onDelete(row)" title="查看日志明细" v-auth="'system:workforce:number:delete'"> 删除 </el-button>
				</template>
			</ProTable>
		</el-card>
		<!-- 新增修改班次 -->
		<number-edit ref="numberEditRef" @updateList="queryList" />
	</div>
</template>

<script lang="tsx" setup name="systemWorkforceNumber">
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 { getWorkforceClassList, deleteWorkforceClass } from '@/api/system/workforce';

// 引入组件
const NumberEdit = defineAsyncComponent(() => import('@/views/system/workforce/components/Number-edit.vue')); // 新增修改班次

const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ type: 'selection', fixed: 'left', width: 55 },
	{ prop: 'name', label: '班次名称' },
	{
		prop: 'workingTime',
		label: '上班时间',
		width: 200,
	},
	{
		prop: 'offDutyTime',
		label: '下班时间',
		width: 200,
	},
	{
		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: 160, 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 getWorkforceClassList(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 numberEditRef = ref<RefType>();
const onAdd = () => {
	numberEditRef.value.openDialog();
};
// 编辑班次
const onEdit = (row: any) => {
	numberEditRef.value.openDialog(row.id);
};
// 删除班次
const onDelete = (row: any) => {
	ElMessageBox.confirm(`确定删除【${row.name}】该班次？若当前班次已排班，删除后该班次将替换为休息日！是否确定删除？`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
	})
		.then(() => {
			deleteWorkforceClass({ 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(() => {
			deleteWorkforceClass({ ids }).then(() => {
				queryList();
				ElMessage.success('删除成功');
			});
		})
		.catch(() => {
			console.log('取消删除');
		});
};
onMounted(() => {
	queryList();
});
</script>
