import { state, saveAsset, deleteAsset } from '../../core/state'; import { BaseModal } from './BaseModal'; import { openSwUserModal } from './SWUserModal'; import { createIcons, History, Plus, X, Save, Edit2, RotateCcw, Calendar, Users } from 'lucide'; import { CORP_LIST } from './SharedData'; import { ASSET_SCHEMA, UI_TEXT } from '../../core/schema'; import { API_BASE_URL } from '../../core/utils'; import { generateOptionsHTML, setFieldValue, getFieldValue, applyDateMask } from './ModalUtils'; class SwAssetModal extends BaseModal { constructor() { super('sw', '소프트웨어 상세 정보'); } protected renderFrameHTML(): string { return ` `; } protected initChildLogic(onSave: () => void, closeModals: () => void): void { const saveBtn = document.getElementById('btn-save-sw-asset')!; const revertBtn = document.getElementById('btn-revert-sw-edit')!; const deleteBtn = document.getElementById('btn-delete-sw-asset')!; const typeSelect = document.getElementById('sw-asset-type') as HTMLSelectElement; const userAssignBtn = document.getElementById('btn-open-sw-user')!; const btnOpenUpdate = document.getElementById('btn-open-sw-update')!; typeSelect?.addEventListener('change', () => this.applySwTypeUI(typeSelect.value)); ['sw-구매일', 'sw-시작일', 'sw-만료일', 'sw-update-start', 'sw-update-end'].forEach(id => { const el = document.getElementById(id) as HTMLInputElement; if (el) applyDateMask(el); }); userAssignBtn.addEventListener('click', () => { if (this.currentAsset) openSwUserModal(this.currentAsset); }); // 업데이트 모달 로직 const subModal = document.getElementById('sw-update-modal')!; const closeUpdate = () => subModal.classList.add('hidden'); document.getElementById('btn-close-sw-update')?.addEventListener('click', closeUpdate); document.getElementById('btn-cancel-sw-update')?.addEventListener('click', closeUpdate); btnOpenUpdate?.addEventListener('click', (e) => { e.preventDefault(); if (!this.isEditMode) { alert('자산을 수정 모드로 변경한 후 업데이트를 진행해주세요.'); return; } subModal.classList.remove('hidden'); }); document.getElementById('btn-save-sw-update')?.addEventListener('click', async (e) => { e.preventDefault(); const date = (document.getElementById('sw-update-date') as HTMLInputElement).value; const start = (document.getElementById('sw-update-start') as HTMLInputElement).value; const end = (document.getElementById('sw-update-end') as HTMLInputElement).value; const cost = (document.getElementById('sw-update-cost') as HTMLInputElement).value; const note = (document.getElementById('sw-update-note') as HTMLInputElement).value; if (start) setFieldValue('sw-시작일', start); if (end) setFieldValue('sw-만료일', end); if (cost) setFieldValue('sw-금액', cost); const log = { assetId: this.currentAsset.id, date, details: `[계약갱신] ${note} (${start} ~ ${end}, 비용: ${cost})`, user: '관리자' }; await fetch(`${API_BASE_URL}/api/asset/history/batch`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify([...state.masterData.logs, log]) }); closeUpdate(); onSave(); }); revertBtn.addEventListener('click', () => { this.setEditLockMode('view'); if (this.currentAsset) this.fillFormData(this.currentAsset); }); saveBtn.addEventListener('click', async () => { if (!this.currentAsset) return; if (!this.isEditMode) { this.setEditLockMode('edit'); this.isEditMode = true; return; } const type = getFieldValue('sw-asset-type'); const formData = new FormData(this.formEl!); const updated = { ...this.currentAsset }; formData.forEach((value, key) => { updated[key] = value; }); let categoryKey = (type === '내부SW') ? 'swInternal' : (type === '클라우드' ? 'cloud' : 'swExternal'); if (await saveAsset(categoryKey, updated)) { onSave(); this.close(); closeModals(); } }); deleteBtn.addEventListener('click', async () => { if (!this.currentAsset || !confirm(UI_TEXT.MESSAGES.CONFIRM_DELETE)) return; const type = this.currentAsset.asset_type || this.currentAsset.type; let categoryKey = (type === '내부SW') ? 'swInternal' : (type === '클라우드' ? 'cloud' : 'swExternal'); if (await deleteAsset(categoryKey, this.currentAsset.id)) { alert('성공적으로 삭제되었습니다.'); onSave(); this.close(); closeModals(); } }); createIcons({ icons: { History, Plus, Save, Calendar, Users, RotateCcw } }); } protected fillFormData(asset: any): void { setFieldValue('sw-asset-id', asset.id); setFieldValue('sw-asset-type', asset.asset_type || asset.type); setFieldValue('sw-분야', asset.sw_field || ''); setFieldValue('sw-법인', asset.purchase_corp || ''); setFieldValue('sw-부서', asset.current_dept || ''); setFieldValue('sw-user-current', asset.user_current || ''); setFieldValue('sw-previous-user', asset.previous_user || ''); setFieldValue('sw-제품명', asset.product_name || ''); setFieldValue('sw-수량', asset.asset_count || ''); setFieldValue('sw-금액', asset.purchase_amount || ''); setFieldValue('sw-구매일', asset.purchase_date || ''); setFieldValue('sw-납품업체', asset.purchase_vendor || ''); setFieldValue('sw-개발담당자', asset.dev_manager || ''); setFieldValue('sw-기획담당자', asset.planning_manager || ''); setFieldValue('sw-영업담당자', asset.sales_manager || ''); setFieldValue('sw-비고', asset.memo || ''); if (asset.type === '클라우드' || asset.asset_type === '클라우드') { setFieldValue('sw-플랫폼명', asset.dev_objective || ''); setFieldValue('sw-계정명', asset.email_account || ''); setFieldValue('sw-결제수단', asset.purchase_method || ''); } else { setFieldValue('sw-만료일', asset.expiry_date || ''); } this.renderHistory(asset.id); } protected onAfterOpen(asset: any, mode: string): void { this.applySwTypeUI(asset.asset_type || asset.type); } private applySwTypeUI(type: string) { const cloudFields = document.querySelectorAll('.cloud-only'); const swFields = document.querySelectorAll('.sw-standard-field'); const userSection = document.getElementById('sw-user-section'); const expiryGroup = document.getElementById('sw-expiry-group'); const userTracking = document.querySelectorAll('.sw-user-tracking'); if (type === '클라우드') { cloudFields.forEach(el => (el as HTMLElement).style.display = 'flex'); swFields.forEach(el => (el as HTMLElement).style.display = 'none'); if (userSection) userSection.style.display = 'none'; userTracking.forEach(el => (el as HTMLElement).style.display = 'none'); } else { cloudFields.forEach(el => (el as HTMLElement).style.display = 'none'); swFields.forEach(el => (el as HTMLElement).style.display = 'flex'); if (userSection) userSection.style.display = 'block'; if (type === '외부SW' || type === '내부SW') { if (expiryGroup) expiryGroup.style.display = 'flex'; userTracking.forEach(el => (el as HTMLElement).style.display = (type === '외부SW') ? 'flex' : 'none'); } } } private renderHistory(swId: string) { const container = document.getElementById('sw-history-list'); if (!container) return; const logs = (state.masterData.logs || []).filter(l => l.assetId === swId); if (logs.length === 0) { container.innerHTML = '
수정 이력이 없습니다.
'; return; } container.innerHTML = logs.map(l => `
${l.date}
${l.user}
${l.details}
`).join(''); } } export const swModal = new SwAssetModal(); export function initSwModal(onSave: () => void, closeModals: () => void) { swModal.init(onSave, closeModals); } export function openSwModal(asset: any, mode: 'view' | 'add' | 'edit' = 'view') { swModal.open(asset, mode); }