refactor: 코드 분리 + 성능 최적화 + 모바일 개선
tkqc 5개 페이지 인라인 JS/CSS를 외부 파일로 추출 (HTML 82% 감소) tkuser index.html을 CSS 1개 + JS 10개 모듈로 분리 (3283→1155줄) - 공통 유틸 추출: issue-helpers, photo-modal, toast - 공통 CSS 확장: tkqc-common.css (모바일 반응형 포함) - 모바일 하단 네비게이션 추가 (mobile-bottom-nav.js) - nginx: JS/CSS 1시간 캐싱 + gzip 압축 활성화 - Tailwind CDN preload, 캐시버스터 통일 (?v=20260213) - 카메라 capture="environment" 추가 - tkuser Dockerfile에 static/ 디렉토리 복사 추가 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
72
user-management/web/static/js/tkuser-core.js
Normal file
72
user-management/web/static/js/tkuser-core.js
Normal file
@@ -0,0 +1,72 @@
|
||||
/* ===== Config ===== */
|
||||
const API_BASE = '/api';
|
||||
|
||||
/* ===== Token ===== */
|
||||
function _cookieGet(n) { const m = document.cookie.match(new RegExp('(?:^|; )' + n + '=([^;]*)')); return m ? decodeURIComponent(m[1]) : null; }
|
||||
function _cookieRemove(n) { let c = n + '=; path=/; max-age=0'; if (location.hostname.includes('technicalkorea.net')) c += '; domain=.technicalkorea.net'; document.cookie = c; }
|
||||
function getToken() { return _cookieGet('sso_token') || localStorage.getItem('sso_token') || localStorage.getItem('access_token'); }
|
||||
function getLoginUrl() {
|
||||
const h = location.hostname;
|
||||
if (h.includes('technicalkorea.net')) return location.protocol + '//tkfb.technicalkorea.net/login?redirect=' + encodeURIComponent(location.href);
|
||||
return location.protocol + '//' + h + ':30000/login?redirect=' + encodeURIComponent(location.href);
|
||||
}
|
||||
function decodeToken(t) { try { return JSON.parse(atob(t.split('.')[1].replace(/-/g,'+').replace(/_/g,'/'))); } catch { return null; } }
|
||||
|
||||
/* ===== API ===== */
|
||||
async function api(path, opts = {}) {
|
||||
const token = getToken();
|
||||
const res = await fetch(API_BASE + path, { ...opts, headers: { 'Content-Type': 'application/json', 'Authorization': token ? `Bearer ${token}` : '', ...(opts.headers||{}) } });
|
||||
if (res.status === 401) { location.href = getLoginUrl(); throw new Error('인증 만료'); }
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error || data.detail || '요청 실패');
|
||||
return data;
|
||||
}
|
||||
|
||||
/* ===== Toast ===== */
|
||||
function showToast(msg, type = 'success') {
|
||||
document.querySelector('.toast-message')?.remove();
|
||||
const el = document.createElement('div');
|
||||
el.className = `toast-message fixed bottom-4 right-4 px-4 py-3 rounded-lg text-white z-[10000] shadow-lg ${type==='success'?'bg-emerald-500':'bg-red-500'}`;
|
||||
el.innerHTML = `<i class="fas ${type==='success'?'fa-check-circle':'fa-exclamation-circle'} mr-2"></i>${msg}`;
|
||||
document.body.appendChild(el);
|
||||
setTimeout(() => { el.classList.add('opacity-0'); setTimeout(() => el.remove(), 300); }, 3000);
|
||||
}
|
||||
|
||||
/* ===== Helpers ===== */
|
||||
const DEPT = { production:'생산', quality:'품질', purchasing:'구매', design:'설계', sales:'영업' };
|
||||
function deptLabel(d) { return DEPT[d] || d || ''; }
|
||||
function formatDate(d) { if (!d) return ''; return d.substring(0, 10); }
|
||||
function escHtml(s) { if (!s) return ''; const d = document.createElement('div'); d.textContent = s; return d.innerHTML; }
|
||||
|
||||
/* ===== Logout ===== */
|
||||
function doLogout() {
|
||||
if (!confirm('로그아웃?')) return;
|
||||
_cookieRemove('sso_token'); localStorage.removeItem('sso_token'); localStorage.removeItem('access_token'); localStorage.removeItem('currentUser');
|
||||
location.href = getLoginUrl();
|
||||
}
|
||||
|
||||
/* ===== State ===== */
|
||||
let currentUser = null;
|
||||
|
||||
/* ===== Init ===== */
|
||||
async function init() {
|
||||
const token = getToken();
|
||||
if (!token) { location.href = getLoginUrl(); return; }
|
||||
const decoded = decodeToken(token);
|
||||
if (!decoded) { location.href = getLoginUrl(); return; }
|
||||
|
||||
currentUser = { id: decoded.user_id||decoded.id, username: decoded.username||decoded.sub, name: decoded.name||decoded.full_name, role: decoded.role||decoded.access_level };
|
||||
const dn = currentUser.name || currentUser.username;
|
||||
document.getElementById('headerUserName').textContent = dn;
|
||||
document.getElementById('headerUserRole').textContent = currentUser.role === 'admin' ? '관리자' : '사용자';
|
||||
document.getElementById('headerUserAvatar').textContent = dn.charAt(0).toUpperCase();
|
||||
|
||||
if (currentUser.role === 'admin') {
|
||||
document.getElementById('tabNav').classList.remove('hidden');
|
||||
document.getElementById('adminSection').classList.remove('hidden');
|
||||
await loadUsers();
|
||||
} else {
|
||||
document.getElementById('passwordChangeSection').classList.remove('hidden');
|
||||
}
|
||||
setTimeout(() => document.querySelector('.fade-in').classList.add('visible'), 50);
|
||||
}
|
||||
91
user-management/web/static/js/tkuser-departments.js
Normal file
91
user-management/web/static/js/tkuser-departments.js
Normal file
@@ -0,0 +1,91 @@
|
||||
/* ===== Departments CRUD ===== */
|
||||
let departments = [], departmentsLoaded = false;
|
||||
|
||||
async function loadDepartments() {
|
||||
try {
|
||||
const r = await api('/departments'); departments = r.data || r;
|
||||
departmentsLoaded = true;
|
||||
populateParentDeptSelects();
|
||||
displayDepartments();
|
||||
} catch (err) {
|
||||
document.getElementById('departmentList').innerHTML = `<div class="text-red-500 text-center py-6"><i class="fas fa-exclamation-triangle text-xl"></i><p class="text-sm mt-2">${err.message}</p></div>`;
|
||||
}
|
||||
}
|
||||
|
||||
function populateParentDeptSelects() {
|
||||
['newDeptParent','editDeptParent'].forEach(id => {
|
||||
const sel = document.getElementById(id); if (!sel) return;
|
||||
const val = sel.value;
|
||||
sel.innerHTML = '<option value="">없음 (최상위)</option>';
|
||||
departments.filter(d => d.is_active !== 0 && d.is_active !== false).forEach(d => {
|
||||
const o = document.createElement('option'); o.value = d.department_id; o.textContent = d.department_name; sel.appendChild(o);
|
||||
});
|
||||
sel.value = val;
|
||||
});
|
||||
}
|
||||
|
||||
function displayDepartments() {
|
||||
const c = document.getElementById('departmentList');
|
||||
if (!departments.length) { c.innerHTML = '<p class="text-gray-400 text-center py-4 text-sm">등록된 부서가 없습니다.</p>'; return; }
|
||||
c.innerHTML = departments.map(d => `
|
||||
<div class="flex items-center justify-between p-2.5 bg-gray-50 rounded-lg hover:bg-gray-100 transition-colors">
|
||||
<div class="flex-1 min-w-0">
|
||||
<div class="text-sm font-medium text-gray-800 truncate"><i class="fas fa-sitemap mr-1.5 text-gray-400 text-xs"></i>${d.department_name}</div>
|
||||
<div class="text-xs text-gray-500 flex items-center gap-1.5 mt-0.5 flex-wrap">
|
||||
${d.parent_name ? `<span class="px-1.5 py-0.5 rounded bg-slate-50 text-slate-500">상위: ${d.parent_name}</span>` : '<span class="px-1.5 py-0.5 rounded bg-indigo-50 text-indigo-500">최상위</span>'}
|
||||
<span class="text-gray-400">순서: ${d.display_order || 0}</span>
|
||||
${d.is_active === 0 || d.is_active === false ? '<span class="px-1.5 py-0.5 rounded bg-gray-100 text-gray-400">비활성</span>' : '<span class="px-1.5 py-0.5 rounded bg-emerald-50 text-emerald-600">활성</span>'}
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex gap-1 ml-2 flex-shrink-0">
|
||||
<button onclick="editDepartment(${d.department_id})" class="p-1.5 text-slate-500 hover:text-slate-700 hover:bg-slate-200 rounded" title="편집"><i class="fas fa-pen-to-square text-xs"></i></button>
|
||||
${d.is_active !== 0 && d.is_active !== false ? `<button onclick="deactivateDepartment(${d.department_id},'${(d.department_name||'').replace(/'/g,"\\'")}')" class="p-1.5 text-red-400 hover:text-red-600 hover:bg-red-100 rounded" title="비활성화"><i class="fas fa-ban text-xs"></i></button>` : ''}
|
||||
</div>
|
||||
</div>`).join('');
|
||||
}
|
||||
|
||||
document.getElementById('addDepartmentForm').addEventListener('submit', async e => {
|
||||
e.preventDefault();
|
||||
try {
|
||||
await api('/departments', { method: 'POST', body: JSON.stringify({
|
||||
department_name: document.getElementById('newDeptName').value.trim(),
|
||||
parent_id: document.getElementById('newDeptParent').value ? parseInt(document.getElementById('newDeptParent').value) : null,
|
||||
description: document.getElementById('newDeptDescription').value.trim() || null,
|
||||
display_order: parseInt(document.getElementById('newDeptOrder').value) || 0
|
||||
})});
|
||||
showToast('부서가 추가되었습니다.'); document.getElementById('addDepartmentForm').reset(); await loadDepartments();
|
||||
} catch(e) { showToast(e.message, 'error'); }
|
||||
});
|
||||
|
||||
function editDepartment(id) {
|
||||
const d = departments.find(x => x.department_id === id); if (!d) return;
|
||||
document.getElementById('editDeptId').value = d.department_id;
|
||||
document.getElementById('editDeptName').value = d.department_name;
|
||||
document.getElementById('editDeptDescription').value = d.description || '';
|
||||
document.getElementById('editDeptOrder').value = d.display_order || 0;
|
||||
document.getElementById('editDeptActive').value = (d.is_active === 0 || d.is_active === false) ? '0' : '1';
|
||||
populateParentDeptSelects();
|
||||
document.getElementById('editDeptParent').value = d.parent_id || '';
|
||||
document.getElementById('editDepartmentModal').classList.remove('hidden');
|
||||
}
|
||||
function closeDepartmentModal() { document.getElementById('editDepartmentModal').classList.add('hidden'); }
|
||||
|
||||
document.getElementById('editDepartmentForm').addEventListener('submit', async e => {
|
||||
e.preventDefault();
|
||||
try {
|
||||
await api(`/departments/${document.getElementById('editDeptId').value}`, { method: 'PUT', body: JSON.stringify({
|
||||
department_name: document.getElementById('editDeptName').value.trim(),
|
||||
parent_id: document.getElementById('editDeptParent').value ? parseInt(document.getElementById('editDeptParent').value) : null,
|
||||
description: document.getElementById('editDeptDescription').value.trim() || null,
|
||||
display_order: parseInt(document.getElementById('editDeptOrder').value) || 0,
|
||||
is_active: document.getElementById('editDeptActive').value === '1'
|
||||
})});
|
||||
showToast('수정되었습니다.'); closeDepartmentModal(); await loadDepartments();
|
||||
await loadDepartmentsForSelect();
|
||||
} catch(e) { showToast(e.message, 'error'); }
|
||||
});
|
||||
|
||||
async function deactivateDepartment(id, name) {
|
||||
if (!confirm(`"${name}" 부서를 비활성화?`)) return;
|
||||
try { await api(`/departments/${id}`, { method: 'DELETE' }); showToast('부서 비활성화 완료'); await loadDepartments(); } catch(e) { showToast(e.message, 'error'); }
|
||||
}
|
||||
5
user-management/web/static/js/tkuser-issue-types.js
Normal file
5
user-management/web/static/js/tkuser-issue-types.js
Normal file
@@ -0,0 +1,5 @@
|
||||
/* ===== Issue Types ===== */
|
||||
/* Placeholder module for issue type CRUD operations.
|
||||
This file is reserved for future issue category management functionality.
|
||||
Currently, issue types are managed through System 3 permissions in tkuser-users.js.
|
||||
*/
|
||||
315
user-management/web/static/js/tkuser-layout-map.js
Normal file
315
user-management/web/static/js/tkuser-layout-map.js
Normal file
@@ -0,0 +1,315 @@
|
||||
/* ===== Layout Map (구역지도) ===== */
|
||||
let layoutMapImage = null;
|
||||
let mapRegions = [];
|
||||
let mapCanvas = null;
|
||||
let mapCtx = null;
|
||||
let isDrawing = false;
|
||||
let drawStartX = 0;
|
||||
let drawStartY = 0;
|
||||
let currentRect = null;
|
||||
let selectedMapCategoryId = null;
|
||||
|
||||
// 구역지도 프리뷰 캔버스 클릭 -> 해당 영역의 작업장으로 드릴다운
|
||||
document.getElementById('previewCanvas')?.addEventListener('click', function(e) {
|
||||
if (!previewMapRegions.length) return;
|
||||
const rect = this.getBoundingClientRect();
|
||||
const xPct = ((e.clientX - rect.left) / rect.width) * 100;
|
||||
const yPct = ((e.clientY - rect.top) / rect.height) * 100;
|
||||
for (const region of previewMapRegions) {
|
||||
if (xPct >= region.x_start && xPct <= region.x_end && yPct >= region.y_start && yPct <= region.y_end) {
|
||||
const wp = workplaces.find(w => w.workplace_id === region.workplace_id);
|
||||
if (wp) {
|
||||
selectWorkplaceForEquipments(wp.workplace_id, wp.workplace_name);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
async function loadLayoutPreview(categoryId) {
|
||||
const cat = workplaceCategories.find(c => c.category_id == categoryId);
|
||||
if (!cat || !cat.layout_image) {
|
||||
document.getElementById('layoutPreviewArea').classList.remove('hidden');
|
||||
document.getElementById('layoutPreviewCanvas').classList.add('hidden');
|
||||
document.getElementById('layoutPreviewArea').innerHTML = '<i class="fas fa-image text-3xl mb-2"></i><p>레이아웃 이미지가 없습니다. "지도 설정"에서 업로드하세요.</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
document.getElementById('layoutPreviewArea').classList.add('hidden');
|
||||
document.getElementById('layoutPreviewCanvas').classList.remove('hidden');
|
||||
|
||||
const pCanvas = document.getElementById('previewCanvas');
|
||||
const pCtx = pCanvas.getContext('2d');
|
||||
|
||||
const imgUrl = cat.layout_image.startsWith('http') ? cat.layout_image : '/uploads/' + cat.layout_image.replace(/^\/uploads\//, '');
|
||||
|
||||
const img = new Image();
|
||||
img.onload = async function() {
|
||||
const maxW = 800;
|
||||
const scale = img.width > maxW ? maxW / img.width : 1;
|
||||
pCanvas.width = img.width * scale;
|
||||
pCanvas.height = img.height * scale;
|
||||
pCtx.drawImage(img, 0, 0, pCanvas.width, pCanvas.height);
|
||||
|
||||
try {
|
||||
const r = await api(`/workplaces/categories/${categoryId}/map-regions`);
|
||||
const regions = r.data || [];
|
||||
previewMapRegions = regions;
|
||||
regions.forEach(region => {
|
||||
const x1 = (region.x_start / 100) * pCanvas.width;
|
||||
const y1 = (region.y_start / 100) * pCanvas.height;
|
||||
const x2 = (region.x_end / 100) * pCanvas.width;
|
||||
const y2 = (region.y_end / 100) * pCanvas.height;
|
||||
pCtx.strokeStyle = '#10b981';
|
||||
pCtx.lineWidth = 2;
|
||||
pCtx.strokeRect(x1, y1, x2 - x1, y2 - y1);
|
||||
pCtx.fillStyle = 'rgba(16, 185, 129, 0.15)';
|
||||
pCtx.fillRect(x1, y1, x2 - x1, y2 - y1);
|
||||
pCtx.fillStyle = '#10b981';
|
||||
pCtx.font = '14px sans-serif';
|
||||
pCtx.fillText(region.workplace_name || '', x1 + 5, y1 + 20);
|
||||
});
|
||||
} catch(e) { console.warn('영역 로드 실패:', e); }
|
||||
};
|
||||
img.src = imgUrl;
|
||||
}
|
||||
|
||||
// 구역지도 모달
|
||||
function openLayoutMapModal() {
|
||||
if (!selectedMapCategoryId) {
|
||||
showToast('공장을 먼저 선택해주세요.', 'error');
|
||||
return;
|
||||
}
|
||||
const modal = document.getElementById('layoutMapModal');
|
||||
mapCanvas = document.getElementById('regionCanvas');
|
||||
mapCtx = mapCanvas.getContext('2d');
|
||||
modal.style.display = 'flex';
|
||||
document.body.style.overflow = 'hidden';
|
||||
loadLayoutMapData();
|
||||
updateRegionWorkplaceSelect();
|
||||
}
|
||||
|
||||
function closeLayoutMapModal() {
|
||||
const modal = document.getElementById('layoutMapModal');
|
||||
modal.style.display = 'none';
|
||||
document.body.style.overflow = '';
|
||||
if (mapCanvas) {
|
||||
mapCanvas.removeEventListener('mousedown', onCanvasMouseDown);
|
||||
mapCanvas.removeEventListener('mousemove', onCanvasMouseMove);
|
||||
mapCanvas.removeEventListener('mouseup', onCanvasMouseUp);
|
||||
mapCanvas.removeEventListener('mouseleave', onCanvasMouseUp);
|
||||
}
|
||||
currentRect = null;
|
||||
if (selectedMapCategoryId) loadLayoutPreview(selectedMapCategoryId);
|
||||
}
|
||||
|
||||
async function loadLayoutMapData() {
|
||||
try {
|
||||
const cat = workplaceCategories.find(c => c.category_id == selectedMapCategoryId);
|
||||
if (!cat) return;
|
||||
|
||||
const imgDiv = document.getElementById('currentLayoutImage');
|
||||
if (cat.layout_image) {
|
||||
const imgUrl = cat.layout_image.startsWith('http') ? cat.layout_image : '/uploads/' + cat.layout_image.replace(/^\/uploads\//, '');
|
||||
imgDiv.innerHTML = `<img src="${imgUrl}" style="max-width:100%;max-height:300px;border-radius:4px;" alt="레이아웃">`;
|
||||
loadImageToCanvas(imgUrl);
|
||||
} else {
|
||||
imgDiv.innerHTML = '<span class="text-sm text-gray-400">업로드된 이미지가 없습니다</span>';
|
||||
}
|
||||
|
||||
const r = await api(`/workplaces/categories/${selectedMapCategoryId}/map-regions`);
|
||||
mapRegions = r.data || [];
|
||||
renderRegionList();
|
||||
} catch(e) {
|
||||
console.error('레이아웃 데이터 로딩 오류:', e);
|
||||
}
|
||||
}
|
||||
|
||||
function loadImageToCanvas(imgUrl) {
|
||||
const img = new Image();
|
||||
img.onload = function() {
|
||||
const maxW = 800;
|
||||
const scale = img.width > maxW ? maxW / img.width : 1;
|
||||
mapCanvas.width = img.width * scale;
|
||||
mapCanvas.height = img.height * scale;
|
||||
mapCtx.drawImage(img, 0, 0, mapCanvas.width, mapCanvas.height);
|
||||
layoutMapImage = img;
|
||||
drawExistingRegions();
|
||||
setupCanvasEvents();
|
||||
};
|
||||
img.src = imgUrl;
|
||||
}
|
||||
|
||||
function updateRegionWorkplaceSelect() {
|
||||
const sel = document.getElementById('regionWorkplaceSelect');
|
||||
if (!sel) return;
|
||||
const catWps = workplaces.filter(w => w.category_id == selectedMapCategoryId);
|
||||
let html = '<option value="">작업장을 선택하세요</option>';
|
||||
catWps.forEach(wp => {
|
||||
const hasRegion = mapRegions.some(r => r.workplace_id === wp.workplace_id);
|
||||
html += `<option value="${wp.workplace_id}">${wp.workplace_name}${hasRegion ? ' (영역 정의됨)' : ''}</option>`;
|
||||
});
|
||||
sel.innerHTML = html;
|
||||
}
|
||||
|
||||
function previewLayoutImage(event) {
|
||||
const file = event.target.files[0];
|
||||
if (!file) return;
|
||||
const reader = new FileReader();
|
||||
reader.onload = function(e) {
|
||||
document.getElementById('currentLayoutImage').innerHTML = `
|
||||
<img src="${e.target.result}" style="max-width:100%;max-height:300px;border-radius:4px;" alt="미리보기">
|
||||
<p class="text-xs text-gray-400 mt-1">미리보기 (저장하려면 "이미지 업로드" 버튼 클릭)</p>`;
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
}
|
||||
|
||||
async function uploadLayoutImage() {
|
||||
const file = document.getElementById('layoutImageFile').files[0];
|
||||
if (!file) { showToast('이미지를 선택해주세요.', 'error'); return; }
|
||||
if (!selectedMapCategoryId) { showToast('공장을 먼저 선택해주세요.', 'error'); return; }
|
||||
|
||||
try {
|
||||
const fd = new FormData();
|
||||
fd.append('image', file);
|
||||
const token = getToken();
|
||||
const res = await fetch(`${API_BASE}/workplaces/categories/${selectedMapCategoryId}/layout-image`, {
|
||||
method: 'POST',
|
||||
headers: { 'Authorization': token ? `Bearer ${token}` : '' },
|
||||
body: fd
|
||||
});
|
||||
const result = await res.json();
|
||||
if (!res.ok) throw new Error(result.error || '업로드 실패');
|
||||
|
||||
showToast('이미지가 업로드되었습니다.');
|
||||
const imgUrl = '/uploads/' + result.data.image_path.replace(/^\/uploads\//, '');
|
||||
document.getElementById('currentLayoutImage').innerHTML = `<img src="${imgUrl}" style="max-width:100%;max-height:300px;border-radius:4px;" alt="레이아웃">`;
|
||||
loadImageToCanvas(imgUrl);
|
||||
|
||||
// 카테고리 데이터 갱신
|
||||
await loadWorkplaceCategories();
|
||||
} catch(e) {
|
||||
showToast(e.message || '업로드 실패', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
// 캔버스 드로잉
|
||||
function setupCanvasEvents() {
|
||||
mapCanvas.removeEventListener('mousedown', onCanvasMouseDown);
|
||||
mapCanvas.removeEventListener('mousemove', onCanvasMouseMove);
|
||||
mapCanvas.removeEventListener('mouseup', onCanvasMouseUp);
|
||||
mapCanvas.removeEventListener('mouseleave', onCanvasMouseUp);
|
||||
mapCanvas.addEventListener('mousedown', onCanvasMouseDown);
|
||||
mapCanvas.addEventListener('mousemove', onCanvasMouseMove);
|
||||
mapCanvas.addEventListener('mouseup', onCanvasMouseUp);
|
||||
mapCanvas.addEventListener('mouseleave', onCanvasMouseUp);
|
||||
}
|
||||
|
||||
function onCanvasMouseDown(e) {
|
||||
const r = mapCanvas.getBoundingClientRect();
|
||||
const scaleX = mapCanvas.width / r.width;
|
||||
const scaleY = mapCanvas.height / r.height;
|
||||
drawStartX = (e.clientX - r.left) * scaleX;
|
||||
drawStartY = (e.clientY - r.top) * scaleY;
|
||||
isDrawing = true;
|
||||
}
|
||||
|
||||
function onCanvasMouseMove(e) {
|
||||
if (!isDrawing) return;
|
||||
const r = mapCanvas.getBoundingClientRect();
|
||||
const scaleX = mapCanvas.width / r.width;
|
||||
const scaleY = mapCanvas.height / r.height;
|
||||
const curX = (e.clientX - r.left) * scaleX;
|
||||
const curY = (e.clientY - r.top) * scaleY;
|
||||
|
||||
mapCtx.clearRect(0, 0, mapCanvas.width, mapCanvas.height);
|
||||
if (layoutMapImage) mapCtx.drawImage(layoutMapImage, 0, 0, mapCanvas.width, mapCanvas.height);
|
||||
drawExistingRegions();
|
||||
|
||||
const w = curX - drawStartX;
|
||||
const h = curY - drawStartY;
|
||||
mapCtx.strokeStyle = '#3b82f6';
|
||||
mapCtx.lineWidth = 3;
|
||||
mapCtx.strokeRect(drawStartX, drawStartY, w, h);
|
||||
mapCtx.fillStyle = 'rgba(59, 130, 246, 0.2)';
|
||||
mapCtx.fillRect(drawStartX, drawStartY, w, h);
|
||||
|
||||
currentRect = { startX: drawStartX, startY: drawStartY, endX: curX, endY: curY };
|
||||
}
|
||||
|
||||
function onCanvasMouseUp() { isDrawing = false; }
|
||||
|
||||
function drawExistingRegions() {
|
||||
mapRegions.forEach(region => {
|
||||
const x1 = (region.x_start / 100) * mapCanvas.width;
|
||||
const y1 = (region.y_start / 100) * mapCanvas.height;
|
||||
const x2 = (region.x_end / 100) * mapCanvas.width;
|
||||
const y2 = (region.y_end / 100) * mapCanvas.height;
|
||||
mapCtx.strokeStyle = '#10b981';
|
||||
mapCtx.lineWidth = 2;
|
||||
mapCtx.strokeRect(x1, y1, x2 - x1, y2 - y1);
|
||||
mapCtx.fillStyle = 'rgba(16, 185, 129, 0.15)';
|
||||
mapCtx.fillRect(x1, y1, x2 - x1, y2 - y1);
|
||||
mapCtx.fillStyle = '#10b981';
|
||||
mapCtx.font = '14px sans-serif';
|
||||
mapCtx.fillText(region.workplace_name || '', x1 + 5, y1 + 20);
|
||||
});
|
||||
}
|
||||
|
||||
function clearCurrentRegion() {
|
||||
currentRect = null;
|
||||
mapCtx.clearRect(0, 0, mapCanvas.width, mapCanvas.height);
|
||||
if (layoutMapImage) mapCtx.drawImage(layoutMapImage, 0, 0, mapCanvas.width, mapCanvas.height);
|
||||
drawExistingRegions();
|
||||
}
|
||||
|
||||
async function saveRegion() {
|
||||
const wpId = document.getElementById('regionWorkplaceSelect').value;
|
||||
if (!wpId) { showToast('작업장을 선택해주세요.', 'error'); return; }
|
||||
if (!currentRect) { showToast('영역을 그려주세요.', 'error'); return; }
|
||||
|
||||
try {
|
||||
const xStart = (Math.min(currentRect.startX, currentRect.endX) / mapCanvas.width * 100).toFixed(2);
|
||||
const yStart = (Math.min(currentRect.startY, currentRect.endY) / mapCanvas.height * 100).toFixed(2);
|
||||
const xEnd = (Math.max(currentRect.startX, currentRect.endX) / mapCanvas.width * 100).toFixed(2);
|
||||
const yEnd = (Math.max(currentRect.startY, currentRect.endY) / mapCanvas.height * 100).toFixed(2);
|
||||
|
||||
const existing = mapRegions.find(r => r.workplace_id == wpId);
|
||||
const body = { workplace_id: parseInt(wpId), category_id: selectedMapCategoryId, x_start: xStart, y_start: yStart, x_end: xEnd, y_end: yEnd, shape: 'rect' };
|
||||
|
||||
if (existing) {
|
||||
await api(`/workplaces/map-regions/${existing.region_id}`, { method: 'PUT', body: JSON.stringify(body) });
|
||||
} else {
|
||||
await api('/workplaces/map-regions', { method: 'POST', body: JSON.stringify(body) });
|
||||
}
|
||||
|
||||
showToast('영역이 저장되었습니다.');
|
||||
await loadLayoutMapData();
|
||||
updateRegionWorkplaceSelect();
|
||||
clearCurrentRegion();
|
||||
document.getElementById('regionWorkplaceSelect').value = '';
|
||||
} catch(e) { showToast(e.message || '저장 실패', 'error'); }
|
||||
}
|
||||
|
||||
function renderRegionList() {
|
||||
const div = document.getElementById('regionList');
|
||||
if (!mapRegions.length) { div.innerHTML = '<p class="text-sm text-gray-400 text-center py-4">정의된 영역이 없습니다</p>'; return; }
|
||||
div.innerHTML = '<div class="space-y-2">' + mapRegions.map(r => `
|
||||
<div class="flex items-center justify-between p-2.5 bg-gray-50 rounded-lg">
|
||||
<div>
|
||||
<span class="text-sm font-semibold text-gray-800">${r.workplace_name || ''}</span>
|
||||
<span class="text-xs text-gray-400 ml-2">(${Number(r.x_start).toFixed(1)}%, ${Number(r.y_start).toFixed(1)}%) ~ (${Number(r.x_end).toFixed(1)}%, ${Number(r.y_end).toFixed(1)}%)</span>
|
||||
</div>
|
||||
<button onclick="deleteRegion(${r.region_id})" class="p-1.5 text-red-400 hover:text-red-600 hover:bg-red-100 rounded text-xs"><i class="fas fa-trash-alt"></i> 삭제</button>
|
||||
</div>`).join('') + '</div>';
|
||||
}
|
||||
|
||||
async function deleteRegion(regionId) {
|
||||
if (!confirm('이 영역을 삭제하시겠습니까?')) return;
|
||||
try {
|
||||
await api(`/workplaces/map-regions/${regionId}`, { method: 'DELETE' });
|
||||
showToast('영역이 삭제되었습니다.');
|
||||
await loadLayoutMapData();
|
||||
updateRegionWorkplaceSelect();
|
||||
} catch(e) { showToast(e.message || '삭제 실패', 'error'); }
|
||||
}
|
||||
92
user-management/web/static/js/tkuser-projects.js
Normal file
92
user-management/web/static/js/tkuser-projects.js
Normal file
@@ -0,0 +1,92 @@
|
||||
/* ===== Projects CRUD ===== */
|
||||
let projects = [], projectsLoaded = false;
|
||||
|
||||
function statusBadge(status, isActive) {
|
||||
if (!isActive || isActive === 0 || isActive === false) return '<span class="px-1.5 py-0.5 rounded text-xs bg-gray-100 text-gray-400">비활성</span>';
|
||||
if (status === 'completed') return '<span class="px-1.5 py-0.5 rounded text-xs bg-blue-50 text-blue-600">완료</span>';
|
||||
return '<span class="px-1.5 py-0.5 rounded text-xs bg-emerald-50 text-emerald-600">진행중</span>';
|
||||
}
|
||||
|
||||
async function loadProjects() {
|
||||
try {
|
||||
const r = await api('/projects'); projects = r.data || r;
|
||||
projectsLoaded = true;
|
||||
displayProjects();
|
||||
} catch (err) {
|
||||
document.getElementById('projectList').innerHTML = `<div class="text-red-500 text-center py-6"><i class="fas fa-exclamation-triangle text-xl"></i><p class="text-sm mt-2">${err.message}</p></div>`;
|
||||
}
|
||||
}
|
||||
|
||||
function displayProjects() {
|
||||
const c = document.getElementById('projectList');
|
||||
if (!projects.length) { c.innerHTML = '<p class="text-gray-400 text-center py-4 text-sm">등록된 프로젝트가 없습니다.</p>'; return; }
|
||||
c.innerHTML = projects.map(p => `
|
||||
<div class="flex items-center justify-between p-2.5 bg-gray-50 rounded-lg hover:bg-gray-100 transition-colors">
|
||||
<div class="flex-1 min-w-0">
|
||||
<div class="text-sm font-medium text-gray-800 truncate"><i class="fas fa-folder mr-1.5 text-gray-400 text-xs"></i>${p.project_name}</div>
|
||||
<div class="text-xs text-gray-500 flex items-center gap-1.5 mt-0.5 flex-wrap">
|
||||
<span class="font-mono">${p.job_no}</span>
|
||||
${p.site?`<span class="px-1.5 py-0.5 rounded bg-amber-50 text-amber-600">${p.site}</span>`:''}
|
||||
${p.pm?`<span class="px-1.5 py-0.5 rounded bg-slate-50 text-slate-500">${p.pm}</span>`:''}
|
||||
${statusBadge(p.project_status, p.is_active)}
|
||||
${p.due_date?`<span class="text-gray-400">${formatDate(p.due_date)}</span>`:''}
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex gap-1 ml-2 flex-shrink-0">
|
||||
<button onclick="editProject(${p.project_id})" class="p-1.5 text-slate-500 hover:text-slate-700 hover:bg-slate-200 rounded" title="편집"><i class="fas fa-pen-to-square text-xs"></i></button>
|
||||
${p.is_active?`<button onclick="deactivateProject(${p.project_id},'${p.project_name.replace(/'/g,"\\'")}')" class="p-1.5 text-red-400 hover:text-red-600 hover:bg-red-100 rounded" title="비활성화"><i class="fas fa-ban text-xs"></i></button>`:''}
|
||||
</div>
|
||||
</div>`).join('');
|
||||
}
|
||||
|
||||
document.getElementById('addProjectForm').addEventListener('submit', async e => {
|
||||
e.preventDefault();
|
||||
try {
|
||||
await api('/projects', { method: 'POST', body: JSON.stringify({
|
||||
job_no: document.getElementById('newJobNo').value.trim(),
|
||||
project_name: document.getElementById('newProjectName').value.trim(),
|
||||
contract_date: document.getElementById('newContractDate').value || null,
|
||||
due_date: document.getElementById('newDueDate').value || null,
|
||||
site: document.getElementById('newSite').value.trim() || null,
|
||||
pm: document.getElementById('newPm').value.trim() || null
|
||||
})});
|
||||
showToast('프로젝트가 추가되었습니다.'); document.getElementById('addProjectForm').reset(); await loadProjects();
|
||||
} catch(e) { showToast(e.message, 'error'); }
|
||||
});
|
||||
|
||||
function editProject(id) {
|
||||
const p = projects.find(x => x.project_id === id); if (!p) return;
|
||||
document.getElementById('editProjectId').value = p.project_id;
|
||||
document.getElementById('editJobNo').value = p.job_no;
|
||||
document.getElementById('editProjectName').value = p.project_name;
|
||||
document.getElementById('editContractDate').value = formatDate(p.contract_date);
|
||||
document.getElementById('editDueDate').value = formatDate(p.due_date);
|
||||
document.getElementById('editSite').value = p.site || '';
|
||||
document.getElementById('editPm').value = p.pm || '';
|
||||
document.getElementById('editProjectStatus').value = p.project_status || 'active';
|
||||
document.getElementById('editIsActive').value = p.is_active ? '1' : '0';
|
||||
document.getElementById('editProjectModal').classList.remove('hidden');
|
||||
}
|
||||
function closeProjectModal() { document.getElementById('editProjectModal').classList.add('hidden'); }
|
||||
|
||||
document.getElementById('editProjectForm').addEventListener('submit', async e => {
|
||||
e.preventDefault();
|
||||
try {
|
||||
await api(`/projects/${document.getElementById('editProjectId').value}`, { method: 'PUT', body: JSON.stringify({
|
||||
job_no: document.getElementById('editJobNo').value.trim(),
|
||||
project_name: document.getElementById('editProjectName').value.trim(),
|
||||
contract_date: document.getElementById('editContractDate').value || null,
|
||||
due_date: document.getElementById('editDueDate').value || null,
|
||||
site: document.getElementById('editSite').value.trim() || null,
|
||||
pm: document.getElementById('editPm').value.trim() || null,
|
||||
project_status: document.getElementById('editProjectStatus').value,
|
||||
is_active: document.getElementById('editIsActive').value === '1'
|
||||
})});
|
||||
showToast('수정되었습니다.'); closeProjectModal(); await loadProjects();
|
||||
} catch(e) { showToast(e.message, 'error'); }
|
||||
});
|
||||
|
||||
async function deactivateProject(id, name) {
|
||||
if (!confirm(`"${name}" 프로젝트를 비활성화?`)) return;
|
||||
try { await api(`/projects/${id}`, { method: 'DELETE' }); showToast('프로젝트 비활성화 완료'); await loadProjects(); } catch(e) { showToast(e.message, 'error'); }
|
||||
}
|
||||
24
user-management/web/static/js/tkuser-tabs.js
Normal file
24
user-management/web/static/js/tkuser-tabs.js
Normal file
@@ -0,0 +1,24 @@
|
||||
/* ===== Tab ===== */
|
||||
function switchTab(name) {
|
||||
document.querySelectorAll('[id^="tab-"]').forEach(el => el.classList.add('hidden'));
|
||||
document.getElementById('tab-' + name)?.classList.remove('hidden');
|
||||
document.querySelectorAll('.tab-btn').forEach(btn => btn.classList.remove('active'));
|
||||
event.currentTarget.classList.add('active');
|
||||
// 사이드바 레이아웃 탭에서 main/nav/header 너비 확장
|
||||
const mainEl = document.querySelector('main');
|
||||
const navInner = document.getElementById('tabNavInner');
|
||||
const headerInner = document.getElementById('headerInner');
|
||||
const wideClass = 'max-w-[1600px]';
|
||||
const defaultClass = 'max-w-7xl';
|
||||
if (name === 'workplaces' || name === 'tasks' || name === 'vacations') {
|
||||
[mainEl, navInner, headerInner].forEach(el => { el.classList.remove(defaultClass); el.classList.add(wideClass); });
|
||||
} else {
|
||||
[mainEl, navInner, headerInner].forEach(el => { el.classList.remove(wideClass); el.classList.add(defaultClass); });
|
||||
}
|
||||
if (name === 'projects' && !projectsLoaded) loadProjects();
|
||||
if (name === 'workers' && !workersLoaded) loadWorkers();
|
||||
if (name === 'departments' && !departmentsLoaded) loadDepartments();
|
||||
if (name === 'workplaces' && !workplacesLoaded) loadWorkplaces();
|
||||
if (name === 'tasks' && !tasksLoaded) loadTasksTab();
|
||||
if (name === 'vacations' && !vacationsLoaded) loadVacationsTab();
|
||||
}
|
||||
218
user-management/web/static/js/tkuser-tasks.js
Normal file
218
user-management/web/static/js/tkuser-tasks.js
Normal file
@@ -0,0 +1,218 @@
|
||||
/* ===== Tasks CRUD ===== */
|
||||
let taskWorkTypes = [], allTasks = [], tasksLoaded = false;
|
||||
let selectedTaskWorkTypeFilter = null;
|
||||
|
||||
async function loadTasksTab() {
|
||||
await loadWorkTypes();
|
||||
await loadTasks();
|
||||
tasksLoaded = true;
|
||||
}
|
||||
|
||||
async function loadWorkTypes() {
|
||||
try {
|
||||
const r = await api('/tasks/work-types');
|
||||
taskWorkTypes = r.data || [];
|
||||
renderWorkTypeSidebar();
|
||||
populateTaskWorkTypeSelect();
|
||||
} catch(e) { console.warn('공정 로드 실패:', e); }
|
||||
}
|
||||
|
||||
function renderWorkTypeSidebar() {
|
||||
const c = document.getElementById('workTypeSidebar');
|
||||
if (!c) return;
|
||||
let html = `<div onclick="filterTasksByWorkType(null)" class="flex items-center justify-between p-2 rounded-lg cursor-pointer transition-colors ${selectedTaskWorkTypeFilter === null ? 'bg-blue-50 ring-1 ring-blue-200' : 'hover:bg-gray-50'}">
|
||||
<span class="text-sm font-medium ${selectedTaskWorkTypeFilter === null ? 'text-blue-700' : 'text-gray-700'}">전체</span>
|
||||
<span class="text-xs text-gray-400">${allTasks.length}</span>
|
||||
</div>`;
|
||||
// 카테고리별 그룹핑
|
||||
const grouped = {};
|
||||
taskWorkTypes.forEach(wt => {
|
||||
const cat = wt.category || '미분류';
|
||||
if (!grouped[cat]) grouped[cat] = [];
|
||||
grouped[cat].push(wt);
|
||||
});
|
||||
Object.keys(grouped).sort().forEach(cat => {
|
||||
html += `<div class="text-[10px] font-semibold text-gray-400 uppercase tracking-wider mt-3 mb-1 px-2">${cat}</div>`;
|
||||
grouped[cat].forEach(wt => {
|
||||
const count = allTasks.filter(t => t.work_type_id === wt.id).length;
|
||||
const isActive = selectedTaskWorkTypeFilter === wt.id;
|
||||
html += `<div onclick="filterTasksByWorkType(${wt.id})" class="group flex items-center justify-between p-2 rounded-lg cursor-pointer transition-colors ${isActive ? 'bg-blue-50 ring-1 ring-blue-200' : 'hover:bg-gray-50'}">
|
||||
<span class="text-sm ${isActive ? 'font-medium text-blue-700' : 'text-gray-700'} truncate">${wt.name}</span>
|
||||
<div class="flex items-center gap-1">
|
||||
<span class="text-xs text-gray-400">${count}</span>
|
||||
<button onclick="event.stopPropagation(); editWorkType(${wt.id})" class="p-0.5 text-gray-300 hover:text-slate-600 opacity-0 group-hover:opacity-100 transition-opacity" title="수정"><i class="fas fa-pen text-[10px]"></i></button>
|
||||
</div>
|
||||
</div>`;
|
||||
});
|
||||
});
|
||||
// 미지정 작업 수
|
||||
const noType = allTasks.filter(t => !t.work_type_id).length;
|
||||
if (noType > 0) {
|
||||
html += `<div onclick="filterTasksByWorkType(0)" class="flex items-center justify-between p-2 rounded-lg cursor-pointer transition-colors mt-2 ${selectedTaskWorkTypeFilter === 0 ? 'bg-blue-50 ring-1 ring-blue-200' : 'hover:bg-gray-50'}">
|
||||
<span class="text-sm text-gray-400 italic">미지정</span>
|
||||
<span class="text-xs text-gray-400">${noType}</span>
|
||||
</div>`;
|
||||
}
|
||||
c.innerHTML = html;
|
||||
}
|
||||
|
||||
function populateTaskWorkTypeSelect() {
|
||||
const sel = document.getElementById('taskWorkType');
|
||||
if (!sel) return;
|
||||
const val = sel.value;
|
||||
sel.innerHTML = '<option value="">미지정</option>';
|
||||
taskWorkTypes.forEach(wt => {
|
||||
sel.innerHTML += `<option value="${wt.id}">${wt.category ? wt.category + ' > ' : ''}${wt.name}</option>`;
|
||||
});
|
||||
sel.value = val;
|
||||
}
|
||||
|
||||
function filterTasksByWorkType(wtId) {
|
||||
selectedTaskWorkTypeFilter = wtId;
|
||||
renderWorkTypeSidebar();
|
||||
displayTasks();
|
||||
}
|
||||
|
||||
async function loadTasks() {
|
||||
try {
|
||||
const r = await api('/tasks');
|
||||
allTasks = r.data || [];
|
||||
renderWorkTypeSidebar();
|
||||
displayTasks();
|
||||
} catch(e) {
|
||||
document.getElementById('taskList').innerHTML = `<div class="text-red-500 text-center py-6"><i class="fas fa-exclamation-triangle text-xl"></i><p class="text-sm mt-2">${e.message}</p></div>`;
|
||||
}
|
||||
}
|
||||
|
||||
function displayTasks() {
|
||||
const c = document.getElementById('taskList');
|
||||
let filtered = allTasks;
|
||||
let label = '전체';
|
||||
if (selectedTaskWorkTypeFilter === 0) {
|
||||
filtered = allTasks.filter(t => !t.work_type_id);
|
||||
label = '미지정';
|
||||
} else if (selectedTaskWorkTypeFilter) {
|
||||
filtered = allTasks.filter(t => t.work_type_id === selectedTaskWorkTypeFilter);
|
||||
const wt = taskWorkTypes.find(w => w.id === selectedTaskWorkTypeFilter);
|
||||
label = wt ? wt.name : '';
|
||||
}
|
||||
document.getElementById('taskFilterLabel').textContent = `- ${label}`;
|
||||
const active = filtered.filter(t => t.is_active).length;
|
||||
const inactive = filtered.length - active;
|
||||
document.getElementById('taskStats').textContent = `활성 ${active} / 비활성 ${inactive}`;
|
||||
|
||||
if (!filtered.length) { c.innerHTML = '<p class="text-gray-400 text-center py-8 text-sm">등록된 작업이 없습니다.</p>'; return; }
|
||||
c.innerHTML = filtered.map(t => `
|
||||
<div class="flex items-center justify-between p-2.5 bg-gray-50 rounded-lg hover:bg-gray-100 transition-colors">
|
||||
<div class="flex-1 min-w-0">
|
||||
<div class="text-sm font-medium text-gray-800 truncate">${escHtml(t.task_name)}</div>
|
||||
<div class="text-xs text-gray-500 flex items-center gap-1.5 mt-0.5 flex-wrap">
|
||||
${t.work_type_name ? `<span class="px-1.5 py-0.5 rounded bg-slate-50 text-slate-600">${escHtml(t.work_type_name)}</span>` : '<span class="text-gray-300 italic">미지정</span>'}
|
||||
${t.description ? `<span class="text-gray-400 truncate max-w-[200px]" title="${escHtml(t.description)}">${escHtml(t.description)}</span>` : ''}
|
||||
${t.is_active ? '<span class="px-1.5 py-0.5 rounded bg-emerald-50 text-emerald-600">활성</span>' : '<span class="px-1.5 py-0.5 rounded bg-gray-100 text-gray-400">비활성</span>'}
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex gap-1 ml-2 flex-shrink-0">
|
||||
<button onclick="editTask(${t.task_id})" class="p-1.5 text-slate-500 hover:text-slate-700 hover:bg-slate-200 rounded" title="편집"><i class="fas fa-pen-to-square text-xs"></i></button>
|
||||
<button onclick="deleteTask(${t.task_id},'${escHtml(t.task_name).replace(/'/g,"\\'")}')" class="p-1.5 text-red-400 hover:text-red-600 hover:bg-red-100 rounded" title="삭제"><i class="fas fa-trash-alt text-xs"></i></button>
|
||||
</div>
|
||||
</div>`).join('');
|
||||
}
|
||||
|
||||
// 공정 모달
|
||||
function openWorkTypeModal(editId) {
|
||||
document.getElementById('wtEditId').value = '';
|
||||
document.getElementById('workTypeForm').reset();
|
||||
document.getElementById('workTypeModalTitle').textContent = '공정 추가';
|
||||
if (editId) {
|
||||
const wt = taskWorkTypes.find(w => w.id === editId);
|
||||
if (!wt) return;
|
||||
document.getElementById('workTypeModalTitle').textContent = '공정 수정';
|
||||
document.getElementById('wtEditId').value = wt.id;
|
||||
document.getElementById('wtName').value = wt.name || '';
|
||||
document.getElementById('wtCategory').value = wt.category || '';
|
||||
document.getElementById('wtDesc').value = wt.description || '';
|
||||
}
|
||||
document.getElementById('workTypeModal').classList.remove('hidden');
|
||||
}
|
||||
function closeWorkTypeModal() { document.getElementById('workTypeModal').classList.add('hidden'); }
|
||||
function editWorkType(id) { openWorkTypeModal(id); }
|
||||
|
||||
document.getElementById('workTypeForm').addEventListener('submit', async e => {
|
||||
e.preventDefault();
|
||||
const editId = document.getElementById('wtEditId').value;
|
||||
const body = {
|
||||
name: document.getElementById('wtName').value.trim(),
|
||||
category: document.getElementById('wtCategory').value.trim() || null,
|
||||
description: document.getElementById('wtDesc').value.trim() || null
|
||||
};
|
||||
try {
|
||||
if (editId) {
|
||||
await api(`/tasks/work-types/${editId}`, { method: 'PUT', body: JSON.stringify(body) });
|
||||
showToast('공정이 수정되었습니다.');
|
||||
} else {
|
||||
await api('/tasks/work-types', { method: 'POST', body: JSON.stringify(body) });
|
||||
showToast('공정이 추가되었습니다.');
|
||||
}
|
||||
closeWorkTypeModal();
|
||||
await loadWorkTypes();
|
||||
await loadTasks();
|
||||
} catch(e) { showToast(e.message, 'error'); }
|
||||
});
|
||||
|
||||
// 작업 모달
|
||||
function openTaskModal(editId) {
|
||||
document.getElementById('taskEditId').value = '';
|
||||
document.getElementById('taskForm').reset();
|
||||
document.getElementById('taskActive').checked = true;
|
||||
document.getElementById('taskModalTitle').textContent = '작업 추가';
|
||||
populateTaskWorkTypeSelect();
|
||||
// 사이드바 필터 선택된 공정 자동 선택
|
||||
if (!editId && selectedTaskWorkTypeFilter && selectedTaskWorkTypeFilter !== 0) {
|
||||
document.getElementById('taskWorkType').value = selectedTaskWorkTypeFilter;
|
||||
}
|
||||
if (editId) {
|
||||
const t = allTasks.find(x => x.task_id === editId);
|
||||
if (!t) return;
|
||||
document.getElementById('taskModalTitle').textContent = '작업 수정';
|
||||
document.getElementById('taskEditId').value = t.task_id;
|
||||
document.getElementById('taskName').value = t.task_name || '';
|
||||
document.getElementById('taskWorkType').value = t.work_type_id || '';
|
||||
document.getElementById('taskDesc').value = t.description || '';
|
||||
document.getElementById('taskActive').checked = !!t.is_active;
|
||||
}
|
||||
document.getElementById('taskModal').classList.remove('hidden');
|
||||
}
|
||||
function closeTaskModal() { document.getElementById('taskModal').classList.add('hidden'); }
|
||||
function editTask(id) { openTaskModal(id); }
|
||||
|
||||
document.getElementById('taskForm').addEventListener('submit', async e => {
|
||||
e.preventDefault();
|
||||
const editId = document.getElementById('taskEditId').value;
|
||||
const body = {
|
||||
task_name: document.getElementById('taskName').value.trim(),
|
||||
work_type_id: document.getElementById('taskWorkType').value ? parseInt(document.getElementById('taskWorkType').value) : null,
|
||||
description: document.getElementById('taskDesc').value.trim() || null,
|
||||
is_active: document.getElementById('taskActive').checked ? 1 : 0
|
||||
};
|
||||
try {
|
||||
if (editId) {
|
||||
await api(`/tasks/${editId}`, { method: 'PUT', body: JSON.stringify(body) });
|
||||
showToast('작업이 수정되었습니다.');
|
||||
} else {
|
||||
await api('/tasks', { method: 'POST', body: JSON.stringify(body) });
|
||||
showToast('작업이 추가되었습니다.');
|
||||
}
|
||||
closeTaskModal();
|
||||
await loadTasks();
|
||||
} catch(e) { showToast(e.message, 'error'); }
|
||||
});
|
||||
|
||||
async function deleteTask(id, name) {
|
||||
if (!confirm(`"${name}" 작업을 삭제하시겠습니까?`)) return;
|
||||
try {
|
||||
await api(`/tasks/${id}`, { method: 'DELETE' });
|
||||
showToast('작업이 삭제되었습니다.');
|
||||
await loadTasks();
|
||||
} catch(e) { showToast(e.message, 'error'); }
|
||||
}
|
||||
385
user-management/web/static/js/tkuser-users.js
Normal file
385
user-management/web/static/js/tkuser-users.js
Normal file
@@ -0,0 +1,385 @@
|
||||
/* ===== Permission Page Definitions ===== */
|
||||
const SYSTEM1_PAGES = {
|
||||
'작업 관리': [
|
||||
{ key: 's1.dashboard', title: '대시보드', icon: 'fa-chart-line', def: true },
|
||||
{ key: 's1.work.tbm', title: 'TBM 관리', icon: 'fa-hard-hat', def: true },
|
||||
{ key: 's1.work.report_create', title: '작업보고서 작성', icon: 'fa-file-pen', def: true },
|
||||
{ key: 's1.work.analysis', title: '작업 분석', icon: 'fa-magnifying-glass-chart', def: false },
|
||||
{ key: 's1.work.nonconformity', title: '부적합 현황', icon: 'fa-triangle-exclamation', def: true },
|
||||
],
|
||||
'공장 관리': [
|
||||
{ key: 's1.factory.repair_management', title: '시설설비 관리', icon: 'fa-wrench', def: false },
|
||||
{ key: 's1.inspection.daily_patrol', title: '일일순회점검', icon: 'fa-clipboard-check', def: false },
|
||||
{ key: 's1.inspection.checkin', title: '출근 체크', icon: 'fa-fingerprint', def: true },
|
||||
{ key: 's1.inspection.work_status', title: '근무 현황', icon: 'fa-user-clock', def: false },
|
||||
],
|
||||
'안전 관리': [
|
||||
{ key: 's1.safety.visit_request', title: '출입 신청', icon: 'fa-id-badge', def: true },
|
||||
{ key: 's1.safety.management', title: '안전 관리', icon: 'fa-fire-extinguisher', def: false },
|
||||
{ key: 's1.safety.checklist_manage', title: '체크리스트 관리', icon: 'fa-list-check', def: false },
|
||||
],
|
||||
'근태 관리': [
|
||||
{ key: 's1.attendance.my_vacation_info', title: '내 연차 정보', icon: 'fa-umbrella-beach', def: true },
|
||||
{ key: 's1.attendance.monthly', title: '월간 근태', icon: 'fa-calendar-days', def: true },
|
||||
{ key: 's1.attendance.vacation_request', title: '휴가 신청', icon: 'fa-paper-plane', def: true },
|
||||
{ key: 's1.attendance.vacation_management', title: '휴가 관리', icon: 'fa-calendar-check', def: false },
|
||||
{ key: 's1.attendance.vacation_allocation', title: '휴가 발생 입력', icon: 'fa-calendar-plus', def: false },
|
||||
{ key: 's1.attendance.annual_overview', title: '연간 휴가 현황', icon: 'fa-chart-pie', def: false },
|
||||
],
|
||||
'시스템 관리': [
|
||||
{ key: 's1.admin.workers', title: '작업자 관리', icon: 'fa-people-group', def: false },
|
||||
{ key: 's1.admin.projects', title: '프로젝트 관리', icon: 'fa-folder-open', def: false },
|
||||
{ key: 's1.admin.tasks', title: '작업 관리', icon: 'fa-list-check', def: false },
|
||||
{ key: 's1.admin.workplaces', title: '작업장 관리', icon: 'fa-warehouse', def: false },
|
||||
{ key: 's1.admin.equipments', title: '설비 관리', icon: 'fa-gears', def: false },
|
||||
{ key: 's1.admin.issue_categories', title: '신고 카테고리', icon: 'fa-tags', def: false },
|
||||
{ key: 's1.admin.attendance_report', title: '출퇴근-보고서 대조', icon: 'fa-scale-balanced', def: false },
|
||||
]
|
||||
};
|
||||
|
||||
const SYSTEM3_PAGES = {
|
||||
'메인': [
|
||||
{ key: 'issues_dashboard', title: '현황판', icon: 'fa-chart-line', def: true },
|
||||
{ key: 'issues_inbox', title: '수신함', icon: 'fa-inbox', def: true },
|
||||
{ key: 'issues_management', title: '관리함', icon: 'fa-cog', def: false },
|
||||
{ key: 'issues_archive', title: '폐기함', icon: 'fa-archive', def: false },
|
||||
],
|
||||
'업무': [
|
||||
{ key: 'daily_work', title: '일일 공수', icon: 'fa-calendar-check', def: false },
|
||||
{ key: 'projects_manage', title: '프로젝트 관리', icon: 'fa-folder-open', def: false },
|
||||
],
|
||||
'보고서': [
|
||||
{ key: 'reports', title: '보고서', icon: 'fa-chart-bar', def: false },
|
||||
{ key: 'reports_daily', title: '일일보고서', icon: 'fa-file-excel', def: false },
|
||||
{ key: 'reports_weekly', title: '주간보고서', icon: 'fa-calendar-week', def: false },
|
||||
{ key: 'reports_monthly', title: '월간보고서', icon: 'fa-calendar-alt', def: false },
|
||||
]
|
||||
};
|
||||
|
||||
/* ===== Users State ===== */
|
||||
let users = [], selectedUserId = null, currentPermissions = {};
|
||||
|
||||
/* ===== Users CRUD ===== */
|
||||
async function loadUsers() {
|
||||
try {
|
||||
const r = await api('/users'); users = r.data || r;
|
||||
displayUsers(); updatePermissionUserSelect();
|
||||
} catch (err) {
|
||||
document.getElementById('userList').innerHTML = `<div class="text-red-500 text-center py-6"><i class="fas fa-exclamation-triangle text-xl"></i><p class="text-sm mt-2">${err.message}</p></div>`;
|
||||
}
|
||||
}
|
||||
function displayUsers() {
|
||||
const c = document.getElementById('userList');
|
||||
if (!users.length) { c.innerHTML = '<p class="text-gray-400 text-center py-4 text-sm">등록된 사용자가 없습니다.</p>'; return; }
|
||||
c.innerHTML = users.map(u => `
|
||||
<div class="flex items-center justify-between p-2.5 bg-gray-50 rounded-lg hover:bg-gray-100 transition-colors">
|
||||
<div class="flex-1 min-w-0">
|
||||
<div class="text-sm font-medium text-gray-800 truncate"><i class="fas fa-user mr-1.5 text-gray-400 text-xs"></i>${u.name||u.username}</div>
|
||||
<div class="text-xs text-gray-500 flex items-center gap-1.5 mt-0.5 flex-wrap">
|
||||
<span>${u.username}</span>
|
||||
${u.department?`<span class="px-1.5 py-0.5 rounded bg-green-50 text-green-600">${deptLabel(u.department)}</span>`:''}
|
||||
<span class="px-1.5 py-0.5 rounded ${u.role==='admin'?'bg-red-50 text-red-600':'bg-slate-50 text-slate-500'}">${u.role==='admin'?'관리자':'사용자'}</span>
|
||||
${u.is_active===0||u.is_active===false?'<span class="px-1.5 py-0.5 rounded bg-gray-100 text-gray-400">비활성</span>':''}
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex gap-1 ml-2 flex-shrink-0">
|
||||
<button onclick="editUser(${u.user_id})" class="p-1.5 text-slate-500 hover:text-slate-700 hover:bg-slate-200 rounded" title="편집"><i class="fas fa-pen-to-square text-xs"></i></button>
|
||||
<button onclick="resetPassword(${u.user_id},'${u.username}')" class="p-1.5 text-amber-500 hover:text-amber-700 hover:bg-amber-100 rounded" title="비밀번호 초기화"><i class="fas fa-key text-xs"></i></button>
|
||||
${u.username!=='hyungi'?`<button onclick="deleteUser(${u.user_id},'${u.username}')" class="p-1.5 text-red-400 hover:text-red-600 hover:bg-red-100 rounded" title="삭제"><i class="fas fa-trash text-xs"></i></button>`:''}
|
||||
</div>
|
||||
</div>`).join('');
|
||||
}
|
||||
|
||||
document.getElementById('addUserForm').addEventListener('submit', async e => {
|
||||
e.preventDefault();
|
||||
try {
|
||||
await api('/users', { method:'POST', body: JSON.stringify({ username: document.getElementById('newUsername').value.trim(), name: document.getElementById('newFullName').value.trim(), password: document.getElementById('newPassword').value, department: document.getElementById('newDepartment').value||null, role: document.getElementById('newRole').value }) });
|
||||
showToast('사용자가 추가되었습니다.'); document.getElementById('addUserForm').reset(); await loadUsers();
|
||||
} catch(e) { showToast(e.message,'error'); }
|
||||
});
|
||||
|
||||
function editUser(id) {
|
||||
const u = users.find(x=>x.user_id===id); if(!u) return;
|
||||
document.getElementById('editUserId').value=u.user_id; document.getElementById('editUsername').value=u.username;
|
||||
document.getElementById('editFullName').value=u.name||''; document.getElementById('editDepartment').value=u.department||''; document.getElementById('editRole').value=u.role;
|
||||
document.getElementById('editUserModal').classList.remove('hidden');
|
||||
}
|
||||
function closeEditModal() { document.getElementById('editUserModal').classList.add('hidden'); }
|
||||
|
||||
document.getElementById('editUserForm').addEventListener('submit', async e => {
|
||||
e.preventDefault();
|
||||
try {
|
||||
await api(`/users/${document.getElementById('editUserId').value}`, { method:'PUT', body: JSON.stringify({ name: document.getElementById('editFullName').value.trim()||null, department: document.getElementById('editDepartment').value||null, role: document.getElementById('editRole').value }) });
|
||||
showToast('수정되었습니다.'); closeEditModal(); await loadUsers();
|
||||
} catch(e) { showToast(e.message,'error'); }
|
||||
});
|
||||
|
||||
async function resetPassword(id, name) {
|
||||
if (!confirm(`${name}의 비밀번호를 "000000"으로 초기화?`)) return;
|
||||
try { await api(`/users/${id}/reset-password`,{method:'POST',body:JSON.stringify({new_password:'000000'})}); showToast(`${name} 비밀번호 초기화 완료`); } catch(e) { showToast(e.message,'error'); }
|
||||
}
|
||||
async function deleteUser(id, name) {
|
||||
if (!confirm(`${name}을(를) 비활성화?`)) return;
|
||||
try { await api(`/users/${id}`,{method:'DELETE'}); showToast('비활성화 완료'); await loadUsers(); } catch(e) { showToast(e.message,'error'); }
|
||||
}
|
||||
|
||||
document.getElementById('changePasswordForm').addEventListener('submit', async e => {
|
||||
e.preventDefault();
|
||||
const np = document.getElementById('newPasswordChange').value;
|
||||
if (np !== document.getElementById('confirmPassword').value) { showToast('비밀번호 불일치','error'); return; }
|
||||
try {
|
||||
await api('/users/change-password',{method:'POST',body:JSON.stringify({current_password:document.getElementById('currentPassword').value,new_password:np})});
|
||||
showToast('비밀번호 변경 완료'); document.getElementById('changePasswordForm').reset();
|
||||
} catch(e) { showToast(e.message,'error'); }
|
||||
});
|
||||
|
||||
/* ===== Permissions ===== */
|
||||
function updatePermissionUserSelect() {
|
||||
const sel = document.getElementById('permissionUserSelect');
|
||||
sel.innerHTML = '<option value="">사용자 선택</option>';
|
||||
users.filter(u=>u.role==='user').forEach(u => { const o=document.createElement('option'); o.value=u.user_id; o.textContent=`${u.name||u.username} (${u.username})`; sel.appendChild(o); });
|
||||
}
|
||||
|
||||
document.getElementById('permissionUserSelect').addEventListener('change', async e => {
|
||||
selectedUserId = e.target.value;
|
||||
if (selectedUserId) {
|
||||
await loadUserPermissions(selectedUserId);
|
||||
renderPermissionGrid();
|
||||
document.getElementById('permissionPanel').classList.remove('hidden');
|
||||
document.getElementById('permissionEmpty').classList.add('hidden');
|
||||
} else {
|
||||
document.getElementById('permissionPanel').classList.add('hidden');
|
||||
document.getElementById('permissionEmpty').classList.remove('hidden');
|
||||
}
|
||||
});
|
||||
|
||||
async function loadUserPermissions(userId) {
|
||||
// 기본값 세팅
|
||||
currentPermissions = {};
|
||||
const allDefs = { ...SYSTEM1_PAGES, ...SYSTEM3_PAGES };
|
||||
Object.values(allDefs).flat().forEach(p => { currentPermissions[p.key] = p.def; });
|
||||
try {
|
||||
const perms = await api(`/users/${userId}/page-permissions`);
|
||||
(Array.isArray(perms)?perms:[]).forEach(p => { currentPermissions[p.page_name] = !!p.can_access; });
|
||||
} catch(e) { console.warn('권한 로드 실패:', e); }
|
||||
}
|
||||
|
||||
function renderPermissionGrid() {
|
||||
renderSystemPerms('s1-perms', SYSTEM1_PAGES, 'blue');
|
||||
renderSystemPerms('s3-perms', SYSTEM3_PAGES, 'purple');
|
||||
}
|
||||
|
||||
function renderSystemPerms(containerId, pageDef, color) {
|
||||
const container = document.getElementById(containerId);
|
||||
let html = '';
|
||||
Object.entries(pageDef).forEach(([groupName, pages]) => {
|
||||
const groupId = containerId + '-' + groupName.replace(/\s/g,'');
|
||||
const allChecked = pages.every(p => currentPermissions[p.key]);
|
||||
html += `
|
||||
<div>
|
||||
<div class="group-header flex items-center justify-between py-2 px-1 rounded" onclick="toggleGroup('${groupId}')">
|
||||
<div class="flex items-center gap-2">
|
||||
<i class="fas fa-chevron-down text-xs text-gray-400 transition-transform" id="arrow-${groupId}"></i>
|
||||
<span class="text-xs font-semibold text-gray-600 uppercase tracking-wide">${groupName}</span>
|
||||
<span class="text-xs text-gray-400">${pages.length}</span>
|
||||
</div>
|
||||
<label class="flex items-center gap-1.5 text-xs text-gray-500 cursor-pointer" onclick="event.stopPropagation()">
|
||||
<input type="checkbox" ${allChecked?'checked':''} onchange="toggleGroupAll('${groupId}', this.checked)"
|
||||
class="h-3.5 w-3.5 text-${color}-500 rounded border-gray-300">
|
||||
<span>전체</span>
|
||||
</label>
|
||||
</div>
|
||||
<div id="${groupId}" class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-2 mt-1">
|
||||
${pages.map(p => {
|
||||
const checked = currentPermissions[p.key] || false;
|
||||
return `
|
||||
<label class="perm-item flex items-center gap-2.5 p-2.5 border rounded-lg cursor-pointer ${checked?'checked':'border-gray-200'}" data-group="${groupId}">
|
||||
<input type="checkbox" id="perm_${p.key}" ${checked?'checked':''} class="h-4 w-4 text-${color}-500 rounded border-gray-300 focus:ring-${color}-400"
|
||||
onchange="onPermChange(this)">
|
||||
<i class="fas ${p.icon} text-sm ${checked?`text-${color}-500`:'text-gray-400'}" data-color="${color}"></i>
|
||||
<span class="text-sm text-gray-700">${p.title}</span>
|
||||
</label>`;
|
||||
}).join('')}
|
||||
</div>
|
||||
</div>`;
|
||||
});
|
||||
container.innerHTML = html;
|
||||
}
|
||||
|
||||
function onPermChange(cb) {
|
||||
const item = cb.closest('.perm-item');
|
||||
const icon = item.querySelector('i[data-color]');
|
||||
const color = icon.dataset.color;
|
||||
item.classList.toggle('checked', cb.checked);
|
||||
icon.classList.toggle(`text-${color}-500`, cb.checked);
|
||||
icon.classList.toggle('text-gray-400', !cb.checked);
|
||||
// 그룹 전체 체크박스 동기화
|
||||
const group = item.dataset.group;
|
||||
const groupCbs = document.querySelectorAll(`[data-group="${group}"] input[type="checkbox"]`);
|
||||
const allChecked = [...groupCbs].every(c => c.checked);
|
||||
const groupHeader = document.getElementById(group)?.previousElementSibling;
|
||||
if (groupHeader) { const gc = groupHeader.querySelector('input[type="checkbox"]'); if(gc) gc.checked = allChecked; }
|
||||
}
|
||||
|
||||
function toggleGroup(groupId) {
|
||||
const el = document.getElementById(groupId);
|
||||
const arrow = document.getElementById('arrow-' + groupId);
|
||||
el.classList.toggle('hidden');
|
||||
arrow?.classList.toggle('-rotate-90');
|
||||
}
|
||||
|
||||
function toggleGroupAll(groupId, checked) {
|
||||
document.querySelectorAll(`#${groupId} input[type="checkbox"]`).forEach(cb => {
|
||||
cb.checked = checked;
|
||||
onPermChange(cb);
|
||||
});
|
||||
}
|
||||
|
||||
function toggleSystemAll(prefix, checked) {
|
||||
const containerId = prefix === 's1' ? 's1-perms' : 's3-perms';
|
||||
document.querySelectorAll(`#${containerId} input[type="checkbox"]`).forEach(cb => {
|
||||
cb.checked = checked;
|
||||
onPermChange(cb);
|
||||
});
|
||||
// 그룹 전체 체크박스도 동기화
|
||||
document.querySelectorAll(`#${containerId} .group-header input[type="checkbox"]`).forEach(cb => cb.checked = checked);
|
||||
}
|
||||
|
||||
// 저장
|
||||
document.getElementById('savePermissionsBtn').addEventListener('click', async () => {
|
||||
if (!selectedUserId) return;
|
||||
const btn = document.getElementById('savePermissionsBtn');
|
||||
const st = document.getElementById('permissionSaveStatus');
|
||||
btn.disabled = true; btn.innerHTML = '<i class="fas fa-spinner fa-spin mr-2"></i>저장 중...';
|
||||
|
||||
try {
|
||||
const allPages = [...Object.values(SYSTEM1_PAGES).flat(), ...Object.values(SYSTEM3_PAGES).flat()];
|
||||
const permissions = allPages.map(p => {
|
||||
const cb = document.getElementById('perm_' + p.key);
|
||||
return { page_name: p.key, can_access: cb ? cb.checked : false };
|
||||
});
|
||||
await api('/permissions/bulk-grant', { method:'POST', body: JSON.stringify({ user_id: parseInt(selectedUserId), permissions }) });
|
||||
st.textContent = '저장 완료'; st.className = 'text-sm text-emerald-600';
|
||||
showToast('권한이 저장되었습니다.');
|
||||
setTimeout(() => { st.textContent = ''; }, 3000);
|
||||
} catch(e) {
|
||||
st.textContent = e.message; st.className = 'text-sm text-red-500';
|
||||
showToast('저장 실패: ' + e.message, 'error');
|
||||
} finally { btn.disabled = false; btn.innerHTML = '<i class="fas fa-save mr-2"></i>권한 저장'; }
|
||||
});
|
||||
|
||||
/* ===== Workers CRUD ===== */
|
||||
let workers = [], workersLoaded = false, departmentsForSelect = [];
|
||||
|
||||
const JOB_TYPE = { leader: '반장', worker: '작업자' };
|
||||
function jobTypeBadge(t) {
|
||||
if (t === 'leader') return '<span class="px-1.5 py-0.5 rounded text-xs bg-amber-50 text-amber-600">반장</span>';
|
||||
if (t === 'worker') return '<span class="px-1.5 py-0.5 rounded text-xs bg-blue-50 text-blue-600">작업자</span>';
|
||||
return t ? `<span class="px-1.5 py-0.5 rounded text-xs bg-gray-50 text-gray-500">${t}</span>` : '';
|
||||
}
|
||||
function workerStatusBadge(s) {
|
||||
if (s === 'inactive') return '<span class="px-1.5 py-0.5 rounded text-xs bg-gray-100 text-gray-400">비활성</span>';
|
||||
return '<span class="px-1.5 py-0.5 rounded text-xs bg-emerald-50 text-emerald-600">재직</span>';
|
||||
}
|
||||
|
||||
async function loadDepartmentsForSelect() {
|
||||
try {
|
||||
const r = await api('/departments'); departmentsForSelect = (r.data || r).filter(d => d.is_active !== 0 && d.is_active !== false);
|
||||
populateDeptSelects();
|
||||
} catch(e) { console.warn('부서 로드 실패:', e); }
|
||||
}
|
||||
function populateDeptSelects() {
|
||||
['newWorkerDept','editWorkerDept'].forEach(id => {
|
||||
const sel = document.getElementById(id); if (!sel) return;
|
||||
const val = sel.value;
|
||||
sel.innerHTML = '<option value="">선택</option>';
|
||||
departmentsForSelect.forEach(d => { const o = document.createElement('option'); o.value = d.department_id; o.textContent = d.department_name; sel.appendChild(o); });
|
||||
sel.value = val;
|
||||
});
|
||||
}
|
||||
|
||||
async function loadWorkers() {
|
||||
await loadDepartmentsForSelect();
|
||||
try {
|
||||
const r = await api('/workers'); workers = r.data || r;
|
||||
workersLoaded = true;
|
||||
displayWorkers();
|
||||
} catch (err) {
|
||||
document.getElementById('workerList').innerHTML = `<div class="text-red-500 text-center py-6"><i class="fas fa-exclamation-triangle text-xl"></i><p class="text-sm mt-2">${err.message}</p></div>`;
|
||||
}
|
||||
}
|
||||
|
||||
function displayWorkers() {
|
||||
const c = document.getElementById('workerList');
|
||||
if (!workers.length) { c.innerHTML = '<p class="text-gray-400 text-center py-4 text-sm">등록된 작업자가 없습니다.</p>'; return; }
|
||||
c.innerHTML = workers.map(w => `
|
||||
<div class="flex items-center justify-between p-2.5 bg-gray-50 rounded-lg hover:bg-gray-100 transition-colors">
|
||||
<div class="flex-1 min-w-0">
|
||||
<div class="text-sm font-medium text-gray-800 truncate"><i class="fas fa-hard-hat mr-1.5 text-gray-400 text-xs"></i>${w.worker_name}</div>
|
||||
<div class="text-xs text-gray-500 flex items-center gap-1.5 mt-0.5 flex-wrap">
|
||||
${jobTypeBadge(w.job_type)}
|
||||
${w.department_name ? `<span class="px-1.5 py-0.5 rounded bg-green-50 text-green-600">${w.department_name}</span>` : ''}
|
||||
${workerStatusBadge(w.status)}
|
||||
${w.phone_number ? `<span class="text-gray-400">${w.phone_number}</span>` : ''}
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex gap-1 ml-2 flex-shrink-0">
|
||||
<button onclick="editWorker(${w.worker_id})" class="p-1.5 text-slate-500 hover:text-slate-700 hover:bg-slate-200 rounded" title="편집"><i class="fas fa-pen-to-square text-xs"></i></button>
|
||||
${w.status !== 'inactive' ? `<button onclick="deactivateWorker(${w.worker_id},'${(w.worker_name||'').replace(/'/g,"\\'")}')" class="p-1.5 text-red-400 hover:text-red-600 hover:bg-red-100 rounded" title="비활성화"><i class="fas fa-ban text-xs"></i></button>` : ''}
|
||||
</div>
|
||||
</div>`).join('');
|
||||
}
|
||||
|
||||
document.getElementById('addWorkerForm').addEventListener('submit', async e => {
|
||||
e.preventDefault();
|
||||
try {
|
||||
await api('/workers', { method: 'POST', body: JSON.stringify({
|
||||
worker_name: document.getElementById('newWorkerName').value.trim(),
|
||||
job_type: document.getElementById('newJobType').value || null,
|
||||
department_id: document.getElementById('newWorkerDept').value ? parseInt(document.getElementById('newWorkerDept').value) : null,
|
||||
phone_number: document.getElementById('newWorkerPhone').value.trim() || null,
|
||||
hire_date: document.getElementById('newWorkerHireDate').value || null,
|
||||
notes: document.getElementById('newWorkerNotes').value.trim() || null
|
||||
})});
|
||||
showToast('작업자가 추가되었습니다.'); document.getElementById('addWorkerForm').reset(); await loadWorkers();
|
||||
} catch(e) { showToast(e.message, 'error'); }
|
||||
});
|
||||
|
||||
function editWorker(id) {
|
||||
const w = workers.find(x => x.worker_id === id); if (!w) return;
|
||||
document.getElementById('editWorkerId').value = w.worker_id;
|
||||
document.getElementById('editWorkerName').value = w.worker_name;
|
||||
document.getElementById('editJobType').value = w.job_type || '';
|
||||
document.getElementById('editWorkerDept').value = w.department_id || '';
|
||||
document.getElementById('editWorkerPhone').value = w.phone_number || '';
|
||||
document.getElementById('editWorkerHireDate').value = formatDate(w.hire_date);
|
||||
document.getElementById('editWorkerNotes').value = w.notes || '';
|
||||
document.getElementById('editWorkerStatus').value = w.status || 'active';
|
||||
document.getElementById('editEmploymentStatus').value = w.employment_status || 'employed';
|
||||
populateDeptSelects();
|
||||
document.getElementById('editWorkerDept').value = w.department_id || '';
|
||||
document.getElementById('editWorkerModal').classList.remove('hidden');
|
||||
}
|
||||
function closeWorkerModal() { document.getElementById('editWorkerModal').classList.add('hidden'); }
|
||||
|
||||
document.getElementById('editWorkerForm').addEventListener('submit', async e => {
|
||||
e.preventDefault();
|
||||
try {
|
||||
await api(`/workers/${document.getElementById('editWorkerId').value}`, { method: 'PUT', body: JSON.stringify({
|
||||
worker_name: document.getElementById('editWorkerName').value.trim(),
|
||||
job_type: document.getElementById('editJobType').value || null,
|
||||
department_id: document.getElementById('editWorkerDept').value ? parseInt(document.getElementById('editWorkerDept').value) : null,
|
||||
phone_number: document.getElementById('editWorkerPhone').value.trim() || null,
|
||||
hire_date: document.getElementById('editWorkerHireDate').value || null,
|
||||
notes: document.getElementById('editWorkerNotes').value.trim() || null,
|
||||
status: document.getElementById('editWorkerStatus').value,
|
||||
employment_status: document.getElementById('editEmploymentStatus').value
|
||||
})});
|
||||
showToast('수정되었습니다.'); closeWorkerModal(); await loadWorkers();
|
||||
} catch(e) { showToast(e.message, 'error'); }
|
||||
});
|
||||
|
||||
async function deactivateWorker(id, name) {
|
||||
if (!confirm(`"${name}" 작업자를 비활성화?`)) return;
|
||||
try { await api(`/workers/${id}`, { method: 'DELETE' }); showToast('작업자 비활성화 완료'); await loadWorkers(); } catch(e) { showToast(e.message, 'error'); }
|
||||
}
|
||||
310
user-management/web/static/js/tkuser-vacations.js
Normal file
310
user-management/web/static/js/tkuser-vacations.js
Normal file
@@ -0,0 +1,310 @@
|
||||
/* ===== Vacation CRUD ===== */
|
||||
let vacTypes = [], vacBalances = [], vacationsLoaded = false, vacWorkers = [];
|
||||
|
||||
async function loadVacationsTab() {
|
||||
// 연도 셀렉트 초기화
|
||||
const sel = document.getElementById('vacYear');
|
||||
if (sel && !sel.children.length) {
|
||||
const curYear = new Date().getFullYear();
|
||||
for (let y = curYear + 1; y >= curYear - 2; y--) {
|
||||
const o = document.createElement('option');
|
||||
o.value = y; o.textContent = y + '년';
|
||||
if (y === curYear) o.selected = true;
|
||||
sel.appendChild(o);
|
||||
}
|
||||
}
|
||||
await loadVacTypes();
|
||||
await loadVacWorkers();
|
||||
await loadVacBalances();
|
||||
vacationsLoaded = true;
|
||||
}
|
||||
|
||||
async function loadVacTypes() {
|
||||
try {
|
||||
const r = await api('/vacations/types?all=true');
|
||||
vacTypes = r.data || [];
|
||||
renderVacTypeSidebar();
|
||||
} catch(e) { console.warn('휴가 유형 로드 실패:', e); }
|
||||
}
|
||||
|
||||
async function loadVacWorkers() {
|
||||
try {
|
||||
const r = await api('/workers');
|
||||
vacWorkers = (r.data || []).filter(w => w.status !== 'inactive');
|
||||
} catch(e) { console.warn('작업자 로드 실패:', e); }
|
||||
}
|
||||
|
||||
function renderVacTypeSidebar() {
|
||||
const c = document.getElementById('vacTypeSidebar');
|
||||
if (!c) return;
|
||||
if (!vacTypes.length) { c.innerHTML = '<p class="text-gray-400 text-center py-4 text-sm">등록된 유형이 없습니다.</p>'; return; }
|
||||
c.innerHTML = vacTypes.map(vt => `
|
||||
<div class="group flex items-center justify-between p-2 rounded-lg ${vt.is_active ? 'bg-gray-50' : 'bg-gray-50 opacity-50'} hover:bg-blue-50 transition-colors">
|
||||
<div class="flex-1 min-w-0">
|
||||
<div class="text-sm font-medium text-gray-800 truncate flex items-center gap-1.5">
|
||||
${vt.type_name}
|
||||
${vt.is_system ? '<span class="text-[10px] px-1 py-0.5 rounded bg-blue-50 text-blue-500">시스템</span>' : ''}
|
||||
${vt.is_special ? '<span class="text-[10px] px-1 py-0.5 rounded bg-purple-50 text-purple-500">특별</span>' : ''}
|
||||
${!vt.is_active ? '<span class="text-[10px] px-1 py-0.5 rounded bg-gray-100 text-gray-400">비활성</span>' : ''}
|
||||
</div>
|
||||
<div class="text-xs text-gray-400 mt-0.5">
|
||||
${vt.type_code} | 차감 ${vt.deduct_days}일 | 우선순위 ${vt.priority}
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex gap-0.5 ml-1 flex-shrink-0 opacity-0 group-hover:opacity-100 transition-opacity">
|
||||
<button onclick="editVacType(${vt.id})" class="p-1 text-slate-400 hover:text-slate-600 rounded" title="수정"><i class="fas fa-pen text-[10px]"></i></button>
|
||||
${!vt.is_system ? `<button onclick="deleteVacType(${vt.id},'${(vt.type_name||'').replace(/'/g,"\\'")}')" class="p-1 text-red-300 hover:text-red-500 rounded" title="비활성화"><i class="fas fa-ban text-[10px]"></i></button>` : ''}
|
||||
</div>
|
||||
</div>`).join('');
|
||||
}
|
||||
|
||||
// 유형 모달
|
||||
function openVacTypeModal(editId) {
|
||||
document.getElementById('vtEditId').value = '';
|
||||
document.getElementById('vacTypeForm').reset();
|
||||
document.getElementById('vtDeductDays').value = '1.0';
|
||||
document.getElementById('vtPriority').value = '99';
|
||||
document.getElementById('vacTypeModalTitle').textContent = '휴가 유형 추가';
|
||||
document.getElementById('vtCode').readOnly = false;
|
||||
if (editId) {
|
||||
const vt = vacTypes.find(v => v.id === editId);
|
||||
if (!vt) return;
|
||||
document.getElementById('vacTypeModalTitle').textContent = '휴가 유형 수정';
|
||||
document.getElementById('vtEditId').value = vt.id;
|
||||
document.getElementById('vtCode').value = vt.type_code;
|
||||
document.getElementById('vtCode').readOnly = !!vt.is_system;
|
||||
document.getElementById('vtName').value = vt.type_name;
|
||||
document.getElementById('vtDeductDays').value = vt.deduct_days;
|
||||
document.getElementById('vtPriority').value = vt.priority;
|
||||
document.getElementById('vtDescription').value = vt.description || '';
|
||||
document.getElementById('vtSpecial').checked = !!vt.is_special;
|
||||
}
|
||||
document.getElementById('vacTypeModal').classList.remove('hidden');
|
||||
}
|
||||
function closeVacTypeModal() { document.getElementById('vacTypeModal').classList.add('hidden'); }
|
||||
function editVacType(id) { openVacTypeModal(id); }
|
||||
|
||||
document.getElementById('vacTypeForm').addEventListener('submit', async e => {
|
||||
e.preventDefault();
|
||||
const editId = document.getElementById('vtEditId').value;
|
||||
const body = {
|
||||
type_code: document.getElementById('vtCode').value.trim().toUpperCase(),
|
||||
type_name: document.getElementById('vtName').value.trim(),
|
||||
deduct_days: parseFloat(document.getElementById('vtDeductDays').value) || 1.0,
|
||||
priority: parseInt(document.getElementById('vtPriority').value) || 99,
|
||||
description: document.getElementById('vtDescription').value.trim() || null,
|
||||
is_special: document.getElementById('vtSpecial').checked
|
||||
};
|
||||
try {
|
||||
if (editId) {
|
||||
await api(`/vacations/types/${editId}`, { method: 'PUT', body: JSON.stringify(body) });
|
||||
showToast('휴가 유형이 수정되었습니다.');
|
||||
} else {
|
||||
await api('/vacations/types', { method: 'POST', body: JSON.stringify(body) });
|
||||
showToast('휴가 유형이 추가되었습니다.');
|
||||
}
|
||||
closeVacTypeModal(); await loadVacTypes();
|
||||
} catch(e) { showToast(e.message, 'error'); }
|
||||
});
|
||||
|
||||
async function deleteVacType(id, name) {
|
||||
if (!confirm(`"${name}" 유형을 비활성화하시겠습니까?`)) return;
|
||||
try { await api(`/vacations/types/${id}`, { method: 'DELETE' }); showToast('비활성화되었습니다.'); await loadVacTypes(); }
|
||||
catch(e) { showToast(e.message, 'error'); }
|
||||
}
|
||||
|
||||
// 연차 배정 테이블
|
||||
async function loadVacBalances() {
|
||||
const year = document.getElementById('vacYear')?.value || new Date().getFullYear();
|
||||
try {
|
||||
const r = await api(`/vacations/balances/year/${year}`);
|
||||
vacBalances = r.data || [];
|
||||
renderVacBalanceTable();
|
||||
} catch(e) {
|
||||
document.getElementById('vacBalanceTable').innerHTML = `<div class="text-red-500 text-center py-6"><p class="text-sm">${e.message}</p></div>`;
|
||||
}
|
||||
}
|
||||
|
||||
let vacDeptCollapsed = {};
|
||||
|
||||
function toggleVacDept(deptName) {
|
||||
vacDeptCollapsed[deptName] = !vacDeptCollapsed[deptName];
|
||||
const body = document.getElementById('vacDept_' + CSS.escape(deptName));
|
||||
const icon = document.getElementById('vacDeptIcon_' + CSS.escape(deptName));
|
||||
if (body) body.classList.toggle('hidden');
|
||||
if (icon) icon.style.transform = vacDeptCollapsed[deptName] ? 'rotate(-90deg)' : '';
|
||||
}
|
||||
|
||||
function renderVacBalanceTable() {
|
||||
const c = document.getElementById('vacBalanceTable');
|
||||
if (!vacBalances.length) {
|
||||
c.innerHTML = '<div class="text-gray-400 text-center py-8 text-sm"><i class="fas fa-calendar-xmark text-3xl mb-2"></i><p>배정된 연차가 없습니다. "자동 계산" 또는 "개별 배정"을 이용하세요.</p></div>';
|
||||
return;
|
||||
}
|
||||
// 부서 -> 작업자 -> 휴가유형 그룹핑
|
||||
const deptMap = {};
|
||||
vacBalances.forEach(b => {
|
||||
const deptName = b.department_name || '미배정';
|
||||
if (!deptMap[deptName]) deptMap[deptName] = {};
|
||||
if (!deptMap[deptName][b.worker_id]) deptMap[deptName][b.worker_id] = { name: b.worker_name, hire_date: b.hire_date, items: [] };
|
||||
deptMap[deptName][b.worker_id].items.push(b);
|
||||
});
|
||||
|
||||
const deptNames = Object.keys(deptMap).sort((a, b) => a === '미배정' ? 1 : b === '미배정' ? -1 : a.localeCompare(b));
|
||||
let html = '<div class="space-y-3">';
|
||||
|
||||
deptNames.forEach(deptName => {
|
||||
const workers = deptMap[deptName];
|
||||
const workerCount = Object.keys(workers).length;
|
||||
// 부서 합계
|
||||
let dTotal = 0, dUsed = 0;
|
||||
Object.values(workers).forEach(g => g.items.forEach(b => { dTotal += parseFloat(b.total_days) || 0; dUsed += parseFloat(b.used_days) || 0; }));
|
||||
const dRemain = dTotal - dUsed;
|
||||
const usagePct = dTotal > 0 ? Math.round((dUsed / dTotal) * 100) : 0;
|
||||
const barColor = usagePct >= 80 ? 'bg-red-400' : usagePct >= 50 ? 'bg-amber-400' : 'bg-emerald-400';
|
||||
const collapsed = vacDeptCollapsed[deptName];
|
||||
const eid = CSS.escape(deptName);
|
||||
|
||||
html += `<div class="border border-gray-200 rounded-lg overflow-hidden">`;
|
||||
// 헤더 (클릭으로 접기/펼치기)
|
||||
html += `<div onclick="toggleVacDept('${escHtml(deptName).replace(/'/g, "\\'")}')" class="flex items-center justify-between px-4 py-3 bg-slate-50 cursor-pointer hover:bg-slate-100 select-none transition-colors">
|
||||
<div class="flex items-center gap-3">
|
||||
<i id="vacDeptIcon_${eid}" class="fas fa-chevron-down text-xs text-gray-400 transition-transform" style="${collapsed ? 'transform:rotate(-90deg)' : ''}"></i>
|
||||
<span class="text-sm font-bold text-gray-700"><i class="fas fa-building mr-1.5 text-blue-400"></i>${escHtml(deptName)}</span>
|
||||
<span class="text-xs text-gray-400">${workerCount}명</span>
|
||||
</div>
|
||||
<div class="flex items-center gap-4">
|
||||
<div class="flex items-center gap-2 text-xs">
|
||||
<span class="text-gray-400">배정 <b class="text-gray-600">${dTotal}</b></span>
|
||||
<span class="text-gray-400">사용 <b class="text-gray-600">${dUsed}</b></span>
|
||||
<span class="text-gray-400">잔여 <b class="${dRemain <= 0 ? 'text-red-500' : 'text-emerald-600'}">${dRemain}</b></span>
|
||||
</div>
|
||||
<div class="w-24 h-2 bg-gray-200 rounded-full overflow-hidden">
|
||||
<div class="${barColor} h-full rounded-full transition-all" style="width:${usagePct}%"></div>
|
||||
</div>
|
||||
<span class="text-xs font-medium ${usagePct >= 80 ? 'text-red-500' : usagePct >= 50 ? 'text-amber-500' : 'text-emerald-600'}">${usagePct}%</span>
|
||||
</div>
|
||||
</div>`;
|
||||
// 테이블 본문
|
||||
html += `<div id="vacDept_${eid}" class="${collapsed ? 'hidden' : ''}">`;
|
||||
html += '<table class="w-full text-sm"><thead class="bg-gray-50"><tr>';
|
||||
html += '<th class="text-left px-3 py-2 text-xs font-semibold text-gray-500">작업자</th>';
|
||||
html += '<th class="text-left px-3 py-2 text-xs font-semibold text-gray-500">입사일</th>';
|
||||
html += '<th class="text-left px-3 py-2 text-xs font-semibold text-gray-500">휴가유형</th>';
|
||||
html += '<th class="text-center px-3 py-2 text-xs font-semibold text-gray-500">배정</th>';
|
||||
html += '<th class="text-center px-3 py-2 text-xs font-semibold text-gray-500">사용</th>';
|
||||
html += '<th class="text-center px-3 py-2 text-xs font-semibold text-gray-500">잔여</th>';
|
||||
html += '<th class="text-left px-3 py-2 text-xs font-semibold text-gray-500">비고</th>';
|
||||
html += '<th class="px-3 py-2 text-xs font-semibold text-gray-500 w-16"></th>';
|
||||
html += '</tr></thead><tbody>';
|
||||
|
||||
Object.values(workers).forEach(g => {
|
||||
g.items.forEach((b, i) => {
|
||||
const remaining = parseFloat(b.remaining_days || (b.total_days - b.used_days));
|
||||
const remClass = remaining <= 0 ? 'text-red-500 font-semibold' : remaining <= 3 ? 'text-amber-500 font-medium' : 'text-emerald-600';
|
||||
html += `<tr class="border-t border-gray-100 hover:bg-gray-50">`;
|
||||
if (i === 0) {
|
||||
html += `<td class="px-3 py-2 font-medium text-gray-800" rowspan="${g.items.length}">${escHtml(g.name)}</td>`;
|
||||
html += `<td class="px-3 py-2 text-xs text-gray-400" rowspan="${g.items.length}">${g.hire_date ? new Date(g.hire_date).toISOString().substring(0,10) : '-'}</td>`;
|
||||
}
|
||||
html += `<td class="px-3 py-2"><span class="px-1.5 py-0.5 rounded text-xs bg-slate-50 text-slate-600">${escHtml(b.type_name)}</span></td>`;
|
||||
html += `<td class="px-3 py-2 text-center">${b.total_days}</td>`;
|
||||
html += `<td class="px-3 py-2 text-center">${b.used_days}</td>`;
|
||||
html += `<td class="px-3 py-2 text-center ${remClass}">${remaining}</td>`;
|
||||
html += `<td class="px-3 py-2 text-xs text-gray-400 truncate max-w-[150px]" title="${escHtml(b.notes||'')}">${escHtml(b.notes||'')}</td>`;
|
||||
html += `<td class="px-3 py-2 text-center">
|
||||
<button onclick="editVacBalance(${b.id})" class="p-1 text-slate-400 hover:text-slate-600" title="수정"><i class="fas fa-pen text-xs"></i></button>
|
||||
<button onclick="deleteVacBalance(${b.id})" class="p-1 text-red-300 hover:text-red-500" title="삭제"><i class="fas fa-trash-alt text-xs"></i></button>
|
||||
</td>`;
|
||||
html += '</tr>';
|
||||
});
|
||||
});
|
||||
html += '</tbody></table></div></div>';
|
||||
});
|
||||
html += '</div>';
|
||||
c.innerHTML = html;
|
||||
}
|
||||
|
||||
// 자동 계산
|
||||
async function autoCalcVacation() {
|
||||
const year = document.getElementById('vacYear')?.value || new Date().getFullYear();
|
||||
if (!confirm(`${year}년 전체 작업자 연차를 입사일 기준으로 자동 계산합니다.\n기존 배정이 있으면 덮어씁니다. 진행하시겠습니까?`)) return;
|
||||
try {
|
||||
const r = await api('/vacations/balances/auto-calculate', { method: 'POST', body: JSON.stringify({ year: parseInt(year) }) });
|
||||
showToast(`${r.data.count}명 자동 배정 완료`);
|
||||
await loadVacBalances();
|
||||
} catch(e) { showToast(e.message, 'error'); }
|
||||
}
|
||||
|
||||
// 개별 배정 모달
|
||||
function openVacBalanceModal(editId) {
|
||||
document.getElementById('vbEditId').value = '';
|
||||
document.getElementById('vacBalanceForm').reset();
|
||||
document.getElementById('vbTotalDays').value = '0';
|
||||
document.getElementById('vbUsedDays').value = '0';
|
||||
document.getElementById('vacBalModalTitle').textContent = '연차 배정';
|
||||
// 작업자 셀렉트
|
||||
const wSel = document.getElementById('vbWorker');
|
||||
wSel.innerHTML = '<option value="">선택</option>';
|
||||
vacWorkers.forEach(w => { wSel.innerHTML += `<option value="${w.worker_id}">${w.worker_name}</option>`; });
|
||||
// 유형 셀렉트
|
||||
const tSel = document.getElementById('vbType');
|
||||
tSel.innerHTML = '<option value="">선택</option>';
|
||||
vacTypes.filter(t => t.is_active).forEach(t => { tSel.innerHTML += `<option value="${t.id}">${t.type_name} (${t.type_code})</option>`; });
|
||||
if (editId) {
|
||||
const b = vacBalances.find(x => x.id === editId);
|
||||
if (!b) return;
|
||||
document.getElementById('vacBalModalTitle').textContent = '배정 수정';
|
||||
document.getElementById('vbEditId').value = b.id;
|
||||
wSel.value = b.worker_id;
|
||||
wSel.disabled = true;
|
||||
tSel.value = b.vacation_type_id;
|
||||
tSel.disabled = true;
|
||||
document.getElementById('vbTotalDays').value = b.total_days;
|
||||
document.getElementById('vbUsedDays').value = b.used_days;
|
||||
document.getElementById('vbNotes').value = b.notes || '';
|
||||
} else {
|
||||
wSel.disabled = false;
|
||||
tSel.disabled = false;
|
||||
}
|
||||
document.getElementById('vacBalanceModal').classList.remove('hidden');
|
||||
}
|
||||
function closeVacBalanceModal() {
|
||||
document.getElementById('vacBalanceModal').classList.add('hidden');
|
||||
document.getElementById('vbWorker').disabled = false;
|
||||
document.getElementById('vbType').disabled = false;
|
||||
}
|
||||
function editVacBalance(id) { openVacBalanceModal(id); }
|
||||
|
||||
document.getElementById('vacBalanceForm').addEventListener('submit', async e => {
|
||||
e.preventDefault();
|
||||
const editId = document.getElementById('vbEditId').value;
|
||||
try {
|
||||
if (editId) {
|
||||
await api(`/vacations/balances/${editId}`, { method: 'PUT', body: JSON.stringify({
|
||||
total_days: parseFloat(document.getElementById('vbTotalDays').value) || 0,
|
||||
used_days: parseFloat(document.getElementById('vbUsedDays').value) || 0,
|
||||
notes: document.getElementById('vbNotes').value.trim() || null
|
||||
})});
|
||||
showToast('수정되었습니다.');
|
||||
} else {
|
||||
const year = document.getElementById('vacYear')?.value || new Date().getFullYear();
|
||||
await api('/vacations/balances', { method: 'POST', body: JSON.stringify({
|
||||
worker_id: parseInt(document.getElementById('vbWorker').value),
|
||||
vacation_type_id: parseInt(document.getElementById('vbType').value),
|
||||
year: parseInt(year),
|
||||
total_days: parseFloat(document.getElementById('vbTotalDays').value) || 0,
|
||||
used_days: parseFloat(document.getElementById('vbUsedDays').value) || 0,
|
||||
notes: document.getElementById('vbNotes').value.trim() || null
|
||||
})});
|
||||
showToast('배정되었습니다.');
|
||||
}
|
||||
closeVacBalanceModal(); await loadVacBalances();
|
||||
} catch(e) { showToast(e.message, 'error'); }
|
||||
});
|
||||
|
||||
async function deleteVacBalance(id) {
|
||||
if (!confirm('이 배정을 삭제하시겠습니까?')) return;
|
||||
try { await api(`/vacations/balances/${id}`, { method: 'DELETE' }); showToast('삭제되었습니다.'); await loadVacBalances(); }
|
||||
catch(e) { showToast(e.message, 'error'); }
|
||||
}
|
||||
514
user-management/web/static/js/tkuser-workplaces.js
Normal file
514
user-management/web/static/js/tkuser-workplaces.js
Normal file
@@ -0,0 +1,514 @@
|
||||
/* ===== Workplaces CRUD ===== */
|
||||
let workplaces = [], workplacesLoaded = false, workplaceCategories = [];
|
||||
let selectedWorkplaceId = null, selectedWorkplaceName = '';
|
||||
let equipments = [], equipmentTypes = [];
|
||||
let wpNavLevel = 'categories'; // 'categories' | 'workplaces'
|
||||
let wpNavCategoryId = null;
|
||||
let wpNavCategoryName = '';
|
||||
let previewMapRegions = [];
|
||||
|
||||
function purposeBadge(p) {
|
||||
const colors = { '작업구역': 'bg-blue-50 text-blue-600', '창고': 'bg-amber-50 text-amber-600', '설비': 'bg-purple-50 text-purple-600', '휴게시설': 'bg-green-50 text-green-600' };
|
||||
return p ? `<span class="px-1.5 py-0.5 rounded text-xs ${colors[p] || 'bg-gray-50 text-gray-500'}">${p}</span>` : '';
|
||||
}
|
||||
|
||||
async function loadWorkplaceCategories() {
|
||||
try {
|
||||
const r = await api('/workplaces/categories'); workplaceCategories = r.data || r;
|
||||
populateCategorySelects();
|
||||
renderSidebar();
|
||||
} catch(e) { console.warn('카테고리 로드 실패:', e); }
|
||||
}
|
||||
function populateCategorySelects() {
|
||||
['newWorkplaceCategory','editWorkplaceCategory'].forEach(id => {
|
||||
const sel = document.getElementById(id); if (!sel) return;
|
||||
const val = sel.value;
|
||||
sel.innerHTML = '<option value="">선택</option>';
|
||||
workplaceCategories.forEach(c => { const o = document.createElement('option'); o.value = c.category_id; o.textContent = c.category_name; sel.appendChild(o); });
|
||||
sel.value = val;
|
||||
});
|
||||
}
|
||||
|
||||
async function loadWorkplaces() {
|
||||
await loadWorkplaceCategories();
|
||||
try {
|
||||
const r = await api('/workplaces'); workplaces = r.data || r;
|
||||
workplacesLoaded = true;
|
||||
renderSidebar();
|
||||
} catch (err) {
|
||||
document.getElementById('wpSidebarContent').innerHTML = `<div class="text-red-500 text-center py-6"><i class="fas fa-exclamation-triangle text-xl"></i><p class="text-sm mt-2">${err.message}</p></div>`;
|
||||
}
|
||||
}
|
||||
|
||||
function renderSidebar() {
|
||||
const c = document.getElementById('wpSidebarContent');
|
||||
if (!c) return;
|
||||
let html = '';
|
||||
if (wpNavLevel === 'categories') {
|
||||
// 공장 목록 레벨
|
||||
html += '<div class="text-xs font-semibold text-gray-400 uppercase tracking-wider mb-2">공장 선택</div>';
|
||||
html += `<button onclick="openAddWorkplaceModal()" class="w-full px-3 py-2 mb-3 bg-slate-700 text-white rounded-lg hover:bg-slate-800 text-sm font-medium flex items-center justify-center gap-1.5"><i class="fas fa-plus text-xs"></i>작업장 추가</button>`;
|
||||
if (!workplaceCategories.length) {
|
||||
html += '<p class="text-gray-400 text-center py-4 text-sm">등록된 공장이 없습니다.</p>';
|
||||
} else {
|
||||
html += '<div class="space-y-1.5 flex-1 overflow-y-auto">';
|
||||
workplaceCategories.forEach(cat => {
|
||||
const count = workplaces.filter(w => w.category_id == cat.category_id).length;
|
||||
html += `<div onclick="drillIntoCategory(${cat.category_id},'${(cat.category_name||'').replace(/'/g,"\\'")}')" class="flex items-center justify-between p-2.5 rounded-lg hover:bg-blue-50 transition-colors cursor-pointer bg-gray-50">
|
||||
<div class="flex items-center gap-2 min-w-0">
|
||||
<i class="fas fa-industry text-gray-400 text-sm"></i>
|
||||
<span class="text-sm font-medium text-gray-800 truncate">${cat.category_name}</span>
|
||||
</div>
|
||||
<div class="flex items-center gap-1.5 flex-shrink-0">
|
||||
<span class="text-xs text-gray-400">${count}</span>
|
||||
<i class="fas fa-chevron-right text-gray-300 text-xs"></i>
|
||||
</div>
|
||||
</div>`;
|
||||
});
|
||||
// 미분류 작업장
|
||||
const uncategorized = workplaces.filter(w => !w.category_id);
|
||||
if (uncategorized.length) {
|
||||
html += `<div onclick="drillIntoCategory(0,'미분류')" class="flex items-center justify-between p-2.5 rounded-lg hover:bg-blue-50 transition-colors cursor-pointer bg-gray-50">
|
||||
<div class="flex items-center gap-2 min-w-0">
|
||||
<i class="fas fa-folder-open text-gray-300 text-sm"></i>
|
||||
<span class="text-sm font-medium text-gray-500 truncate">미분류</span>
|
||||
</div>
|
||||
<div class="flex items-center gap-1.5 flex-shrink-0">
|
||||
<span class="text-xs text-gray-400">${uncategorized.length}</span>
|
||||
<i class="fas fa-chevron-right text-gray-300 text-xs"></i>
|
||||
</div>
|
||||
</div>`;
|
||||
}
|
||||
html += '</div>';
|
||||
}
|
||||
} else {
|
||||
// 작업장 목록 레벨 (특정 공장 내)
|
||||
html += `<button onclick="backToCategories()" class="flex items-center gap-1.5 text-sm text-slate-600 hover:text-slate-800 mb-2 px-1 py-1 rounded hover:bg-gray-100 transition-colors"><i class="fas fa-arrow-left text-xs"></i>전체 공장</button>`;
|
||||
html += `<div class="text-sm font-semibold text-gray-800 mb-2 px-1"><i class="fas fa-industry text-gray-400 mr-1.5"></i>${wpNavCategoryName}</div>`;
|
||||
html += `<button onclick="openAddWorkplaceModal()" class="w-full px-3 py-2 mb-3 bg-slate-700 text-white rounded-lg hover:bg-slate-800 text-sm font-medium flex items-center justify-center gap-1.5"><i class="fas fa-plus text-xs"></i>작업장 추가</button>`;
|
||||
const filtered = wpNavCategoryId === 0
|
||||
? workplaces.filter(w => !w.category_id)
|
||||
: workplaces.filter(w => w.category_id == wpNavCategoryId);
|
||||
if (!filtered.length) {
|
||||
html += '<p class="text-gray-400 text-center py-4 text-sm">등록된 작업장이 없습니다.</p>';
|
||||
} else {
|
||||
html += '<div class="space-y-1.5 flex-1 overflow-y-auto">';
|
||||
filtered.forEach(w => {
|
||||
html += `<div class="flex items-center justify-between p-2.5 rounded-lg hover:bg-blue-50 transition-colors cursor-pointer ${selectedWorkplaceId === w.workplace_id ? 'bg-blue-50 ring-1 ring-blue-200' : 'bg-gray-50'}" onclick="selectWorkplaceForEquipments(${w.workplace_id},'${(w.workplace_name||'').replace(/'/g,"\\'")}')">
|
||||
<div class="flex-1 min-w-0">
|
||||
<div class="text-sm font-medium text-gray-800 truncate"><i class="fas fa-building mr-1.5 text-gray-400 text-xs"></i>${w.workplace_name}</div>
|
||||
<div class="text-xs text-gray-500 flex items-center gap-1.5 mt-0.5 flex-wrap">
|
||||
${purposeBadge(w.workplace_purpose)}
|
||||
${w.is_active === 0 || w.is_active === false ? '<span class="px-1.5 py-0.5 rounded bg-gray-100 text-gray-400">비활성</span>' : ''}
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex gap-1 ml-2 flex-shrink-0">
|
||||
<button onclick="event.stopPropagation(); editWorkplace(${w.workplace_id})" class="p-1.5 text-slate-500 hover:text-slate-700 hover:bg-slate-200 rounded" title="편집"><i class="fas fa-pen-to-square text-xs"></i></button>
|
||||
${w.is_active !== 0 && w.is_active !== false ? `<button onclick="event.stopPropagation(); deactivateWorkplace(${w.workplace_id},'${(w.workplace_name||'').replace(/'/g,"\\'")}')" class="p-1.5 text-red-400 hover:text-red-600 hover:bg-red-100 rounded" title="비활성화"><i class="fas fa-ban text-xs"></i></button>` : ''}
|
||||
</div>
|
||||
</div>`;
|
||||
});
|
||||
html += '</div>';
|
||||
}
|
||||
}
|
||||
c.innerHTML = html;
|
||||
}
|
||||
|
||||
function drillIntoCategory(categoryId, categoryName) {
|
||||
wpNavLevel = 'workplaces';
|
||||
wpNavCategoryId = categoryId;
|
||||
wpNavCategoryName = categoryName;
|
||||
selectedWorkplaceId = null;
|
||||
renderSidebar();
|
||||
showZoneMapForCategory(categoryId);
|
||||
}
|
||||
|
||||
function backToCategories() {
|
||||
wpNavLevel = 'categories';
|
||||
wpNavCategoryId = null;
|
||||
wpNavCategoryName = '';
|
||||
selectedWorkplaceId = null;
|
||||
renderSidebar();
|
||||
showEmptyState();
|
||||
}
|
||||
|
||||
function showEmptyState() {
|
||||
document.getElementById('workplaceEmptyState')?.classList.remove('hidden');
|
||||
document.getElementById('equipmentSection')?.classList.add('hidden');
|
||||
document.getElementById('zoneMapSection')?.classList.add('hidden');
|
||||
}
|
||||
|
||||
function showZoneMapForCategory(categoryId) {
|
||||
document.getElementById('workplaceEmptyState')?.classList.add('hidden');
|
||||
document.getElementById('equipmentSection')?.classList.add('hidden');
|
||||
document.getElementById('zoneMapSection')?.classList.remove('hidden');
|
||||
const catName = categoryId === 0 ? '미분류' : (workplaceCategories.find(c => c.category_id == categoryId)?.category_name || '');
|
||||
document.getElementById('zoneMapTitle').innerHTML = `<i class="fas fa-map text-slate-400 mr-2"></i>${catName} - 구역지도`;
|
||||
selectedMapCategoryId = categoryId;
|
||||
if (categoryId === 0) {
|
||||
document.getElementById('layoutPreviewArea').classList.remove('hidden');
|
||||
document.getElementById('layoutPreviewCanvas').classList.add('hidden');
|
||||
document.getElementById('layoutPreviewArea').innerHTML = '<i class="fas fa-info-circle text-2xl mb-2"></i><p>미분류 작업장에는 구역지도가 없습니다.</p>';
|
||||
return;
|
||||
}
|
||||
loadLayoutPreview(categoryId);
|
||||
}
|
||||
|
||||
function backToCategory() {
|
||||
if (!wpNavCategoryId && wpNavCategoryId !== 0) { backToCategories(); return; }
|
||||
selectedWorkplaceId = null;
|
||||
renderSidebar();
|
||||
showZoneMapForCategory(wpNavCategoryId);
|
||||
}
|
||||
|
||||
function openAddWorkplaceModal() {
|
||||
populateCategorySelects();
|
||||
document.getElementById('addWorkplaceForm').reset();
|
||||
// 공장 드릴다운 상태이면 카테고리 자동 선택
|
||||
if (wpNavLevel === 'workplaces' && wpNavCategoryId && wpNavCategoryId !== 0) {
|
||||
document.getElementById('newWorkplaceCategory').value = wpNavCategoryId;
|
||||
}
|
||||
document.getElementById('addWorkplaceModal').classList.remove('hidden');
|
||||
}
|
||||
function closeAddWorkplaceModal() { document.getElementById('addWorkplaceModal').classList.add('hidden'); }
|
||||
|
||||
document.getElementById('addWorkplaceForm').addEventListener('submit', async e => {
|
||||
e.preventDefault();
|
||||
try {
|
||||
await api('/workplaces', { method: 'POST', body: JSON.stringify({
|
||||
workplace_name: document.getElementById('newWorkplaceName').value.trim(),
|
||||
category_id: document.getElementById('newWorkplaceCategory').value ? parseInt(document.getElementById('newWorkplaceCategory').value) : null,
|
||||
workplace_purpose: document.getElementById('newWorkplacePurpose').value || null,
|
||||
description: document.getElementById('newWorkplaceDesc').value.trim() || null,
|
||||
display_priority: parseInt(document.getElementById('newWorkplacePriority').value) || 0
|
||||
})});
|
||||
showToast('작업장이 추가되었습니다.'); document.getElementById('addWorkplaceForm').reset(); closeAddWorkplaceModal(); await loadWorkplaces();
|
||||
} catch(e) { showToast(e.message, 'error'); }
|
||||
});
|
||||
|
||||
function editWorkplace(id) {
|
||||
const w = workplaces.find(x => x.workplace_id === id); if (!w) return;
|
||||
document.getElementById('editWorkplaceId').value = w.workplace_id;
|
||||
document.getElementById('editWorkplaceName').value = w.workplace_name;
|
||||
document.getElementById('editWorkplaceDesc').value = w.description || '';
|
||||
document.getElementById('editWorkplacePriority').value = w.display_priority || 0;
|
||||
document.getElementById('editWorkplaceActive').value = (w.is_active === 0 || w.is_active === false) ? '0' : '1';
|
||||
document.getElementById('editWorkplacePurpose').value = w.workplace_purpose || '';
|
||||
populateCategorySelects();
|
||||
document.getElementById('editWorkplaceCategory').value = w.category_id || '';
|
||||
document.getElementById('editWorkplaceModal').classList.remove('hidden');
|
||||
}
|
||||
function closeWorkplaceModal() { document.getElementById('editWorkplaceModal').classList.add('hidden'); }
|
||||
|
||||
document.getElementById('editWorkplaceForm').addEventListener('submit', async e => {
|
||||
e.preventDefault();
|
||||
try {
|
||||
await api(`/workplaces/${document.getElementById('editWorkplaceId').value}`, { method: 'PUT', body: JSON.stringify({
|
||||
workplace_name: document.getElementById('editWorkplaceName').value.trim(),
|
||||
category_id: document.getElementById('editWorkplaceCategory').value ? parseInt(document.getElementById('editWorkplaceCategory').value) : null,
|
||||
workplace_purpose: document.getElementById('editWorkplacePurpose').value || null,
|
||||
description: document.getElementById('editWorkplaceDesc').value.trim() || null,
|
||||
display_priority: parseInt(document.getElementById('editWorkplacePriority').value) || 0,
|
||||
is_active: document.getElementById('editWorkplaceActive').value === '1'
|
||||
})});
|
||||
showToast('수정되었습니다.'); closeWorkplaceModal(); await loadWorkplaces();
|
||||
} catch(e) { showToast(e.message, 'error'); }
|
||||
});
|
||||
|
||||
async function deactivateWorkplace(id, name) {
|
||||
if (!confirm(`"${name}" 작업장을 비활성화?`)) return;
|
||||
try { await api(`/workplaces/${id}`, { method: 'DELETE' }); showToast('작업장 비활성화 완료'); await loadWorkplaces(); } catch(e) { showToast(e.message, 'error'); }
|
||||
}
|
||||
|
||||
/* ===== Equipment CRUD ===== */
|
||||
let eqMapImg = null, eqMapCanvas = null, eqMapCtx = null, eqDetailEqId = null;
|
||||
|
||||
function eqStatusBadge(status) {
|
||||
const map = { active:'bg-emerald-50 text-emerald-600', maintenance:'bg-amber-50 text-amber-600', inactive:'bg-gray-100 text-gray-500', external:'bg-blue-50 text-blue-600', repair_external:'bg-blue-50 text-blue-600', repair_needed:'bg-red-50 text-red-600' };
|
||||
const labels = { active:'가동중', maintenance:'점검중', inactive:'비활성', external:'외부반출', repair_external:'수리외주', repair_needed:'수리필요' };
|
||||
return `<span class="px-1.5 py-0.5 rounded text-xs ${map[status] || 'bg-gray-100 text-gray-500'}">${labels[status] || status || ''}</span>`;
|
||||
}
|
||||
|
||||
function selectWorkplaceForEquipments(id, name) {
|
||||
selectedWorkplaceId = id;
|
||||
selectedWorkplaceName = name;
|
||||
// 카테고리 레벨에서 직접 호출된 경우, 해당 카테고리로 드릴인
|
||||
if (wpNavLevel === 'categories') {
|
||||
const wp = workplaces.find(w => w.workplace_id === id);
|
||||
if (wp && wp.category_id) {
|
||||
wpNavLevel = 'workplaces';
|
||||
wpNavCategoryId = wp.category_id;
|
||||
wpNavCategoryName = wp.category_name || '';
|
||||
}
|
||||
}
|
||||
renderSidebar();
|
||||
document.getElementById('workplaceEmptyState')?.classList.add('hidden');
|
||||
document.getElementById('zoneMapSection')?.classList.add('hidden');
|
||||
document.getElementById('equipmentSection').classList.remove('hidden');
|
||||
document.getElementById('eqWorkplaceName').textContent = name;
|
||||
// 뒤로가기 버튼 표시 (공장 구역지도로 돌아가기)
|
||||
const backBtn = document.getElementById('eqBackToCategory');
|
||||
if (backBtn && wpNavCategoryId !== null) {
|
||||
document.getElementById('eqBackLabel').textContent = `${wpNavCategoryName} 구역지도`;
|
||||
backBtn.classList.remove('hidden');
|
||||
} else if (backBtn) {
|
||||
backBtn.classList.add('hidden');
|
||||
}
|
||||
loadEquipments();
|
||||
loadEquipmentTypes();
|
||||
loadEqMap();
|
||||
}
|
||||
|
||||
async function loadEquipments() {
|
||||
try {
|
||||
const r = await api(`/equipments/workplace/${selectedWorkplaceId}`);
|
||||
equipments = r.data || [];
|
||||
displayEquipments();
|
||||
drawEqMapEquipments();
|
||||
} catch(e) {
|
||||
document.getElementById('equipmentList').innerHTML = `<div class="text-red-500 text-center py-4"><p class="text-sm">${e.message}</p></div>`;
|
||||
}
|
||||
}
|
||||
|
||||
function displayEquipments() {
|
||||
const statusFilter = document.getElementById('eqStatusFilter').value;
|
||||
const typeFilter = document.getElementById('eqTypeFilter').value;
|
||||
let filtered = equipments;
|
||||
if (statusFilter) filtered = filtered.filter(e => e.status === statusFilter);
|
||||
if (typeFilter) filtered = filtered.filter(e => e.equipment_type === typeFilter);
|
||||
const c = document.getElementById('equipmentList');
|
||||
if (!filtered.length) { c.innerHTML = '<p class="text-gray-400 text-center py-4 text-sm">설비가 없습니다.</p>'; return; }
|
||||
c.innerHTML = filtered.map(e => `
|
||||
<div class="flex items-center justify-between p-2.5 bg-gray-50 rounded-lg hover:bg-gray-100 transition-colors cursor-pointer" onclick="openEqDetailModal(${e.equipment_id})">
|
||||
<div class="flex-1 min-w-0">
|
||||
<div class="text-sm font-medium text-gray-800 truncate">
|
||||
<span class="text-xs text-gray-400 font-mono mr-1.5">${e.equipment_code || ''}</span>${e.equipment_name}
|
||||
${e.is_temporarily_moved ? '<i class="fas fa-arrows-alt text-blue-400 ml-1" title="임시이동중"></i>' : ''}
|
||||
</div>
|
||||
<div class="text-xs text-gray-500 flex items-center gap-1.5 mt-0.5 flex-wrap">
|
||||
${e.equipment_type ? `<span class="px-1.5 py-0.5 rounded bg-slate-50 text-slate-600">${e.equipment_type}</span>` : ''}
|
||||
${e.manufacturer ? `<span class="text-gray-400">${e.manufacturer}</span>` : ''}
|
||||
${eqStatusBadge(e.status)}
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex gap-1 ml-2 flex-shrink-0">
|
||||
<button onclick="event.stopPropagation(); editEquipment(${e.equipment_id})" class="p-1.5 text-slate-500 hover:text-slate-700 hover:bg-slate-200 rounded" title="편집"><i class="fas fa-pen-to-square text-xs"></i></button>
|
||||
<button onclick="event.stopPropagation(); deleteEquipment(${e.equipment_id},'${(e.equipment_name||'').replace(/'/g,"\\'")}')" class="p-1.5 text-red-400 hover:text-red-600 hover:bg-red-100 rounded" title="삭제"><i class="fas fa-trash-alt text-xs"></i></button>
|
||||
</div>
|
||||
</div>`).join('');
|
||||
}
|
||||
|
||||
function filterEquipments() { displayEquipments(); }
|
||||
|
||||
async function loadEquipmentTypes() {
|
||||
try {
|
||||
const r = await api('/equipments/types'); equipmentTypes = r.data || [];
|
||||
const sel = document.getElementById('eqTypeFilter'); const val = sel.value;
|
||||
sel.innerHTML = '<option value="">전체 유형</option>';
|
||||
equipmentTypes.forEach(t => { const o = document.createElement('option'); o.value = t; o.textContent = t; sel.appendChild(o); });
|
||||
sel.value = val;
|
||||
const dl = document.getElementById('eqTypeDatalist');
|
||||
if (dl) { dl.innerHTML = ''; equipmentTypes.forEach(t => { const o = document.createElement('option'); o.value = t; dl.appendChild(o); }); }
|
||||
} catch(e) { console.warn('설비 유형 로드 실패:', e); }
|
||||
}
|
||||
|
||||
async function openEquipmentModal(editId) {
|
||||
document.getElementById('eqEditId').value = '';
|
||||
document.getElementById('equipmentForm').reset();
|
||||
if (editId) {
|
||||
document.getElementById('eqModalTitle').textContent = '설비 수정';
|
||||
const eq = equipments.find(e => e.equipment_id === editId);
|
||||
if (!eq) return;
|
||||
document.getElementById('eqEditId').value = eq.equipment_id;
|
||||
document.getElementById('eqCode').value = eq.equipment_code || '';
|
||||
document.getElementById('eqName').value = eq.equipment_name || '';
|
||||
document.getElementById('eqType').value = eq.equipment_type || '';
|
||||
document.getElementById('eqStatus').value = eq.status || 'active';
|
||||
document.getElementById('eqManufacturer').value = eq.manufacturer || '';
|
||||
document.getElementById('eqModel').value = eq.model_name || '';
|
||||
document.getElementById('eqSupplier').value = eq.supplier || '';
|
||||
document.getElementById('eqPrice').value = eq.purchase_price || '';
|
||||
document.getElementById('eqInstallDate').value = eq.installation_date ? eq.installation_date.substring(0, 10) : '';
|
||||
document.getElementById('eqSerial').value = eq.serial_number || '';
|
||||
document.getElementById('eqSpecs').value = eq.specifications || '';
|
||||
document.getElementById('eqNotes').value = eq.notes || '';
|
||||
} else {
|
||||
document.getElementById('eqModalTitle').textContent = '설비 추가';
|
||||
generateEquipmentCode();
|
||||
}
|
||||
document.getElementById('equipmentModal').classList.remove('hidden');
|
||||
}
|
||||
function closeEquipmentModal() { document.getElementById('equipmentModal').classList.add('hidden'); }
|
||||
async function generateEquipmentCode() { try { const r = await api('/equipments/next-code?prefix=TKP'); document.getElementById('eqCode').value = r.data || ''; } catch(e) {} }
|
||||
function editEquipment(id) { openEquipmentModal(id); }
|
||||
async function deleteEquipment(id, name) {
|
||||
if (!confirm(`"${name}" 설비를 삭제하시겠습니까?`)) return;
|
||||
try { await api(`/equipments/${id}`, { method: 'DELETE' }); showToast('설비가 삭제되었습니다.'); await loadEquipments(); } catch(e) { showToast(e.message, 'error'); }
|
||||
}
|
||||
|
||||
document.getElementById('equipmentForm').addEventListener('submit', async e => {
|
||||
e.preventDefault();
|
||||
const editId = document.getElementById('eqEditId').value;
|
||||
const body = {
|
||||
equipment_code: document.getElementById('eqCode').value.trim(),
|
||||
equipment_name: document.getElementById('eqName').value.trim(),
|
||||
equipment_type: document.getElementById('eqType').value.trim() || null,
|
||||
status: document.getElementById('eqStatus').value,
|
||||
manufacturer: document.getElementById('eqManufacturer').value.trim() || null,
|
||||
model_name: document.getElementById('eqModel').value.trim() || null,
|
||||
supplier: document.getElementById('eqSupplier').value.trim() || null,
|
||||
purchase_price: document.getElementById('eqPrice').value ? parseFloat(document.getElementById('eqPrice').value) : null,
|
||||
installation_date: document.getElementById('eqInstallDate').value || null,
|
||||
serial_number: document.getElementById('eqSerial').value.trim() || null,
|
||||
specifications: document.getElementById('eqSpecs').value.trim() || null,
|
||||
notes: document.getElementById('eqNotes').value.trim() || null,
|
||||
workplace_id: selectedWorkplaceId
|
||||
};
|
||||
try {
|
||||
if (editId) { await api(`/equipments/${editId}`, { method: 'PUT', body: JSON.stringify(body) }); showToast('설비가 수정되었습니다.'); }
|
||||
else { await api('/equipments', { method: 'POST', body: JSON.stringify(body) }); showToast('설비가 추가되었습니다.'); }
|
||||
closeEquipmentModal(); await loadEquipments(); loadEquipmentTypes();
|
||||
} catch(e) { showToast(e.message, 'error'); }
|
||||
});
|
||||
|
||||
/* ===== Equipment Map (설비 배치도) ===== */
|
||||
function loadEqMap() {
|
||||
const wp = workplaces.find(w => w.workplace_id === selectedWorkplaceId);
|
||||
if (!wp || !wp.layout_image) {
|
||||
document.getElementById('eqMapArea').classList.remove('hidden');
|
||||
document.getElementById('eqMapCanvasWrap').classList.add('hidden');
|
||||
return;
|
||||
}
|
||||
document.getElementById('eqMapArea').classList.add('hidden');
|
||||
document.getElementById('eqMapCanvasWrap').classList.remove('hidden');
|
||||
eqMapCanvas = document.getElementById('eqMapCanvas');
|
||||
eqMapCtx = eqMapCanvas.getContext('2d');
|
||||
const imgUrl = wp.layout_image.startsWith('/') ? '/uploads/' + wp.layout_image.replace(/^\/uploads\//, '') : wp.layout_image;
|
||||
const img = new Image();
|
||||
img.onload = function() {
|
||||
const maxW = 780; const scale = img.width > maxW ? maxW / img.width : 1;
|
||||
eqMapCanvas.width = img.width * scale; eqMapCanvas.height = img.height * scale;
|
||||
eqMapCtx.drawImage(img, 0, 0, eqMapCanvas.width, eqMapCanvas.height);
|
||||
eqMapImg = img;
|
||||
drawEqMapEquipments();
|
||||
eqMapCanvas.onclick = onEqMapClick;
|
||||
};
|
||||
img.src = imgUrl;
|
||||
}
|
||||
|
||||
function drawEqMapEquipments() {
|
||||
if (!eqMapCanvas || !eqMapCtx || !eqMapImg) return;
|
||||
eqMapCtx.clearRect(0, 0, eqMapCanvas.width, eqMapCanvas.height);
|
||||
eqMapCtx.drawImage(eqMapImg, 0, 0, eqMapCanvas.width, eqMapCanvas.height);
|
||||
equipments.forEach(eq => {
|
||||
if (eq.map_x_percent == null || eq.map_y_percent == null) return;
|
||||
const x = (eq.map_x_percent / 100) * eqMapCanvas.width;
|
||||
const y = (eq.map_y_percent / 100) * eqMapCanvas.height;
|
||||
const w = ((eq.map_width_percent || 3) / 100) * eqMapCanvas.width;
|
||||
const h = ((eq.map_height_percent || 3) / 100) * eqMapCanvas.height;
|
||||
const colors = { active:'#10b981', maintenance:'#f59e0b', inactive:'#94a3b8', external:'#3b82f6', repair_external:'#3b82f6', repair_needed:'#ef4444' };
|
||||
const color = colors[eq.status] || '#64748b';
|
||||
eqMapCtx.fillStyle = color + '33'; eqMapCtx.fillRect(x - w/2, y - h/2, w, h);
|
||||
eqMapCtx.strokeStyle = color; eqMapCtx.lineWidth = 2; eqMapCtx.strokeRect(x - w/2, y - h/2, w, h);
|
||||
eqMapCtx.fillStyle = color; eqMapCtx.font = 'bold 10px sans-serif'; eqMapCtx.textAlign = 'center';
|
||||
eqMapCtx.fillText(eq.equipment_code || eq.equipment_name, x, y - h/2 - 3);
|
||||
eqMapCtx.textAlign = 'start';
|
||||
});
|
||||
}
|
||||
|
||||
let eqMapPlacingId = null;
|
||||
function onEqMapClick(e) {
|
||||
if (!eqMapPlacingId) return;
|
||||
const r = eqMapCanvas.getBoundingClientRect();
|
||||
const scaleX = eqMapCanvas.width / r.width; const scaleY = eqMapCanvas.height / r.height;
|
||||
const px = (e.clientX - r.left) * scaleX; const py = (e.clientY - r.top) * scaleY;
|
||||
const xPct = (px / eqMapCanvas.width * 100).toFixed(2); const yPct = (py / eqMapCanvas.height * 100).toFixed(2);
|
||||
saveEqMapPosition(eqMapPlacingId, xPct, yPct);
|
||||
eqMapPlacingId = null; eqMapCanvas.style.cursor = 'default';
|
||||
}
|
||||
|
||||
async function saveEqMapPosition(eqId, x, y) {
|
||||
try {
|
||||
await api(`/equipments/${eqId}/map-position`, { method: 'PATCH', body: JSON.stringify({ map_x_percent: parseFloat(x), map_y_percent: parseFloat(y), map_width_percent: 3, map_height_percent: 3 }) });
|
||||
showToast('설비 위치가 저장되었습니다.'); await loadEquipments();
|
||||
} catch(e) { showToast(e.message, 'error'); }
|
||||
}
|
||||
|
||||
function startPlaceEquipment(eqId) {
|
||||
eqMapPlacingId = eqId; if(eqMapCanvas) eqMapCanvas.style.cursor = 'crosshair';
|
||||
showToast('배치도에서 위치를 클릭하세요');
|
||||
}
|
||||
|
||||
async function uploadWorkplaceLayoutImage() {
|
||||
const file = document.getElementById('wpLayoutImageFile').files[0];
|
||||
if (!file) return;
|
||||
try {
|
||||
const fd = new FormData(); fd.append('image', file); const token = getToken();
|
||||
const res = await fetch(`${API_BASE}/workplaces/${selectedWorkplaceId}/layout-image`, { method: 'POST', headers: { 'Authorization': token ? `Bearer ${token}` : '' }, body: fd });
|
||||
const result = await res.json(); if (!res.ok) throw new Error(result.error || '업로드 실패');
|
||||
showToast('배치도 이미지가 업로드되었습니다.');
|
||||
const wp = workplaces.find(w => w.workplace_id === selectedWorkplaceId);
|
||||
if (wp) wp.layout_image = result.data.image_path;
|
||||
loadEqMap();
|
||||
} catch(e) { showToast(e.message || '업로드 실패', 'error'); }
|
||||
}
|
||||
|
||||
/* ===== Equipment Detail Modal ===== */
|
||||
async function openEqDetailModal(eqId) {
|
||||
eqDetailEqId = eqId;
|
||||
const eq = equipments.find(e => e.equipment_id === eqId);
|
||||
if (!eq) return;
|
||||
document.getElementById('eqDetailTitle').textContent = `${eq.equipment_code} - ${eq.equipment_name}`;
|
||||
document.getElementById('eqReturnBtn').classList.toggle('hidden', !eq.is_temporarily_moved);
|
||||
const fmt = v => v || '-';
|
||||
const fmtDate = v => v ? v.substring(0, 10) : '-';
|
||||
const fmtPrice = v => v ? Number(v).toLocaleString() + '원' : '-';
|
||||
document.getElementById('eqDetailContent').innerHTML = `
|
||||
<div class="grid grid-cols-2 gap-x-4 gap-y-2 text-sm">
|
||||
<div><span class="text-gray-400">유형:</span> ${fmt(eq.equipment_type)}</div>
|
||||
<div><span class="text-gray-400">상태:</span> ${eqStatusBadge(eq.status)}</div>
|
||||
<div><span class="text-gray-400">제조사:</span> ${fmt(eq.manufacturer)}</div>
|
||||
<div><span class="text-gray-400">모델:</span> ${fmt(eq.model_name)}</div>
|
||||
<div><span class="text-gray-400">공급업체:</span> ${fmt(eq.supplier)}</div>
|
||||
<div><span class="text-gray-400">구매가격:</span> ${fmtPrice(eq.purchase_price)}</div>
|
||||
<div><span class="text-gray-400">설치일:</span> ${fmtDate(eq.installation_date)}</div>
|
||||
<div><span class="text-gray-400">시리얼:</span> ${fmt(eq.serial_number)}</div>
|
||||
<div class="col-span-2"><span class="text-gray-400">사양:</span> ${fmt(eq.specifications)}</div>
|
||||
<div class="col-span-2"><span class="text-gray-400">비고:</span> ${fmt(eq.notes)}</div>
|
||||
</div>
|
||||
<div class="mt-2"><button onclick="startPlaceEquipment(${eq.equipment_id}); closeEqDetailModal();" class="text-xs text-blue-600 hover:underline"><i class="fas fa-map-pin mr-1"></i>배치도에 위치 지정</button></div>`;
|
||||
loadEqPhotos(eqId);
|
||||
document.getElementById('eqDetailModal').classList.remove('hidden');
|
||||
}
|
||||
function closeEqDetailModal() { document.getElementById('eqDetailModal').classList.add('hidden'); }
|
||||
|
||||
async function loadEqPhotos(eqId) {
|
||||
const c = document.getElementById('eqPhotoGrid');
|
||||
try {
|
||||
const r = await api(`/equipments/${eqId}/photos`); const photos = r.data || [];
|
||||
if (!photos.length) { c.innerHTML = '<p class="text-gray-400 text-xs col-span-4 text-center py-2">사진 없음</p>'; return; }
|
||||
c.innerHTML = photos.map(p => {
|
||||
const fname = (p.photo_path||'').replace(/^\/uploads\//, '');
|
||||
return `
|
||||
<div class="relative group cursor-pointer" onclick="document.getElementById('photoViewImage').src='/uploads/${fname}'; document.getElementById('photoViewModal').classList.remove('hidden');">
|
||||
<img src="/uploads/${fname}" class="w-full h-20 object-cover rounded">
|
||||
<button onclick="event.stopPropagation(); deleteEqPhoto(${p.photo_id})" class="absolute top-0.5 right-0.5 bg-red-500 text-white rounded-full w-4 h-4 text-[10px] leading-4 text-center opacity-0 group-hover:opacity-100">×</button>
|
||||
</div>`; }).join('');
|
||||
} catch(e) { c.innerHTML = '<p class="text-red-400 text-xs col-span-4">로드 실패</p>'; }
|
||||
}
|
||||
|
||||
async function uploadEqPhoto() {
|
||||
const file = document.getElementById('eqPhotoFile').files[0]; if (!file || !eqDetailEqId) return;
|
||||
try {
|
||||
const fd = new FormData(); fd.append('photo', file); const token = getToken();
|
||||
const res = await fetch(`${API_BASE}/equipments/${eqDetailEqId}/photos`, { method: 'POST', headers: { 'Authorization': token ? `Bearer ${token}` : '' }, body: fd });
|
||||
const result = await res.json(); if (!res.ok) throw new Error(result.error || '업로드 실패');
|
||||
showToast('사진이 추가되었습니다.'); loadEqPhotos(eqDetailEqId);
|
||||
} catch(e) { showToast(e.message, 'error'); }
|
||||
document.getElementById('eqPhotoFile').value = '';
|
||||
}
|
||||
|
||||
async function deleteEqPhoto(photoId) {
|
||||
if (!confirm('사진을 삭제하시겠습니까?')) return;
|
||||
try { await api(`/equipments/photos/${photoId}`, { method: 'DELETE' }); showToast('삭제됨'); loadEqPhotos(eqDetailEqId); } catch(e) { showToast(e.message, 'error'); }
|
||||
}
|
||||
Reference in New Issue
Block a user