import { state } from '../../core/state'; import { SoftwareAsset, SWUser } from '../../core/excelHandler'; import { openModal } from './BaseModal'; import { createIcons, Edit2, X, Paperclip } from 'lucide'; import { CORP_LIST, ORG_LIST } from './SharedData'; import { generateOptionsHTML, setFieldValue, getFieldValue } from './ModalUtils'; let currentSwUserAsset: SoftwareAsset | null = null; let tempSwUsers: SWUser[] = []; const SW_USER_MODAL_HTML = ` `; export function openSwUserModal(asset: SoftwareAsset) { currentSwUserAsset = asset; const modal = document.getElementById('sw-user-modal')!; const swInfo = document.getElementById('sw-user-sw-info')!; swInfo.innerHTML = `
${asset.법인} | ${asset.자산번호}
${asset.제품명}
`; // 기존 사용자 데이터 복사 (원본 보호를 위해 temp 사용) const existingMapping = state.masterData.swUsers.find(u => u.sw_id === asset.id); tempSwUsers = existingMapping ? JSON.parse(JSON.stringify(existingMapping.userDataList || [])) : []; renderUserList(); modal.classList.remove('hidden'); createIcons({ icons: { Edit2, X, Paperclip } }); } function renderUserList() { const tbody = document.getElementById('sw-user-table-body')!; tbody.innerHTML = ''; if (tempSwUsers.length === 0) { tbody.innerHTML = '할당된 사용자가 없습니다.'; return; } tempSwUsers.forEach((user, idx) => { const tr = document.createElement('tr'); tr.innerHTML = ` ${user.구매법인 || user.법인 || ''} ${user.부서 || ''} ${user.직위 || ''} ${user.이름 || ''} ${user.사용기간 || ''} ${user.신청서명 ? '' : '-'}
`; tbody.appendChild(tr); }); // 이벤트 연결 tbody.querySelectorAll('.btn-edit-user').forEach(btn => { btn.addEventListener('click', (e) => { const idx = parseInt((e.currentTarget as HTMLElement).getAttribute('data-idx')!); openUserEditSubModal(idx); }); }); tbody.querySelectorAll('.btn-del-user').forEach(btn => { btn.addEventListener('click', (e) => { const idx = parseInt((e.currentTarget as HTMLElement).getAttribute('data-idx')!); if (confirm('사용자 할당을 삭제하시겠습니까?')) { tempSwUsers.splice(idx, 1); renderUserList(); } }); }); createIcons({ icons: { Paperclip } }); } function openUserEditSubModal(idx: number = -1) { const subModal = document.getElementById('sw-user-edit-modal')!; const form = document.getElementById('sw-user-edit-form') as HTMLFormElement; form.reset(); setFieldValue('edit-user-index', idx); if (idx > -1) { const user = tempSwUsers[idx]; setFieldValue('new-user-법인', user.구매법인 || user.법인); setFieldValue('new-user-부서', user.부서); setFieldValue('new-user-직위', user.직위); setFieldValue('new-user-이름', user.이름); setFieldValue('new-user-사용기간', user.사용기간); } else { setFieldValue('new-user-법인', currentSwUserAsset?.법인); } subModal.classList.remove('hidden'); } export function initSwUserModal(onSave: () => void, closeModals: () => void) { if (!document.getElementById('sw-user-modal')) { document.body.insertAdjacentHTML('beforeend', SW_USER_MODAL_HTML); } const mainSaveBtn = document.getElementById('btn-save-sw-user')!; const addUserBtn = document.getElementById('btn-open-add-user')!; const confirmUserBtn = document.getElementById('btn-confirm-user-edit')!; addUserBtn.addEventListener('click', () => openUserEditSubModal()); confirmUserBtn.addEventListener('click', () => { saveUserDataToList(); }); mainSaveBtn.addEventListener('click', () => { if (!currentSwUserAsset) return; // 전역 상태 업데이트 const existingIdx = state.masterData.swUsers.findIndex(u => u.sw_id === currentSwUserAsset!.id); const newMapping = { sw_id: currentSwUserAsset!.id, userDataList: tempSwUsers }; if (existingIdx > -1) state.masterData.swUsers[existingIdx] = newMapping as any; else state.masterData.swUsers.push(newMapping as any); onSave(); document.getElementById('sw-user-modal')?.classList.add('hidden'); }); document.getElementById('btn-close-sw-user-modal')?.addEventListener('click', () => { document.getElementById('sw-user-modal')?.classList.add('hidden'); }); document.getElementById('btn-cancel-sw-user')?.addEventListener('click', () => { document.getElementById('sw-user-modal')?.classList.add('hidden'); }); document.getElementById('btn-close-user-edit')?.addEventListener('click', () => { document.getElementById('sw-user-edit-modal')?.classList.add('hidden'); }); document.getElementById('btn-close-user-sub')?.addEventListener('click', () => { document.getElementById('sw-user-edit-modal')?.classList.add('hidden'); }); } function saveUserDataToList() { const idx = parseInt(getFieldValue('edit-user-index')); const 신청서Input = document.getElementById('new-user-신청서') as HTMLInputElement; const 신청서명 = 신청서Input.files && 신청서Input.files.length > 0 ? 신청서Input.files[0].name : (idx > -1 ? tempSwUsers[idx].신청서명 : ''); const userData: any = { 구매법인: getFieldValue('new-user-법인'), 부서: getFieldValue('new-user-부서'), 직위: getFieldValue('new-user-직위'), 이름: getFieldValue('new-user-이름'), 사용기간: getFieldValue('new-user-사용기간'), 신청서명 }; if (idx === -1) tempSwUsers.push(userData); else tempSwUsers[idx] = userData; document.getElementById('sw-user-edit-modal')?.classList.add('hidden'); renderUserList(); }