import { state, saveAsset } from '../../core/state'; import { ASSET_SCHEMA, UI_TEXT } from '../../core/schema'; import { generateOptionsHTML, setFieldValue, getFieldValue, setEditLock, parseAndSetLocation, bindLocationEvents, getCombinedLocation, applyDateMask } from './ModalUtils'; import { CORP_LIST, LOCATION_DATA, ORG_LIST } from './SharedData'; import { createIcons, X, History, Plus, Save, Paperclip, Calendar, Monitor, Cpu, Network, ShieldCheck } from 'lucide'; let currentHwAsset: any | null = null; let isEditMode = false; const HW_MODAL_HTML = ` `; export function initHwModal(onSave: () => void, closeModals: () => void) { if (!document.getElementById('hw-asset-modal')) { document.body.insertAdjacentHTML('beforeend', HW_MODAL_HTML); } const form = document.getElementById('hw-asset-form') as HTMLFormElement; const saveBtn = document.getElementById('btn-save-hw-asset')!; const revertBtn = document.getElementById('btn-revert-hw-edit')!; const deleteBtn = document.getElementById('btn-delete-hw-asset')!; const btnCloseHeader = document.getElementById('btn-close-hw-modal')!; const btnCancelFooter = document.getElementById('btn-cancel-hw-modal')!; bindLocationEvents('hw-bldg-select', 'hw-floor-select', 'hw-loc-etc-group', 'hw-loc-etc'); applyDateMask(document.getElementById('hw-purchase_date') as HTMLInputElement); const closeModalAction = () => { closeModals(); isEditMode = false; }; btnCloseHeader.addEventListener('click', closeModalAction); btnCancelFooter.addEventListener('click', closeModalAction); revertBtn.addEventListener('click', () => { setEditLock('hw-asset-form', 'view', { saveBtnId: 'btn-save-hw-asset', revertBtnId: 'btn-revert-hw-edit' }); isEditMode = false; if (currentHwAsset) fillHwFormData(currentHwAsset); }); saveBtn.addEventListener('click', async () => { if (!currentHwAsset) return; if (!isEditMode) { setEditLock('hw-asset-form', 'edit', { saveBtnId: 'btn-save-hw-asset', revertBtnId: 'btn-revert-hw-edit' }); isEditMode = true; return; } const formData = new FormData(form); const updated: any = { ...currentHwAsset }; formData.forEach((value, key) => { if (key !== 'id') updated[key] = value; }); // Handle combined location updated.location = getCombinedLocation('hw-bldg-select', 'hw-floor-select', 'hw-loc-etc'); let categoryKey = 'pc'; if (updated.asset_code?.startsWith('SVR')) categoryKey = 'server'; else if (updated.asset_code?.startsWith('STO')) categoryKey = 'storage'; else if (updated.asset_code?.startsWith('EQP')) categoryKey = 'equipment'; const success = await saveAsset(categoryKey, updated); if (success) { alert(UI_TEXT.MESSAGES.SAVE_SUCCESS); onSave(); closeModalAction(); } }); createIcons({ icons: { X, History, Plus, Save, Paperclip, Calendar, Monitor, Cpu, Network, ShieldCheck } }); } export function openHwModal(asset: any, mode: 'view' | 'edit' | 'add' = 'view') { currentHwAsset = asset; const modal = document.getElementById('hw-asset-modal')!; setEditLock('hw-asset-form', mode, { saveBtnId: 'btn-save-hw-asset', revertBtnId: 'btn-revert-hw-edit', addLogBtnId: 'btn-add-hw-log' }); isEditMode = (mode === 'add' || mode === 'edit'); fillHwFormData(asset); // Show/Hide category specific fields const serverOnly = document.querySelectorAll('.server-only'); const nonServer = document.querySelectorAll('.non-server'); const pcOnly = document.querySelectorAll('.pc-only'); const isServer = asset.category === '서버' || asset.asset_code?.startsWith('SVR'); const isPc = asset.category === 'PC' || asset.asset_code?.startsWith('PC'); serverOnly.forEach(el => (el as HTMLElement).style.display = isServer ? 'flex' : 'none'); nonServer.forEach(el => (el as HTMLElement).style.display = !isServer ? 'flex' : 'none'); pcOnly.forEach(el => (el as HTMLElement).style.display = isPc ? 'flex' : 'none'); modal.classList.remove('hidden'); } function fillHwFormData(asset: any) { setFieldValue('hw-id', asset.id); setFieldValue('hw-asset_code', asset.asset_code || ''); setFieldValue('hw-purchase_corp', asset.purchase_corp || ''); setFieldValue('hw-category', asset.category || ''); setFieldValue('hw-hw_status', asset.hw_status || '운영'); setFieldValue('hw-current_dept', asset.current_dept || ''); setFieldValue('hw-previous_dept', asset.previous_dept || ''); setFieldValue('hw-manager_primary', asset.manager_primary || ''); setFieldValue('hw-manager_secondary', asset.manager_secondary || ''); setFieldValue('hw-current_user', asset.current_user || ''); setFieldValue('hw-previous_user', asset.previous_user || ''); setFieldValue('hw-asset_purpose', asset.asset_purpose || ''); setFieldValue('hw-model_name', asset.model_name || ''); setFieldValue('hw-cpu', asset.cpu || ''); setFieldValue('hw-ram', asset.ram || ''); setFieldValue('hw-gpu', asset.gpu || ''); setFieldValue('hw-ssd_1', asset.ssd_1 || ''); setFieldValue('hw-ssd_2', asset.ssd_2 || ''); setFieldValue('hw-hdd_1', asset.hdd_1 || ''); setFieldValue('hw-hdd_2', asset.hdd_2 || ''); setFieldValue('hw-hdd_3', asset.hdd_3 || ''); setFieldValue('hw-hdd_4', asset.hdd_4 || ''); setFieldValue('hw-mainboard', asset.mainboard || ''); setFieldValue('hw-mac_address', asset.mac_address || ''); setFieldValue('hw-ip_address', asset.ip_address || ''); setFieldValue('hw-ip_address_2', asset.ip_address_2 || ''); setFieldValue('hw-remote_tool', asset.remote_tool || ''); setFieldValue('hw-remote_id', asset.remote_id || ''); setFieldValue('hw-remote_pw', asset.remote_pw || ''); setFieldValue('hw-monitoring', asset.monitoring || '비대상'); setFieldValue('hw-purchase_date', asset.purchase_date || ''); setFieldValue('hw-purchase_vendor', asset.purchase_vendor || ''); setFieldValue('hw-purchase_amount', asset.purchase_amount || ''); (document.getElementById('hw-approval_document_name') as HTMLElement).textContent = asset.approval_document || ''; setFieldValue('hw-memo', asset.memo || ''); parseAndSetLocation(asset.location || '', 'hw-bldg-select', 'hw-floor-select', 'hw-loc-etc-group', 'hw-loc-etc'); renderHwHistory(asset.id); } function renderHwHistory(assetId: string) { const container = document.getElementById('hw-history-list'); if (!container) return; const logs = (state.masterData.logs || []).filter(l => l.assetId === assetId); if (logs.length === 0) { container.innerHTML = '
이력이 없습니다.
'; return; } container.innerHTML = logs.map(l => `
${l.date}
${l.user}
${l.details}
`).join(''); }