/**
* 휴가 관리 공통 함수
* 모든 휴가 관련 페이지에서 사용하는 공통 함수 모음
*/
// 전역 변수
window.VacationCommon = {
workers: [],
vacationTypes: [],
currentUser: null
};
/**
* 작업자 목록 로드
*/
async function loadWorkers() {
try {
const response = await axios.get('/workers?limit=100');
if (response.data.success) {
window.VacationCommon.workers = response.data.data.filter(w => w.employment_status === 'employed');
return window.VacationCommon.workers;
}
} catch (error) {
console.error('작업자 목록 로드 오류:', error);
throw error;
}
}
/**
* 휴가 유형 목록 로드
*/
async function loadVacationTypes() {
try {
const response = await axios.get('/attendance/vacation-types');
if (response.data.success) {
window.VacationCommon.vacationTypes = response.data.data;
return window.VacationCommon.vacationTypes;
}
} catch (error) {
console.error('휴가 유형 로드 오류:', error);
throw error;
}
}
/**
* 현재 사용자 정보 가져오기
*/
function getCurrentUser() {
if (!window.VacationCommon.currentUser) {
window.VacationCommon.currentUser = JSON.parse(localStorage.getItem('user'));
}
return window.VacationCommon.currentUser;
}
/**
* 휴가 신청 목록 렌더링
*/
function renderVacationRequests(requests, containerId, showActions = false, actionType = 'approval') {
const container = document.getElementById(containerId);
if (!requests || requests.length === 0) {
container.innerHTML = `
`;
return;
}
const tableHTML = `
| 작업자 |
휴가 유형 |
시작일 |
종료일 |
일수 |
상태 |
사유 |
${showActions ? '관리 | ' : ''}
${requests.map(request => {
const statusClass = request.status === 'pending' ? 'status-pending' :
request.status === 'approved' ? 'status-approved' : 'status-rejected';
const statusText = request.status === 'pending' ? '대기' :
request.status === 'approved' ? '승인' : '거부';
return `
| ${request.worker_name || '알 수 없음'} |
${request.vacation_type_name || request.type_name || '알 수 없음'} |
${request.start_date} |
${request.end_date} |
${request.days_used}일 |
${statusText}
|
${request.reason || '-'}
|
${showActions ? renderActionButtons(request, actionType) : ''}
`;
}).join('')}
`;
container.innerHTML = tableHTML;
}
/**
* 액션 버튼 렌더링
*/
function renderActionButtons(request, actionType) {
if (actionType === 'approval' && request.status === 'pending') {
return `
|
`;
} else if (actionType === 'delete' && request.status === 'pending') {
return `
|
`;
}
return '- | ';
}
/**
* 휴가 신청 승인
*/
async function approveVacationRequest(requestId) {
if (!confirm('이 휴가 신청을 승인하시겠습니까?')) {
return;
}
try {
const response = await axios.patch(`/vacation-requests/${requestId}/approve`);
if (response.data.success) {
alert('휴가 신청이 승인되었습니다.');
// 페이지 새로고침 이벤트 발생
window.dispatchEvent(new Event('vacation-updated'));
return true;
}
} catch (error) {
console.error('승인 오류:', error);
alert(error.response?.data?.message || '승인 중 오류가 발생했습니다.');
return false;
}
}
/**
* 휴가 신청 거부
*/
async function rejectVacationRequest(requestId) {
const reason = prompt('거부 사유를 입력하세요:');
if (!reason) {
return;
}
try {
const response = await axios.patch(`/vacation-requests/${requestId}/reject`, {
review_note: reason
});
if (response.data.success) {
alert('휴가 신청이 거부되었습니다.');
// 페이지 새로고침 이벤트 발생
window.dispatchEvent(new Event('vacation-updated'));
return true;
}
} catch (error) {
console.error('거부 오류:', error);
alert(error.response?.data?.message || '거부 중 오류가 발생했습니다.');
return false;
}
}
/**
* 휴가 신청 삭제
*/
async function deleteVacationRequest(requestId) {
if (!confirm('이 휴가 신청을 삭제하시겠습니까?')) {
return;
}
try {
const response = await axios.delete(`/vacation-requests/${requestId}`);
if (response.data.success) {
alert('휴가 신청이 삭제되었습니다.');
// 페이지 새로고침 이벤트 발생
window.dispatchEvent(new Event('vacation-updated'));
return true;
}
} catch (error) {
console.error('삭제 오류:', error);
alert(error.response?.data?.message || '삭제 중 오류가 발생했습니다.');
return false;
}
}
/**
* axios 설정 대기
*/
function waitForAxiosConfig() {
return new Promise((resolve) => {
const check = setInterval(() => {
if (axios.defaults.baseURL) {
clearInterval(check);
resolve();
}
}, 50);
setTimeout(() => {
clearInterval(check);
resolve();
}, 5000);
});
}