// VaultSentinel - Account Management Application // Main JavaScript functionality const authenticatedFetch = window.fetch.bind(window); const DOCUMENT_FORMATS = window.DocumentFormats || { formats: {}, accept: '', description: 'Tài liệu' }; let authRedirectInProgress = false; window.fetch = async (input, init = {}) => { const response = await authenticatedFetch(input, { ...init, credentials: init.credentials || 'same-origin' }); const requestUrl = typeof input === 'string' ? input : String(input?.url || ''); const isAuthRequest = requestUrl.includes('/api/auth/login') || requestUrl.includes('/api/auth/session') || requestUrl.includes('/api/auth/logout'); if (response.status === 401 && !isAuthRequest && !authRedirectInProgress) { authRedirectInProgress = true; localStorage.removeItem('currentUser'); window.location.replace('../pages/login.html?reason=session-expired'); } return response; }; const APP_TIME_ZONE = 'Asia/Ho_Chi_Minh'; const APP_DATE_FORMATTER = new Intl.DateTimeFormat('vi-VN', { timeZone: APP_TIME_ZONE, year: 'numeric', month: '2-digit', day: '2-digit' }); const APP_DATE_TIME_FORMATTER = new Intl.DateTimeFormat('vi-VN', { timeZone: APP_TIME_ZONE, year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hourCycle: 'h23' }); const APP_TIME_PARTS_FORMATTER = new Intl.DateTimeFormat('en-CA', { timeZone: APP_TIME_ZONE, year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hourCycle: 'h23' }); class AccountManager { constructor() { // Check if user is logged in const currentUser = this.loadFromStorage('currentUser'); if (!currentUser) { window.location.href = '../pages/login.html'; return; } this.currentUser = currentUser; this.accounts = []; this.applications = []; this.users = []; this.assets = []; this.archivedAssets = []; this.archivedAssetSearchTerm = ''; this.archivingAssets = false; this.consumables = []; this.documents = []; this.robots = []; this.robotTypes = []; this.robotTypeSearchTerm = ''; this.robotTypesLoadError = ''; this.robotSearchTerm = ''; this.robotTypeFilter = ''; this.robotPage = 1; this.robotPageSize = 12; this.robotLoadError = ''; this.roles = []; this.notificationPreviewItems = []; this.notificationItems = []; this.notificationUnreadCount = 0; this.notificationPage = 1; this.notificationPageSize = 20; this.notificationFilter = 'all'; this.notificationSourceFilter = ''; this.notificationPagination = { page: 1, pageSize: 20, total: 0, totalPages: 1 }; this.notificationLoading = false; this.notificationLoadError = ''; this.notificationWithdrawalPending = false; this.notificationPollTimer = undefined; this.announcementItems = []; this.announcementShownIds = new Set(); this.announcementLoading = false; this.announcementClosing = false; this.announcementExpiryTimer = undefined; this.accountPage = 1; this.accountPageSize = 9; this.appPage = 1; this.appPageSize = 9; this.userPage = 1; this.userPageSize = 9; this.assetPage = 1; this.assetPageSize = 10; this.consumablePage = 1; this.consumablePageSize = 10; this.consumableExportPage = 1; this.consumableExportPageSize = 10; this.assetBorrowPage = 1; this.assetBorrowPageSize = 10; this.myBorrowedAssetPage = 1; this.myBorrowedAssetPageSize = 10; this.apiBase = '/api'; this.currentPage = 'dashboard'; this.dashboardLoading = false; this.dashboardSourceErrors = {}; this.dashboardUpdatedAt = null; this.accountSearchTerm = ''; this.applicationSearchTerm = ''; this.accountServiceFilter = ''; this.userSearchTerm = ''; this.userRoleFilter = ''; this.assetSearchTerm = ''; this.assetStatusFilter = ''; this.consumableSearchTerm = ''; this.consumableMonthFilter = ''; this.consumableStatusFilter = ''; this.consumableExportSearchTerm = ''; this.consumableExportRecipientFilter = ''; this.consumableExportProjectFilter = ''; this.consumableExportDateFilter = ''; this.documentSearchTerm = ''; this.documentMaxFileSizeMb = 25; this.assetBorrows = []; this.assetBorrowSearchTerm = ''; this.assetBorrowTypeFilter = ''; this.assetBorrowCatalogFilter = 'current'; this.myBorrowedAssetSearchTerm = ''; this.assetBorrowProductSearchTimer = undefined; this.assetBorrowProductItems = []; this.assetBorrowProductQuery = ''; this.assetBorrowProductOffset = 0; this.assetBorrowProductLimit = 40; this.assetBorrowProductHasMore = false; this.assetBorrowProductLoading = false; this.consumableBorrowProductQuery = ''; this.assetDepartments = []; this.assetDepartmentSearchTerm = ''; this.assetProjects = []; this.assetProjectSearchTerm = ''; this.assetExportHistories = []; this.assetExportRequests = []; this.assetExportTab = 'requests'; this.assetExportSearchTerm = ''; this.assetExportStatusFilter = ''; this.assetExportPage = 1; this.assetExportLoading = false; this.assetExportLoadError = ''; this.assetDamageHistories = []; this.consumableExportHistories = []; this.consumableBorrowRequests = []; this.selectedAssetIds = new Set(); this.mobileBreakpoint = 900; this.boundResizeHandler = null; this.boundMobileKeyHandler = null; this.responsiveTableObserver = null; this.responsiveTableUpdatePending = false; this.configureNotifications(); this.initPromise = this.init(); this.pendingAccountAppId = undefined; this.editingAssetBorrowerEntries = []; this.editingAssetStockSnapshot = null; this.pendingBorrowAssetId = undefined; this.editingAssetDepartmentId = undefined; this.pendingDeleteAssetDepartmentId = undefined; this.editingAssetProjectId = undefined; this.pendingDeleteAssetProjectId = undefined; this.assetBorrowRequestType = 'borrow'; this.pendingAssetRequestRejectId = undefined; this.assetBorrowAutoRefreshTimer = undefined; this.pendingAssetRequestDeleteConfirmResolver = undefined; this.pendingBulkAssetDeleteConfirmResolver = undefined; this.pendingAssetDamageId = undefined; this.editingConsumableId = undefined; this.pendingDeleteConsumableId = undefined; this.pendingConsumableExportId = undefined; this.pendingConsumableReturnHistoryId = undefined; this.pendingConsumableRequestRejectId = undefined; } configureNotifications() { if (window.Notiflix?.Notify) { Notiflix.Notify.init({ position: 'right-top', timeout: 2500, clickToClose: true, pauseOnHover: true, distance: '12px', fontSize: '14px' }); } } notifySuccess(message) { if (window.Notiflix?.Notify) { Notiflix.Notify.success(message); } else { alert(message); } } notifyFailure(message) { if (window.Notiflix?.Notify) { Notiflix.Notify.failure(message); } else { alert(message); } } notifyWarning(message) { if (window.Notiflix?.Notify) { Notiflix.Notify.warning(message); } else { alert(message); } } getUserId() { const u = this.currentUser; const detected = u?.UserId ?? u?.userId ?? u?.id ?? u?.ID ?? u?.userid ?? u?.user_id ?? u?.user?.UserId ?? u?.user?.userId; // Fallback: if only username/role exist (no id), use default admin id = 1 return detected ?? 1; } getCurrentUserRoleRaw() { return this.currentUser?.Role ?? this.currentUser?.role ?? this.currentUser?.RoleName ?? this.currentUser?.user?.Role ?? this.currentUser?.user?.role ?? ''; } getCurrentUserRole() { return String(this.getCurrentUserRoleRaw() || '').trim().toLowerCase(); } getCurrentUserDisplayName() { const fullName = String( this.currentUser?.FullName ?? this.currentUser?.fullname ?? this.currentUser?.user?.FullName ?? this.currentUser?.user?.fullname ?? '' ).trim(); const username = String( this.currentUser?.Username ?? this.currentUser?.username ?? this.currentUser?.user?.Username ?? this.currentUser?.user?.username ?? '' ).trim(); return fullName || username || 'Unknown'; } isCurrentUserAdmin() { return this.getCurrentUserRole() === 'admin'; } canCurrentUserManageAssets() { const role = this.getCurrentUserRole(); return role === 'admin' || role === 'asset'; } ensureAssetManagePermission(actionLabel = 'thực hiện thao tác này') { if (this.canCurrentUserManageAssets()) { return true; } this.notifyWarning(`Bạn chỉ có quyền xem tài sản. Chỉ role Asset/Admin mới được ${actionLabel}.`); return false; } getAuthHeaders(includeJson = false) { const headers = {}; if (includeJson) { headers['Content-Type'] = 'application/json'; } return headers; } async init() { await this.fetchApplications(); await this.fetchAccounts(); await this.fetchAssets(); await this.fetchConsumables(); await this.fetchConsumableBorrowRequests(); await this.fetchAssetBorrows(); await this.fetchAssetDepartments(); await this.fetchAssetProjects(); await this.fetchDocuments(); await this.fetchRobotTypes(); await this.fetchRobots(); await this.fetchNotificationPreview(); if (this.canCurrentUserManageAssets()) { await this.fetchUsers(); const administrationSection = document.getElementById('usersSection'); if (administrationSection) administrationSection.style.display = ''; } // Check if user is admin and fetch roles if (this.isCurrentUserAdmin()) { await this.fetchRoles(); // Show Users menu const usersNav = document.getElementById('usersNav'); if (usersNav) usersNav.style.display = ''; const usersSection = document.getElementById('usersSection'); if (usersSection) usersSection.style.display = ''; } this.setupEventListeners(); this.setupNotificationShell(); this.startNotificationPolling(); this.setupResponsiveShell(); this.loadModals(); // Load modals từ file riêng // Single-page navigation based on hash this.handleRoute(location.hash || '#dashboard'); window.addEventListener('hashchange', () => this.handleRoute(location.hash)); this.fetchAnnouncementPopups(); } handleRoute(hash) { const route = (hash || '#dashboard').replace('#', '') || 'dashboard'; if (route !== this.currentPage) document.getElementById('robotDeleteDialog')?.close(); if (route !== this.currentPage) document.getElementById('notificationWithdrawDialog')?.close(); if (this.currentPage === 'robot-bom' && route !== 'robot-bom') { if (this.robotBom?.dirty && !this.robotBom.discardChanges()) { history.replaceState(null, '', '#robot-bom'); return; } this.robotBom?.unmount(); } if (this.isMobileViewport()) { this.closeMobileNav(); } this.renderView(route); } renderView(page) { if (page === 'notification-management' && !this.canCurrentUserManageAssets()) page = 'notifications'; if (page !== this.currentPage && ['notifications', 'notification-management'].includes(page)) { this.notificationPage = 1; this.notificationItems = []; this.notificationLoadError = ''; this.notificationPagination = { page: 1, pageSize: this.notificationPageSize, total: 0, totalPages: 1 }; } if (page === 'asset-borrows' && this.currentPage !== 'asset-borrows') { this.assetBorrowCatalogFilter = 'current'; this.assetBorrowPage = 1; } this.currentPage = page; const mainContent = document.getElementById('mainContent'); if (!mainContent) return; if (page === 'applications') { mainContent.innerHTML = this.getApplicationsContent(); this.setupAccountRowListeners(); this.setupAddButtonListeners(); this.setupFilters(); this.setupAppPagerListeners(); } else if (page === 'assets') { mainContent.innerHTML = this.getAssetsContent(); this.setupAssetRowListeners(); this.setupAddButtonListeners(); this.setupFilters(); this.setupAssetPagerListeners(); } else if (page === 'consumables') { mainContent.innerHTML = this.getConsumablesContent(); this.setupConsumableRowListeners(); this.setupAddButtonListeners(); this.setupFilters(); this.setupConsumablePagerListeners(); } else if (page === 'consumable-exports') { mainContent.innerHTML = this.getConsumableExportsContent(); this.setupConsumableExportHistoryListeners(); this.refreshConsumableExportsPage(); } else if (page === 'asset-borrows') { mainContent.innerHTML = this.getAssetBorrowsContent(); this.setupAssetBorrowListeners(); this.setupAddButtonListeners(); } else if (page === 'asset-exports') { mainContent.innerHTML = this.getAssetExportsContent(); this.setupAssetExportsListeners(); this.refreshAssetExportsPage(); } else if (page === 'my-borrowed-assets') { mainContent.innerHTML = this.getMyBorrowedAssetsContent(); this.setupMyBorrowedAssetsListeners(); } else if (page === 'asset-departments') { mainContent.innerHTML = this.getAssetDepartmentsContent(); this.setupAssetDepartmentListeners(); this.setupAddButtonListeners(); } else if (page === 'asset-projects') { mainContent.innerHTML = this.getAssetProjectsContent(); this.setupAssetProjectListeners(); this.setupAddButtonListeners(); } else if (page === 'accounts') { mainContent.innerHTML = this.getAccountsContent(); this.setupAccountRowListeners(); this.setupAddButtonListeners(); this.setupFilters(); this.setupAccountPagerListeners(); } else if (page === 'robots') { mainContent.innerHTML = this.getRobotsContent(); this.setupRobotListeners(); } else if (page === 'robot-types') { mainContent.innerHTML = this.getRobotTypesContent(); this.setupRobotTypeListeners(); } else if (page === 'robot-bom') { this.robotBom ||= new window.RobotBomController(this); this.robotBom.mount(mainContent, this.pendingBomModel); this.pendingBomModel = null; } else if (page === 'documents') { mainContent.innerHTML = this.getDocumentsContent(); this.setupDocumentListeners(); } else if (page === 'notifications' || page === 'notification-management') { mainContent.innerHTML = this.getNotificationsContent(); this.setupNotificationsPageListeners(); this.refreshNotificationsPage(); } else if (page === 'users') { // Check if user is admin if (!this.isCurrentUserAdmin()) { mainContent.innerHTML = this.renderDashboard(); } else { mainContent.innerHTML = this.getUsersContent(); this.setupUsersRowListeners(); this.setupAddButtonListeners(); this.setupUsersPagerListeners(); } } else { mainContent.innerHTML = this.renderDashboard(); this.setupDashboardListeners(); this.refreshDashboard(); } if (page === 'asset-borrows') { this.startAssetBorrowAutoRefresh(); } else { this.stopAssetBorrowAutoRefresh(); } this.restoreSearchFocus(); this.enhanceResponsiveTables(mainContent); this.updatePendingAssetRequestsBadge(); this.setActiveNav(page); } setActiveNav(page) { let activeGroup = null; document.querySelectorAll('[data-nav]').forEach(link => { const isActive = link.dataset.nav === page; if (isActive) { link.setAttribute('aria-current', 'page'); activeGroup = link.closest('[data-nav-group]')?.dataset.navGroup || null; } else { link.removeAttribute('aria-current'); } }); document.querySelectorAll('[data-nav-toggle]').forEach(button => { button.dataset.active = String(button.dataset.navToggle === activeGroup); }); // Refreshing a view should preserve a group the user opened or collapsed. if (this.activeSidebarPage !== page) { this.setOpenSidebarGroup(activeGroup); this.activeSidebarPage = page; } } setOpenSidebarGroup(groupName = null) { document.querySelectorAll('[data-nav-toggle]').forEach(button => { const isOpen = button.dataset.navToggle === groupName; const panel = document.getElementById(button.getAttribute('aria-controls')); button.setAttribute('aria-expanded', String(isOpen)); if (panel) panel.hidden = !isOpen; }); } setupSidebarNavigation() { document.querySelectorAll('[data-nav-toggle]').forEach(button => { if (button.dataset.boundSidebarToggle) return; button.addEventListener('click', () => { const nextGroup = button.getAttribute('aria-expanded') === 'true' ? null : button.dataset.navToggle; this.setOpenSidebarGroup(nextGroup); }); button.dataset.boundSidebarToggle = 'true'; }); } isMobileViewport() { return window.matchMedia(`(max-width: ${this.mobileBreakpoint}px)`).matches; } setupResponsiveShell() { this.setupSidebarNavigation(); const menuBtn = document.getElementById('mobileMenuBtn'); const backdrop = document.getElementById('sidebarBackdrop'); if (menuBtn && !menuBtn.dataset.boundClick) { menuBtn.addEventListener('click', () => this.toggleMobileNav()); menuBtn.dataset.boundClick = 'true'; } if (backdrop && !backdrop.dataset.boundClick) { backdrop.addEventListener('click', () => this.closeMobileNav()); backdrop.dataset.boundClick = 'true'; } document.querySelectorAll('[data-nav]').forEach(link => { if (!link.dataset.boundMobileClose) { link.addEventListener('click', () => { if (this.isMobileViewport()) { this.closeMobileNav(); } }); link.dataset.boundMobileClose = 'true'; } }); if (!this.boundResizeHandler) { this.boundResizeHandler = () => { if (!this.isMobileViewport()) { this.closeMobileNav(); } }; window.addEventListener('resize', this.boundResizeHandler); } if (!this.boundMobileKeyHandler) { this.boundMobileKeyHandler = (event) => { if (event.key === 'Escape' && document.body.classList.contains('mobile-nav-open')) { this.closeMobileNav(); menuBtn?.focus(); } }; document.addEventListener('keydown', this.boundMobileKeyHandler); } this.setupResponsiveTableObserver(); if (!this.isMobileViewport()) { this.closeMobileNav(); } } toggleMobileNav() { if (document.body.classList.contains('mobile-nav-open')) { this.closeMobileNav(); return; } this.openMobileNav(); } openMobileNav() { if (!this.isMobileViewport()) return; document.body.classList.add('mobile-nav-open'); const menuBtn = document.getElementById('mobileMenuBtn'); if (menuBtn) { menuBtn.setAttribute('aria-expanded', 'true'); } } closeMobileNav() { document.body.classList.remove('mobile-nav-open'); const menuBtn = document.getElementById('mobileMenuBtn'); if (menuBtn) { menuBtn.setAttribute('aria-expanded', 'false'); } } async fetchApplications() { const res = await fetch(`${this.apiBase}/applications`); const data = await res.json(); if (data.success) { this.applications = data.data; } else { console.error('Load applications failed:', data.message); } } async fetchAccounts() { try { const res = await fetch(`${this.apiBase}/accounts/all`); const data = await res.json(); if (data.success) { this.accounts = data.data; } else { console.error('Load accounts failed:', data.message); } } catch (err) { console.error('Fetch accounts error:', err); } } async fetchDocuments(search = '') { try { const query = String(search || '').trim(); const url = query ? `${this.apiBase}/documents?search=${encodeURIComponent(query)}` : `${this.apiBase}/documents`; const response = await fetch(url, { cache: 'no-store' }); const data = await response.json(); if (!response.ok || !data.success) { throw new Error(data.message || 'Không thể tải danh sách tài liệu'); } this.documents = Array.isArray(data.data) ? data.data : []; this.documentMaxFileSizeMb = Number(data.maxFileSizeMb) || 25; } catch (err) { console.error('Fetch documents error:', err); this.documents = []; } } async fetchNotificationJson(endpoint = '', options = {}) { const response = await fetch(`${this.apiBase}/notifications${endpoint}`, { cache: 'no-store', ...options, headers: { Accept: 'application/json', ...options.headers } }); const contentType = response.headers.get('content-type') || ''; if (!/\bapplication\/(?:[\w.-]+\+)?json\b/i.test(contentType)) { const unavailable = response.status === 404 || response.ok; const error = new Error(unavailable ? 'Chức năng thông báo chưa được nạp trên máy chủ. Vui lòng khởi động lại backend rồi tải lại trang.' : `Không tải được thông báo (HTTP ${response.status}). Vui lòng thử lại sau.`); error.status = response.status; throw error; } let data; try { data = await response.json(); } catch { throw new Error('Dữ liệu thông báo trả về không hợp lệ. Vui lòng tải lại trang hoặc thử lại sau.'); } if (!response.ok || !data?.success) { const error = new Error(data?.code === 'API_NOT_FOUND' ? 'Chức năng thông báo chưa được nạp trên máy chủ. Vui lòng khởi động lại backend rồi tải lại trang.' : data?.message || 'Không thể xử lý thông báo. Vui lòng thử lại sau.'); error.status = response.status; throw error; } return data; } async fetchNotificationPreview() { try { const data = await this.fetchNotificationJson('?page=1&pageSize=5&filter=all'); this.notificationPreviewItems = Array.isArray(data.data) ? data.data : []; this.notificationUnreadCount = Number(data.unreadCount) || 0; this.renderNotificationShell(); } catch (err) { console.error('Fetch notification preview error:', err); } } startNotificationPolling() { if (this.notificationPollTimer) window.clearInterval(this.notificationPollTimer); this.notificationPollTimer = window.setInterval(() => Promise.all([ this.fetchNotificationPreview(), this.fetchAnnouncementPopups() ]), 60000); } async fetchAnnouncementPopups() { if (this.announcementLoading) return; this.announcementLoading = true; try { const data = await this.fetchNotificationJson('/announcements', { signal: AbortSignal.timeout(15000) }); if (!Array.isArray(data.data)) throw new Error('Không tải được thông báo popup'); this.announcementItems = data.data; const current = document.getElementById('announcementDialog'); if (current && !this.announcementClosing && !data.data.some(item => Number(item.NotificationId) === Number(current.dataset.notificationId))) { window.clearTimeout(this.announcementExpiryTimer); current.close(); current.remove(); } this.showNextAnnouncement(); } catch (error) { console.error('Fetch announcement error:', error); } finally { this.announcementLoading = false; } } getNextAnnouncement() { return this.announcementItems.find(item => String(item.Source).toLowerCase() === 'admin' && !this.announcementShownIds.has(Number(item.NotificationId)) && (!item.ExpiresAt || new Date(item.ExpiresAt).getTime() > Date.now())); } showNextAnnouncement() { if (document.getElementById('announcementDialog')) return; const item = this.getNextAnnouncement(); if (!item) return; const dialog = document.createElement('dialog'); dialog.id = 'announcementDialog'; dialog.className = 'announcement-dialog'; dialog.dataset.notificationId = String(Number(item.NotificationId)); dialog.setAttribute('aria-labelledby', 'announcementTitle'); dialog.setAttribute('aria-describedby', 'announcementMessage'); dialog.innerHTML = `

THÔNG BÁO CHUNG

${this.escapeHtml(item.Title)}

${this.escapeHtml(item.CreatedByName || 'Admin / Asset')}${item.ExpiresAt ? ` · Kết thúc: ${this.escapeHtml(this.formatDateTime(item.ExpiresAt))}` : ''}

${this.escapeHtml(item.Message)}

`; document.body.appendChild(dialog); this.announcementShownIds.add(Number(item.NotificationId)); dialog.querySelectorAll('[data-announcement-close]').forEach(button => button.addEventListener('click', () => this.finishAnnouncement())); dialog.querySelector('[data-announcement-action]')?.addEventListener('click', () => this.finishAnnouncement({ navigate: true })); dialog.addEventListener('cancel', event => { event.preventDefault(); this.finishAnnouncement(); }); dialog.addEventListener('click', event => { if (event.target !== dialog) return; const rect = dialog.getBoundingClientRect(); if (event.clientX < rect.left || event.clientX > rect.right || event.clientY < rect.top || event.clientY > rect.bottom) this.finishAnnouncement(); }); dialog.showModal(); this.scheduleAnnouncementExpiry(item); } scheduleAnnouncementExpiry(item) { window.clearTimeout(this.announcementExpiryTimer); if (!item.ExpiresAt) return; const remaining = new Date(item.ExpiresAt).getTime() - Date.now(); this.announcementExpiryTimer = window.setTimeout(() => { if (new Date(item.ExpiresAt).getTime() > Date.now()) this.scheduleAnnouncementExpiry(item); else this.finishAnnouncement({ expired: true }); }, Math.max(0, Math.min(remaining, 2147483647))); } async finishAnnouncement({ navigate = false, expired = false } = {}) { const dialog = document.getElementById('announcementDialog'); if (!dialog || this.announcementClosing) return; const notificationId = Number(dialog.dataset.notificationId); const item = this.announcementItems.find(entry => Number(entry.NotificationId) === notificationId); this.announcementClosing = true; dialog.querySelectorAll('button').forEach(button => { button.disabled = true; }); try { if (!expired && dialog.querySelector('#announcementDontShowAgain').checked) { await this.fetchNotificationJson(`/${notificationId}/dismiss`, { method: 'PUT', signal: AbortSignal.timeout(15000) }); } if (!expired && item && !item.IsRead) await this.markNotificationRead(notificationId); window.clearTimeout(this.announcementExpiryTimer); dialog.close(); dialog.remove(); if (navigate && item) { this.closeNotificationPopover(); const actionUrl = String(item.ActionUrl || ''); if (actionUrl.startsWith('#')) window.location.hash = actionUrl; else if (/^\/(?!\/)[^\\\r\n]*$/.test(actionUrl)) window.location.assign(actionUrl); } } catch (error) { const errorBox = dialog.querySelector('#announcementError'); errorBox.textContent = error.message || 'Không lưu được lựa chọn.'; errorBox.hidden = false; dialog.querySelectorAll('button').forEach(button => { button.disabled = false; }); } finally { this.announcementClosing = false; } if (document.getElementById('announcementDialog') === dialog && item?.ExpiresAt && new Date(item.ExpiresAt).getTime() <= Date.now()) { return this.finishAnnouncement({ expired: true }); } if (!document.getElementById('announcementDialog')) this.showNextAnnouncement(); } setupNotificationShell() { const shell = document.getElementById('notificationCenterShell'); const bell = document.getElementById('notificationsBellBtn'); const markAll = document.getElementById('notificationMarkAllBtn'); const viewAll = document.getElementById('notificationViewAllLink'); if (bell && !bell.dataset.boundClick) { bell.addEventListener('click', async event => { event.stopPropagation(); const popover = document.getElementById('notificationPopover'); const willOpen = popover?.classList.contains('hidden'); if (willOpen) { await this.fetchNotificationPreview(); popover?.classList.remove('hidden'); bell.setAttribute('aria-expanded', 'true'); } else { this.closeNotificationPopover(); } }); bell.dataset.boundClick = 'true'; } if (markAll && !markAll.dataset.boundClick) { markAll.addEventListener('click', event => { event.stopPropagation(); this.markAllNotificationsRead(); }); markAll.dataset.boundClick = 'true'; } if (viewAll && !viewAll.dataset.boundClick) { viewAll.addEventListener('click', () => this.closeNotificationPopover()); viewAll.dataset.boundClick = 'true'; } if (shell && !shell.dataset.boundOutsideClick) { document.addEventListener('click', event => { if (!shell.contains(event.target)) this.closeNotificationPopover(); }); shell.dataset.boundOutsideClick = 'true'; } this.renderNotificationShell(); } closeNotificationPopover() { document.getElementById('notificationPopover')?.classList.add('hidden'); document.getElementById('notificationsBellBtn')?.setAttribute('aria-expanded', 'false'); } getNotificationMeta(item = {}) { const severity = String(item.Severity || 'info').toLowerCase(); const severityMap = { success: { icon: 'check_circle', iconClass: 'bg-emerald-100 text-emerald-700' }, warning: { icon: 'warning', iconClass: 'bg-amber-100 text-amber-700' }, error: { icon: 'error', iconClass: 'bg-red-100 text-red-700' }, info: { icon: 'info', iconClass: 'bg-blue-100 text-blue-700' } }; const source = String(item.Source || 'system').toLowerCase(); return { ...(severityMap[severity] || severityMap.info), sourceLabel: source === 'admin' ? 'Thông báo chung' : 'Cá nhân / Hệ thống', sourceIcon: source === 'admin' ? 'campaign' : 'settings_suggest' }; } formatNotificationTime(value) { if (!value) return ''; const date = new Date(value); if (Number.isNaN(date.getTime())) return this.formatDateTime(value); const diffMinutes = Math.round((date.getTime() - Date.now()) / 60000); const absMinutes = Math.abs(diffMinutes); if (absMinutes < 1) return 'Vừa xong'; if (absMinutes < 60) return `${absMinutes} phút trước`; const hours = Math.round(absMinutes / 60); if (hours < 24) return `${hours} giờ trước`; const days = Math.round(hours / 24); if (days < 7) return `${days} ngày trước`; return this.formatDateTime(value); } renderNotificationShell() { const unread = Math.max(0, Number(this.notificationUnreadCount) || 0); const badges = [document.getElementById('notificationBadge'), document.getElementById('notificationNavBadge')]; badges.forEach(badge => { if (!badge) return; badge.textContent = unread > 99 ? '99+' : String(unread); badge.classList.toggle('hidden', unread === 0); }); const summary = document.getElementById('notificationPopoverSummary'); if (summary) summary.textContent = unread ? `${unread} thông báo chưa đọc` : 'Bạn đã xem hết thông báo'; const markAll = document.getElementById('notificationMarkAllBtn'); if (markAll) markAll.classList.toggle('invisible', unread === 0); const list = document.getElementById('notificationPopoverList'); if (!list) return; if (!this.notificationPreviewItems.length) { list.innerHTML = `
notifications_off

Chưa có thông báo

Thông báo mới sẽ xuất hiện tại đây.

`; return; } list.innerHTML = this.notificationPreviewItems.map(item => { const meta = this.getNotificationMeta(item); const isRead = item.IsRead === true || item.IsRead === 1; return ` `; }).join(''); list.querySelectorAll('.notification-preview-item').forEach(button => { button.addEventListener('click', () => this.openNotification(Number(button.dataset.notificationId))); }); } async fetchNotifications() { const params = new URLSearchParams({ page: String(this.notificationPage), pageSize: String(this.notificationPageSize), filter: this.notificationFilter }); if (this.notificationSourceFilter) params.set('source', this.notificationSourceFilter); const route = this.currentPage; const endpoint = route === 'notification-management' ? '/manage' : ''; const data = await this.fetchNotificationJson(`${endpoint}?${params.toString()}`); if (this.currentPage !== route) return; this.notificationItems = Array.isArray(data.data) ? data.data : []; this.notificationPagination = data.pagination || this.notificationPagination; } async refreshNotificationsPage() { this.notificationLoading = true; this.notificationLoadError = ''; this.renderNotificationsPage(); try { await this.fetchNotifications(); } catch (err) { console.error('Fetch notifications error:', err); this.notificationLoadError = err.message || 'Không thể tải thông báo'; this.notifyFailure(err.message || 'Không thể tải thông báo'); } finally { this.notificationLoading = false; this.renderNotificationsPage(); } } renderNotificationsPage() { if (!['notifications', 'notification-management'].includes(this.currentPage)) return; const mainContent = document.getElementById('mainContent'); if (!mainContent) return; mainContent.innerHTML = this.getNotificationsContent(); this.setupNotificationsPageListeners(); } getNotificationCardHtml(item) { const meta = this.getNotificationMeta(item); const isRead = item.IsRead === true || item.IsRead === 1; const managing = this.currentPage === 'notification-management'; const active = item.IsActive !== false && item.IsActive !== 0; const expired = item.ExpiresAt && new Date(item.ExpiresAt).getTime() <= Date.now(); const canWithdraw = managing && active && this.canCurrentUserManageAssets() && (this.isCurrentUserAdmin() || Number(item.CreatedBy) === Number(this.getUserId())) && String(item.Source).toLowerCase() === 'admin'; const author = String(item.Source).toLowerCase() === 'admin' ? (item.CreatedByName || 'Admin / Asset') : 'Hệ thống'; return `
${meta.icon}

${this.escapeHtml(item.Title)}

${isRead || managing ? '' : ''}

${this.escapeHtml(author)} · ${this.formatNotificationTime(item.CreatedDate)}

${meta.sourceIcon}${meta.sourceLabel}

${this.escapeHtml(item.Message)}

${item.ExpiresAt ? `

Kết thúc: ${this.escapeHtml(this.formatDateTime(item.ExpiresAt))}

` : ''} ${managing ? `

${!active ? 'Đã thu hồi' : expired ? 'Đã kết thúc' : 'Đang hiển thị popup'}

` : ''}
${item.ActionUrl ? `` : ''} ${!isRead && !managing ? `` : ''} ${canWithdraw ? `` : ''}
`; } getNotificationsContent() { const managing = this.currentPage === 'notification-management' && this.canCurrentUserManageAssets(); const pagination = this.notificationPagination || {}; const total = Number(pagination.total) || 0; const totalPages = Math.max(1, Number(pagination.totalPages) || 1); const currentPage = Math.max(1, Number(pagination.page) || this.notificationPage); const listHtml = this.notificationLoading ? `
progress_activity

Đang tải thông báo...

` : this.notificationLoadError ? `` : this.notificationItems.length ? this.notificationItems.map(item => this.getNotificationCardHtml(item)).join('') : `
notifications_off

Không có thông báo phù hợp

Hãy thử thay đổi bộ lọc.

`; return `
${managing ? 'campaign' : 'notifications'}${managing ? 'Quản trị viên' : 'Trung tâm thông báo'}

${managing ? 'Đăng thông báo' : 'Thông báo của bạn'}

${managing ? 'Admin / Asset đăng thông báo popup cho người dùng và đặt thời gian kết thúc.' : 'Thông báo chung và kết quả mượn / trả dành riêng cho tài khoản của bạn.'}

${managing ? `` : ''}
${!managing ? `

Chưa đọc

${this.notificationUnreadCount}

Theo bộ lọc

${total}

` : `

${total} thông báo đã đăng. Popup tự dừng khi hết hạn hoặc bị thu hồi. Asset có thể thu hồi thông báo do mình đăng; Admin có thể thu hồi mọi thông báo.

`}
${listHtml}
${!this.notificationLoading && totalPages > 1 ? `` : ''}
`; } setupNotificationsPageListeners() { document.querySelectorAll('.notification-filter-btn').forEach(button => { button.addEventListener('click', () => { this.notificationFilter = button.dataset.notificationFilter || 'all'; this.notificationPage = 1; this.refreshNotificationsPage(); }); }); const sourceFilter = document.getElementById('notificationSourceFilter'); if (sourceFilter) sourceFilter.addEventListener('change', () => { this.notificationSourceFilter = sourceFilter.value; this.notificationPage = 1; this.refreshNotificationsPage(); }); document.querySelectorAll('.notification-page-btn').forEach(button => { button.addEventListener('click', () => { const page = Number(button.dataset.page); if (page > 0) { this.notificationPage = page; this.refreshNotificationsPage(); } }); }); document.querySelectorAll('.notification-read-btn').forEach(button => button.addEventListener('click', () => this.markNotificationRead(Number(button.dataset.notificationId)))); document.querySelectorAll('.notification-action-btn').forEach(button => button.addEventListener('click', () => this.openNotification(Number(button.dataset.notificationId)))); document.querySelectorAll('.notification-withdraw-btn').forEach(button => button.addEventListener('click', () => this.withdrawNotification(Number(button.dataset.notificationId)))); document.getElementById('notificationsMarkAllPageBtn')?.addEventListener('click', () => this.markAllNotificationsRead()); document.getElementById('composeNotificationBtn')?.addEventListener('click', () => this.openNotificationComposer()); document.getElementById('retryNotificationsBtn')?.addEventListener('click', () => this.refreshNotificationsPage()); } findNotification(notificationId) { return this.notificationItems.find(item => Number(item.NotificationId) === Number(notificationId)) || this.notificationPreviewItems.find(item => Number(item.NotificationId) === Number(notificationId)) || this.announcementItems?.find(item => Number(item.NotificationId) === Number(notificationId)); } async markNotificationRead(notificationId, { refreshPage = true } = {}) { const item = this.findNotification(notificationId); if (item && (item.IsRead === true || item.IsRead === 1)) return true; try { await this.fetchNotificationJson(`/${notificationId}/read`, { method: 'PUT' }); [...this.notificationItems, ...this.notificationPreviewItems, ...(this.announcementItems || [])].forEach(entry => { if (Number(entry.NotificationId) === Number(notificationId)) entry.IsRead = true; }); this.notificationUnreadCount = Math.max(0, this.notificationUnreadCount - 1); this.renderNotificationShell(); if (refreshPage) this.renderNotificationsPage(); return true; } catch (err) { this.notifyFailure(err.message || 'Không thể đánh dấu đã đọc'); return false; } } async openNotification(notificationId) { const item = this.findNotification(notificationId); if (!item) return; await this.markNotificationRead(notificationId, { refreshPage: true }); this.closeNotificationPopover(); const actionUrl = String(item.ActionUrl || '').trim(); if (actionUrl.startsWith('#')) window.location.hash = actionUrl; else if (/^\/(?!\/)[^\\\r\n]*$/.test(actionUrl)) window.location.assign(actionUrl); } async markAllNotificationsRead() { try { await this.fetchNotificationJson('/read-all', { method: 'PUT' }); this.notificationItems.forEach(item => { item.IsRead = true; }); this.notificationPreviewItems.forEach(item => { item.IsRead = true; }); this.notificationUnreadCount = 0; this.renderNotificationShell(); if (this.notificationFilter === 'unread') await this.refreshNotificationsPage(); else this.renderNotificationsPage(); } catch (err) { this.notifyFailure(err.message || 'Không thể đánh dấu tất cả đã đọc'); } } confirmNotificationWithdrawal(item) { if (document.getElementById('notificationWithdrawDialog')) return Promise.resolve(false); const dialog = document.createElement('dialog'); dialog.id = 'notificationWithdrawDialog'; dialog.className = 'bom-dialog bom-confirm-dialog notification-withdraw-dialog'; dialog.setAttribute('aria-labelledby', 'notificationWithdrawTitle'); dialog.setAttribute('aria-describedby', 'notificationWithdrawDescription'); dialog.innerHTML = `

QUẢN LÝ THÔNG BÁO

Thu hồi thông báo?

Thông báo này sẽ ngừng hiển thị với tất cả người dùng.

${this.escapeHtml(item.Title || 'Thông báo')} ${item.CreatedByName ? `

Đăng bởi ${this.escapeHtml(item.CreatedByName)}

` : ''}

Thông báo đã thu hồi vẫn được lưu trong danh sách quản trị.

`; return new Promise(resolve => { dialog.addEventListener('click', event => { if (event.target.closest('[data-confirm-notification-withdraw]')) dialog.close('confirm'); else if (event.target.closest('[data-cancel-notification-withdraw]')) dialog.close(); else if (event.target === dialog) { const rect = dialog.getBoundingClientRect(); if (event.clientX < rect.left || event.clientX > rect.right || event.clientY < rect.top || event.clientY > rect.bottom) dialog.close(); } }); dialog.addEventListener('close', () => { dialog.remove(); resolve(dialog.returnValue === 'confirm'); }, { once: true }); document.body.appendChild(dialog); dialog.showModal(); dialog.querySelector('.bom-dialog-footer [data-cancel-notification-withdraw]').focus({ preventScroll: true }); }); } async withdrawNotification(notificationId) { if (this.notificationWithdrawalPending || !this.canCurrentUserManageAssets()) return; const item = this.findNotification(notificationId); if (!item) return; this.notificationWithdrawalPending = true; try { if (!await this.confirmNotificationWithdrawal(item)) return; await this.fetchNotificationJson(`/${notificationId}`, { method: 'DELETE' }); this.notifySuccess('Đã thu hồi thông báo'); await Promise.all([this.fetchNotificationPreview(), this.refreshNotificationsPage()]); } catch (err) { this.notifyFailure(err.message || 'Không thể thu hồi thông báo'); } finally { this.notificationWithdrawalPending = false; } } openNotificationComposer() { if (!this.canCurrentUserManageAssets()) return; document.getElementById('notificationComposerModal')?.remove(); const container = document.createElement('div'); container.id = 'notificationComposerModal'; container.className = 'fixed inset-0 z-[100] flex items-center justify-center bg-slate-950/60 p-4 modal-backdrop open'; container.innerHTML = ` `; document.body.appendChild(container); const expiryParts = this.getAppTimeParts(new Date(Date.now() + 7 * 86400000)); document.getElementById('notificationExpiryInput').value = `${expiryParts.year}-${expiryParts.month}-${expiryParts.day}T${expiryParts.hour}:${expiryParts.minute}`; const close = () => container.remove(); document.getElementById('closeNotificationComposerBtn')?.addEventListener('click', close); document.getElementById('cancelNotificationComposerBtn')?.addEventListener('click', close); container.addEventListener('click', event => { if (event.target === container) close(); }); const messageInput = document.getElementById('notificationMessageInput'); messageInput?.addEventListener('input', () => { document.getElementById('notificationMessageCounter').textContent = `${messageInput.value.length}/2000`; }); document.getElementById('notificationComposerForm')?.addEventListener('submit', event => this.submitNotification(event, close)); document.getElementById('notificationTitleInput')?.focus(); } async submitNotification(event, closeComposer) { event.preventDefault(); if (!this.canCurrentUserManageAssets()) return; const expiryValue = document.getElementById('notificationExpiryInput')?.value || ''; const expiry = expiryValue ? new Date(`${expiryValue}+07:00`) : null; if (!expiry || !Number.isFinite(expiry.getTime()) || expiry.getTime() <= Date.now()) { this.notifyWarning('Vui lòng chọn ngày giờ kết thúc trong tương lai (giờ Việt Nam).'); return; } const payload = { title: document.getElementById('notificationTitleInput')?.value.trim(), message: document.getElementById('notificationMessageInput')?.value.trim(), severity: document.getElementById('notificationSeverityInput')?.value || 'info', actionUrl: document.getElementById('notificationActionInput')?.value || '', expiresAt: expiry.toISOString() }; const sendButton = document.getElementById('sendNotificationBtn'); if (sendButton) sendButton.disabled = true; try { await this.fetchNotificationJson('', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }); closeComposer(); this.notifySuccess('Đã gửi thông báo tới mọi người'); this.notificationPage = 1; await Promise.all([this.fetchNotificationPreview(), this.refreshNotificationsPage()]); } catch (err) { this.notifyFailure(err.message || 'Không thể gửi thông báo'); } finally { if (sendButton?.isConnected) sendButton.disabled = false; } } async fetchAccountSecret(accountId) { const response = await fetch(`${this.apiBase}/accounts/${accountId}/secret`, { cache: 'no-store' }); const data = await response.json(); if (!response.ok || !data.success) { throw new Error(data.message || 'Unable to reveal stored credential'); } return String(data.password || ''); } async fetchUsers() { try { const res = await fetch(`${this.apiBase}/users`); const data = await res.json(); if (data.success) { this.users = data.data; this.refreshAssetCustodianOptions(document.getElementById('assetCustodianInput')?.value || ''); this.refreshBorrowAssetUserOptions(document.getElementById('borrowAssetUserInput')?.value || ''); this.refreshConsumableExportUserOptions(document.getElementById('consumableExportUserInput')?.value || ''); } else { console.error('Load users failed:', data.message); } } catch (err) { console.error('Fetch users error:', err); } } getUserDisplayName(user) { const fullname = String(user?.FullName || user?.fullname || '').trim(); const username = String(user?.Username || user?.username || '').trim(); return fullname || username || ''; } getUniqueUserDisplayNames() { const users = Array.isArray(this.users) ? this.users : []; const seenNames = new Set(); return users .map(user => this.getUserDisplayName(user)) .filter(name => { if (!name) return false; const key = name.toLowerCase(); if (seenNames.has(key)) return false; seenNames.add(key); return true; }) .sort((a, b) => a.localeCompare(b, 'vi', { sensitivity: 'base' })); } populateUserSelectOptions(selectId, { selectedValue = '', emptyLabel = '-- Chon --' } = {}) { const select = document.getElementById(selectId); if (!select) { return; } const normalizedSelected = String(selectedValue || '').trim(); const userNames = this.getUniqueUserDisplayNames(); select.innerHTML = ''; const emptyOption = document.createElement('option'); emptyOption.value = ''; emptyOption.textContent = emptyLabel; select.appendChild(emptyOption); let hasSelected = false; userNames.forEach(name => { const option = document.createElement('option'); option.value = name; option.textContent = name; if (normalizedSelected && name === normalizedSelected) { option.selected = true; hasSelected = true; } select.appendChild(option); }); if (normalizedSelected && !hasSelected) { const legacyOption = document.createElement('option'); legacyOption.value = normalizedSelected; legacyOption.textContent = normalizedSelected; legacyOption.selected = true; select.appendChild(legacyOption); } else if (!normalizedSelected) { select.value = ''; } } refreshAssetCustodianOptions(selectedValue = '') { this.populateUserSelectOptions('assetCustodianInput', { selectedValue, emptyLabel: '-- Chon nguoi phu trach --' }); } refreshBorrowAssetUserOptions(selectedValue = '') { const input = document.getElementById('borrowAssetUserInput'); if (input) input.value = this.getCurrentUserDisplayName(); } refreshConsumableExportUserOptions(selectedValue = '') { const select = document.getElementById('consumableExportUserInput'); if (!select) { return; } const normalizedSelected = String(selectedValue || '').trim(); const users = (Array.isArray(this.users) ? this.users : []) .map(user => ({ id: Number(user?.UserId ?? user?.userId ?? user?.id), name: this.getUserDisplayName(user) })) .filter(user => Number.isInteger(user.id) && user.id > 0 && user.name) .sort((a, b) => a.name.localeCompare(b.name, 'vi', { sensitivity: 'base' })); select.innerHTML = ''; users.forEach(user => { const option = document.createElement('option'); option.value = String(user.id); option.textContent = user.name; option.dataset.userName = user.name; if (normalizedSelected === String(user.id) || normalizedSelected === user.name) { option.selected = true; } select.appendChild(option); }); } refreshBorrowAssetProjectOptions(selectedValue = '') { const select = document.getElementById('borrowAssetProjectInput'); if (!select) { return; } const normalizedSelected = String(selectedValue || select.value || '').trim(); const projectNames = this.getUniqueAssetProjectNames(); select.innerHTML = ''; const emptyOption = document.createElement('option'); emptyOption.value = ''; emptyOption.textContent = '-- Chọn dự án --'; select.appendChild(emptyOption); let hasSelected = false; projectNames.forEach(name => { const option = document.createElement('option'); option.value = name; option.textContent = name; if (normalizedSelected && name === normalizedSelected) { option.selected = true; hasSelected = true; } select.appendChild(option); }); if (normalizedSelected && !hasSelected) { const legacyOption = document.createElement('option'); legacyOption.value = normalizedSelected; legacyOption.textContent = normalizedSelected; legacyOption.selected = true; select.appendChild(legacyOption); } else if (!normalizedSelected) { select.value = ''; } } getAssetBorrowProductDisplayName(asset) { if (!asset) { return '-- Chọn tài sản --'; } const code = String(asset.AssetCode || '').trim(); const name = String(asset.AssetName || '').trim(); return `${code} - ${name}`.replace(/^\s*-\s*|\s*-\s*$/g, '').trim() || name || '-- Chọn tài sản --'; } isBorrowAssetRequestMode() { const typeInput = document.getElementById('assetBorrowRequestTypeInput'); return this.normalizeAssetRequestType(typeInput?.value || this.assetBorrowRequestType) === 'borrow'; } isAssetAvailableForBorrow(asset) { if (!asset) { return false; } const endingBalance = this.parseOptionalNonNegativeInteger(asset?.EndingBalance ?? asset?.endingBalance); if (endingBalance !== null) { return endingBalance > 0; } const status = String(asset?.Status || asset?.status || '').trim().toLowerCase(); return status !== 'exported'; } getAssetBorrowProductById(assetIdValue) { const assetId = Number(assetIdValue); if (!Number.isFinite(assetId) || assetId <= 0) { return null; } const asset = this.assetBorrowProductItems.find(item => Number(item?.AssetId) === assetId) || this.assets.find(item => Number(item?.AssetId) === assetId) || null; if (this.isBorrowAssetRequestMode() && !this.isAssetAvailableForBorrow(asset)) { return null; } return asset; } updateAssetBorrowProductDisplay(assetIdValue) { const hiddenInput = document.getElementById('assetBorrowProductInput'); const displayNode = document.getElementById('assetBorrowProductDisplayText'); const unitInput = document.getElementById('assetBorrowUnitInput'); const asset = this.getAssetBorrowProductById(assetIdValue); if (hiddenInput) { hiddenInput.value = asset ? String(asset.AssetId) : ''; } if (displayNode) { displayNode.textContent = this.getAssetBorrowProductDisplayName(asset); displayNode.classList.toggle('text-slate-600', !asset); displayNode.classList.toggle('text-slate-700', !!asset); } if (unitInput) { unitInput.value = asset ? String(asset.Unit || '').trim() : ''; } } openAssetBorrowProductDropdown() { const dropdown = document.getElementById('assetBorrowProductDropdown'); const searchInput = document.getElementById('assetBorrowProductSearchInput'); if (!dropdown) { return; } dropdown.classList.remove('hidden'); if (searchInput) { searchInput.focus(); searchInput.select(); } } closeAssetBorrowProductDropdown() { const dropdown = document.getElementById('assetBorrowProductDropdown'); if (dropdown) { dropdown.classList.add('hidden'); } } renderAssetBorrowProductList() { const listNode = document.getElementById('assetBorrowProductList'); const loadingNode = document.getElementById('assetBorrowProductLoading'); const hiddenInput = document.getElementById('assetBorrowProductInput'); if (!listNode) { return; } const selectedAssetId = Number(hiddenInput?.value || 0); listNode.style.maxHeight = '224px'; listNode.style.overflow = 'auto'; if (!this.assetBorrowProductItems.length && !this.assetBorrowProductLoading) { listNode.innerHTML = `
Không tìm thấy tài sản phù hợp.
`; } else { listNode.innerHTML = this.assetBorrowProductItems.map(asset => { const assetId = Number(asset?.AssetId); const isSelected = Number.isFinite(selectedAssetId) && selectedAssetId === assetId; const displayName = this.getAssetBorrowProductDisplayName(asset); return ` `; }).join(''); } if (loadingNode) { loadingNode.classList.toggle('hidden', !this.assetBorrowProductLoading); } document.querySelectorAll('.asset-borrow-product-option').forEach(button => { if (button.dataset.boundClick === 'true') { return; } button.addEventListener('click', () => { const assetId = Number(button.dataset.assetId); this.updateAssetBorrowProductDisplay(assetId); this.closeAssetBorrowProductDropdown(); }); button.dataset.boundClick = 'true'; }); } resetAssetBorrowProductSearchState(query = '') { this.assetBorrowProductQuery = String(query || '').trim(); this.assetBorrowProductOffset = 0; this.assetBorrowProductHasMore = true; this.assetBorrowProductItems = []; } async searchAssetBorrowProducts(keyword = '', selectedAssetId = '', options = {}) { const { reset = true } = options; const query = String(keyword || '').trim(); const currentSelectedId = selectedAssetId || document.getElementById('assetBorrowProductInput')?.value || ''; if (reset) { this.resetAssetBorrowProductSearchState(query); } if (this.assetBorrowProductLoading || !this.assetBorrowProductHasMore) { return; } this.assetBorrowProductLoading = true; this.renderAssetBorrowProductList(); const encodedKeyword = encodeURIComponent(this.assetBorrowProductQuery); const offset = this.assetBorrowProductOffset; const limit = this.assetBorrowProductLimit; const borrowableOnly = this.isBorrowAssetRequestMode(); const appendRows = (rows = [], hasMore = false) => { const rowsArray = Array.isArray(rows) ? rows : []; const source = rowsArray .filter(asset => !borrowableOnly || this.isAssetAvailableForBorrow(asset)); if (source.length) { const merged = new Map( this.assetBorrowProductItems.map(item => [String(item.AssetId), item]) ); source.forEach(item => { merged.set(String(item.AssetId), item); }); this.assetBorrowProductItems = Array.from(merged.values()); } this.assetBorrowProductOffset += rowsArray.length; this.assetBorrowProductHasMore = Boolean(hasMore); this.assetBorrowProductLoading = false; this.renderAssetBorrowProductList(); const selectedValue = String(currentSelectedId || '').trim(); if (selectedValue && this.getAssetBorrowProductById(selectedValue)) { this.updateAssetBorrowProductDisplay(selectedValue); } else if (!document.getElementById('assetBorrowProductInput')?.value && this.assetBorrowProductItems.length) { this.updateAssetBorrowProductDisplay(this.assetBorrowProductItems[0].AssetId); } else { this.updateAssetBorrowProductDisplay(document.getElementById('assetBorrowProductInput')?.value || ''); } }; const fallbackFromLocalAssets = () => { const source = Array.isArray(this.assets) ? this.assets : []; const normalized = this.assetBorrowProductQuery.toLowerCase(); const filtered = source.filter(asset => { if (borrowableOnly && !this.isAssetAvailableForBorrow(asset)) { return false; } if (!normalized) { return true; } const haystack = [ asset?.AssetCode, asset?.AssetName, asset?.Model ].map(value => String(value || '').toLowerCase()); return haystack.some(value => value.includes(normalized)); }); const pageRows = filtered.slice(offset, offset + limit); const hasMore = (offset + pageRows.length) < filtered.length; appendRows(pageRows, hasMore); }; try { const response = await fetch(`${this.apiBase}/assets/search?q=${encodedKeyword}&limit=${limit}&offset=${offset}&borrowableOnly=${borrowableOnly ? '1' : '0'}`, { headers: this.getAuthHeaders(false) }); const data = await response.json(); if (!response.ok || !data.success) { fallbackFromLocalAssets(); return; } appendRows(data.data || [], data.hasMore === true); } catch (err) { console.error('Search asset borrow products error:', err); fallbackFromLocalAssets(); } } setupAssetBorrowRequestModalListeners() { const picker = document.getElementById('assetBorrowProductPicker'); const displayBtn = document.getElementById('assetBorrowProductDisplayBtn'); const dropdown = document.getElementById('assetBorrowProductDropdown'); const productList = document.getElementById('assetBorrowProductList'); const productSearchInput = document.getElementById('assetBorrowProductSearchInput'); if (displayBtn && displayBtn.dataset.boundClick !== 'true') { displayBtn.addEventListener('click', async () => { const isOpen = dropdown && !dropdown.classList.contains('hidden'); if (isOpen) { this.closeAssetBorrowProductDropdown(); return; } this.openAssetBorrowProductDropdown(); if (!this.assetBorrowProductItems.length) { await this.searchAssetBorrowProducts(productSearchInput?.value || '', document.getElementById('assetBorrowProductInput')?.value || '', { reset: true }); } }); displayBtn.dataset.boundClick = 'true'; } if (productSearchInput && productSearchInput.dataset.boundInput !== 'true') { productSearchInput.addEventListener('input', () => { if (this.assetBorrowProductSearchTimer) { clearTimeout(this.assetBorrowProductSearchTimer); } const selectedAssetId = document.getElementById('assetBorrowProductInput')?.value || ''; this.assetBorrowProductSearchTimer = setTimeout(() => { this.searchAssetBorrowProducts(productSearchInput.value, selectedAssetId, { reset: true }); }, 250); }); productSearchInput.dataset.boundInput = 'true'; } if (productList && productList.dataset.boundScroll !== 'true') { productList.addEventListener('scroll', () => { const threshold = 40; const isNearBottom = productList.scrollTop + productList.clientHeight >= productList.scrollHeight - threshold; if (!isNearBottom) { return; } if (this.assetBorrowProductHasMore && !this.assetBorrowProductLoading) { this.searchAssetBorrowProducts( productSearchInput?.value || '', document.getElementById('assetBorrowProductInput')?.value || '', { reset: false } ); } }); productList.dataset.boundScroll = 'true'; } if (picker && picker.dataset.boundOutsideClick !== 'true') { document.addEventListener('click', (event) => { if (!picker.contains(event.target)) { this.closeAssetBorrowProductDropdown(); } }); picker.dataset.boundOutsideClick = 'true'; } } getUniqueAssetDepartmentNames() { const rows = Array.isArray(this.assetDepartments) ? this.assetDepartments : []; const seen = new Set(); return rows .map(item => String(item?.DepartmentName || '').trim()) .filter(name => { if (!name) return false; const key = name.toLowerCase(); if (seen.has(key)) return false; seen.add(key); return true; }) .sort((a, b) => a.localeCompare(b, 'vi', { sensitivity: 'base' })); } refreshAssetDepartmentOptions(selectedValue = '') { const select = document.getElementById('assetDepartmentInput'); if (!select) { return; } const normalizedSelected = String(selectedValue || select.value || '').trim(); const departmentNames = this.getUniqueAssetDepartmentNames(); select.innerHTML = ''; const emptyOption = document.createElement('option'); emptyOption.value = ''; emptyOption.textContent = '-- Chọn phòng ban --'; select.appendChild(emptyOption); let hasSelected = false; departmentNames.forEach(name => { const option = document.createElement('option'); option.value = name; option.textContent = name; if (normalizedSelected && name === normalizedSelected) { option.selected = true; hasSelected = true; } select.appendChild(option); }); if (normalizedSelected && !hasSelected) { const legacyOption = document.createElement('option'); legacyOption.value = normalizedSelected; legacyOption.textContent = normalizedSelected; legacyOption.selected = true; select.appendChild(legacyOption); } else if (!normalizedSelected) { select.value = ''; } } getUniqueAssetProjectNames() { const rows = Array.isArray(this.assetProjects) ? this.assetProjects : []; const seen = new Set(); return rows .map(item => String(item?.ProjectName || '').trim()) .filter(name => { if (!name) return false; const key = name.toLowerCase(); if (seen.has(key)) return false; seen.add(key); return true; }) .sort((a, b) => a.localeCompare(b, 'vi', { sensitivity: 'base' })); } refreshAssetProjectOptions(selectedValue = '') { const select = document.getElementById('assetProjectInput'); if (!select) { return; } const normalizedSelected = String(selectedValue || select.value || '').trim(); const projectNames = this.getUniqueAssetProjectNames(); select.innerHTML = ''; const emptyOption = document.createElement('option'); emptyOption.value = ''; emptyOption.textContent = '-- Chọn dự án --'; select.appendChild(emptyOption); let hasSelected = false; projectNames.forEach(name => { const option = document.createElement('option'); option.value = name; option.textContent = name; if (normalizedSelected && name === normalizedSelected) { option.selected = true; hasSelected = true; } select.appendChild(option); }); if (normalizedSelected && !hasSelected) { const legacyOption = document.createElement('option'); legacyOption.value = normalizedSelected; legacyOption.textContent = normalizedSelected; legacyOption.selected = true; select.appendChild(legacyOption); } else if (!normalizedSelected) { select.value = ''; } } setupResponsiveTableObserver() { const mainContent = document.getElementById('mainContent'); if (!mainContent || this.responsiveTableObserver || typeof MutationObserver === 'undefined') { return; } this.responsiveTableObserver = new MutationObserver(() => { if (this.responsiveTableUpdatePending) { return; } this.responsiveTableUpdatePending = true; window.requestAnimationFrame(() => { this.responsiveTableUpdatePending = false; this.enhanceResponsiveTables(mainContent); }); }); this.responsiveTableObserver.observe(mainContent, { childList: true, subtree: true }); } enhanceResponsiveTables(root = document) { if (!root?.querySelectorAll) { return; } root.querySelectorAll('table:not(.keep-table-mobile)').forEach(table => { table.classList.add('mobile-card-table'); const headers = Array.from(table.querySelectorAll('thead th')).map((header, index) => { return String(header.textContent || '').trim() || `Cột ${index + 1}`; }); table.querySelectorAll('tbody tr').forEach(row => { Array.from(row.children).forEach((cell, index) => { if (cell.tagName !== 'TD' || cell.hasAttribute('colspan')) { return; } const headerIndex = Number.isInteger(cell.cellIndex) ? cell.cellIndex : index; cell.dataset.label = headers[headerIndex] || `Cột ${headerIndex + 1}`; }); }); }); } refreshConsumableExportProjectOptions(selectedValue = '') { const select = document.getElementById('consumableExportProjectInput'); if (!select) { return; } const normalizedSelected = String(selectedValue || select.value || '').trim(); const projectNames = this.getUniqueAssetProjectNames(); select.innerHTML = ''; const emptyOption = document.createElement('option'); emptyOption.value = ''; emptyOption.textContent = '-- Chọn dự án --'; select.appendChild(emptyOption); let hasSelected = false; projectNames.forEach(name => { const option = document.createElement('option'); option.value = name; option.textContent = name; if (normalizedSelected && name === normalizedSelected) { option.selected = true; hasSelected = true; } select.appendChild(option); }); if (normalizedSelected && !hasSelected) { const legacyOption = document.createElement('option'); legacyOption.value = normalizedSelected; legacyOption.textContent = normalizedSelected; legacyOption.selected = true; select.appendChild(legacyOption); } else if (!normalizedSelected) { select.value = ''; } } setupConsumableExportTargetTypeListeners() { const targetTypeInput = document.getElementById('consumableExportTargetTypeInput'); const recipientInput = document.getElementById('consumableExportUserInput'); const recipientLabel = document.getElementById('consumableExportRecipientLabel'); const projectInput = document.getElementById('consumableExportProjectInput'); const projectField = document.getElementById('consumableExportProjectField'); const quantityLabel = document.getElementById('consumableExportQuantityLabel'); const noteLabel = document.getElementById('consumableExportNoteLabel'); const submitBtn = document.getElementById('consumableExportSubmitBtn'); if (!targetTypeInput || !recipientInput || !projectInput || !projectField) { return; } const syncTargetType = () => { const isProject = String(targetTypeInput.value || '').trim() === 'project'; projectField.classList.toggle('hidden', !isProject); if (quantityLabel) { quantityLabel.textContent = isProject ? 'Số lượng xuất' : 'Số lượng mượn'; } if (noteLabel) { noteLabel.textContent = isProject ? 'Ghi chú xuất' : 'Ghi chú mượn'; } if (submitBtn) { submitBtn.textContent = isProject ? 'Xuất và tải Excel' : 'Mượn và tải Excel'; } if (isProject) { projectInput.required = true; projectInput.setAttribute('required', 'required'); recipientInput.required = false; recipientInput.removeAttribute('required'); recipientInput.setAttribute('aria-required', 'false'); if (recipientLabel) { recipientLabel.textContent = 'Người nhận (nếu có)'; } } else { projectInput.required = false; projectInput.removeAttribute('required'); recipientInput.required = true; recipientInput.setAttribute('required', 'required'); recipientInput.setAttribute('aria-required', 'true'); if (recipientLabel) { recipientLabel.textContent = 'Người nhận'; } projectInput.value = ''; } }; if (targetTypeInput.dataset.boundChange !== 'true') { targetTypeInput.addEventListener('change', syncTargetType); targetTypeInput.dataset.boundChange = 'true'; } syncTargetType(); } async fetchAssets() { try { const res = await fetch(`${this.apiBase}/assets`); const data = await res.json(); if (data.success) { this.assets = data.data.map(asset => this.normalizeAssetComputedFields(asset)); this.syncSelectedAssetIds(); const borrowModal = document.getElementById('assetBorrowRequestModal'); if (borrowModal?.classList.contains('open')) { await this.searchAssetBorrowProducts( document.getElementById('assetBorrowProductSearchInput')?.value || '', document.getElementById('assetBorrowProductInput')?.value || '' ); } } else { console.error('Load assets failed:', data.message); } } catch (err) { console.error('Fetch assets error:', err); } await this.fetchArchivedAssets(); } async fetchArchivedAssets() { try { const response = await fetch(`${this.apiBase}/assets?archived=1`, { headers: this.getAuthHeaders(false), cache: 'no-store' }); const data = await response.json(); if (!response.ok || !data.success) { throw new Error(data.message || 'Không tải được tài sản cũ'); } this.archivedAssets = (Array.isArray(data.data) ? data.data : []) .map(asset => this.normalizeAssetComputedFields(asset)); return true; } catch (err) { console.error('Fetch archived assets error:', err); return false; } } async fetchConsumables() { try { const res = await fetch(`${this.apiBase}/consumables`); const data = await res.json(); if (data.success) { this.consumables = Array.isArray(data.data) ? data.data : []; } else { console.error('Load consumables failed:', data.message); } } catch (err) { console.error('Fetch consumables error:', err); } } async fetchConsumableBorrowRequests() { try { const res = await fetch(`${this.apiBase}/consumable-borrows`, { headers: this.getAuthHeaders(false), cache: 'no-store' }); const data = await res.json(); if (res.ok && data.success) { this.consumableBorrowRequests = Array.isArray(data.data) ? data.data : []; this.updateConsumableBorrowRequestBadges(); } else { console.error('Load consumable borrow requests failed:', data.message); } } catch (err) { console.error('Fetch consumable borrow requests error:', err); } } async fetchAssetBorrows() { try { const res = await fetch(`${this.apiBase}/asset-borrows`, { headers: this.getAuthHeaders(false) }); const data = await res.json(); if (data.success) { this.assetBorrows = Array.isArray(data.data) ? data.data : []; this.updatePendingAssetRequestsBadge(); } else { console.error('Load asset borrows failed:', data.message); } } catch (err) { console.error('Fetch asset borrows error:', err); } } async fetchAssetDepartments() { try { const res = await fetch(`${this.apiBase}/asset-departments`); const data = await res.json(); if (data.success) { this.assetDepartments = Array.isArray(data.data) ? data.data : []; this.refreshAssetDepartmentOptions(document.getElementById('assetDepartmentInput')?.value || ''); } else { console.error('Load asset departments failed:', data.message); } } catch (err) { console.error('Fetch asset departments error:', err); } } async fetchAssetProjects() { try { const res = await fetch(`${this.apiBase}/asset-projects`); const data = await res.json(); if (data.success) { this.assetProjects = Array.isArray(data.data) ? data.data : []; this.refreshAssetProjectOptions(document.getElementById('assetProjectInput')?.value || ''); this.refreshBorrowAssetProjectOptions(document.getElementById('borrowAssetProjectInput')?.value || ''); this.refreshConsumableExportProjectOptions(document.getElementById('consumableExportProjectInput')?.value || ''); } else { console.error('Load asset projects failed:', data.message); } } catch (err) { console.error('Fetch asset projects error:', err); } } getAssetExportsContent() { const manager = this.canCurrentUserManageAssets(); return `

`; } getFilteredAssetExportRows() { const history = this.assetExportTab === 'history'; const search = String(this.assetExportSearchTerm || '').trim().toLocaleLowerCase('vi'); return (history ? this.assetExportHistories : this.assetExportRequests).filter(row => { // The API also enforces ownership, including saved document URLs. if (!this.canCurrentUserManageAssets() && Number(row.CreatedBy) !== Number(this.getUserId())) return false; if (!history && this.assetExportStatusFilter && row.RequestStatus !== this.assetExportStatusFilter) return false; const values = history ? [row.AssetCode, row.AssetName, row.ProjectName, row.ExportedByName, row.ExportNote] : [row.ExportRequestId, row.RequesterName, row.ProcessedByName, row.RequestNote, row.RejectReason, ...(row.Items || []).flatMap(item => [item.assetCode, item.assetName, item.model, ...item.allocations.map(allocation => allocation.projectName)])]; return !search || values.some(value => String(value || '').toLocaleLowerCase('vi').includes(search)); }); } renderAssetExportRequestsRows(rows) { const safe = value => this.escapeHtml(String(value || '-')); const manager = this.canCurrentUserManageAssets(); return rows.map(row => { const pending = row.RequestStatus === 'pending'; const status = { pending: ['Chờ duyệt', 'bg-amber-50 text-amber-700'], approved: ['Đã duyệt / Đã xuất', 'bg-emerald-50 text-emerald-700'], rejected: ['Đã từ chối', 'bg-red-50 text-red-700'] }[row.RequestStatus] || ['Không xác định', 'bg-slate-50']; const items = (row.Items || []).map(item => `
${safe(item.assetCode)} · ${safe(item.assetName)}
${item.allocations.map(allocation => `
${safe(allocation.projectName)}: ${Number(allocation.quantity)} ${safe(item.unit)}
`).join('')}
`).join(''); return ` #${Number(row.ExportRequestId)}
${this.formatDateTime(row.CreatedDate)}
${safe(row.RequesterName)} ${items} ${status[0]} ${safe(row.RequestNote)}${row.RejectReason ? `
Lý do từ chối: ${safe(row.RejectReason)}
` : ''} ${safe(row.ProcessedByName)}${row.ProcessedDate ? `
${this.formatDateTime(row.ProcessedDate)}
` : ''} ${manager && pending ? `
` : ''} ${row.ExportDocumentId ? `
` : pending && !manager ? 'Chờ admin/asset duyệt' : ''} `; }).join(''); } renderAssetExportsTable() { if (this.currentPage !== 'asset-exports') return; const tbody = document.getElementById('assetExportsTableBody'); if (!tbody) return; const history = this.assetExportTab === 'history'; const columns = history ? ['Ngày xuất', 'Tài sản', 'Số lượng', 'Dự án', 'Người phụ trách', 'Người duyệt xuất', 'Ghi chú', 'Phiếu Excel'] : ['Đơn / Ngày tạo', 'Người đề xuất', 'Tài sản / Dự án / Số lượng', 'Trạng thái', 'Ghi chú', 'Người xử lý', 'Thao tác']; document.getElementById('assetExportsTableHead').innerHTML = `${columns.map(label => `${label}`).join('')}`; const page = this.getPaged(this.getFilteredAssetExportRows(), this.assetExportPage, 10); this.assetExportPage = page.current; tbody.innerHTML = this.assetExportLoading ? `Đang tải đơn xuất...` : !page.total ? `${this.assetExportLoadError ? 'Chưa tải được dữ liệu. Bấm Làm mới để thử lại.' : 'Chưa có đơn xuất phù hợp.'}` : history ? this.buildAssetExportHistoryRowsHtml(page.data) : this.renderAssetExportRequestsRows(page.data); document.getElementById('assetExportsFeedback').textContent = this.assetExportLoadError; document.getElementById('assetExportStatusFilter').hidden = history; document.getElementById('assetExportsPanel').setAttribute('aria-labelledby', history ? 'assetExportHistoryTab' : 'assetExportRequestsTab'); for (const [id, selected] of [['assetExportRequestsTab', !history], ['assetExportHistoryTab', history]]) { const button = document.getElementById(id); button.setAttribute('aria-selected', String(selected)); button.classList.toggle('bg-primary', selected); button.classList.toggle('text-on-primary', selected); } document.getElementById('assetExportsPager').innerHTML = `Hiển thị ${page.start}-${page.end} / ${page.total}
Trang ${page.current} / ${page.totalPages}
`; this.enhanceResponsiveTables(document.getElementById('assetExportsPanel')); } async refreshAssetExportsPage() { if (this.assetExportLoading) return; this.assetExportLoading = true; this.assetExportLoadError = ''; this.renderAssetExportsTable(); try { const [requests, history] = await Promise.all([ fetch(`${this.apiBase}/asset-export-requests`, { headers: this.getAuthHeaders(false), cache: 'no-store' }), fetch(`${this.apiBase}/asset-export-history?limit=2000`, { headers: this.getAuthHeaders(false), cache: 'no-store' }) ]); const [requestData, historyData] = await Promise.all([requests.json(), history.json()]); if (!requests.ok || !requestData.success || !history.ok || !historyData.success) throw new Error(requestData.message || historyData.message || 'Không tải được danh sách đơn xuất.'); this.assetExportRequests = requestData.data || []; this.assetExportHistories = historyData.data || []; } catch (err) { this.assetExportRequests = []; this.assetExportHistories = []; this.assetExportLoadError = err.message || 'Không tải được danh sách đơn xuất.'; } finally { this.assetExportLoading = false; this.renderAssetExportsTable(); } } setupAssetExportsListeners() { document.getElementById('assetExportStatusFilter').value = this.assetExportStatusFilter; document.getElementById('assetExportStatusFilter').onchange = event => { this.assetExportStatusFilter = event.target.value; this.assetExportPage = 1; this.renderAssetExportsTable(); }; document.getElementById('assetExportSearchInput').oninput = event => { this.assetExportSearchTerm = event.target.value; this.assetExportPage = 1; this.renderAssetExportsTable(); }; document.getElementById('refreshAssetExportsBtn').onclick = () => this.refreshAssetExportsPage(); document.getElementById('createAssetExportBtn').onclick = () => { location.hash = '#assets'; this.notifyWarning('Chọn một hoặc nhiều tài sản, sau đó bấm Tạo đơn xuất dự án.'); }; const exportForOthersButton = document.getElementById('createAssetExportForOthersBtn'); if (exportForOthersButton) exportForOthersButton.onclick = () => { location.hash = '#assets'; this.notifyWarning('Chọn một hoặc nhiều tài sản, sau đó bấm Xuất cho người khác.'); }; for (const [id, tab] of [['assetExportRequestsTab', 'requests'], ['assetExportHistoryTab', 'history']]) { document.getElementById(id).onclick = () => { this.assetExportTab = tab; this.assetExportPage = 1; this.renderAssetExportsTable(); }; } document.getElementById('assetExportsPanel').onclick = event => { const button = event.target.closest('button'); if (!button) return; if (button.matches('.asset-export-page-btn')) { this.assetExportPage = Number(button.dataset.page); this.renderAssetExportsTable(); } if (button.matches('.view-asset-export-document')) this.openSavedAssetExportPreview(button.dataset.documentId); if (button.matches('.download-asset-export-document')) this.downloadSavedAssetExport(button.dataset.documentId, button.dataset.fileName, button); if (button.matches('.process-asset-export')) this.openAssetExportProcessModal(Number(button.dataset.requestId), button.dataset.action); }; this.renderAssetExportsTable(); } openAssetExportProcessModal(requestId, action) { if (!this.canCurrentUserManageAssets() || this.assetExportProcessing) return; const modal = document.getElementById('assetExportProcessModal'); const rejected = action === 'rejected'; const reason = document.getElementById('assetExportRejectReason'); reason.value = ''; reason.required = rejected; document.getElementById('assetExportRejectField').hidden = !rejected; document.getElementById('assetExportProcessTitle').textContent = `${rejected ? 'Từ chối' : 'Duyệt xuất dự án'} — Đơn #${requestId}`; document.getElementById('assetExportProcessMessage').textContent = rejected ? 'Nhập lý do để người đề xuất biết và điều chỉnh.' : 'Khi duyệt, toàn bộ số lượng trong đơn sẽ được xuất vào các dự án đã chọn và trừ tồn kho. Tài sản đã xuất không hoàn lại kho.'; document.getElementById('assetExportProcessSubmit').textContent = rejected ? 'Từ chối đơn' : 'Duyệt và xuất vào dự án'; document.getElementById('assetExportProcessForm').onsubmit = event => { event.preventDefault(); this.processAssetExportRequest(requestId, action, reason.value.trim()); }; modal.classList.add('open'); if (rejected) reason.focus(); } async processAssetExportRequest(requestId, action, rejectReason) { if (!this.canCurrentUserManageAssets() || this.assetExportProcessing) return; const button = document.getElementById('assetExportProcessSubmit'); this.assetExportProcessing = true; button.disabled = true; try { const response = await fetch(`${this.apiBase}/asset-export-requests/${requestId}/process`, { method: 'POST', headers: this.getAuthHeaders(true), body: JSON.stringify({ action, rejectReason }) }); const data = await response.json(); if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Không xử lý được đơn xuất.'); return; } document.getElementById('assetExportProcessModal').classList.remove('open'); this.notifySuccess(data.message); await Promise.all([this.refreshAssetExportsPage(), this.fetchAssets()]); } catch (err) { this.notifyFailure(err.message || 'Không xử lý được đơn xuất.'); } finally { this.assetExportProcessing = false; button.disabled = false; } } buildAssetExportHistoryRowsHtml(rows = []) { if (!Array.isArray(rows) || rows.length === 0) { return ` Chưa có dữ liệu lịch sử xuất. `; } return rows.map(item => { const assetLabel = [String(item?.AssetCode || '').trim(), String(item?.AssetName || '').trim()] .filter(Boolean) .join(' - ') || '-'; return ` ${this.formatDateTime(item?.ExportedDate || item?.CreatedDate)} ${this.escapeHtml(assetLabel)} ${Number(item?.ExportQuantity) || 0} ${this.escapeHtml(item?.ProjectName || '-')} ${this.escapeHtml(item?.CustodianName || '-')} ${this.escapeHtml(item?.ExportedByName || '-')} ${this.escapeHtml(item?.ExportNote || '-')} ${item?.ExportDocumentId ? `
${this.escapeHtml(item.DocumentFileName || 'Phiếu xuất Excel')}
` : 'Chưa lưu file'} `; }).join(''); } openAssetExportsPage() { location.hash = '#asset-exports'; } buildSavedAssetExportPreviewHtml(snapshot) { const safe = value => this.escapeHtml(String(value ?? '').trim() || '-'); let index = 0; const rows = (snapshot.items || []).flatMap(item => { let remaining = Number(item.availableStock) || 0; return (item.allocations || []).map(allocation => { const stockBefore = remaining; remaining -= Number(allocation.quantity) || 0; return ` ${++index}
${safe(item.assetName)}
${safe(item.assetCode)}
${safe(item.model)}
Serial: ${safe(item.serialNumber)}
${Number(allocation.quantity) || 0} ${safe(item.unit)} ${stockBefore} ${safe(allocation.projectName)} `; }); }).join(''); return `

Ngày xuất: ${this.formatDateTime(snapshot.exportedDate)}

Người xuất: ${safe(snapshot.exportedByName)}

Người đề xuất: ${safe(snapshot.requesterName || snapshot.borrowerName)}

Số tài sản: ${(snapshot.items || []).length}

Ghi chú: ${safe(snapshot.note)}

${rows}
STTTài sảnModel / SerialSố lượngTồn trước xuấtDự án
`; } async openSavedAssetExportPreview(documentId) { const modal = document.getElementById('assetExportPreviewModal'); const content = document.getElementById('assetExportPreviewContent'); const downloadButton = document.getElementById('assetExportPreviewDownloadBtn'); if (!modal || !content || !downloadButton) return; this.assetExportPreviewRequest = documentId; document.getElementById('assetExportPreviewFileName').textContent = ''; content.textContent = 'Đang tải phiếu xuất...'; downloadButton.disabled = true; modal.classList.add('open'); try { const response = await fetch(`${this.apiBase}/asset-export-documents/${encodeURIComponent(documentId)}`, { headers: this.getAuthHeaders(false) }); const data = await response.json(); if (this.assetExportPreviewRequest !== documentId) return; if (!response.ok || !data.success) throw new Error(data.message || 'Không tải được phiếu xuất.'); document.getElementById('assetExportPreviewFileName').textContent = data.data.fileName; content.innerHTML = this.buildSavedAssetExportPreviewHtml(data.data.snapshot); downloadButton.disabled = false; downloadButton.onclick = () => this.downloadSavedAssetExport(documentId, data.data.fileName, downloadButton); } catch (err) { if (this.assetExportPreviewRequest === documentId) content.textContent = err.message || 'Không tải được phiếu xuất.'; } } async downloadSavedAssetExport(documentId, fileName, button) { if (button?.disabled) return; if (button) button.disabled = true; try { const response = await fetch(`${this.apiBase}/asset-export-documents/${encodeURIComponent(documentId)}/download`, { headers: this.getAuthHeaders(false) }); if (!response.ok) { const error = await response.json(); throw new Error(error.message || 'Không tải được file Excel.'); } this.downloadBlobFile(await response.blob(), fileName); } catch (err) { this.notifyFailure(err.message || 'Không tải được file Excel, vui lòng thử lại.'); } finally { if (button) button.disabled = false; } } async fetchConsumableExportHistories(limit = 300) { try { const safeLimit = Number.isFinite(Number(limit)) ? Math.max(1, Math.min(Number(limit), 2000)) : 300; const res = await fetch(`${this.apiBase}/consumable-export-history?limit=${safeLimit}`, { headers: this.getAuthHeaders(false) }); const data = await res.json(); if (data.success) { this.consumableExportHistories = Array.isArray(data.data) ? data.data : []; } else { console.error('Load consumable export history failed:', data.message); } } catch (err) { console.error('Fetch consumable export history error:', err); } } buildConsumableExportHistoryRowsHtml(rows = []) { if (!Array.isArray(rows) || rows.length === 0) { return ` Chưa có dữ liệu lịch sử mượn / xuất vật tư. `; } return rows.map(item => { const consumableLabel = [String(item?.ConsumableCode || '').trim(), String(item?.ConsumableName || '').trim()] .filter(Boolean) .join(' - ') || '-'; const balanceLabel = `${Number(item?.PreviousEndingBalance) || 0} -> ${Number(item?.NextEndingBalance) || 0}`; const exportedQuantity = this.parseNonNegativeInteger(item?.ExportQuantity, 0); const returnedQuantity = this.parseNonNegativeInteger(item?.ReturnedQuantity, 0); const remainingQuantity = this.parseNonNegativeInteger(item?.RemainingQuantity, 0); const statusMeta = this.getConsumableReturnStatusMeta(item); const returnAction = this.getConsumableReturnActionMeta(item); const returnRequestNotice = this.buildConsumableReturnRequestNoticeHtml(item); const destination = String(item?.ProjectName || '').trim() ? `Dự án: ${String(item.ProjectName).trim()}` : `Người mượn: ${String(item?.RecipientName || '-').trim() || '-'}`; const historyNote = [ item?.ExportNote ? `Xuất/mượn: ${item.ExportNote}` : '', item?.LastReturnedDate ? `Trả gần nhất ${this.formatDateTime(item.LastReturnedDate)}${item?.LastReturnedByName ? ` - ${item.LastReturnedByName}` : ''}${item?.LastReturnNote ? `: ${item.LastReturnNote}` : ''}` : '' ].filter(Boolean).join('\n') || '-'; return ` ${this.formatDateTime(item?.ExportedDate || item?.CreatedDate)} ${this.escapeHtml(consumableLabel)} ${this.escapeHtml(destination)} ${exportedQuantity} ${returnedQuantity} ${remainingQuantity} ${statusMeta.label}${returnRequestNotice} ${this.escapeHtml(item?.ExportedByName || '-')} ${this.escapeHtml(balanceLabel)} ${this.escapeHtml(historyNote)} ${returnAction ? ` ` : '-'} `; }).join(''); } renderConsumableExportHistoryModal() { const tbody = document.getElementById('consumableExportHistoryTableBody'); if (!tbody) { return; } tbody.innerHTML = this.buildConsumableExportHistoryRowsHtml(this.consumableExportHistories); this.setupConsumableReturnActionListeners(); } async openConsumableExportHistoryModal() { const modal = document.getElementById('consumableExportHistoryModal'); const tbody = document.getElementById('consumableExportHistoryTableBody'); if (!modal || !tbody) { this.notifyFailure('Không tìm thấy biểu mẫu lịch sử xuất vật tư.'); return; } tbody.innerHTML = ` Đang tải lịch sử mượn / xuất... `; modal.classList.add('open'); await Promise.all([ this.fetchConsumableExportHistories(), this.fetchConsumableBorrowRequests() ]); this.renderConsumableExportHistoryModal(); } normalizeAssetDamageType(value) { const normalized = String(value || '').trim().toLowerCase(); return normalized === 'disposed' || normalized === 'thanh_ly' || normalized === 'thanh ly' ? 'disposed' : 'damaged'; } getAssetDamageTypeMeta(value) { const type = this.normalizeAssetDamageType(value); if (type === 'disposed') { return { value: 'disposed', label: 'Thanh lý', className: 'bg-slate-100 text-slate-700 border border-slate-200' }; } return { value: 'damaged', label: 'Hỏng', className: 'bg-red-100 text-red-700 border border-red-200' }; } async fetchAssetDamageHistories(limit = 300) { try { const safeLimit = Number.isFinite(Number(limit)) ? Math.max(1, Math.min(Number(limit), 2000)) : 300; const res = await fetch(`${this.apiBase}/asset-damage-disposal-history?limit=${safeLimit}`, { headers: this.getAuthHeaders(false) }); const data = await res.json(); if (data.success) { this.assetDamageHistories = Array.isArray(data.data) ? data.data : []; } else { console.error('Load asset damage/disposal history failed:', data.message); } } catch (err) { console.error('Fetch asset damage/disposal history error:', err); } } buildAssetDamageHistoryRowsHtml(rows = []) { if (!Array.isArray(rows) || rows.length === 0) { return ` Chưa có dữ liệu tài sản hỏng/thanh lý. `; } return rows.map(item => { const typeMeta = this.getAssetDamageTypeMeta(item?.ActionType); const assetLabel = [String(item?.AssetCode || '').trim(), String(item?.AssetName || '').trim()] .filter(Boolean) .join(' - ') || '-'; const unit = String(item?.Unit || '').trim(); const quantityLabel = `${Number(item?.ActionQuantity) || 0}${unit ? ` ${unit}` : ''}`; return ` ${this.formatDateTime(item?.ActionDate || item?.CreatedDate)} ${typeMeta.label} ${this.escapeHtml(assetLabel)} ${this.escapeHtml(quantityLabel)} ${Number(item?.PreviousQuantity) || 0} -> ${Number(item?.NextQuantity) || 0} ${Number(item?.PreviousEndingBalance) || 0} -> ${Number(item?.NextEndingBalance) || 0} ${Number(item?.PreviousNewQuantity) || 0} -> ${Number(item?.NextNewQuantity) || 0} ${Number(item?.PreviousUsedQuantity) || 0} -> ${Number(item?.NextUsedQuantity) || 0} ${this.escapeHtml(item?.CreatedByName || '-')} ${this.escapeHtml(item?.ActionNote || '-')} `; }).join(''); } renderAssetDamageHistoryModal() { const tbody = document.getElementById('assetDamageHistoryTableBody'); if (!tbody) { return; } tbody.innerHTML = this.buildAssetDamageHistoryRowsHtml(this.assetDamageHistories); } async openAssetDamageHistoryModal() { if (!this.ensureAssetManagePermission('xem danh sach tai san hong/thanh ly')) { return; } const modal = document.getElementById('assetDamageHistoryModal'); const tbody = document.getElementById('assetDamageHistoryTableBody'); if (!modal || !tbody) { this.notifyFailure('Không tìm thấy bảng tài sản hỏng/thanh lý.'); return; } tbody.innerHTML = ` Đang tải dữ liệu tài sản hỏng/thanh lý... `; modal.classList.add('open'); await this.fetchAssetDamageHistories(); this.renderAssetDamageHistoryModal(); } async fetchRoles() { try { const res = await fetch(`${this.apiBase}/roles`); const data = await res.json(); if (data.success) { this.roles = data.data; } else { console.error('Load roles failed:', data.message); } } catch (err) { console.error('Fetch roles error:', err); } } async loadModals() { try { const existingContainer = document.getElementById('modalsContainer'); if (existingContainer && existingContainer.children.length) { return; } const response = await fetch('../modals.html', { cache: 'no-store' }); const modalsHTML = await response.text(); const container = existingContainer || document.createElement('div'); if (!container.id) container.id = 'modalsContainer'; container.innerHTML = modalsHTML; if (!container.parentElement) { document.body.appendChild(container); } this.setupFormListeners(); this.setupAccountRowListeners(); this.setupAddButtonListeners(); this.setupFilters(); this.refreshAssetDepartmentOptions(document.getElementById('assetDepartmentInput')?.value || ''); this.refreshAssetProjectOptions(document.getElementById('assetProjectInput')?.value || ''); this.refreshBorrowAssetProjectOptions(document.getElementById('borrowAssetProjectInput')?.value || ''); this.refreshConsumableExportProjectOptions(document.getElementById('consumableExportProjectInput')?.value || ''); this.refreshConsumableExportUserOptions(document.getElementById('consumableExportUserInput')?.value || ''); } catch (error) { console.error('Lỗi load modals:', error); } } restoreSearchFocus() { const accountSearch = document.getElementById('accountSearch'); const appSearch = document.getElementById('appSearch'); const assetSearch = document.getElementById('assetSearch'); const consumableSearch = document.getElementById('consumableSearch'); const consumableExportSearch = document.getElementById('consumableExportSearch'); const assetBorrowSearch = document.getElementById('assetBorrowSearch'); const myBorrowedAssetSearch = document.getElementById('myBorrowedAssetSearch'); const assetDepartmentSearch = document.getElementById('assetDepartmentSearch'); const assetProjectSearch = document.getElementById('assetProjectSearch'); if (accountSearch && accountSearch.dataset.focused === 'true') { const pos = accountSearch.selectionStart || accountSearch.value.length; accountSearch.focus(); accountSearch.setSelectionRange(pos, pos); } if (appSearch && appSearch.dataset.focused === 'true') { const pos = appSearch.selectionStart || appSearch.value.length; appSearch.focus(); appSearch.setSelectionRange(pos, pos); } if (assetSearch && assetSearch.dataset.focused === 'true') { const pos = assetSearch.selectionStart || assetSearch.value.length; assetSearch.focus(); assetSearch.setSelectionRange(pos, pos); } if (consumableSearch && consumableSearch.dataset.focused === 'true') { const pos = consumableSearch.selectionStart || consumableSearch.value.length; consumableSearch.focus(); consumableSearch.setSelectionRange(pos, pos); } if (consumableExportSearch && consumableExportSearch.dataset.focused === 'true') { const pos = consumableExportSearch.selectionStart || consumableExportSearch.value.length; consumableExportSearch.focus(); consumableExportSearch.setSelectionRange(pos, pos); } if (assetBorrowSearch && assetBorrowSearch.dataset.focused === 'true') { const pos = assetBorrowSearch.selectionStart || assetBorrowSearch.value.length; assetBorrowSearch.focus(); assetBorrowSearch.setSelectionRange(pos, pos); } if (myBorrowedAssetSearch && myBorrowedAssetSearch.dataset.focused === 'true') { const pos = myBorrowedAssetSearch.selectionStart || myBorrowedAssetSearch.value.length; myBorrowedAssetSearch.focus(); myBorrowedAssetSearch.setSelectionRange(pos, pos); } if (assetDepartmentSearch && assetDepartmentSearch.dataset.focused === 'true') { const pos = assetDepartmentSearch.selectionStart || assetDepartmentSearch.value.length; assetDepartmentSearch.focus(); assetDepartmentSearch.setSelectionRange(pos, pos); } if (assetProjectSearch && assetProjectSearch.dataset.focused === 'true') { const pos = assetProjectSearch.selectionStart || assetProjectSearch.value.length; assetProjectSearch.focus(); assetProjectSearch.setSelectionRange(pos, pos); } } setupEventListeners() { this.setupModalCloseListeners(); // Close with Escape key document.addEventListener('keydown', (e) => { if (e.key === 'Escape') { this.closeMobileNav(); this.closeModals(); this.closeNotificationPopover(); } }); // Form submissions // Logout button const logoutBtn = document.getElementById('logoutBtn'); if (logoutBtn) { logoutBtn.addEventListener('click', () => this.handleLogout()); } const profileBtn = document.getElementById('profileBtn'); if (profileBtn) { profileBtn.addEventListener('click', () => this.openProfileModal()); } const pendingAssetRequestsBtn = document.getElementById('pendingAssetRequestsBtn'); if (pendingAssetRequestsBtn) { pendingAssetRequestsBtn.addEventListener('click', () => this.openPendingAssetRequestsModal()); } // Update account display this.updateAccountDisplay(); // Account table row clicks this.setupAccountRowListeners(); this.setupFilters(); this.setupResponsiveShell(); } setupFormListeners() { const accountForm = document.getElementById('accountForm'); if (accountForm) { if (!accountForm.dataset.boundSubmit) { accountForm.addEventListener('submit', (e) => this.handleAccountSubmit(e)); accountForm.dataset.boundSubmit = 'true'; } } const appForm = document.getElementById('appForm'); if (appForm) { if (!appForm.dataset.boundSubmit) { appForm.addEventListener('submit', (e) => this.handleAppSubmit(e)); appForm.dataset.boundSubmit = 'true'; } } const assetForm = document.getElementById('assetForm'); if (assetForm) { if (!assetForm.dataset.boundSubmit) { assetForm.addEventListener('submit', (e) => this.handleAssetSubmit(e)); assetForm.dataset.boundSubmit = 'true'; } this.refreshAssetDepartmentOptions(document.getElementById('assetDepartmentInput')?.value || ''); this.refreshAssetProjectOptions(document.getElementById('assetProjectInput')?.value || ''); this.setupAssetStockListeners(); this.setupAssetFormValidationListeners(); } const consumableForm = document.getElementById('consumableForm'); if (consumableForm) { if (!consumableForm.dataset.boundSubmit) { consumableForm.addEventListener('submit', (e) => this.handleConsumableSubmit(e)); consumableForm.dataset.boundSubmit = 'true'; } this.setupConsumableStockListeners(); } const consumableExportForm = document.getElementById('consumableExportForm'); if (consumableExportForm) { if (!consumableExportForm.dataset.boundSubmit) { consumableExportForm.addEventListener('submit', (e) => this.handleConsumableExportSubmit(e)); consumableExportForm.dataset.boundSubmit = 'true'; } this.refreshConsumableExportUserOptions(document.getElementById('consumableExportUserInput')?.value || ''); this.refreshConsumableExportProjectOptions(document.getElementById('consumableExportProjectInput')?.value || ''); this.setupConsumableExportTargetTypeListeners(); } const consumableReturnForm = document.getElementById('consumableReturnForm'); if (consumableReturnForm && !consumableReturnForm.dataset.boundSubmit) { consumableReturnForm.addEventListener('submit', (e) => this.handleConsumableReturnSubmit(e)); consumableReturnForm.dataset.boundSubmit = 'true'; } const consumableBorrowRequestForm = document.getElementById('consumableBorrowRequestForm'); if (consumableBorrowRequestForm && !consumableBorrowRequestForm.dataset.boundSubmit) { consumableBorrowRequestForm.addEventListener('submit', (e) => this.handleConsumableBorrowRequestSubmit(e)); consumableBorrowRequestForm.dataset.boundSubmit = 'true'; } const consumableRequestRejectForm = document.getElementById('consumableRequestRejectForm'); if (consumableRequestRejectForm && !consumableRequestRejectForm.dataset.boundSubmit) { consumableRequestRejectForm.addEventListener('submit', (e) => this.handleConsumableRequestRejectSubmit(e)); consumableRequestRejectForm.dataset.boundSubmit = 'true'; } document.querySelectorAll('.confirm-delete-consumable').forEach(btn => { if (btn.dataset.boundClick === 'true') { return; } btn.addEventListener('click', () => this.confirmDeleteConsumable()); btn.dataset.boundClick = 'true'; }); const borrowAssetForm = document.getElementById('borrowAssetForm'); if (borrowAssetForm) { if (!borrowAssetForm.dataset.boundSubmit) { borrowAssetForm.addEventListener('submit', (e) => this.handleBorrowAssetSubmit(e)); borrowAssetForm.dataset.boundSubmit = 'true'; } } const assetDamageForm = document.getElementById('assetDamageForm'); if (assetDamageForm) { if (!assetDamageForm.dataset.boundSubmit) { assetDamageForm.addEventListener('submit', (e) => this.handleAssetDamageSubmit(e)); assetDamageForm.dataset.boundSubmit = 'true'; } } const assetBorrowRequestForm = document.getElementById('assetBorrowRequestForm'); if (assetBorrowRequestForm) { if (!assetBorrowRequestForm.dataset.boundSubmit) { assetBorrowRequestForm.addEventListener('submit', (e) => this.handleAssetBorrowRequestSubmit(e)); assetBorrowRequestForm.dataset.boundSubmit = 'true'; } } const assetRequestRejectForm = document.getElementById('assetRequestRejectForm'); if (assetRequestRejectForm) { if (!assetRequestRejectForm.dataset.boundSubmit) { assetRequestRejectForm.addEventListener('submit', (e) => this.handleAssetRequestRejectSubmit(e)); assetRequestRejectForm.dataset.boundSubmit = 'true'; } } const confirmAssetRequestDeleteBtn = document.getElementById('confirmAssetRequestDeleteBtn'); if (confirmAssetRequestDeleteBtn && confirmAssetRequestDeleteBtn.dataset.boundClick !== 'true') { confirmAssetRequestDeleteBtn.addEventListener('click', () => this.resolveAssetRequestDeleteConfirm(true)); confirmAssetRequestDeleteBtn.dataset.boundClick = 'true'; } const confirmBulkAssetDeleteBtn = document.getElementById('confirmBulkAssetDeleteBtn'); if (confirmBulkAssetDeleteBtn && confirmBulkAssetDeleteBtn.dataset.boundClick !== 'true') { confirmBulkAssetDeleteBtn.addEventListener('click', () => this.resolveBulkAssetDeleteConfirm(true)); confirmBulkAssetDeleteBtn.dataset.boundClick = 'true'; } this.setupAssetBorrowRequestModalListeners(); const assetDepartmentForm = document.getElementById('assetDepartmentForm'); if (assetDepartmentForm) { if (!assetDepartmentForm.dataset.boundSubmit) { assetDepartmentForm.addEventListener('submit', (e) => this.handleAssetDepartmentSubmit(e)); assetDepartmentForm.dataset.boundSubmit = 'true'; } } document.querySelectorAll('.confirm-delete-asset-department').forEach(btn => { if (btn.dataset.boundClick === 'true') { return; } btn.addEventListener('click', () => this.confirmDeleteAssetDepartment()); btn.dataset.boundClick = 'true'; }); const assetProjectForm = document.getElementById('assetProjectForm'); if (assetProjectForm) { if (!assetProjectForm.dataset.boundSubmit) { assetProjectForm.addEventListener('submit', (e) => this.handleAssetProjectSubmit(e)); assetProjectForm.dataset.boundSubmit = 'true'; } } document.querySelectorAll('.confirm-delete-asset-project').forEach(btn => { if (btn.dataset.boundClick === 'true') { return; } btn.addEventListener('click', () => this.confirmDeleteAssetProject()); btn.dataset.boundClick = 'true'; }); } updateAccountDisplay() { // Use the logged-in user from constructor const usernameEl = document.getElementById('accountUsername'); const roleEl = document.getElementById('accountRole'); if (usernameEl) usernameEl.textContent = this.currentUser?.username || this.currentUser?.Username || 'User'; if (roleEl) roleEl.textContent = this.getCurrentUserRoleRaw() || 'Guest'; } getFilteredAccounts() { const svcFilter = this.accountServiceFilter || ''; const search = (this.accountSearchTerm || '').toLowerCase(); return this.accounts.filter(acc => { const matchesService = !svcFilter || String(acc.AppId) === String(svcFilter); if (!matchesService) return false; if (!search) return true; const hay = [acc.AccountUsername, acc.Email, acc.AppName, acc.AppType].map(v => (v || '').toLowerCase()); return hay.some(val => val.includes(search)); }); } getFilteredApplications() { const search = (this.applicationSearchTerm || '').toLowerCase(); if (!search) return this.applications; return this.applications.filter(app => { const hay = [app.Name, app.Type, app.Description, app.Url, app.Icon].map(v => (v || '').toLowerCase()); return hay.some(val => val.includes(search)); }); } getFilteredAssets() { const statusFilter = (this.assetStatusFilter || '').toLowerCase(); const search = (this.assetSearchTerm || '').toLowerCase(); return this.assets.filter(asset => { const status = String(asset.Status || '').toLowerCase(); const matchesStatus = !statusFilter || status === statusFilter; if (!matchesStatus) { return false; } if (!search) { return true; } const haystack = [ asset.AssetCode, asset.AssetName, asset.Model, asset.SerialNumber, asset.ImportInPeriod, asset.ExportInPeriod, asset.EndingBalance, asset.NewQuantity, asset.UsedQuantity, asset.Department, asset.Project, this.formatAssetProjectsText(asset), asset.Location, asset.Custodian, asset.Borrower, asset.ExportedBy, asset.Notes ].map(v => String(v || '').toLowerCase()); return haystack.some(value => value.includes(search)); }); } getConsumableMonthOptions() { const months = new Set(); (Array.isArray(this.consumables) ? this.consumables : []).forEach(item => { const value = String(item?.RequestMonth || '').trim(); if (value) { months.add(value); } }); return [...months].sort((a, b) => a.localeCompare(b, 'vi')); } getFilteredConsumables() { const monthFilter = String(this.consumableMonthFilter || '').trim().toLowerCase(); const statusFilter = String(this.consumableStatusFilter || '').trim().toLowerCase(); const search = String(this.consumableSearchTerm || '').trim().toLowerCase(); const rows = Array.isArray(this.consumables) ? this.consumables : []; return rows.filter(item => { const matchesMonth = !monthFilter || String(item?.RequestMonth || '').trim().toLowerCase() === monthFilter; if (!matchesMonth) { return false; } const statusMeta = this.getConsumableStockStatusMeta(item); if (statusFilter && statusMeta.key !== statusFilter) { return false; } if (!search) { return true; } const haystack = [ item.ConsumableCode, item.ConsumableName, item.Model, item.Unit, item.RequestMonth, item.OpeningBalance, item.ImportInPeriod, item.ExportInPeriod, item.ExportedSummary, item.RecipientSummary, item.ProjectSummary, item.EndingBalance, item.ExportReason ].map(value => String(value || '').toLowerCase()); return haystack.some(value => value.includes(search)); }); } getConsumableExportRecipientOptions() { const recipients = new Set(); (Array.isArray(this.consumableExportHistories) ? this.consumableExportHistories : []).forEach(item => { const value = String(item?.RecipientName || '').trim(); if (value) { recipients.add(value); } }); return [...recipients].sort((a, b) => a.localeCompare(b, 'vi', { sensitivity: 'base' })); } getConsumableExportProjectOptions() { const projects = new Set(); (Array.isArray(this.consumableExportHistories) ? this.consumableExportHistories : []).forEach(item => { const value = String(item?.ProjectName || '').trim(); if (value) { projects.add(value); } }); return [...projects].sort((a, b) => a.localeCompare(b, 'vi', { sensitivity: 'base' })); } getFilteredConsumableExportHistories() { const search = String(this.consumableExportSearchTerm || '').trim().toLowerCase(); const recipientFilter = String(this.consumableExportRecipientFilter || '').trim().toLowerCase(); const projectFilter = String(this.consumableExportProjectFilter || '').trim().toLowerCase(); const dateFilter = String(this.consumableExportDateFilter || '').trim(); const rows = Array.isArray(this.consumableExportHistories) ? this.consumableExportHistories : []; return rows.filter(item => { const recipientName = String(item?.RecipientName || '').trim(); if (recipientFilter && recipientName.toLowerCase() !== recipientFilter) { return false; } const projectName = String(item?.ProjectName || '').trim(); if (projectFilter && projectName.toLowerCase() !== projectFilter) { return false; } const exportedDateInput = this.toDateInputValue(item?.ExportedDate || item?.CreatedDate); if (dateFilter && exportedDateInput !== dateFilter) { return false; } if (!search) { return true; } const haystack = [ item.ExportHistoryId, item.ConsumableCode, item.ConsumableName, item.Unit, item.ExportQuantity, item.ReturnedQuantity, item.RemainingQuantity, this.getConsumableReturnStatusMeta(item).label, item.RecipientName, item.ProjectName, item.ExportedByName, item.ExportNote, item.PreviousEndingBalance, item.NextEndingBalance, item.ExportedDate, this.formatDateTime(item?.ExportedDate || item?.CreatedDate) ].map(value => String(value || '').toLowerCase()); return haystack.some(value => value.includes(search)); }); } getConsumableStockStatusMeta(consumable = {}) { const endingBalance = this.parseNonNegativeInteger(consumable?.EndingBalance, 0); if (endingBalance <= 0) { return { key: 'out_of_stock', label: 'Hết tồn', className: 'bg-red-50 text-red-700 border border-red-100' }; } return { key: 'in_stock', label: 'Còn tồn', className: 'bg-emerald-50 text-emerald-700 border border-emerald-100' }; } getConsumableReturnStatusMeta(history = {}) { const exportedQuantity = this.parseNonNegativeInteger(history?.ExportQuantity, 0); const returnedQuantity = this.parseNonNegativeInteger(history?.ReturnedQuantity, 0); const isProject = String(history?.TargetType || '').trim() === 'project' || Boolean(String(history?.ProjectName || '').trim()); if (exportedQuantity > 0 && returnedQuantity >= exportedQuantity) { return { key: 'returned', label: 'Đã trả kho', className: 'bg-emerald-50 text-emerald-700 border border-emerald-100' }; } if (returnedQuantity > 0) { return { key: 'partial', label: 'Trả một phần', className: 'bg-amber-50 text-amber-700 border border-amber-100' }; } return { key: 'active', label: isProject ? 'Đang ở dự án' : 'Đang mượn', className: 'bg-blue-50 text-blue-700 border border-blue-100' }; } getConsumableReturnActionMeta(history = {}) { const isProject = String(history?.TargetType || '').trim() === 'project' || Boolean(String(history?.ProjectName || '').trim()); const remainingQuantity = this.parseNonNegativeInteger(history?.RemainingQuantity, 0); const availableQuantity = this.parseNonNegativeInteger( history?.AvailableReturnRequestQuantity, remainingQuantity ); if (isProject || remainingQuantity <= 0 || availableQuantity <= 0) { return null; } if (this.canCurrentUserManageAssets()) { return { mode: 'direct', label: 'Trả kho', title: 'Ghi nhận trả vật tư về kho', availableQuantity }; } if (history?.IsReturnOwner === true || Number(history?.IsReturnOwner) === 1) { return { mode: 'request', label: 'Tạo đơn trả', title: 'Tạo đơn trả vật tư đang nhận', availableQuantity }; } return null; } getConsumableBorrowRequestStatusMeta(value) { const status = String(value || '').trim().toLowerCase(); if (status === 'approved') { return { key: 'approved', label: 'Đã duyệt / đã xuất', className: 'bg-emerald-50 text-emerald-700 border border-emerald-100' }; } if (status === 'rejected') { return { key: 'rejected', label: 'Đã từ chối', className: 'bg-red-50 text-red-700 border border-red-100' }; } return { key: 'pending', label: 'Chờ duyệt', className: 'bg-amber-50 text-amber-700 border border-amber-100' }; } getConsumableRequestTypeMeta(value) { const requestType = String(value || 'borrow').trim().toLowerCase() === 'return' ? 'return' : 'borrow'; return requestType === 'return' ? { key: 'return', label: 'Trả vật tư', className: 'bg-emerald-50 text-emerald-700 border border-emerald-100' } : { key: 'borrow', label: 'Mượn vật tư', className: 'bg-blue-50 text-blue-700 border border-blue-100' }; } getPendingConsumableBorrowRequestCount() { return (Array.isArray(this.consumableBorrowRequests) ? this.consumableBorrowRequests : []) .filter(item => this.getConsumableBorrowRequestStatusMeta(item?.RequestStatus).key === 'pending') .length; } getRejectedConsumableReturnRequests() { const seenExportIds = new Set(); return (Array.isArray(this.consumableBorrowRequests) ? this.consumableBorrowRequests : []) .filter(item => { if (this.getConsumableRequestTypeMeta(item?.RequestType).key !== 'return') { return false; } const exportHistoryId = Number(item?.ExportHistoryId); const key = Number.isInteger(exportHistoryId) && exportHistoryId > 0 ? `export:${exportHistoryId}` : `request:${Number(item?.BorrowRequestId) || 0}`; if (seenExportIds.has(key)) { return false; } seenExportIds.add(key); return this.getConsumableBorrowRequestStatusMeta(item?.RequestStatus).key === 'rejected'; }); } getLatestConsumableReturnRequest(exportHistoryId) { const targetId = Number(exportHistoryId); if (!Number.isInteger(targetId) || targetId <= 0) { return null; } return (Array.isArray(this.consumableBorrowRequests) ? this.consumableBorrowRequests : []) .find(item => Number(item?.ExportHistoryId) === targetId && this.getConsumableRequestTypeMeta(item?.RequestType).key === 'return') || null; } buildConsumableReturnRequestNoticeHtml(history = {}) { const latestRequest = this.getLatestConsumableReturnRequest(history?.ExportHistoryId); if (!latestRequest || this.getConsumableBorrowRequestStatusMeta(latestRequest?.RequestStatus).key !== 'rejected') { return ''; } const requestId = Number(latestRequest?.BorrowRequestId) || '-'; const rejectReason = String(latestRequest?.RejectReason || '').trim() || 'Chưa ghi nhận lý do từ chối'; return `
Đơn trả #${requestId} đã bị từ chối
Lý do: ${this.escapeHtml(rejectReason)}
`; } updateConsumableBorrowRequestBadges() { const pendingCount = this.getPendingConsumableBorrowRequestCount(); const count = this.canCurrentUserManageAssets() ? pendingCount : pendingCount + this.getRejectedConsumableReturnRequests().length; const badge = document.getElementById('consumableBorrowRequestsCountBadge'); if (badge) { badge.textContent = count > 99 ? '99+' : String(count); badge.classList.toggle('hidden', count <= 0); } } normalizeNameForMatching(value) { const normalized = String(value || '').trim().toLowerCase(); if (!normalized) { return ''; } return normalized .normalize('NFD') .replace(/[\u0300-\u036f]/g, '') .replace(/\s+/g, ' '); } getCurrentUserBorrowerNameKeys() { const candidates = [ this.getCurrentUserDisplayName(), this.currentUser?.FullName, this.currentUser?.fullname, this.currentUser?.user?.FullName, this.currentUser?.user?.fullname, this.currentUser?.Username, this.currentUser?.username, this.currentUser?.user?.Username, this.currentUser?.user?.username ]; const keys = new Set(); candidates.forEach(item => { const key = this.normalizeNameForMatching(item); if (key) { keys.add(key); } }); return [...keys]; } getCurrentUserBorrowedAssets() { const borrowerKeys = new Set(this.getCurrentUserBorrowerNameKeys()); if (!borrowerKeys.size) { return []; } return [...this.assets, ...(this.archivedAssets || [])] .map(asset => { const borrowerEntries = this.parseBorrowerEntries(asset?.Borrower); const matchedEntries = borrowerEntries.filter(entry => { const key = this.normalizeNameForMatching(entry?.name); return key && borrowerKeys.has(key); }); const borrowedQuantity = matchedEntries.reduce((sum, entry) => ( sum + this.parseNonNegativeInteger(entry?.quantity, 0) ), 0); if (borrowedQuantity <= 0) { return null; } return { ...asset, BorrowedQuantityByCurrentUser: borrowedQuantity, BorrowedNamesByCurrentUser: matchedEntries .map(entry => this.formatBorrowerDisplay(entry?.name, entry?.quantity)) .filter(Boolean) .join('; ') }; }) .filter(Boolean); } getFilteredMyBorrowedAssets() { const search = String(this.myBorrowedAssetSearchTerm || '').toLowerCase(); const rows = this.getCurrentUserBorrowedAssets(); if (!search) { return rows; } return rows.filter(item => { const haystack = [ item.AssetCode, item.AssetName, item.Model, item.SerialNumber, item.Project, item.Department, item.Location, item.Unit, item.Status, item.BorrowedQuantityByCurrentUser, item.BorrowedNamesByCurrentUser, item.Notes ].map(value => String(value || '').toLowerCase()); return haystack.some(value => value.includes(search)); }); } getFilteredAssetBorrows() { const search = String(this.assetBorrowSearchTerm || '').toLowerCase(); const typeFilter = String(this.assetBorrowTypeFilter || '').trim().toLowerCase(); const catalogFilter = String(this.assetBorrowCatalogFilter || ''); const rows = Array.isArray(this.assetBorrows) ? this.assetBorrows : []; return rows.filter(item => { const requestType = this.normalizeAssetRequestType(item.RequestType); const requestStatus = this.normalizeAssetRequestStatus(item.RequestStatus); const isArchived = this.isArchivedAsset(item); if ((catalogFilter === 'archived' && !isArchived) || (catalogFilter === 'current' && isArchived)) { return false; } const remainingQuantity = this.parseNonNegativeInteger(item?.RemainingQuantity, Math.max(this.parseNonNegativeInteger(item?.BorrowQuantity, 0) - this.parseNonNegativeInteger(item?.ReturnedQuantity, 0), 0)); const matchesType = !typeFilter || requestType === typeFilter || (typeFilter === 'outstanding' && requestType === 'borrow' && requestStatus === 'approved' && remainingQuantity > 0); if (!matchesType) { return false; } if (!search) { return true; } const haystack = [ item.BorrowerName, item.AssetCode, item.AssetName, isArchived ? 'Tài sản cũ' : 'Danh mục hiện tại', this.getAssetRequestTypeMeta(item.RequestType).label, this.getAssetRequestStatusMeta(item.RequestStatus, item).label, item.Unit, item.BorrowQuantity, item.ReturnedQuantity, item.RemainingQuantity, item.BorrowDate, item.RequestNote, item.RejectReason ].map(value => String(value || '').toLowerCase()); return haystack.some(value => value.includes(search)); }); } hasActiveReturnForBorrowRequest(borrowRequest, rows = []) { const assetId = Number(borrowRequest?.AssetId); const borrowerName = String(borrowRequest?.BorrowerName || '').trim().toLowerCase(); const createdBy = Number(borrowRequest?.CreatedBy); const borrowDate = new Date(borrowRequest?.BorrowDate || 0); const borrowTime = Number.isNaN(borrowDate.getTime()) ? null : borrowDate.getTime(); if (!Number.isFinite(assetId) || assetId <= 0) { return false; } return (Array.isArray(rows) ? rows : []).some(candidate => { if (!candidate || candidate === borrowRequest) { return false; } if (this.normalizeAssetRequestType(candidate?.RequestType) !== 'return') { return false; } const status = this.normalizeAssetRequestStatus(candidate?.RequestStatus); if (status !== 'pending' && status !== 'approved') { return false; } if (Number(candidate?.AssetId) !== assetId) { return false; } const candidateCreatedBy = Number(candidate?.CreatedBy); const sameCreator = Number.isFinite(createdBy) && Number.isFinite(candidateCreatedBy) && createdBy === candidateCreatedBy; const sameBorrower = borrowerName && String(candidate?.BorrowerName || '').trim().toLowerCase() === borrowerName; if (!sameCreator && !sameBorrower) { return false; } if (borrowTime === null) { return true; } const returnDate = new Date(candidate?.BorrowDate || 0); const returnTime = Number.isNaN(returnDate.getTime()) ? null : returnDate.getTime(); return returnTime === null || returnTime >= borrowTime; }); } syncSelectedAssetIds() { if (!(this.selectedAssetIds instanceof Set)) { this.selectedAssetIds = new Set(); } const validIds = new Set( this.assets .map(asset => Number(asset.AssetId)) .filter(id => Number.isFinite(id)) ); this.selectedAssetIds = new Set( [...this.selectedAssetIds].filter(id => validIds.has(Number(id))) ); } getPaged(items, page, pageSize) { const total = items.length; const totalPages = Math.max(1, Math.ceil(total / pageSize)); const current = Math.min(Math.max(1, page), totalPages); const start = (current - 1) * pageSize; return { current, total, totalPages, data: items.slice(start, start + pageSize), start: total === 0 ? 0 : start + 1, end: Math.min(total, start + pageSize) }; } maskForeignAccountUsername(username) { const value = String(username || '').trim(); if (!value) return '-'; if (value.length < 5) { return `${value.slice(0, 1)}*****`; } return `${value.slice(0, 3)}*****`; } async handleLogout() { if (confirm('Are you sure you want to logout?')) { try { await fetch(`${this.apiBase}/auth/logout`, { method: 'POST' }); } catch (error) { console.error('Logout request failed:', error); } finally { this.saveToStorage('currentUser', null); localStorage.removeItem('currentUser'); window.location.replace('../pages/login.html'); } } } getDashboardSummary() { const rows = key => this.dashboardSourceErrors?.[key] ? [] : (Array.isArray(this[key]) ? this[key] : []); const assets = rows('assets').filter(item => !this.isArchivedAsset(item)); const assetStock = { inStock: 0, inUse: 0, exported: 0 }; for (const asset of assets) { const metrics = this.buildAssetQuantityMetrics(asset); const available = this.getAssetAvailableForExport(asset); assetStock[available <= 0 ? 'exported' : metrics.exportInPeriod > 0 ? 'inUse' : 'inStock']++; } const consumables = rows('consumables'); const outOfStock = consumables.filter(item => this.getConsumableStockStatusMeta(item).key === 'out_of_stock'); const pendingAssets = rows('assetBorrows').filter(item => this.normalizeAssetRequestStatus(item.RequestStatus) === 'pending'); const pendingConsumables = rows('consumableBorrowRequests').filter(item => this.getConsumableBorrowRequestStatusMeta(item.RequestStatus).key === 'pending'); const recent = [ ...assets.map(item => ({ source: 'Tài sản', icon: 'inventory_2', tone: 'blue', href: '#assets', name: item.AssetName, code: item.AssetCode, date: item.UpdatedDate || item.CreatedDate })), ...consumables.map(item => ({ source: 'Vật tư', icon: 'category', tone: 'green', href: '#consumables', name: item.ConsumableName, code: item.ConsumableCode, date: item.UpdatedDate || item.CreatedDate })), ...rows('robots').map(item => ({ source: 'Robot', icon: 'smart_toy', tone: 'purple', href: '#robots', name: item.Name, code: item.Model, date: item.UpdatedDate || item.CreatedDate })), ...rows('documents').map(item => ({ source: 'Tài liệu', icon: 'description', tone: 'blue', href: '#documents', name: item.Title || item.OriginalFileName || item.FileName, code: item.RobotName, date: item.UploadedDate || item.CreatedDate })) ].filter(item => item.date && Number.isFinite(new Date(item.date).getTime())) .sort((a, b) => new Date(b.date) - new Date(a.date)).slice(0, 6); const robots = [...rows('robots')].sort((a, b) => (Date.parse(b.UpdatedDate || b.CreatedDate) || 0) - (Date.parse(a.UpdatedDate || a.CreatedDate) || 0)); return { assets, assetStock, consumables, outOfStock, pendingAssets, pendingConsumables, recent, robots }; } setupDashboardListeners() { document.getElementById('refreshDashboardBtn')?.addEventListener('click', () => this.refreshDashboard()); document.querySelectorAll('[data-dashboard-action]').forEach(button => { button.addEventListener('click', async () => { if (button.dataset.dashboardAction === 'consumable-requests') { await this.openConsumableBorrowRequestsModal(); } else if (this.canCurrentUserManageAssets()) { await this.openPendingAssetRequestsModal(); } else { location.hash = '#asset-borrows'; } }); }); } async refreshDashboard() { if (this.dashboardLoading) return; this.dashboardSourceErrors ||= {}; this.dashboardLoading = true; const button = document.getElementById('refreshDashboardBtn'); if (button) { button.disabled = true; button.setAttribute('aria-busy', 'true'); button.innerHTML = 'Đang tải…'; } const sources = [ ['assets', 'assets', 'Tài sản'], ['consumables', 'consumables', 'Vật tư tiêu hao'], ['robots', 'robots', 'Robot'], ['robotTypes', 'robots/types', 'Thể loại robot'], ['assetBorrows', 'asset-borrows', 'Yêu cầu tài sản'], ['consumableBorrowRequests', 'consumable-borrows', 'Yêu cầu vật tư'], ['documents', 'documents', 'Tài liệu'], ['accounts', 'accounts/all', 'Tài khoản'], ['assetProjects', 'asset-projects', 'Dự án'], ['assetDepartments', 'asset-departments', 'Phòng ban'] ]; const results = await Promise.allSettled(sources.map(async ([, endpoint]) => { const response = await fetch(`${this.apiBase}/${endpoint}`, { cache: 'no-store', signal: AbortSignal.timeout(15000) }); const data = await response.json(); if (!response.ok || !data.success || !Array.isArray(data.data)) throw new Error('Không tải được dữ liệu'); return data.data; })); results.forEach((result, index) => { const [key, , label] = sources[index]; if (result.status === 'fulfilled') { this[key] = key === 'assets' ? result.value.map(asset => this.normalizeAssetComputedFields(asset)) : result.value; if (key === 'robots') this.robotLoadError = ''; if (key === 'robotTypes') this.robotTypesLoadError = ''; delete this.dashboardSourceErrors[key]; } else { this.dashboardSourceErrors[key] = label; } }); this.dashboardLoading = false; this.dashboardUpdatedAt = new Date(); this.syncSelectedAssetIds(); this.updatePendingAssetRequestsBadge(); this.updateConsumableBorrowRequestBadges(); if (this.currentPage === 'dashboard') { const mainContent = document.getElementById('mainContent'); if (mainContent) { const scrollTop = mainContent.querySelector('.dashboard-page')?.scrollTop || 0; mainContent.innerHTML = this.renderDashboard(); this.setupDashboardListeners(); mainContent.querySelector('.dashboard-page').scrollTop = scrollTop; } } } renderDashboard() { const summary = this.getDashboardSummary(); const errors = this.dashboardSourceErrors || {}; const failedSources = Object.values(errors); const safe = value => this.escapeHtml(value); const number = value => Number(value).toLocaleString('vi-VN'); const count = (key, value) => errors[key] ? '—' : number(value); const length = key => Array.isArray(this[key]) ? this[key].length : 0; const requestsFailed = errors.assetBorrows || errors.consumableBorrowRequests; const pendingCount = summary.pendingAssets.length + summary.pendingConsumables.length; const icon = name => ``; const empty = message => `
${icon('inbox')}

${message}

`; const card = (href, symbol, tone, label, value, unit, detail) => `
${icon(symbol)}${label}${icon('arrow_outward')}
${value}${unit}

${detail}

`; const stockRow = (label, value, total, tone) => `
${label}${number(value)} mã
`; const requestButton = (kind, total, label) => ``; const secondary = (key, href, symbol, label) => `${icon(symbol)}${label}${count(key, length(key))}${icon('chevron_right')}`; return `
KHÔNG GIAN QUẢN LÝ

Tổng quan hệ thống

Theo dõi tài sản, vật tư tiêu hao và robot tại một nơi.

${this.dashboardUpdatedAt ? `Lần tải gần nhất: ${safe(this.formatDateTime(this.dashboardUpdatedAt))}` : 'Đang tổng hợp dữ liệu…'}
${failedSources.length ? `` : ''}
${card('#assets', 'inventory_2', 'blue', 'Tài sản', count('assets', summary.assets.length), 'mã tài sản', errors.assets ? 'Chưa tải được dữ liệu' : `${number(summary.assetStock.inStock + summary.assetStock.inUse)} mã còn trong kho`)} ${card('#consumables', 'category', 'green', 'Vật tư tiêu hao', count('consumables', summary.consumables.length), 'mã vật tư', errors.consumables ? 'Chưa tải được dữ liệu' : `${number(summary.outOfStock.length)} mã hết tồn cần theo dõi`)} ${card('#robots', 'smart_toy', 'purple', 'Robot', count('robots', summary.robots.length), 'robot', errors.robots ? 'Chưa tải được dữ liệu' : `${count('robotTypes', length('robotTypes'))} thể loại robot`)} ${card('#asset-borrows', 'assignment', 'amber', 'Yêu cầu chờ duyệt', requestsFailed ? '—' : number(pendingCount), 'yêu cầu', requestsFailed ? 'Dữ liệu yêu cầu chưa đầy đủ' : `${number(summary.pendingAssets.length)} tài sản · ${number(summary.pendingConsumables.length)} vật tư`)}

Phân bổ theo mã tài sản trong danh mục hiện tại

${errors.assets ? empty('Chưa tải được dữ liệu tài sản.') : summary.assets.length ? ` ${stockRow('Sẵn trong kho', summary.assetStock.inStock, summary.assets.length, 'blue')} ${stockRow('Đang sử dụng một phần', summary.assetStock.inUse, summary.assets.length, 'purple')} ${stockRow('Đã xuất hết / hết tồn', summary.assetStock.exported, summary.assets.length, 'amber')}` : empty('Chưa có tài sản trong danh mục.')}

Theo dõi số mã vật tư còn tồn và hết tồn

${errors.consumables ? empty('Chưa tải được dữ liệu vật tư.') : summary.consumables.length ? ` ${stockRow('Còn tồn trong kho', summary.consumables.length - summary.outOfStock.length, summary.consumables.length, 'green')} ${stockRow('Đã hết tồn', summary.outOfStock.length, summary.consumables.length, 'rose')} ${icon('swap_horiz')}Xem mượn / trả vật tư ${icon('arrow_forward')}` : empty('Chưa có vật tư tiêu hao.')}

${icon('history')}Cập nhật gần đây

Tài sản · Vật tư · Robot · Tài liệu
${summary.recent.length ? `` : empty(failedSources.length ? 'Chưa có dữ liệu cập nhật từ các phân hệ đã tải.' : 'Các mục có ngày tạo hoặc cập nhật sẽ hiển thị tại đây.')}
${secondary('documents', '#documents', 'folder_open', 'Tài liệu')} ${secondary('assetProjects', '#asset-projects', 'work_outline', 'Dự án')} ${secondary('assetDepartments', '#asset-departments', 'corporate_fare', 'Phòng ban')} ${secondary('accounts', '#accounts', 'manage_accounts', 'Tài khoản')}

${icon('notifications_active')}Cần chú ý

${requestsFailed || errors.consumables ? 'Chưa đủ dữ liệu' : `${number(pendingCount + summary.outOfStock.length)} mục`}
${requestsFailed ? '

Chưa tải đủ yêu cầu tài sản / vật tư.

' : ''} ${summary.pendingAssets.length ? requestButton('asset-requests', summary.pendingAssets.length, 'Yêu cầu tài sản chờ duyệt') : ''} ${summary.pendingConsumables.length ? requestButton('consumable-requests', summary.pendingConsumables.length, 'Yêu cầu vật tư chờ duyệt') : ''} ${summary.outOfStock.length ? `${icon('inventory')}Vật tư đã hết tồnCần kiểm tra kế hoạch bổ sung${number(summary.outOfStock.length)}${icon('chevron_right')}
${summary.outOfStock.slice(0, 3).map(item => `${safe(item.ConsumableName || item.ConsumableCode || 'Vật tư')}Hết tồn`).join('')}${summary.outOfStock.length > 3 ? `Xem thêm ${number(summary.outOfStock.length - 3)} mã vật tư` : ''}
` : ''} ${!pendingCount && !summary.outOfStock.length ? empty(requestsFailed || errors.consumables ? 'Bấm Làm mới để tải lại các mục cần chú ý.' : 'Không có yêu cầu chờ duyệt hoặc vật tư hết tồn.') : ''}

${icon('smart_toy')}Robot gần đây

Tất cả ${icon('arrow_forward')}
${errors.robots ? empty('Chưa tải được dữ liệu robot.') : summary.robots.length ? summary.robots.slice(0, 3).map(robot => ` ${this.getRobotAvatarHtml(robot)}${safe(robot.Name || 'Robot')}${safe([robot.Model, robot.Type].filter(Boolean).join(' · ') || 'Chưa có thông tin model')}${number(this.parseNonNegativeInteger(robot.DocumentCount, 0))} tài liệu đính kèm${icon('chevron_right')}`).join('') : empty('Chưa có robot. Quản lý robot tại mục Robot.')} ${icon('account_tree')}Tra cứu BOM robot ${icon('arrow_forward')}
`; } getAccountsContent() { const filteredAccounts = this.getFilteredAccounts(); const currentUserId = this.getUserId(); const pageInfo = this.getPaged(filteredAccounts, this.accountPage, this.accountPageSize); this.accountPage = pageInfo.current; return `
Service
Search
${pageInfo.data.length > 0 ? `
${pageInfo.data.map(acc => { const isOwnAccount = acc.UserId == currentUserId; const canAccessAccount = isOwnAccount || this.getCurrentUserRole() === 'admin'; const accountUsername = acc.AccountUsername || '-'; const displayAccountUsername = canAccessAccount ? accountUsername : this.maskForeignAccountUsername(accountUsername); const createdDate = this.formatDateTime(acc.CreatedDate); const updatedDate = this.formatDateTime(acc.UpdatedDate); const actionContent = canAccessAccount ? `` : '-'; return ` `; }).join('')}
Owner Username Service Created Date Last Updated Actions
Showing ${pageInfo.start}-${pageInfo.end} of ${pageInfo.total}
Page ${pageInfo.current} / ${pageInfo.totalPages}
` : `

No accounts yet. Create one to get started.

`}
`; } async fetchRobotTypes() { try { const response = await fetch(`${this.apiBase}/robots/types`, { cache: 'no-store' }); const data = await response.json(); if (!response.ok || !data.success) throw new Error(data.message || 'Không thể tải thể loại robot'); this.robotTypes = Array.isArray(data.data) ? data.data : []; if (this.robotTypeFilter && !this.robotTypes.some(type => String(type.TypeId) === String(this.robotTypeFilter))) this.robotTypeFilter = ''; this.robotTypesLoadError = ''; } catch (error) { this.robotTypesLoadError = error.message || 'Không thể tải thể loại robot'; } } getRobotTypeResultsContent() { const keyword = String(this.robotTypeSearchTerm || '').trim().toLocaleLowerCase('vi'); const types = this.robotTypes.filter(type => !keyword || [type.Name, type.Description] .some(value => String(value || '').toLocaleLowerCase('vi').includes(keyword))); if (this.robotTypesLoadError) return ``; if (!types.length) return `
category

${keyword ? 'Không tìm thấy thể loại phù hợp' : 'Chưa có thể loại robot'}

${keyword ? 'Hãy thử từ khóa khác.' : 'Thêm thể loại trước, sau đó chọn thể loại khi tạo robot.'}

`; const canManage = this.canCurrentUserManageAssets(); return `
${types.map(type => { const used = Number(type.RobotCount) > 0; return `
category

${this.escapeHtml(type.Name)}

${this.escapeHtml(type.Description || 'Chưa có mô tả.')}

${Number(type.RobotCount) || 0} robot ${canManage ? ` ` : ''}
`; }).join('')}
`; } getRobotTypesContent() { return `
${this.getRobotTypeResultsContent()}
`; } setupRobotTypeListeners() { const results = document.getElementById('robotTypeResults'); document.getElementById('robotTypeSearch')?.addEventListener('input', event => { this.robotTypeSearchTerm = event.target.value; results.innerHTML = this.getRobotTypeResultsContent(); }); document.getElementById('addRobotTypeBtn')?.addEventListener('click', () => this.openRobotTypeForm()); document.getElementById('refreshRobotTypesBtn')?.addEventListener('click', async event => { event.currentTarget.disabled = true; await this.fetchRobotTypes(); if (this.currentPage === 'robot-types') this.renderView('robot-types'); }); results?.addEventListener('click', async event => { const button = event.target.closest('[data-robot-type-action]'); if (!button || button.disabled) return; const type = this.robotTypes.find(item => Number(item.TypeId) === Number(button.dataset.typeId)); if (!type) return; if (button.dataset.robotTypeAction === 'edit') this.openRobotTypeForm(type); else { button.disabled = true; try { await this.deleteRobotType(type); } finally { button.disabled = false; } } }); } openRobotTypeForm(type = null) { if (!this.canCurrentUserManageAssets()) return; const dialog = this.showRobotDialog(`

${type ? 'Sửa thể loại robot' : 'Thêm thể loại robot'}

${Number(type?.RobotCount) > 0 ? `

Đổi tên sẽ cập nhật thể loại hiển thị cho ${Number(type.RobotCount)} robot đang sử dụng.

` : ''}
`); dialog.querySelector('#robotTypeForm').addEventListener('submit', async event => { event.preventDefault(); if (dialog.dataset.saving === 'true') return; const form = event.currentTarget; const name = form.elements.namedItem('name').value.trim(); if (!name) { this.notifyWarning('Vui lòng nhập tên thể loại.'); return; } const description = form.elements.namedItem('description').value.trim(); dialog.dataset.saving = 'true'; const controls = [...form.querySelectorAll('input, textarea, button')]; controls.forEach(control => { control.disabled = true; }); const submit = form.querySelector('[type="submit"]'); submit.textContent = 'Đang lưu...'; try { const response = await fetch(`${this.apiBase}/robots/types${type ? `/${Number(type.TypeId)}` : ''}`, { method: type ? 'PUT' : 'POST', headers: this.getAuthHeaders(true), body: JSON.stringify({ name, description }) }); const data = await response.json(); if (!response.ok || !data.success) throw new Error(data.message || 'Không thể lưu thể loại robot'); dialog.close(); await Promise.all([this.fetchRobotTypes(), this.fetchRobots()]); if (['robots', 'robot-types'].includes(this.currentPage)) this.renderView(this.currentPage); this.notifySuccess(data.message || 'Đã lưu thể loại robot'); } catch (error) { this.notifyFailure(error.message); } finally { delete dialog.dataset.saving; controls.forEach(control => { control.disabled = false; }); submit.textContent = 'Lưu thể loại'; } }); } async deleteRobotType(type) { if (!this.canCurrentUserManageAssets() || Number(type.RobotCount) > 0 || this.robotDeletionPending) return; this.robotDeletionPending = true; try { if (!await this.confirmRobotDeletion({ title: 'Xóa thể loại robot?', name: type.Name, description: 'Thể loại này sẽ được gỡ khỏi danh mục robot.', label: 'Xóa thể loại', note: 'Chỉ xóa được thể loại chưa có robot hoặc BOM sử dụng.' })) return; const response = await fetch(`${this.apiBase}/robots/types/${Number(type.TypeId)}`, { method: 'DELETE' }); const data = await response.json(); if (!response.ok || !data.success) throw new Error(data.message || 'Không thể xóa thể loại'); await this.fetchRobotTypes(); if (this.currentPage === 'robot-types') this.renderView('robot-types'); this.notifySuccess(data.message || 'Đã xóa thể loại robot'); } catch (error) { this.notifyFailure(error.message); } finally { this.robotDeletionPending = false; } } async fetchRobots() { try { const response = await fetch(`${this.apiBase}/robots`, { cache: 'no-store' }); const data = await response.json(); if (!response.ok || !data.success) throw new Error(data.message || 'Không thể tải danh sách robot'); this.robots = Array.isArray(data.data) ? data.data : []; this.documentMaxFileSizeMb = Number(data.maxFileSizeMb) || 25; this.robotLoadError = ''; } catch (error) { this.robotLoadError = error.message || 'Không thể tải danh sách robot'; } } getRobotAvatarHtml(robot, large = false) { const size = large ? 'h-32 w-32' : 'h-20 w-20'; return `
${robot?.HasAvatar ? `Ảnh ${this.escapeHtml(robot.Name)}` : ''}
`; } getFilteredRobots() { const keyword = String(this.robotSearchTerm || '').trim().toLocaleLowerCase('vi'); return this.robots.filter(robot => (!this.robotTypeFilter || String(robot.TypeId) === String(this.robotTypeFilter)) && (!keyword || [robot.Name, robot.Model, robot.Type] .some(value => String(value || '').toLocaleLowerCase('vi').includes(keyword)))); } getRobotResultsContent() { const page = this.getPaged(this.getFilteredRobots(), this.robotPage, this.robotPageSize); this.robotPage = page.current; const canManage = this.canCurrentUserManageAssets(); if (this.robotLoadError) return ``; if (!page.total) return `
smart_toy

${this.robots.length ? 'Không tìm thấy robot phù hợp' : 'Chưa có robot'}

${this.robots.length ? 'Hãy thử từ khóa hoặc loại robot khác.' : 'Thông tin, ảnh đại diện và tài liệu của robot sẽ được lưu tại đây.'}

`; return `
${page.data.map(robot => `
${this.getRobotAvatarHtml(robot)}

${this.escapeHtml(robot.Name)}

Model: ${this.escapeHtml(robot.Model)}

${this.escapeHtml(robot.Type)}
attach_file${Number(robot.DocumentCount) || 0} tài liệu ${canManage ? ` ` : ''}
`).join('')}
Hiển thị ${page.start}–${page.end} / ${page.total} robot
Trang ${page.current} / ${page.totalPages}
`; } getRobotsContent() { const types = this.robotTypes || []; return `
${this.getRobotResultsContent()}
`; } setupRobotListeners() { const results = document.getElementById('robotResults'); const refreshResults = () => { if (results) results.innerHTML = this.getRobotResultsContent(); }; document.getElementById('robotSearch')?.addEventListener('input', event => { this.robotSearchTerm = event.target.value; this.robotPage = 1; refreshResults(); }); document.getElementById('robotTypeFilter')?.addEventListener('change', event => { this.robotTypeFilter = event.target.value; this.robotPage = 1; refreshResults(); }); document.getElementById('addRobotBtn')?.addEventListener('click', () => this.openRobotForm()); document.getElementById('refreshRobotsBtn')?.addEventListener('click', async event => { const button = event.currentTarget; button.disabled = true; await Promise.all([this.fetchRobots(), this.fetchRobotTypes()]); if (this.currentPage === 'robots') this.renderView('robots'); }); results?.addEventListener('click', async event => { const pager = event.target.closest('[data-robot-page]'); if (pager && !pager.disabled) { this.robotPage = Number(pager.dataset.robotPage); refreshResults(); return; } const button = event.target.closest('[data-robot-action]'); if (!button || button.disabled) return; button.disabled = true; try { const id = Number(button.dataset.robotId); if (button.dataset.robotAction === 'view') await this.openRobotDetails(id); if (button.dataset.robotAction === 'bom') { const robot = this.robots.find(robot => Number(robot.RobotId) === id); this.pendingBomModel = robot ? { Model: robot.Model, TypeId: robot.TypeId } : null; location.hash = '#robot-bom'; } if (button.dataset.robotAction === 'edit') await this.openRobotForm(id); if (button.dataset.robotAction === 'delete') await this.deleteRobot(id); } finally { button.disabled = false; } }); } async getRobotDetails(robotId) { const response = await fetch(`${this.apiBase}/robots/${Number(robotId)}`, { cache: 'no-store' }); const data = await response.json(); if (!response.ok || !data.success) throw new Error(data.message || 'Không thể tải thông tin robot'); return data.data; } showRobotDialog(content) { document.getElementById('robotDialog')?.close(); const dialog = document.createElement('dialog'); dialog.id = 'robotDialog'; dialog.setAttribute('aria-labelledby', 'robotDialogTitle'); dialog.className = 'm-auto w-[min(94vw,48rem)] max-w-none max-h-[90dvh] overflow-y-auto rounded-xl border-0 bg-white p-0 text-slate-800 shadow-2xl backdrop:bg-slate-950/50'; dialog.innerHTML = content; document.body.appendChild(dialog); dialog.addEventListener('click', event => { if (dialog.dataset.saving === 'true') return; if (event.target.closest('[data-close-robot]')) dialog.close(); if (event.target === dialog) { const rect = dialog.getBoundingClientRect(); if (event.clientX < rect.left || event.clientX > rect.right || event.clientY < rect.top || event.clientY > rect.bottom) dialog.close(); } }); dialog.addEventListener('cancel', event => { if (dialog.dataset.saving === 'true') event.preventDefault(); }); dialog.addEventListener('close', () => { if (dialog.robotPreviewUrl) URL.revokeObjectURL(dialog.robotPreviewUrl); dialog.remove(); }, { once: true }); dialog.showModal(); return dialog; } async openRobotDetails(robotId) { try { const robot = await this.getRobotDetails(robotId); const dialog = this.showRobotDialog(`

${this.escapeHtml(robot.Name)}

${this.getRobotAvatarHtml(robot, true)}
Model
${this.escapeHtml(robot.Model)}
Thể loại robot
${this.escapeHtml(robot.Type)}

Tài liệu đi kèm (${robot.Documents.length})

${robot.Documents.length ? robot.Documents.map(doc => `
${this.getDocumentPresentation(doc).icon}

${this.escapeHtml(doc.Title)}

${this.escapeHtml(doc.OriginalFileName)} · ${this.formatFileSize(doc.FileSize)}

${this.getDocumentPresentation(doc).preview ? `Xem ${this.getDocumentPresentation(doc).label}` : ''} Tải xuống
`).join('') : '

Robot chưa có tài liệu đi kèm.

'}
${this.canCurrentUserManageAssets() ? '' : ''}
`); dialog.querySelector('#editRobotDetailsBtn')?.addEventListener('click', async event => { const button = event.currentTarget; button.disabled = true; try { await this.openRobotForm(robotId); } finally { button.disabled = false; } }); } catch (error) { this.notifyFailure(error.message); } } async openRobotForm(robotId = null) { if (!this.canCurrentUserManageAssets()) { this.notifyWarning('Chỉ Admin/Asset được thay đổi thông tin robot.'); return; } try { const [robot, libraryResponse, typesResponse] = await Promise.all([ robotId ? this.getRobotDetails(robotId) : Promise.resolve(null), fetch(`${this.apiBase}/documents`, { cache: 'no-store' }), fetch(`${this.apiBase}/robots/types`, { cache: 'no-store' }) ]); const library = await libraryResponse.json(); if (!libraryResponse.ok || !library.success) throw new Error(library.message || 'Không thể tải kho tài liệu'); const types = await typesResponse.json(); if (!typesResponse.ok || !types.success) throw new Error(types.message || 'Không thể tải thể loại robot'); this.robotTypes = types.data || []; this.documents = library.data || []; this.documentMaxFileSizeMb = Number(library.maxFileSizeMb) || 25; const selectedIds = new Set((robot?.Documents || []).map(doc => Number(doc.DocumentId))); const fieldClass = 'w-full rounded-lg border-slate-200 py-2 text-sm focus:ring-primary'; const dialog = this.showRobotDialog(`

${robot ? 'Sửa robot' : 'Thêm robot mới'}

${this.robotTypes.length ? 'Quản lý thể loại robot' : 'Chưa có thể loại. Bấm để thêm thể loại trước.'}

Ảnh đại diện

${this.getRobotAvatarHtml(robot)}

JPG, PNG hoặc WebP, tối đa 5 MB. Có thể bổ sung ảnh sau.

${robot?.HasAvatar ? '' : ''}

Tài liệu đi kèm

Tải tài liệu mới lên hoặc chọn file đã có trong kho. Có thể dùng cả hai cách.

${DOCUMENT_FORMATS.description}. Tối đa 10 file mỗi lần, ${this.documentMaxFileSizeMb} MB/file. File mới cũng được lưu trong mục Tài liệu.

${this.documents.length ? this.documents.map(doc => ``).join('') : '

Kho chưa có tài liệu. Bạn có thể tải file mới lên ở trên.

'}

Bỏ chọn để gỡ tài liệu khỏi robot. File vẫn được giữ trong kho.

`); const form = dialog.querySelector('#robotForm'); form.querySelector('#robotManageTypesLink').addEventListener('click', event => { if (dialog.dataset.saving === 'true') { event.preventDefault(); return; } dialog.close(); }); const avatarInput = form.querySelector('#robotAvatarFile'); const removeAvatar = form.querySelector('#robotRemoveAvatar'); const updatePreview = () => { if (dialog.robotPreviewUrl) { URL.revokeObjectURL(dialog.robotPreviewUrl); dialog.robotPreviewUrl = null; } const file = avatarInput.files[0]; if (file && (!['image/jpeg', 'image/png', 'image/webp'].includes(file.type) || file.size > 5 * 1024 * 1024)) { this.notifyWarning('Chọn ảnh JPG, PNG hoặc WebP, tối đa 5 MB.'); avatarInput.value = ''; } if (avatarInput.files[0]) { dialog.robotPreviewUrl = URL.createObjectURL(avatarInput.files[0]); form.querySelector('#robotAvatarPreview').innerHTML = `Xem trước ảnh robot`; } else form.querySelector('#robotAvatarPreview').innerHTML = this.getRobotAvatarHtml(removeAvatar?.checked ? null : robot); }; avatarInput.addEventListener('change', () => { if (removeAvatar) removeAvatar.checked = false; updatePreview(); }); removeAvatar?.addEventListener('change', () => { avatarInput.value = ''; updatePreview(); }); form.querySelector('#robotDocumentSearch').addEventListener('input', event => { const keyword = event.target.value.trim().toLocaleLowerCase('vi'); form.querySelectorAll('[data-robot-library-item]').forEach(item => { item.classList.toggle('hidden', !item.dataset.search.includes(keyword)); }); }); const updateSelection = () => { form.querySelector('#robotDocumentSelection').textContent = `Đã chọn ${form.querySelectorAll('[name="documentId"]:checked').length} tài liệu từ kho và ${form.querySelector('#robotDocumentFiles').files.length} file mới.`; }; form.addEventListener('change', updateSelection); updateSelection(); form.addEventListener('submit', event => this.saveRobotForm(event, robotId, dialog)); } catch (error) { this.notifyFailure(error.message || 'Không thể mở form robot'); } } async saveRobotForm(event, robotId, dialog) { event.preventDefault(); if (dialog.dataset.saving === 'true' || !this.canCurrentUserManageAssets()) return; const form = event.currentTarget; const documentFiles = [...form.querySelector('#robotDocumentFiles').files]; const documentIds = [...form.querySelectorAll('[name="documentId"]:checked')].map(input => Number(input.value)); if (documentFiles.length > 10 || documentIds.length + documentFiles.length > 100) { this.notifyWarning('Tối đa 10 file mới mỗi lần và 100 tài liệu cho một robot.'); return; } if (documentFiles.some(file => !this.isSupportedDocumentName(file.name) || file.size > this.documentMaxFileSizeMb * 1024 * 1024)) { this.notifyWarning(`Chọn định dạng tài liệu được hỗ trợ, tối đa ${this.documentMaxFileSizeMb} MB mỗi file.`); return; } const payload = new FormData(); for (const key of ['name', 'model', 'typeId']) { const value = form.elements.namedItem(key).value.trim(); if (!value) { this.notifyWarning('Vui lòng nhập tên, model và chọn thể loại robot.'); return; } payload.append(key, value); } payload.append('documentIds', JSON.stringify(documentIds)); payload.append('removeAvatar', String(Boolean(form.querySelector('#robotRemoveAvatar')?.checked))); const avatar = form.querySelector('#robotAvatarFile').files[0]; if (avatar) payload.append('avatar', avatar); documentFiles.forEach(file => payload.append('documents', file)); dialog.dataset.saving = 'true'; const controls = [...form.querySelectorAll('input, select, button')]; controls.forEach(control => { control.disabled = true; }); const saveButton = form.querySelector('#robotSaveBtn'); saveButton.textContent = 'Đang lưu...'; try { const response = await fetch(`${this.apiBase}/robots${robotId ? `/${Number(robotId)}` : ''}`, { method: robotId ? 'PUT' : 'POST', body: payload }); const data = await response.json(); if (!response.ok || !data.success) throw new Error(data.message || 'Không thể lưu robot'); dialog.close(); await Promise.all([this.fetchRobots(), this.fetchDocuments(), this.fetchRobotTypes()]); if (this.currentPage === 'robots') this.renderView('robots'); this.notifySuccess(data.message || 'Đã lưu robot'); } catch (error) { this.notifyFailure(error.message || 'Không thể lưu robot'); } finally { delete dialog.dataset.saving; controls.forEach(control => { control.disabled = false; }); saveButton.textContent = 'Lưu robot'; } } confirmRobotDeletion({ title, name, description, note, label, details = [] }) { if (document.getElementById('robotDeleteDialog')) return Promise.resolve(false); const dialog = document.createElement('dialog'); dialog.id = 'robotDeleteDialog'; dialog.className = 'bom-dialog bom-confirm-dialog robot-delete-dialog'; dialog.setAttribute('aria-labelledby', 'robotDeleteTitle'); dialog.setAttribute('aria-describedby', 'robotDeleteDescription'); dialog.innerHTML = `

QUẢN LÝ ROBOT

${this.escapeHtml(title)}

${this.escapeHtml(description)}

${this.escapeHtml(name)}${details.length ? `
${details.map(([key, value]) => `
${this.escapeHtml(key)}
${this.escapeHtml(value || '—')}
`).join('')}
` : ''}

${this.escapeHtml(note)}

`; return new Promise(resolve => { dialog.addEventListener('click', event => { if (event.target.closest('[data-confirm-robot-delete]')) dialog.close('confirm'); else if (event.target.closest('[data-cancel-robot-delete]')) dialog.close(); else if (event.target === dialog) { const rect = dialog.getBoundingClientRect(); if (event.clientX < rect.left || event.clientX > rect.right || event.clientY < rect.top || event.clientY > rect.bottom) dialog.close(); } }); dialog.addEventListener('close', () => { dialog.remove(); resolve(dialog.returnValue === 'confirm'); }, { once: true }); document.body.appendChild(dialog); dialog.showModal(); dialog.querySelector('.bom-dialog-footer [data-cancel-robot-delete]').focus({ preventScroll: true }); }); } async deleteRobot(robotId) { if (!this.canCurrentUserManageAssets() || this.robotDeletionPending) return; const robot = this.robots.find(item => Number(item.RobotId) === Number(robotId)); if (!robot) return; this.robotDeletionPending = true; try { if (!await this.confirmRobotDeletion({ title: 'Xóa robot này?', name: robot.Name, description: 'Robot này sẽ được gỡ khỏi danh sách quản lý.', label: 'Xóa robot', details: [['Model', robot.Model], ['Thể loại', robot.Type]], note: 'Tài liệu đính kèm vẫn được giữ trong kho tài liệu.' })) return; const response = await fetch(`${this.apiBase}/robots/${Number(robotId)}`, { method: 'DELETE' }); const data = await response.json(); if (!response.ok || !data.success) throw new Error(data.message || 'Không thể xóa robot'); await Promise.all([this.fetchRobots(), this.fetchRobotTypes()]); if (this.currentPage === 'robots') this.renderView('robots'); this.notifySuccess(data.message || 'Đã xóa robot'); } catch (error) { this.notifyFailure(error.message); } finally { this.robotDeletionPending = false; } } isSupportedDocumentName(fileName) { const extension = String(fileName || '').match(/\.[^.]+$/)?.[0].toLowerCase(); return Boolean(DOCUMENT_FORMATS.formats[extension]); } getDocumentPresentation(document) { const extension = String(document?.OriginalFileName || '').match(/\.[^.]+$/)?.[0].toLowerCase() || ''; const format = DOCUMENT_FORMATS.formats[extension] || { label: 'File', icon: 'draft', tone: 'slate', preview: false }; const colors = { red: { badgeClass: 'bg-red-100 text-red-600', iconClass: 'text-red-600' }, blue: { badgeClass: 'bg-blue-100 text-blue-700', iconClass: 'text-blue-700' }, green: { badgeClass: 'bg-emerald-100 text-emerald-700', iconClass: 'text-emerald-700' }, orange: { badgeClass: 'bg-orange-100 text-orange-700', iconClass: 'text-orange-700' }, purple: { badgeClass: 'bg-purple-100 text-purple-700', iconClass: 'text-purple-700' }, slate: { badgeClass: 'bg-slate-100 text-slate-600', iconClass: 'text-slate-600' } }; return { ...format, ...colors[format.tone], extension: extension.slice(1).toUpperCase() }; } formatFileSize(value) { const bytes = Number(value) || 0; if (bytes < 1024) return `${bytes} B`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; } getFilteredDocuments() { const keyword = String(this.documentSearchTerm || '').trim().toLocaleLowerCase('vi'); if (!keyword) return this.documents; return this.documents.filter(document => [ document?.Title, document?.OriginalFileName, document?.UploadedByName ].some(value => String(value || '').toLocaleLowerCase('vi').includes(keyword))); } getDocumentsContent() { const documents = this.getFilteredDocuments(); const totalSize = this.documents.reduce((sum, document) => sum + (Number(document?.FileSize) || 0), 0); return `
search
${documents.length} kết quả
${documents.length ? `
${documents.map(document => { const documentId = Number(document?.DocumentId); const canDelete = Boolean(document?.CanDelete); const presentation = this.getDocumentPresentation(document); return `
${presentation.icon}

${this.escapeHtml(document?.Title || 'Tài liệu')}

${this.escapeHtml(document?.OriginalFileName || '-')}

${this.escapeHtml(presentation.extension || 'FILE')} hard_drive${this.formatFileSize(document?.FileSize)} person${this.escapeHtml(document?.UploadedByName || '-')} schedule${this.formatDateTime(document?.UploadedDate)}
${presentation.preview ? ` visibilityXem ${presentation.label} ` : ''} downloadTải xuống ${canDelete ? ` ` : ''}
`; }).join('')}
` : `
folder_open

${this.documentSearchTerm ? 'Không tìm thấy tài liệu' : 'Chưa có tài liệu'}

${this.documentSearchTerm ? 'Hãy thử một từ khóa khác.' : 'Bấm “Thêm tài liệu” để lưu tài liệu đầu tiên.'}

`}
upload_file

Thêm tài liệu

Tải tài liệu lên kho dùng chung.

${DOCUMENT_FORMATS.description}. Tối đa ${this.documentMaxFileSizeMb} MB/file.

`; } setupDocumentListeners() { const dialog = document.getElementById('documentUploadDialog'); const form = document.getElementById('documentUploadForm'); const openDialogButton = document.getElementById('openDocumentUploadDialog'); const closeDialog = () => { if (!dialog || form?.dataset.uploading === 'true') return; if (typeof dialog.close === 'function' && dialog.open) { dialog.close(); } else { dialog.removeAttribute('open'); } }; if (openDialogButton && dialog && !openDialogButton.dataset.boundClick) { openDialogButton.addEventListener('click', () => { if (typeof dialog.showModal === 'function') { if (!dialog.open) dialog.showModal(); } else { dialog.setAttribute('open', ''); } requestAnimationFrame(() => document.getElementById('documentFile')?.focus()); }); openDialogButton.dataset.boundClick = 'true'; } document.querySelectorAll('[data-close-document-dialog]').forEach(button => { if (button.dataset.boundClick) return; button.addEventListener('click', closeDialog); button.dataset.boundClick = 'true'; }); if (dialog && !dialog.dataset.boundDialog) { dialog.addEventListener('cancel', event => { if (form?.dataset.uploading === 'true') event.preventDefault(); }); dialog.addEventListener('close', () => form?.reset()); dialog.addEventListener('click', event => { if (event.target !== dialog || form?.dataset.uploading === 'true') return; const bounds = dialog.getBoundingClientRect(); const outside = event.clientX < bounds.left || event.clientX > bounds.right || event.clientY < bounds.top || event.clientY > bounds.bottom; if (outside) closeDialog(); }); dialog.dataset.boundDialog = 'true'; } if (form && !form.dataset.boundSubmit) { form.addEventListener('submit', event => this.handleDocumentUpload(event)); form.dataset.boundSubmit = 'true'; } const searchInput = document.getElementById('documentSearch'); if (searchInput && !searchInput.dataset.boundInput) { searchInput.addEventListener('input', event => { const cursor = event.target.selectionStart ?? event.target.value.length; this.documentSearchTerm = event.target.value; this.renderView('documents'); const nextInput = document.getElementById('documentSearch'); nextInput?.focus(); nextInput?.setSelectionRange(cursor, cursor); }); searchInput.dataset.boundInput = 'true'; } document.querySelectorAll('.delete-document').forEach(button => { if (button.dataset.boundClick) return; button.addEventListener('click', () => { this.deleteDocument(button.dataset.documentId, button.dataset.documentTitle); }); button.dataset.boundClick = 'true'; }); } isArchivedAsset(item) { return item?.IsArchived === true || Number(item?.IsArchived) === 1; } getArchivedAssetBadgeHtml(item) { return this.isArchivedAsset(item) ? '
Tài sản cũ
' : ''; } async handleDocumentUpload(event) { event.preventDefault(); const form = event.currentTarget; if (form.dataset.uploading === 'true') return; const fileInput = form.querySelector('#documentFile'); const titleInput = form.querySelector('#documentTitle'); const submitButton = form.querySelector('#documentUploadSubmit'); const file = fileInput?.files?.[0]; if (!file) { this.notifyWarning('Vui lòng chọn tài liệu'); return; } if (!this.isSupportedDocumentName(file.name)) { this.notifyFailure(`Định dạng chưa được hỗ trợ. Chấp nhận: ${DOCUMENT_FORMATS.description}.`); return; } if (file.size > this.documentMaxFileSizeMb * 1024 * 1024) { this.notifyFailure(`File vượt quá ${this.documentMaxFileSizeMb} MB`); return; } const payload = new FormData(); payload.append('file', file); payload.append('title', String(titleInput?.value || '').trim()); form.dataset.uploading = 'true'; form.querySelectorAll('[data-close-document-dialog]').forEach(button => { button.disabled = true; }); if (submitButton) { submitButton.disabled = true; submitButton.querySelector('span:last-child').textContent = 'Đang tải...'; } try { const response = await fetch(`${this.apiBase}/documents`, { method: 'POST', body: payload }); const data = await response.json(); if (!response.ok || !data.success) { throw new Error(data.message || 'Không thể tải tài liệu lên'); } this.documentSearchTerm = ''; await this.fetchDocuments(); this.renderView('documents'); this.notifySuccess(data.message || 'Đã lưu tài liệu'); } catch (err) { console.error('Upload document error:', err); this.notifyFailure(err.message || 'Không thể tải tài liệu lên'); delete form.dataset.uploading; form.querySelectorAll('[data-close-document-dialog]').forEach(button => { button.disabled = false; }); if (submitButton) { submitButton.disabled = false; submitButton.querySelector('span:last-child').textContent = 'Tải lên'; } } } async deleteDocument(documentId, title) { const parsedId = Number.parseInt(documentId, 10); if (!Number.isInteger(parsedId) || parsedId <= 0) return; if (!window.confirm(`Xóa tài liệu “${String(title || 'Tài liệu')}”? Thao tác này không thể hoàn tác.`)) return; try { const response = await fetch(`${this.apiBase}/documents/${parsedId}`, { method: 'DELETE' }); const data = await response.json(); if (!response.ok || !data.success) { throw new Error(data.message || 'Không thể xóa tài liệu'); } await this.fetchDocuments(); this.renderView('documents'); this.notifySuccess(data.message || 'Đã xóa tài liệu'); } catch (err) { console.error('Delete document error:', err); this.notifyFailure(err.message || 'Không thể xóa tài liệu'); } } getApplicationsContent() { const filteredApps = this.getFilteredApplications(); const pageInfo = this.getPaged(filteredApps, this.appPage, this.appPageSize); this.appPage = pageInfo.current; return `
Search
${pageInfo.data.map(app => ` `).join('')}
Name Type Description URL Status Actions
${app.Icon || 'apps'}
${app.Name}
${app.Type} ${app.Description || '-'} ${(app.Url || app.url) ? `${app.Url || app.url}` : '-'}
${(app.Status || app.status) === 'online' ? 'Online' : 'Offline'}
Showing ${pageInfo.start}-${pageInfo.end} of ${pageInfo.total}
Page ${pageInfo.current} / ${pageInfo.totalPages}
`; } getAssetStatusMeta(status) { const normalized = String(status || '').toLowerCase(); if (normalized === 'exported') { return { label: 'Đã xuất', className: 'bg-rose-100 text-rose-700' }; } if (normalized === 'in_stock') { return { label: 'Trong kho', className: 'bg-emerald-100 text-emerald-700' }; } return { label: 'Đang sử dụng', className: 'bg-blue-100 text-blue-700' }; } getAppTimeParts(value = new Date()) { const date = value instanceof Date ? value : new Date(value); if (Number.isNaN(date.getTime())) return null; return APP_TIME_PARTS_FORMATTER.formatToParts(date).reduce((parts, part) => { if (part.type !== 'literal') { parts[part.type] = part.value; } return parts; }, {}); } formatTimestampForCode(value = new Date(), includeMilliseconds = false) { const date = value instanceof Date ? value : new Date(value); const parts = this.getAppTimeParts(date); if (!parts) return ''; const timestamp = [ parts.year, parts.month, parts.day, parts.hour, parts.minute, parts.second ].join(''); return includeMilliseconds ? `${timestamp}${String(date.getMilliseconds()).padStart(3, '0')}` : timestamp; } formatDateOnly(value) { if (!value) return '-'; const date = new Date(value); if (Number.isNaN(date.getTime())) return String(value); return APP_DATE_FORMATTER.format(date); } toDateInputValue(value) { if (!value) return ''; const parts = this.getAppTimeParts(value); if (!parts) return ''; return `${parts.year}-${parts.month}-${parts.day}`; } formatBorrowerDisplay(name, quantity = 1) { const cleanName = String(name || '').trim(); if (!cleanName) return null; const quantityNumber = Number(quantity); const safeQuantity = Number.isInteger(quantityNumber) && quantityNumber > 0 ? quantityNumber : 1; return `${cleanName} - số lượng: ${safeQuantity}`; } parseNonNegativeInteger(value, fallback = 0) { if (value === null || value === undefined || value === '') { return fallback; } const parsed = Number.parseInt(String(value).replace(/,/g, '').trim(), 10); if (!Number.isFinite(parsed) || parsed < 0) { return fallback; } return parsed; } parseOptionalNonNegativeInteger(value) { if (value === null || value === undefined || String(value).trim() === '') { return null; } const parsed = Number.parseInt(String(value).replace(/,/g, '').trim(), 10); if (!Number.isFinite(parsed) || parsed < 0) { return null; } return parsed; } escapeHtml(value) { return String(value || '') .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"') .replace(/'/g, '''); } normalizeAssetRequestType(value) { const normalized = String(value || '').trim().toLowerCase(); return normalized === 'return' ? 'return' : 'borrow'; } normalizeAssetRequestStatus(value) { const normalized = String(value || '').trim().toLowerCase(); if (normalized === 'approved') return 'approved'; if (normalized === 'returned') return 'returned'; if (normalized === 'rejected') return 'rejected'; return 'pending'; } getAssetRequestTypeMeta(value) { const requestType = this.normalizeAssetRequestType(value); if (requestType === 'return') { return { value: 'return', label: 'Trả tài sản', className: 'bg-emerald-100 text-emerald-700 border border-emerald-200' }; } return { value: 'borrow', label: 'Mượn tài sản', className: 'bg-blue-100 text-blue-700 border border-blue-200' }; } getAssetRequestStatusMeta(value, item = null) { const status = this.normalizeAssetRequestStatus(value); const requestType = item ? this.normalizeAssetRequestType(item?.RequestType) : ''; const borrowQuantity = this.parseNonNegativeInteger(item?.BorrowQuantity, 0); const returnedQuantity = this.parseNonNegativeInteger(item?.ReturnedQuantity, 0); if (status === 'returned' || (requestType === 'borrow' && status === 'approved' && borrowQuantity > 0 && returnedQuantity >= borrowQuantity)) { return { value: 'returned', label: 'Đã trả', className: 'bg-slate-100 text-slate-700 border border-slate-200' }; } if (status === 'approved') { return { value: 'approved', label: requestType === 'borrow' ? 'Đang mượn' : (requestType === 'return' ? 'Đã trả' : 'Chấp nhận'), className: 'bg-green-100 text-green-700 border border-green-200' }; } if (status === 'rejected') { return { value: 'rejected', label: 'Từ chối', className: 'bg-red-100 text-red-700 border border-red-200' }; } return { value: 'pending', label: 'Đang chờ', className: 'bg-yellow-100 text-yellow-700 border border-yellow-200' }; } getPendingAssetRequestCount() { if (!this.canCurrentUserManageAssets()) { return 0; } return (Array.isArray(this.assetBorrows) ? this.assetBorrows : []) .filter(item => this.normalizeAssetRequestStatus(item?.RequestStatus) === 'pending') .length; } updatePendingAssetRequestsBadge() { const shouldShow = this.canCurrentUserManageAssets(); const count = this.getPendingAssetRequestCount(); const displayCount = count > 99 ? '99+' : String(count); const topButton = document.getElementById('pendingAssetRequestsBtn'); if (topButton) { topButton.classList.toggle('hidden', !shouldShow); } const topBadge = document.getElementById('pendingAssetRequestsBadge'); if (topBadge) { topBadge.classList.toggle('hidden', !shouldShow || count <= 0); topBadge.textContent = displayCount; } const pageBadge = document.getElementById('pendingAssetBorrowsCountBadge'); if (pageBadge) { pageBadge.classList.toggle('hidden', count <= 0); pageBadge.textContent = displayCount; } } parseBorrowerEntries(rawBorrower) { if (Array.isArray(rawBorrower)) { const merged = []; rawBorrower.forEach(item => { if (!item) return; const name = String(item.name || item.Name || '').trim(); const quantity = this.parseNonNegativeInteger(item.quantity ?? item.Quantity, 0); if (!name || quantity <= 0) return; const existed = merged.find(entry => entry.name.toLowerCase() === name.toLowerCase()); if (existed) { existed.quantity += quantity; } else { merged.push({ name, quantity }); } }); return merged; } const source = String(rawBorrower || '').trim(); if (!source) { return []; } const chunks = source .split(/[\n;]+/g) .map(item => String(item || '').trim()) .filter(Boolean); const merged = []; chunks.forEach(chunk => { let name = chunk; let quantity = 1; const labeledMatch = chunk.match(/^(.*?)(?:\s*-\s*[^:]+:\s*(\d+))\s*$/i); if (labeledMatch) { name = String(labeledMatch[1] || '').trim(); quantity = this.parseNonNegativeInteger(labeledMatch[2], 1); } else { const colonMatch = chunk.match(/^(.*?)\s*:\s*(\d+)\s*$/); const xMatch = chunk.match(/^(.*?)\s*x\s*(\d+)\s*$/i); const parenMatch = chunk.match(/^(.*?)\s*\(\s*(\d+)\s*\)\s*$/); const fallbackMatch = colonMatch || xMatch || parenMatch; if (fallbackMatch) { name = String(fallbackMatch[1] || '').trim(); quantity = this.parseNonNegativeInteger(fallbackMatch[2], 1); } } if (!name || quantity <= 0) { return; } const existed = merged.find(entry => entry.name.toLowerCase() === name.toLowerCase()); if (existed) { existed.quantity += quantity; } else { merged.push({ name, quantity }); } }); return merged; } formatBorrowerEntries(entries, separator = '; ') { if (!Array.isArray(entries) || !entries.length) { return ''; } return entries .map(entry => this.formatBorrowerDisplay(entry?.name, entry?.quantity)) .filter(Boolean) .join(separator); } formatBorrowerSummaryText(rawBorrower) { const entries = this.parseBorrowerEntries(rawBorrower); return this.formatBorrowerEntries(entries, '\n'); } formatBorrowerTableHtml(rawBorrower) { const entries = this.parseBorrowerEntries(rawBorrower); if (!entries.length) { return '-'; } return entries .map(entry => this.formatBorrowerDisplay(entry.name, entry.quantity)) .filter(Boolean) .map(item => `
${this.escapeHtml(item)}
`) .join(''); } mergeBorrowerEntries(existingEntries, borrowerName, borrowQuantity) { const merged = this.parseBorrowerEntries(existingEntries); const name = String(borrowerName || '').trim(); const quantity = this.parseNonNegativeInteger(borrowQuantity, 0); if (!name || quantity <= 0) { return merged; } const existed = merged.find(entry => entry.name.toLowerCase() === name.toLowerCase()); if (existed) { existed.quantity += quantity; } else { merged.push({ name, quantity }); } return merged; } buildAssetQuantityMetrics(asset, borrowerEntriesOverride = null) { const quantity = this.parseNonNegativeInteger(asset?.Quantity ?? asset?.quantity, 0); const importInPeriod = this.parseNonNegativeInteger(asset?.ImportInPeriod ?? asset?.importInPeriod, 0); const storedExportInPeriod = this.parseOptionalNonNegativeInteger(asset?.ExportInPeriod ?? asset?.exportInPeriod); const storedEndingBalance = this.parseOptionalNonNegativeInteger(asset?.EndingBalance ?? asset?.endingBalance); const borrowerEntries = Array.isArray(borrowerEntriesOverride) ? this.parseBorrowerEntries(borrowerEntriesOverride) : this.parseBorrowerEntries(asset?.Borrower ?? asset?.borrower); const borrowerExportInPeriod = borrowerEntries.reduce((sum, entry) => ( sum + this.parseNonNegativeInteger(entry?.quantity, 0) ), 0); // Prefer stored stock numbers from DB/file to avoid overriding imported balances. const exportInPeriod = storedExportInPeriod !== null ? storedExportInPeriod : borrowerExportInPeriod; const endingBalance = storedEndingBalance !== null ? storedEndingBalance : Math.max(quantity + importInPeriod - exportInPeriod, 0); return { quantity, importInPeriod, exportInPeriod, endingBalance, borrowerEntries, borrowerExportInPeriod }; } computeAssetStatusCode(endingBalance, borrowingQuantity) { const ending = this.parseNonNegativeInteger(endingBalance, 0); const borrowing = this.parseNonNegativeInteger(borrowingQuantity, 0); if (ending <= 0) { return 'exported'; } if (borrowing > 0) { return 'in_use'; } return 'in_stock'; } normalizeAssetStockSplit(endingBalance, newQuantityValue, usedQuantityValue) { const ending = this.parseNonNegativeInteger(endingBalance, 0); let newQuantity = this.parseNonNegativeInteger(newQuantityValue, ending); let usedQuantity = this.parseNonNegativeInteger(usedQuantityValue, 0); const total = newQuantity + usedQuantity; if (total < ending) { newQuantity += (ending - total); } else if (total > ending) { let overflow = total - ending; const takeFromNew = Math.min(newQuantity, overflow); newQuantity -= takeFromNew; overflow -= takeFromNew; if (overflow > 0) { usedQuantity = Math.max(usedQuantity - overflow, 0); } } return { newQuantity: Math.max(newQuantity, 0), usedQuantity: Math.max(usedQuantity, 0) }; } normalizeAssetComputedFields(asset) { if (!asset || typeof asset !== 'object') { return asset; } const metrics = this.buildAssetQuantityMetrics(asset); const stockSplit = this.normalizeAssetStockSplit( metrics.endingBalance, asset?.NewQuantity ?? asset?.newQuantity, asset?.UsedQuantity ?? asset?.usedQuantity ); const status = this.computeAssetStatusCode(metrics.endingBalance, metrics.exportInPeriod); return { ...asset, Quantity: metrics.quantity, ImportInPeriod: metrics.importInPeriod, ExportInPeriod: metrics.exportInPeriod, EndingBalance: metrics.endingBalance, NewQuantity: stockSplit.newQuantity, UsedQuantity: stockSplit.usedQuantity, Status: status, Borrower: this.formatBorrowerEntries(metrics.borrowerEntries, '; ') || null }; } recalculateAssetStockFields() { const quantityInput = document.getElementById('assetQuantityInput'); const importInput = document.getElementById('assetImportInPeriodInput'); const exportInput = document.getElementById('assetExportInPeriodInput'); const endingInput = document.getElementById('assetEndingBalanceInput'); const statusInput = document.getElementById('assetStatusInput'); if (!quantityInput || !importInput) { return; } const quantity = this.parseNonNegativeInteger(quantityInput.value, 0); const importInPeriod = this.parseNonNegativeInteger(importInput.value, 0); const exportInPeriod = this.parseNonNegativeInteger(exportInput?.value ?? 0, 0); const endingBalance = Math.max(quantity + importInPeriod - exportInPeriod, 0); if (exportInput) { exportInput.value = String(exportInPeriod); } if (endingInput) { endingInput.value = String(endingBalance); } if (statusInput) { const statusCode = this.computeAssetStatusCode(endingBalance, exportInPeriod); const statusMeta = this.getAssetStatusMeta(statusCode); statusInput.value = statusMeta.label; statusInput.dataset.statusCode = statusCode; } } setupAssetStockListeners() { ['assetQuantityInput', 'assetImportInPeriodInput'].forEach(fieldId => { const input = document.getElementById(fieldId); if (!input || input.dataset.boundStockListener) { return; } input.addEventListener('input', () => this.recalculateAssetStockFields()); input.addEventListener('change', () => this.recalculateAssetStockFields()); input.dataset.boundStockListener = 'true'; }); } getFilteredAssetDepartments() { const search = String(this.assetDepartmentSearchTerm || '').toLowerCase(); const source = Array.isArray(this.assetDepartments) ? this.assetDepartments : []; return source.filter(item => { if (!search) { return true; } const name = String(item?.DepartmentName || '').toLowerCase(); return name.includes(search); }); } buildAssetDepartmentsRowsHtml(departments = []) { const canManageAssets = this.canCurrentUserManageAssets(); if (!departments.length) { return ` Chưa có phòng ban nào. `; } return departments.map((item, index) => { const departmentId = Number(item?.DepartmentId); const assetCount = Number(item?.AssetCount) || 0; const departmentName = this.escapeHtml(item?.DepartmentName || '-'); return ` ${index + 1} ${departmentName} ${assetCount}
`; }).join(''); } getAssetDepartmentsContent() { const filteredDepartments = this.getFilteredAssetDepartments(); const canManageAssets = this.canCurrentUserManageAssets(); return `
Tìm kiếm
${this.buildAssetDepartmentsRowsHtml(filteredDepartments)}
STT Phòng ban Sd tài sản Thao tác
Tổng phòng ban: ${filteredDepartments.length}
`; } renderAssetDepartmentsTableBody() { const tbody = document.querySelector('.asset-departments-table-body'); if (!tbody) { return; } const filteredDepartments = this.getFilteredAssetDepartments(); tbody.innerHTML = this.buildAssetDepartmentsRowsHtml(filteredDepartments); const countElement = document.getElementById('assetDepartmentCount'); if (countElement) { countElement.textContent = String(filteredDepartments.length); } this.setupAssetDepartmentActionListeners(); } setupAssetDepartmentActionListeners() { document.querySelectorAll('.edit-asset-department').forEach(btn => { if (btn.dataset.boundClick === 'true') { return; } btn.addEventListener('click', () => { const departmentId = Number(btn.dataset.departmentId); if (!Number.isFinite(departmentId)) { return; } this.handleUpdateAssetDepartment(departmentId); }); btn.dataset.boundClick = 'true'; }); document.querySelectorAll('.delete-asset-department').forEach(btn => { if (btn.dataset.boundClick === 'true') { return; } btn.addEventListener('click', () => { const departmentId = Number(btn.dataset.departmentId); if (!Number.isFinite(departmentId)) { return; } this.handleDeleteAssetDepartment(departmentId); }); btn.dataset.boundClick = 'true'; }); } setupAssetDepartmentListeners() { const searchInput = document.getElementById('assetDepartmentSearch'); if (searchInput && searchInput.dataset.boundInput !== 'true') { searchInput.addEventListener('input', (event) => { this.assetDepartmentSearchTerm = String(event.target.value || '').trim(); this.renderAssetDepartmentsTableBody(); }); searchInput.addEventListener('focus', () => { searchInput.dataset.focused = 'true'; }); searchInput.addEventListener('blur', () => { searchInput.dataset.focused = 'false'; }); searchInput.dataset.boundInput = 'true'; } this.setupAssetDepartmentActionListeners(); } async refreshAssetDepartmentsUI() { await this.fetchAssetDepartments(); if (this.currentPage === 'asset-departments') { this.renderAssetDepartmentsTableBody(); } } getAssetDepartmentById(departmentId) { return this.assetDepartments.find(item => Number(item?.DepartmentId) === Number(departmentId)) || null; } openAssetDepartmentModal(department = null) { const modal = document.getElementById('assetDepartmentModal'); const titleNode = document.getElementById('assetDepartmentModalTitle'); const nameInput = document.getElementById('assetDepartmentNameInput'); if (!modal || !nameInput) { this.notifyFailure('Không mở được biểu mẫu phòng ban'); return; } const editing = department && Number.isFinite(Number(department.DepartmentId)); this.editingAssetDepartmentId = editing ? Number(department.DepartmentId) : undefined; if (titleNode) { titleNode.textContent = editing ? 'Sửa phòng ban' : 'Thêm phòng ban'; } nameInput.value = editing ? String(department.DepartmentName || '') : ''; modal.classList.add('open'); nameInput.focus(); nameInput.select(); } openDeleteAssetDepartmentModal(department) { const modal = document.getElementById('deleteAssetDepartmentModal'); const nameNode = document.getElementById('deleteAssetDepartmentName'); if (!modal) { this.notifyFailure('Không mở được hộp thoại xóa phòng ban'); return; } this.pendingDeleteAssetDepartmentId = Number(department?.DepartmentId); if (nameNode) { nameNode.textContent = String(department?.DepartmentName || '-'); } modal.classList.add('open'); } async handleCreateAssetDepartment() { if (!this.ensureAssetManagePermission('thêm phòng ban')) { return; } this.openAssetDepartmentModal(null); } async handleAssetDepartmentSubmit(event) { event.preventDefault(); if (!this.ensureAssetManagePermission('thêm hoặc sửa phòng ban')) { return; } const nameInput = document.getElementById('assetDepartmentNameInput'); const departmentName = String(nameInput?.value || '').trim(); if (!departmentName) { this.notifyWarning('Tên phòng ban là bắt buộc'); return; } const isEdit = Number.isFinite(Number(this.editingAssetDepartmentId)); const endpoint = isEdit ? `${this.apiBase}/asset-departments/${this.editingAssetDepartmentId}` : `${this.apiBase}/asset-departments`; const method = isEdit ? 'PUT' : 'POST'; try { const response = await fetch(endpoint, { method, headers: this.getAuthHeaders(true), body: JSON.stringify({ departmentName }) }); const data = await response.json(); if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Lưu phòng ban thất bại'); return; } this.editingAssetDepartmentId = undefined; closeAssetDepartmentModal(); this.notifySuccess(isEdit ? 'Cập nhật phòng ban thành công' : 'Thêm phòng ban thành công'); await this.refreshAssetDepartmentsUI(); await this.refreshAssetsUI(); } catch (err) { console.error(err); this.notifyFailure('Lưu phòng ban thất bại'); } } async handleUpdateAssetDepartment(departmentId) { if (!this.ensureAssetManagePermission('sửa phòng ban')) { return; } const targetDepartment = this.getAssetDepartmentById(departmentId); if (!targetDepartment) { this.notifyWarning('Không tìm thấy phòng ban'); return; } this.openAssetDepartmentModal(targetDepartment); } async handleDeleteAssetDepartment(departmentId) { if (!this.ensureAssetManagePermission('xóa phòng ban')) { return; } const targetDepartment = this.getAssetDepartmentById(departmentId); if (!targetDepartment) { this.notifyWarning('Không tìm thấy phòng ban'); return; } this.openDeleteAssetDepartmentModal(targetDepartment); } async confirmDeleteAssetDepartment() { if (!this.ensureAssetManagePermission('xóa phòng ban')) { return; } if (!Number.isFinite(Number(this.pendingDeleteAssetDepartmentId))) { return; } try { const response = await fetch(`${this.apiBase}/asset-departments/${this.pendingDeleteAssetDepartmentId}`, { method: 'DELETE', headers: this.getAuthHeaders(false) }); const data = await response.json(); if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Xóa phòng ban thất bại'); return; } this.pendingDeleteAssetDepartmentId = undefined; closeDeleteAssetDepartmentModal(); this.notifySuccess('Xóa phòng ban thành công'); await this.refreshAssetDepartmentsUI(); await this.refreshAssetsUI(); } catch (err) { console.error(err); this.notifyFailure('Xóa phòng ban thất bại'); } } getFilteredAssetProjects() { const search = String(this.assetProjectSearchTerm || '').toLowerCase(); const source = Array.isArray(this.assetProjects) ? this.assetProjects : []; return source.filter(item => { if (!search) { return true; } const name = String(item?.ProjectName || '').toLowerCase(); return name.includes(search); }); } buildAssetProjectsRowsHtml(projects = []) { const canManageAssets = this.canCurrentUserManageAssets(); if (!projects.length) { return ` Chưa có dự án nào. `; } return projects.map((item, index) => { const projectId = Number(item?.ProjectId); const assetCount = Number(item?.AssetCount) || 0; const projectName = this.escapeHtml(item?.ProjectName || '-'); return ` ${index + 1} ${projectName} ${assetCount}
`; }).join(''); } getAssetProjectsContent() { const filteredProjects = this.getFilteredAssetProjects(); const canManageAssets = this.canCurrentUserManageAssets(); return `
Tìm kiếm
${this.buildAssetProjectsRowsHtml(filteredProjects)}
STT Dự án Sd tài sản Thao tác
Tổng dự án: ${filteredProjects.length}
`; } renderAssetProjectsTableBody() { const tbody = document.querySelector('.asset-projects-table-body'); if (!tbody) { return; } const filteredProjects = this.getFilteredAssetProjects(); tbody.innerHTML = this.buildAssetProjectsRowsHtml(filteredProjects); const countElement = document.getElementById('assetProjectCount'); if (countElement) { countElement.textContent = String(filteredProjects.length); } this.setupAssetProjectActionListeners(); } setupAssetProjectActionListeners() { document.querySelectorAll('.edit-asset-project').forEach(btn => { if (btn.dataset.boundClick === 'true') { return; } btn.addEventListener('click', () => { const projectId = Number(btn.dataset.projectId); if (!Number.isFinite(projectId)) { return; } this.handleUpdateAssetProject(projectId); }); btn.dataset.boundClick = 'true'; }); document.querySelectorAll('.delete-asset-project').forEach(btn => { if (btn.dataset.boundClick === 'true') { return; } btn.addEventListener('click', () => { const projectId = Number(btn.dataset.projectId); if (!Number.isFinite(projectId)) { return; } this.handleDeleteAssetProject(projectId); }); btn.dataset.boundClick = 'true'; }); } setupAssetProjectListeners() { const searchInput = document.getElementById('assetProjectSearch'); if (searchInput && searchInput.dataset.boundInput !== 'true') { searchInput.addEventListener('input', (event) => { this.assetProjectSearchTerm = String(event.target.value || '').trim(); this.renderAssetProjectsTableBody(); }); searchInput.addEventListener('focus', () => { searchInput.dataset.focused = 'true'; }); searchInput.addEventListener('blur', () => { searchInput.dataset.focused = 'false'; }); searchInput.dataset.boundInput = 'true'; } this.setupAssetProjectActionListeners(); } async refreshAssetProjectsUI() { await this.fetchAssetProjects(); if (this.currentPage === 'asset-projects') { this.renderAssetProjectsTableBody(); } } getAssetProjectById(projectId) { return this.assetProjects.find(item => Number(item?.ProjectId) === Number(projectId)) || null; } openAssetProjectModal(project = null) { const modal = document.getElementById('assetProjectModal'); const titleNode = document.getElementById('assetProjectModalTitle'); const nameInput = document.getElementById('assetProjectNameInput'); if (!modal || !nameInput) { this.notifyFailure('Không mở được biểu mẫu dự án'); return; } const editing = project && Number.isFinite(Number(project.ProjectId)); this.editingAssetProjectId = editing ? Number(project.ProjectId) : undefined; if (titleNode) { titleNode.textContent = editing ? 'Sửa dự án' : 'Thêm dự án'; } nameInput.value = editing ? String(project.ProjectName || '') : ''; modal.classList.add('open'); nameInput.focus(); nameInput.select(); } openDeleteAssetProjectModal(project) { const modal = document.getElementById('deleteAssetProjectModal'); const nameNode = document.getElementById('deleteAssetProjectName'); if (!modal) { this.notifyFailure('Không mở được hộp thoại xóa dự án'); return; } this.pendingDeleteAssetProjectId = Number(project?.ProjectId); if (nameNode) { nameNode.textContent = String(project?.ProjectName || '-'); } modal.classList.add('open'); } async handleCreateAssetProject() { if (!this.ensureAssetManagePermission('thêm dự án')) { return; } this.openAssetProjectModal(null); } async handleAssetProjectSubmit(event) { event.preventDefault(); if (!this.ensureAssetManagePermission('thêm hoặc sửa dự án')) { return; } const nameInput = document.getElementById('assetProjectNameInput'); const projectName = String(nameInput?.value || '').trim(); if (!projectName) { this.notifyWarning('Tên dự án là bắt buộc'); return; } const isEdit = Number.isFinite(Number(this.editingAssetProjectId)); const endpoint = isEdit ? `${this.apiBase}/asset-projects/${this.editingAssetProjectId}` : `${this.apiBase}/asset-projects`; const method = isEdit ? 'PUT' : 'POST'; try { const response = await fetch(endpoint, { method, headers: this.getAuthHeaders(true), body: JSON.stringify({ projectName }) }); const data = await response.json(); if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Lưu dự án thất bại'); return; } this.editingAssetProjectId = undefined; closeAssetProjectModal(); this.notifySuccess(isEdit ? 'Cập nhật dự án thành công' : 'Thêm dự án thành công'); await this.refreshAssetProjectsUI(); await this.refreshAssetsUI(); } catch (err) { console.error(err); this.notifyFailure('Lưu dự án thất bại'); } } async handleUpdateAssetProject(projectId) { if (!this.ensureAssetManagePermission('sửa dự án')) { return; } const targetProject = this.getAssetProjectById(projectId); if (!targetProject) { this.notifyWarning('Không tìm thấy dự án'); return; } this.openAssetProjectModal(targetProject); } async handleDeleteAssetProject(projectId) { if (!this.ensureAssetManagePermission('xóa dự án')) { return; } const targetProject = this.getAssetProjectById(projectId); if (!targetProject) { this.notifyWarning('Không tìm thấy dự án'); return; } this.openDeleteAssetProjectModal(targetProject); } async confirmDeleteAssetProject() { if (!this.ensureAssetManagePermission('xóa dự án')) { return; } if (!Number.isFinite(Number(this.pendingDeleteAssetProjectId))) { return; } try { const response = await fetch(`${this.apiBase}/asset-projects/${this.pendingDeleteAssetProjectId}`, { method: 'DELETE', headers: this.getAuthHeaders(false) }); const data = await response.json(); if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Xóa dự án thất bại'); return; } this.pendingDeleteAssetProjectId = undefined; closeDeleteAssetProjectModal(); this.notifySuccess('Xóa dự án thành công'); await this.refreshAssetProjectsUI(); await this.refreshAssetsUI(); } catch (err) { console.error(err); this.notifyFailure('Xóa dự án thất bại'); } } buildAssetBorrowRowHtml(item, rowNumber) { const assetName = item.AssetName || '-'; const assetCode = item.AssetCode ? `
${this.escapeHtml(item.AssetCode)}
` : ''; const typeMeta = this.getAssetRequestTypeMeta(item.RequestType); const statusMeta = this.getAssetRequestStatusMeta(item.RequestStatus, item); const note = String(item?.RequestNote || '').trim(); const rejectReason = String(item?.RejectReason || '').trim(); const canCancel = this.canCurrentUserCancelAssetRequest(item); const requestId = Number(item?.BorrowId) || 0; const requestType = this.normalizeAssetRequestType(item?.RequestType); const returnedQuantity = this.parseNonNegativeInteger(item?.ReturnedQuantity, 0); const borrowQuantity = this.parseNonNegativeInteger(item?.BorrowQuantity, 0); const remainingQuantity = this.parseNonNegativeInteger(item?.RemainingQuantity, Math.max(borrowQuantity - returnedQuantity, 0)); const canCreateReturn = this.canCreateAssetReturnRequestFromBorrow(item); const returnProgressHtml = requestType === 'borrow' && statusMeta.value === 'approved' ? `
Đã trả ${returnedQuantity}/${borrowQuantity}, còn ${remainingQuantity}
` : ''; const detailActionHtml = ` `; const returnActionHtml = canCreateReturn ? `` : ''; const cancelActionHtml = canCancel ? `` : ''; const actionHtml = returnActionHtml || cancelActionHtml ? `
${returnActionHtml}${cancelActionHtml}
` : `-`; return ` ${rowNumber} ${this.escapeHtml(item.BorrowerName || '-')}
${this.escapeHtml(assetName)}
${assetCode} ${this.getArchivedAssetBadgeHtml(item)} ${typeMeta.label} ${statusMeta.label} ${returnProgressHtml} ${this.escapeHtml(item.Unit || '-')} ${Number(item.BorrowQuantity) || 0} ${this.formatDateOnly(item.BorrowDate)} ${this.escapeHtml(note || '-')} ${this.escapeHtml(rejectReason || '-')} ${detailActionHtml} ${actionHtml} `; } canCreateAssetReturnRequestFromBorrow(item) { if (this.normalizeAssetRequestType(item?.RequestType) !== 'borrow') { return false; } if (this.normalizeAssetRequestStatus(item?.RequestStatus) !== 'approved') { return false; } const borrowQuantity = this.parseNonNegativeInteger(item?.BorrowQuantity, 0); const returnedQuantity = this.parseNonNegativeInteger(item?.ReturnedQuantity, 0); const remainingQuantity = this.parseNonNegativeInteger(item?.RemainingQuantity, Math.max(borrowQuantity - returnedQuantity, 0)); const availableReturnQuantity = this.parseNonNegativeInteger(item?.AvailableReturnQuantity, this.parseNonNegativeInteger(item?.RelatedReturnCount, 0) > 0 ? 0 : remainingQuantity); return borrowQuantity > 0 && remainingQuantity > 0 && availableReturnQuantity > 0; } canCurrentUserCancelAssetRequest(item) { const status = this.normalizeAssetRequestStatus(item?.RequestStatus); if (status !== 'pending') { return false; } if (this.canCurrentUserManageAssets()) { return true; } const currentUserId = Number(this.getUserId()); const createdBy = Number(item?.CreatedBy); return Number.isFinite(currentUserId) && currentUserId > 0 && Number.isFinite(createdBy) && createdBy === currentUserId; } buildAssetBorrowEmptyRowHtml() { return ` Chưa có đơn mượn/trả tài sản nào. `; } renderAssetBorrowsPager(pageInfo) { const pager = document.getElementById('assetBorrowsPager'); if (!pager) { return; } pager.innerHTML = ` Hiển thị ${pageInfo.start}-${pageInfo.end} / ${pageInfo.total}
Trang ${pageInfo.current} / ${pageInfo.totalPages}
`; } renderMyBorrowedAssetsPager(pageInfo) { const pager = document.getElementById('myBorrowedAssetsPager'); if (!pager) { return; } pager.innerHTML = ` Hiển thị ${pageInfo.start}-${pageInfo.end} / ${pageInfo.total}
Trang ${pageInfo.current} / ${pageInfo.totalPages}
`; } getMyBorrowedAssetsContent() { const filteredAssets = this.getFilteredMyBorrowedAssets(); const pageInfo = this.getPaged(filteredAssets, this.myBorrowedAssetPage, this.myBorrowedAssetPageSize); this.myBorrowedAssetPage = pageInfo.current; return `
Tìm kiếm
${pageInfo.data.length > 0 ? pageInfo.data.map((asset, index) => { const statusMeta = this.getAssetStatusMeta(asset.Status); return ` `; }).join('') : ` `}
STT Mã tài sản Tên tài sản Số lượng đang mượn Đơn vị Dự án Vị trí Trạng thái Ghi chú
${pageInfo.start + index} ${this.escapeHtml(asset.AssetCode || '-')} ${this.escapeHtml(asset.AssetName || '-')}${this.getArchivedAssetBadgeHtml(asset)} ${Number(asset.BorrowedQuantityByCurrentUser) || 0} ${this.escapeHtml(asset.Unit || '-')} ${this.escapeHtml(asset.Project || '-')} ${this.escapeHtml(asset.Location || '-')} ${statusMeta.label} ${this.escapeHtml(asset.Notes || '-')}
Hiện tại bạn chưa mượn tài sản nào.
Hiển thị ${pageInfo.start}-${pageInfo.end} / ${pageInfo.total}
Trang ${pageInfo.current} / ${pageInfo.totalPages}
`; } renderMyBorrowedAssetsTableBody() { const tbody = document.querySelector('.my-borrowed-assets-table-body'); if (!tbody) { return; } const pageInfo = this.getPaged(this.getFilteredMyBorrowedAssets(), this.myBorrowedAssetPage, this.myBorrowedAssetPageSize); this.myBorrowedAssetPage = pageInfo.current; if (!pageInfo.data.length) { tbody.innerHTML = ` Hiện tại bạn chưa mượn tài sản nào. `; } else { tbody.innerHTML = pageInfo.data.map((asset, index) => { const statusMeta = this.getAssetStatusMeta(asset.Status); return ` ${pageInfo.start + index} ${this.escapeHtml(asset.AssetCode || '-')} ${this.escapeHtml(asset.AssetName || '-')}${this.getArchivedAssetBadgeHtml(asset)} ${Number(asset.BorrowedQuantityByCurrentUser) || 0} ${this.escapeHtml(asset.Unit || '-')} ${this.escapeHtml(asset.Project || '-')} ${this.escapeHtml(asset.Location || '-')} ${statusMeta.label} ${this.escapeHtml(asset.Notes || '-')} `; }).join(''); } this.renderMyBorrowedAssetsPager(pageInfo); this.setupMyBorrowedAssetsPagerListeners(); } setupMyBorrowedAssetsPagerListeners() { document.querySelectorAll('.my-borrowed-asset-page-btn').forEach(btn => { btn.addEventListener('click', () => { const targetPage = Number(btn.dataset.page); if (!targetPage || targetPage < 1) { return; } this.myBorrowedAssetPage = targetPage; this.renderMyBorrowedAssetsTableBody(); }); }); } setupMyBorrowedAssetsListeners() { const searchInput = document.getElementById('myBorrowedAssetSearch'); if (searchInput && searchInput.dataset.boundInput !== 'true') { searchInput.addEventListener('input', event => { this.myBorrowedAssetSearchTerm = String(event.target.value || '').trim(); this.myBorrowedAssetPage = 1; this.renderMyBorrowedAssetsTableBody(); }); searchInput.addEventListener('focus', () => { searchInput.dataset.focused = 'true'; }); searchInput.addEventListener('blur', () => { searchInput.dataset.focused = 'false'; }); searchInput.dataset.boundInput = 'true'; } this.setupMyBorrowedAssetsPagerListeners(); } getAssetBorrowsContent() { const canManageAssets = this.canCurrentUserManageAssets(); const filteredBorrows = this.getFilteredAssetBorrows(); const pageInfo = this.getPaged(filteredBorrows, this.assetBorrowPage, this.assetBorrowPageSize); this.assetBorrowPage = pageInfo.current; const pendingCount = this.getPendingAssetRequestCount(); return `
Đơn
${pageInfo.data.length > 0 ? pageInfo.data.map((item, index) => this.buildAssetBorrowRowHtml(item, pageInfo.start + index)).join('') : this.buildAssetBorrowEmptyRowHtml()}
STT Tên đầy đủ Tài sản Danh mục Trạng thái Đơn vị Số lượng Ngày Ghi chú Lý do Chi tiết Hành động
Hiển thị ${pageInfo.start}-${pageInfo.end} / ${pageInfo.total}
Trang ${pageInfo.current} / ${pageInfo.totalPages}
`; } renderAssetBorrowsTableBody() { const tbody = document.querySelector('.asset-borrows-table-body'); if (!tbody) { return; } const pageInfo = this.getPaged(this.getFilteredAssetBorrows(), this.assetBorrowPage, this.assetBorrowPageSize); this.assetBorrowPage = pageInfo.current; if (!pageInfo.data.length) { tbody.innerHTML = this.buildAssetBorrowEmptyRowHtml(); } else { tbody.innerHTML = pageInfo.data .map((item, index) => this.buildAssetBorrowRowHtml(item, pageInfo.start + index)) .join(''); } this.renderAssetBorrowsPager(pageInfo); this.setupAssetBorrowPagerListeners(); this.updatePendingAssetRequestsBadge(); } async openAssetBorrowDetailsModal(requestId) { try { const response = await fetch(`${this.apiBase}/asset-borrows/${requestId}/history`, { headers: this.getAuthHeaders(false) }); const data = await response.json(); if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Không tải được chi tiết đơn.'); return; } this.showAssetBorrowDetailsModal(data.data || {}); } catch (err) { console.error(err); this.notifyFailure('Không tải được chi tiết đơn.'); } } buildAssetBorrowDetailCardHtml(item) { const typeMeta = this.getAssetRequestTypeMeta(item?.RequestType); const statusMeta = this.getAssetRequestStatusMeta(item?.RequestStatus, item); const requestType = this.normalizeAssetRequestType(item?.RequestType); const dateLabel = requestType === 'return' ? 'Ngày trả' : 'Ngày mượn'; const assetName = `${item?.AssetCode ? `${item.AssetCode} - ` : ''}${item?.AssetName || '-'}`; const note = String(item?.RequestNote || '').trim(); const rejectReason = String(item?.RejectReason || '').trim(); const returnedQuantity = this.parseNonNegativeInteger(item?.ReturnedQuantity, 0); const borrowQuantity = this.parseNonNegativeInteger(item?.BorrowQuantity, 0); const remainingQuantity = this.parseNonNegativeInteger(item?.RemainingQuantity, Math.max(borrowQuantity - returnedQuantity, 0)); const returnSummary = requestType === 'borrow' && returnedQuantity > 0 ? `
Đã trả: ${returnedQuantity}/${borrowQuantity}${remainingQuantity > 0 ? `, còn ${remainingQuantity}` : ''}
` : ''; return `
${typeMeta.label} ${statusMeta.label} #${Number(item?.BorrowId) || '-'}
Người tạo: ${this.escapeHtml(item?.BorrowerName || '-')}
${dateLabel}: ${this.formatDateOnly(item?.BorrowDate)}
Tài sản: ${this.escapeHtml(assetName)}
Số lượng: ${Number(item?.BorrowQuantity) || 0}
Đơn vị: ${this.escapeHtml(item?.Unit || '-')}
${returnSummary}
Ngày xử lý: ${this.formatDateTime(item?.ProcessedDate)}
Người xử lý: ${this.escapeHtml(item?.ProcessedByName || '-')}
Ghi chú: ${this.escapeHtml(note || '-')}
Lý do: ${this.escapeHtml(rejectReason || '-')}
`; } showAssetBorrowDetailsModal(history) { const selected = history?.request || {}; const borrowRequests = Array.isArray(history?.borrowRequests) ? history.borrowRequests : []; const returnRequests = Array.isArray(history?.returnRequests) ? history.returnRequests : []; const selectedTypeMeta = this.getAssetRequestTypeMeta(selected?.RequestType); const selectedStatusMeta = this.getAssetRequestStatusMeta(selected?.RequestStatus, selected); const assetName = `${selected?.AssetCode ? `${selected.AssetCode} - ` : ''}${selected?.AssetName || '-'}`; const renderList = (items, emptyText) => items.length ? items.map(item => this.buildAssetBorrowDetailCardHtml(item)).join('') : `
${emptyText}
`; let container = document.getElementById('assetBorrowDetailsModalContainer'); if (!container) { container = document.createElement('div'); container.id = 'assetBorrowDetailsModalContainer'; document.body.appendChild(container); } container.innerHTML = ` `; } setupAssetBorrowPagerListeners() { document.querySelectorAll('.asset-borrow-page-btn').forEach(btn => { btn.addEventListener('click', () => { const targetPage = Number(btn.dataset.page); if (!targetPage || targetPage < 1) { return; } this.assetBorrowPage = targetPage; this.renderAssetBorrowsTableBody(); }); }); } setupAssetBorrowListeners() { this.setupArchivedAssetListeners(); const typeFilter = document.getElementById('assetBorrowTypeFilter'); if (typeFilter && typeFilter.dataset.boundChange !== 'true') { typeFilter.addEventListener('change', event => { const nextValue = String(event.target.value || '').trim().toLowerCase(); this.assetBorrowTypeFilter = (nextValue === 'borrow' || nextValue === 'return' || nextValue === 'outstanding') ? nextValue : ''; this.assetBorrowPage = 1; this.renderAssetBorrowsTableBody(); }); typeFilter.dataset.boundChange = 'true'; } const catalogFilter = document.getElementById('assetBorrowCatalogFilter'); if (catalogFilter && catalogFilter.dataset.boundChange !== 'true') { catalogFilter.addEventListener('change', event => { this.assetBorrowCatalogFilter = ['current', 'archived'].includes(event.target.value) ? event.target.value : ''; this.assetBorrowPage = 1; this.renderAssetBorrowsTableBody(); }); catalogFilter.dataset.boundChange = 'true'; } const searchInput = document.getElementById('assetBorrowSearch'); if (searchInput && searchInput.dataset.boundInput !== 'true') { searchInput.addEventListener('input', (event) => { this.assetBorrowSearchTerm = String(event.target.value || '').trim(); this.assetBorrowPage = 1; this.renderAssetBorrowsTableBody(); }); searchInput.addEventListener('focus', () => { searchInput.dataset.focused = 'true'; }); searchInput.addEventListener('blur', () => { searchInput.dataset.focused = 'false'; }); searchInput.dataset.boundInput = 'true'; } const tableBody = document.querySelector('.asset-borrows-table-body'); if (tableBody && tableBody.dataset.boundActions !== 'true') { tableBody.addEventListener('click', (event) => { const detailButton = event.target.closest('.asset-borrow-detail-btn'); if (detailButton) { const requestId = Number(detailButton.dataset.requestId); if (Number.isFinite(requestId) && requestId > 0) { this.openAssetBorrowDetailsModal(requestId); } return; } const returnButton = event.target.closest('.asset-borrow-return-btn'); if (returnButton) { const requestId = Number(returnButton.dataset.requestId); if (Number.isFinite(requestId) && requestId > 0) { this.createAssetReturnRequestFromBorrow(requestId, returnButton); } return; } const cancelButton = event.target.closest('.asset-borrow-cancel-btn'); if (!cancelButton) { return; } const requestId = Number(cancelButton.dataset.requestId); if (!Number.isFinite(requestId) || requestId <= 0) { return; } this.deletePendingAssetBorrowRequest(requestId, { confirmMessage: `Bạn có chắc muốn hủy đơn #${requestId}?`, confirmButtonText: 'Hủy đơn', successMessage: 'Đã hủy đơn thành công', failureMessage: 'Hủy đơn thất bại' }); }); tableBody.dataset.boundActions = 'true'; } this.setupAssetBorrowPagerListeners(); } async createAssetReturnRequestFromBorrow(requestId, sourceButton = null) { const targetId = Number(requestId); if (!Number.isFinite(targetId) || targetId <= 0) { this.notifyWarning('Không xác định được đơn mượn cần trả.'); return; } if (sourceButton) { sourceButton.disabled = true; sourceButton.classList.add('opacity-60', 'cursor-not-allowed'); } try { const response = await fetch(`${this.apiBase}/asset-borrows/${targetId}/return`, { method: 'POST', headers: this.getAuthHeaders(false) }); const data = await response.json(); if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Tạo đơn trả tài sản thất bại'); return; } this.notifySuccess('Đã tạo đơn trả tài sản. Đơn đang chờ xử lý.'); await this.fetchAssetBorrows(); await this.fetchAssets(); if (this.currentPage === 'asset-borrows') { this.renderAssetBorrowsTableBody(); } if (this.currentPage === 'assets') { this.renderAssetsTableBody(); } if (this.currentPage === 'my-borrowed-assets') { this.renderMyBorrowedAssetsTableBody(); } const pendingModal = document.getElementById('assetPendingRequestsModal'); if (pendingModal?.classList.contains('open')) { this.renderPendingAssetRequestsModal(); } this.updatePendingAssetRequestsBadge(); } catch (err) { console.error(err); this.notifyFailure('Tạo đơn trả tài sản thất bại'); } finally { if (sourceButton && document.body.contains(sourceButton)) { sourceButton.disabled = false; sourceButton.classList.remove('opacity-60', 'cursor-not-allowed'); } } } async refreshAssetBorrowsUI() { await this.fetchAssetBorrows(); if (this.currentPage === 'asset-borrows') { this.renderAssetBorrowsTableBody(); } const pendingModal = document.getElementById('assetPendingRequestsModal'); if (pendingModal?.classList.contains('open')) { this.renderPendingAssetRequestsModal(); } } startAssetBorrowAutoRefresh() { if (this.assetBorrowAutoRefreshTimer) { return; } this.assetBorrowAutoRefreshTimer = setInterval(() => { if (this.currentPage === 'asset-borrows') { this.refreshAssetBorrowsUI(); } }, 15000); } stopAssetBorrowAutoRefresh() { if (!this.assetBorrowAutoRefreshTimer) { return; } clearInterval(this.assetBorrowAutoRefreshTimer); this.assetBorrowAutoRefreshTimer = undefined; } async openAssetBorrowRequestModal(requestType = 'borrow') { if (!this.assets.length) { await this.fetchAssets(); } this.assetBorrowRequestType = this.normalizeAssetRequestType(requestType); const modal = document.getElementById('assetBorrowRequestModal'); const typeInput = document.getElementById('assetBorrowRequestTypeInput'); const titleNode = document.getElementById('assetBorrowRequestModalTitle'); const dateLabel = document.getElementById('assetBorrowDateLabel'); const submitBtn = document.getElementById('assetBorrowRequestSubmitBtn'); const noteInput = document.getElementById('assetBorrowNoteInput'); const requesterInput = document.getElementById('assetBorrowRequesterInput'); const productSearchInput = document.getElementById('assetBorrowProductSearchInput'); const productInput = document.getElementById('assetBorrowProductInput'); const quantityInput = document.getElementById('assetBorrowQuantityInput'); const dateInput = document.getElementById('assetBorrowDateInput'); if (!modal || !requesterInput || !productInput || !quantityInput || !dateInput || !productSearchInput || !typeInput) { this.notifyFailure('Không tìm thấy biểu mẫu đơn mượn/trả tài sản.'); return; } const isReturnRequest = this.assetBorrowRequestType === 'return'; typeInput.value = this.assetBorrowRequestType; if (titleNode) { titleNode.textContent = isReturnRequest ? 'Tạo đơn trả tài sản' : 'Tạo đơn mượn tài sản'; } if (dateLabel) { dateLabel.textContent = isReturnRequest ? 'Ngày trả' : 'Ngày mượn'; } if (submitBtn) { submitBtn.textContent = isReturnRequest ? 'Tạo đơn trả' : 'Tạo đơn mượn'; } requesterInput.value = this.getCurrentUserDisplayName(); quantityInput.value = '1'; quantityInput.min = '1'; dateInput.value = this.toDateInputValue(new Date()); if (noteInput) { noteInput.value = ''; } productSearchInput.value = ''; productInput.value = ''; this.updateAssetBorrowProductDisplay(''); this.closeAssetBorrowProductDropdown(); await this.searchAssetBorrowProducts('', '', { reset: true }); if (!this.assetBorrowProductItems.length) { this.notifyWarning(isReturnRequest ? 'Hiện chưa có tài sản để tạo đơn trả.' : 'Hiện chưa có tài sản còn tồn cuối kỳ để tạo đơn mượn.'); return; } modal.classList.add('open'); } async handleAssetBorrowRequestSubmit(event) { event.preventDefault(); const typeInput = document.getElementById('assetBorrowRequestTypeInput'); const productInput = document.getElementById('assetBorrowProductInput'); const quantityInput = document.getElementById('assetBorrowQuantityInput'); const unitInput = document.getElementById('assetBorrowUnitInput'); const dateInput = document.getElementById('assetBorrowDateInput'); const requesterInput = document.getElementById('assetBorrowRequesterInput'); const noteInput = document.getElementById('assetBorrowNoteInput'); const requestType = this.normalizeAssetRequestType(typeInput?.value || this.assetBorrowRequestType); const assetId = Number(productInput?.value || 0); if (!Number.isFinite(assetId) || assetId <= 0) { this.notifyWarning('Vui lòng chọn tài sản.'); return; } const quantity = this.parseNonNegativeInteger(quantityInput?.value ?? 0, 0); if (quantity <= 0) { this.notifyWarning('Số lượng phải lớn hơn 0.'); return; } const selectedAsset = this.assetBorrowProductItems.find(item => Number(item?.AssetId) === assetId) || this.assets.find(item => Number(item?.AssetId) === assetId) || null; if (requestType === 'borrow' && selectedAsset && !this.isAssetAvailableForBorrow(selectedAsset)) { this.notifyWarning('Tài sản đã xuất hoặc hết tồn cuối kỳ, không thể tạo đơn mượn.'); await this.searchAssetBorrowProducts( document.getElementById('assetBorrowProductSearchInput')?.value || '', '', { reset: true } ); return; } const borrowDate = String(dateInput?.value || '').trim() || this.toDateInputValue(new Date()); const unit = String(unitInput?.value || '').trim(); const borrowerName = String(requesterInput?.value || this.getCurrentUserDisplayName() || '').trim(); const note = String(noteInput?.value || '').trim(); try { const response = await fetch(`${this.apiBase}/asset-borrows`, { method: 'POST', headers: this.getAuthHeaders(true), body: JSON.stringify({ assetId, requestType, quantity, unit, borrowDate, borrowerName, note }) }); const data = await response.json(); if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Tạo đơn thất bại'); return; } closeAssetBorrowRequestModal(); this.notifySuccess(requestType === 'return' ? 'Tạo đơn trả tài sản thành công' : 'Tạo đơn mượn tài sản thành công'); await this.refreshAssetBorrowsUI(); } catch (err) { console.error(err); this.notifyFailure('Tạo đơn thất bại'); } } buildPendingAssetRequestCardHtml(item) { const typeMeta = this.getAssetRequestTypeMeta(item?.RequestType); const dateLabel = typeMeta.value === 'return' ? 'Ngày trả' : 'Ngày mượn'; const note = String(item?.RequestNote || '').trim(); const requestId = Number(item?.BorrowId) || 0; const blockedArchivedBorrow = this.isArchivedAsset(item) && typeMeta.value === 'borrow'; return `
${typeMeta.label} #${requestId || '-'}
Tên đầy đủ: ${this.escapeHtml(item?.BorrowerName || '-')}
Tên tài sản: ${this.escapeHtml(item?.AssetCode || '')} ${item?.AssetCode ? '- ' : ''}${this.escapeHtml(item?.AssetName || '-')}
${this.getArchivedAssetBadgeHtml(item)} ${blockedArchivedBorrow ? '

Tài sản đã xóa khỏi danh mục. Vui lòng từ chối hoặc hủy đơn mượn này.

' : ''}
Số lượng: ${Number(item?.BorrowQuantity) || 0} ${this.escapeHtml(item?.Unit || '')}
Ghi chú: ${this.escapeHtml(note || '-')}
${dateLabel}: ${this.formatDateOnly(item?.BorrowDate)}
${blockedArchivedBorrow ? '' : ``}
`; } bindPendingAssetRequestActionButtons() { document.querySelectorAll('.asset-request-approve-btn').forEach(btn => { if (btn.dataset.boundClick === 'true') { return; } btn.addEventListener('click', () => { const requestId = Number(btn.dataset.requestId); if (!Number.isFinite(requestId) || requestId <= 0) { return; } this.processAssetBorrowRequest(requestId, 'approved'); }); btn.dataset.boundClick = 'true'; }); document.querySelectorAll('.asset-request-reject-btn').forEach(btn => { if (btn.dataset.boundClick === 'true') { return; } btn.addEventListener('click', () => { const requestId = Number(btn.dataset.requestId); if (!Number.isFinite(requestId) || requestId <= 0) { return; } this.openAssetRequestRejectModal(requestId); }); btn.dataset.boundClick = 'true'; }); document.querySelectorAll('.asset-request-delete-btn').forEach(btn => { if (btn.dataset.boundClick === 'true') { return; } btn.addEventListener('click', () => { const requestId = Number(btn.dataset.requestId); if (!Number.isFinite(requestId) || requestId <= 0) { return; } this.deletePendingAssetBorrowRequest(requestId); }); btn.dataset.boundClick = 'true'; }); } renderPendingAssetRequestsModal() { const borrowList = document.getElementById('pendingBorrowRequestsList'); const returnList = document.getElementById('pendingReturnRequestsList'); if (!borrowList || !returnList) { return; } const pendingRequests = (Array.isArray(this.assetBorrows) ? this.assetBorrows : []) .filter(item => this.normalizeAssetRequestStatus(item?.RequestStatus) === 'pending'); const pendingBorrowRequests = pendingRequests.filter(item => this.normalizeAssetRequestType(item?.RequestType) === 'borrow'); const pendingReturnRequests = pendingRequests.filter(item => this.normalizeAssetRequestType(item?.RequestType) === 'return'); const borrowCountBadge = document.getElementById('pendingBorrowCountBadge'); const returnCountBadge = document.getElementById('pendingReturnCountBadge'); if (borrowCountBadge) { borrowCountBadge.textContent = pendingBorrowRequests.length > 99 ? '99+' : String(pendingBorrowRequests.length); } if (returnCountBadge) { returnCountBadge.textContent = pendingReturnRequests.length > 99 ? '99+' : String(pendingReturnRequests.length); } borrowList.innerHTML = pendingBorrowRequests.length ? pendingBorrowRequests.map(item => this.buildPendingAssetRequestCardHtml(item)).join('') : `
Không có đơn mượn nào đang chờ.
`; returnList.innerHTML = pendingReturnRequests.length ? pendingReturnRequests.map(item => this.buildPendingAssetRequestCardHtml(item)).join('') : `
Không có đơn trả nào đang chờ.
`; this.bindPendingAssetRequestActionButtons(); } async openPendingAssetRequestsModal() { if (!this.canCurrentUserManageAssets()) { this.notifyWarning('Chỉ role Asset/Admin mới được xử lý đơn chờ.'); return; } const modal = document.getElementById('assetPendingRequestsModal'); if (!modal) { this.notifyFailure('Không tìm thấy hộp thoại đơn chờ.'); return; } await this.fetchAssetBorrows(); this.renderPendingAssetRequestsModal(); modal.style.zIndex = '120'; modal.classList.add('open'); } openAssetRequestRejectModal(requestId) { if (!this.canCurrentUserManageAssets()) { return; } const rejectModal = document.getElementById('assetRequestRejectModal'); const idInput = document.getElementById('assetRequestRejectIdInput'); const reasonInput = document.getElementById('assetRequestRejectReasonInput'); if (!rejectModal || !idInput || !reasonInput) { this.notifyFailure('Không tìm thấy hộp thoại từ chối đơn.'); return; } this.pendingAssetRequestRejectId = Number(requestId); idInput.value = String(requestId); reasonInput.value = ''; rejectModal.style.zIndex = '130'; rejectModal.classList.add('open'); reasonInput.focus(); } resolveAssetRequestDeleteConfirm(confirmed) { const modal = document.getElementById('assetRequestDeleteConfirmModal'); if (modal) { modal.classList.remove('open'); } const resolver = this.pendingAssetRequestDeleteConfirmResolver; this.pendingAssetRequestDeleteConfirmResolver = undefined; if (typeof resolver === 'function') { resolver(Boolean(confirmed)); } } resolveBulkAssetDeleteConfirm(confirmed) { const modal = document.getElementById('bulkDeleteAssetsConfirmModal'); if (modal) { modal.classList.remove('open'); } const resolver = this.pendingBulkAssetDeleteConfirmResolver; this.pendingBulkAssetDeleteConfirmResolver = undefined; if (typeof resolver === 'function') { resolver(Boolean(confirmed)); } } async confirmBulkAssetDelete(selectedCount, archiveAll = false) { const modal = document.getElementById('bulkDeleteAssetsConfirmModal'); const messageNode = document.getElementById('bulkDeleteAssetsConfirmMessage'); const countNode = document.getElementById('bulkDeleteAssetsConfirmCount'); const titleNode = document.getElementById('bulkDeleteAssetsConfirmTitle'); const message = archiveAll ? 'Xóa toàn bộ danh mục tài sản hiện tại để nhập dữ liệu mới? Toàn bộ hồ sơ được lưu trong Tài sản cũ, gồm cả lịch sử và số lượng còn đang mượn. Dữ liệu nhập mới được quản lý riêng, kể cả khi trùng mã tài sản.' : `Xóa ${selectedCount} tài sản đã chọn khỏi danh mục hiện tại? Toàn bộ hồ sơ, lịch sử mượn/trả và số lượng còn đang mượn vẫn được giữ trong Tài sản cũ.`; if (!modal || !messageNode || !countNode) { return window.confirm(message); } countNode.textContent = String(selectedCount); messageNode.textContent = message; if (titleNode) titleNode.textContent = archiveAll ? 'Xóa danh mục hiện tại' : 'Xóa tài sản đã chọn'; if (this.pendingBulkAssetDeleteConfirmResolver) { this.resolveBulkAssetDeleteConfirm(false); } modal.classList.add('open'); return new Promise(resolve => { this.pendingBulkAssetDeleteConfirmResolver = resolve; }); } async confirmAssetRequestDelete(message, confirmButtonText = 'Xóa đơn') { const modal = document.getElementById('assetRequestDeleteConfirmModal'); const messageNode = document.getElementById('assetRequestDeleteConfirmMessage'); const confirmButton = document.getElementById('confirmAssetRequestDeleteBtn'); if (!modal || !messageNode || !confirmButton) { return window.confirm(message); } messageNode.textContent = String(message || 'Bạn có chắc muốn thực hiện thao tác này?'); confirmButton.textContent = String(confirmButtonText || 'Xóa đơn'); if (this.pendingAssetRequestDeleteConfirmResolver) { this.resolveAssetRequestDeleteConfirm(false); } modal.style.zIndex = '140'; modal.classList.add('open'); return new Promise(resolve => { this.pendingAssetRequestDeleteConfirmResolver = resolve; }); } async handleAssetRequestRejectSubmit(event) { event.preventDefault(); const idInput = document.getElementById('assetRequestRejectIdInput'); const reasonInput = document.getElementById('assetRequestRejectReasonInput'); const requestId = Number(idInput?.value || this.pendingAssetRequestRejectId); const reason = String(reasonInput?.value || '').trim(); if (!Number.isFinite(requestId) || requestId <= 0) { this.notifyWarning('Không xác định được đơn cần từ chối.'); return; } if (!reason) { this.notifyWarning('Vui lòng nhập lý do từ chối.'); return; } await this.processAssetBorrowRequest(requestId, 'rejected', reason); } async processAssetBorrowRequest(requestId, action, rejectReason = '') { if (!this.canCurrentUserManageAssets()) { this.notifyWarning('Chỉ role Asset/Admin mới được xử lý đơn chờ.'); return; } try { const response = await fetch(`${this.apiBase}/asset-borrows/${requestId}/process`, { method: 'POST', headers: this.getAuthHeaders(true), body: JSON.stringify({ action, rejectReason }) }); const data = await response.json(); if (!response.ok || !data.success) { const failureMessage = data?.message || 'Xử lý đơn thất bại'; this.notifyFailure(failureMessage); const canAutoSuggestDelete = action === 'approved' && typeof failureMessage === 'string' && failureMessage.toLowerCase().includes('xóa đơn chờ'); if (canAutoSuggestDelete) { const shouldDelete = await this.confirmAssetRequestDelete( `Đơn #${requestId} không còn hợp lệ. Bạn có muốn xóa đơn chờ này không?`, 'Xóa đơn' ); if (shouldDelete) { await this.deletePendingAssetBorrowRequest(requestId); } } return; } if (action === 'rejected') { this.pendingAssetRequestRejectId = undefined; closeAssetRequestRejectModal(); } this.notifySuccess(action === 'approved' ? 'Đã chấp nhận đơn' : 'Đã từ chối đơn'); await this.fetchAssetBorrows(); await this.fetchAssets(); if (this.currentPage === 'asset-borrows') { this.renderAssetBorrowsTableBody(); } if (this.currentPage === 'assets') { this.renderAssetsTableBody(); } const pendingModal = document.getElementById('assetPendingRequestsModal'); if (pendingModal?.classList.contains('open')) { this.renderPendingAssetRequestsModal(); } this.updatePendingAssetRequestsBadge(); } catch (err) { console.error(err); this.notifyFailure('Xử lý đơn thất bại'); } } async deletePendingAssetBorrowRequest(requestId, options = {}) { const targetId = Number(requestId); if (!Number.isFinite(targetId) || targetId <= 0) { this.notifyWarning('Không xác định được đơn cần xóa.'); return; } const confirmMessage = String(options?.confirmMessage || `Bạn có chắc muốn xóa đơn chờ #${targetId}?`); const confirmButtonText = String(options?.confirmButtonText || 'Xóa đơn'); const successMessage = String(options?.successMessage || 'Đã xóa đơn chờ'); const failureMessage = String(options?.failureMessage || 'Xóa đơn chờ thất bại'); const confirmed = await this.confirmAssetRequestDelete(confirmMessage, confirmButtonText); if (!confirmed) { return; } try { const response = await fetch(`${this.apiBase}/asset-borrows/${targetId}`, { method: 'DELETE', headers: this.getAuthHeaders(false) }); const data = await response.json(); if (!response.ok || !data.success) { const resolvedFailureMessage = data.message || failureMessage; if (response.status === 403) { this.notifyWarning(resolvedFailureMessage); } else { this.notifyFailure(resolvedFailureMessage); } return; } this.notifySuccess(data.message || successMessage); await this.fetchAssetBorrows(); if (this.currentPage === 'asset-borrows') { this.renderAssetBorrowsTableBody(); } const pendingModal = document.getElementById('assetPendingRequestsModal'); if (pendingModal?.classList.contains('open')) { this.renderPendingAssetRequestsModal(); } this.updatePendingAssetRequestsBadge(); } catch (err) { console.error(err); this.notifyFailure(failureMessage); } } buildConsumableTableRows(pageInfo) { const canManageAssets = this.canCurrentUserManageAssets(); return pageInfo.data.map((item, index) => { const statusMeta = this.getConsumableStockStatusMeta(item); const rowNumber = pageInfo.start + index; const reason = String(item.ExportReason || '').trim(); const exportedSummary = String(item.ExportedSummary || '').trim(); const recipientSummary = String(item.RecipientSummary || '').trim(); const projectSummary = String(item.ProjectSummary || '').trim(); return ` ${rowNumber} ${this.escapeHtml(item.RequestMonth || '-')} ${this.escapeHtml(item.ConsumableCode || '-')} ${this.escapeHtml(item.ConsumableName || '-')} ${this.escapeHtml(item.Model || '-')} ${this.escapeHtml(item.Unit || '-')} ${item.OpeningBalance ?? 0} ${item.ImportInPeriod ?? 0} ${item.ExportInPeriod ?? 0} ${this.escapeHtml(exportedSummary || '-')} ${this.escapeHtml(recipientSummary || '-')} ${this.escapeHtml(projectSummary || '-')} ${item.EndingBalance ?? 0} ${statusMeta.label} ${this.escapeHtml(reason || '-')} ${this.formatDateOnly(item.UpdatedDate || item.CreatedDate)}
`; }).join(''); } getConsumableMobileCardHtml(item, rowNumber, canManageAssets) { const statusMeta = this.getConsumableStockStatusMeta(item); const safe = value => this.escapeHtml(String(value ?? '').trim() || '-'); const number = value => Number.isFinite(Number(value)) ? Number(value) : 0; const unit = String(item?.Unit || '').trim(); const exportedSummary = String(item?.ExportedSummary || '').trim(); const recipientSummary = String(item?.RecipientSummary || '').trim(); const projectSummary = String(item?.ProjectSummary || '').trim(); const reason = String(item?.ExportReason || '').trim(); return `
#${rowNumber} · ${safe(item?.ConsumableCode)}

${safe(item?.ConsumableName)}

Tháng: ${safe(item?.RequestMonth)} Model: ${safe(item?.Model)} ĐVT: ${safe(unit)}
${statusMeta.label}
Tồn đầu ${number(item?.OpeningBalance)}
Nhập ${number(item?.ImportInPeriod)}
Xuất ${number(item?.ExportInPeriod)}
Tồn cuối ${number(item?.EndingBalance)}
${(exportedSummary || recipientSummary || projectSummary) ? `
${exportedSummary ? `
Đã xuất: ${safe(exportedSummary)}
` : ''} ${recipientSummary ? `
Người đang nhận: ${safe(recipientSummary)}
` : ''} ${projectSummary ? `
Dự án nhận: ${safe(projectSummary)}
` : ''}
` : ''}
${reason ? `
Lý do: ${safe(reason)}
` : ''}
Cập nhật: ${this.formatDateOnly(item?.UpdatedDate || item?.CreatedDate)}
`; } getConsumablesContent() { const canManageAssets = this.canCurrentUserManageAssets(); const filteredConsumables = this.getFilteredConsumables(); const pageInfo = this.getPaged(filteredConsumables, this.consumablePage, this.consumablePageSize); const monthOptions = this.getConsumableMonthOptions(); const pendingBorrowRequestCount = this.getPendingConsumableBorrowRequestCount(); const rejectedReturnRequests = canManageAssets ? [] : this.getRejectedConsumableReturnRequests(); const requestNotificationCount = canManageAssets ? pendingBorrowRequestCount : pendingBorrowRequestCount + rejectedReturnRequests.length; const latestRejectedReturn = rejectedReturnRequests[0]; const latestRejectedItemLabel = [latestRejectedReturn?.ConsumableCode, latestRejectedReturn?.ConsumableName] .filter(Boolean) .join(' - ') || 'Vật tư'; const latestRejectReason = String(latestRejectedReturn?.RejectReason || '').trim() || 'Chưa ghi nhận lý do từ chối'; this.consumablePage = pageInfo.current; return `
${latestRejectedReturn ? `
notification_important
${rejectedReturnRequests.length > 1 ? `Có ${rejectedReturnRequests.length} đơn trả bị từ chối` : `Đơn trả #${Number(latestRejectedReturn?.BorrowRequestId) || '-'} đã bị từ chối`}
${this.escapeHtml(latestRejectedItemLabel)} — Lý do: ${this.escapeHtml(latestRejectReason)}
` : ''}
Tháng
Trạng thái
${pageInfo.data.length > 0 ? `
${this.buildConsumableTableRows(pageInfo)}
STT Tháng đề xuất Mã vật tư Tên linh kiện/sp Model ĐVT Tồn đầu Nhập Xuất Đã xuất Người đang nhận Dự án nhận Tồn cuối Trạng thái Lý do xuất Cập nhật Thao tác
${pageInfo.data.map((item, index) => this.getConsumableMobileCardHtml(item, pageInfo.start + index, canManageAssets)).join('')}
Hiển thị ${pageInfo.start}-${pageInfo.end} / ${pageInfo.total}
Trang ${pageInfo.current} / ${pageInfo.totalPages}
` : `

Chưa có dữ liệu vật tư tiêu hao.

`}
`; } renderConsumablesTableBody() { const tbody = document.querySelector('.consumables-table-body'); if (!tbody) { if (this.currentPage === 'consumables') { this.renderView('consumables'); } return; } const pageInfo = this.getPaged(this.getFilteredConsumables(), this.consumablePage, this.consumablePageSize); this.consumablePage = pageInfo.current; tbody.innerHTML = this.buildConsumableTableRows(pageInfo); const mobileList = document.querySelector('.consumables-mobile-list'); if (mobileList) { const canManageAssets = this.canCurrentUserManageAssets(); mobileList.innerHTML = pageInfo.data .map((item, index) => this.getConsumableMobileCardHtml(item, pageInfo.start + index, canManageAssets)) .join(''); } const pager = document.getElementById('consumablesPager'); if (pager) { pager.innerHTML = ` Hiển thị ${pageInfo.start}-${pageInfo.end} / ${pageInfo.total}
Trang ${pageInfo.current} / ${pageInfo.totalPages}
`; } this.setupConsumableRowListeners(); this.setupConsumablePagerListeners(); } setupConsumablePagerListeners() { document.querySelectorAll('.consumable-page-btn').forEach(btn => { if (btn.dataset.boundClick === 'true') { return; } btn.addEventListener('click', () => { this.consumablePage = Number(btn.dataset.page) || 1; this.renderConsumablesTableBody(); }); btn.dataset.boundClick = 'true'; }); } getConsumableExportsContent() { const canManageAssets = this.canCurrentUserManageAssets(); const pageDescription = canManageAssets ? 'Theo dõi toàn bộ số lượng đang mượn, đã xuất cho dự án và đã hoàn trả về kho.' : 'Theo dõi vật tư đã xuất / cho mượn đối với tài khoản của bạn.'; const filteredRows = this.getFilteredConsumableExportHistories(); const pageInfo = this.getPaged(filteredRows, this.consumableExportPage, this.consumableExportPageSize); const recipientOptions = this.getConsumableExportRecipientOptions(); const projectOptions = this.getConsumableExportProjectOptions(); this.consumableExportPage = pageInfo.current; return `
Người nhận
Dự án
Ngày
${this.buildConsumableExportHistoryPageRowsHtml(pageInfo)}
STT Ngày giờ Mã vật tư Tên vật tư Đã xuất Đã trả Còn lại ĐVT Người nhận Dự án nhận Trạng thái Người xuất Tồn trước/sau Ghi chú Thao tác
Hiển thị ${pageInfo.start}-${pageInfo.end} / ${pageInfo.total}
Trang ${pageInfo.current} / ${pageInfo.totalPages}
`; } buildConsumableExportHistoryEmptyRowHtml() { return ` Chưa có dữ liệu lịch sử mượn / xuất vật tư. `; } buildConsumableExportHistoryPageRowHtml(item, rowNumber) { const exportedQuantity = this.parseNonNegativeInteger(item?.ExportQuantity, 0); const returnedQuantity = this.parseNonNegativeInteger(item?.ReturnedQuantity, 0); const remainingQuantity = this.parseNonNegativeInteger(item?.RemainingQuantity, 0); const statusMeta = this.getConsumableReturnStatusMeta(item); const returnAction = this.getConsumableReturnActionMeta(item); const returnRequestNotice = this.buildConsumableReturnRequestNoticeHtml(item); const balanceLabel = `${Number(item?.PreviousEndingBalance) || 0} -> ${Number(item?.NextEndingBalance) || 0}`; const historyNote = [ item?.ExportNote ? `Xuất/mượn: ${item.ExportNote}` : '', item?.LastReturnedDate ? `Trả gần nhất ${this.formatDateTime(item.LastReturnedDate)}${item?.LastReturnedByName ? ` - ${item.LastReturnedByName}` : ''}${item?.LastReturnNote ? `: ${item.LastReturnNote}` : ''}` : '' ].filter(Boolean).join('\n') || '-'; return ` ${rowNumber} ${this.formatDateTime(item?.ExportedDate || item?.CreatedDate)} ${this.escapeHtml(item?.ConsumableCode || '-')} ${this.escapeHtml(item?.ConsumableName || '-')} ${exportedQuantity} ${returnedQuantity} ${remainingQuantity} ${this.escapeHtml(item?.Unit || '-')} ${this.escapeHtml(item?.RecipientName || '-')} ${this.escapeHtml(item?.ProjectName || '-')} ${statusMeta.label}${returnRequestNotice} ${this.escapeHtml(item?.ExportedByName || '-')} ${this.escapeHtml(balanceLabel)} ${this.escapeHtml(historyNote)} ${returnAction ? ` ` : '-'} `; } buildConsumableExportHistoryPageRowsHtml(pageInfo) { if (!pageInfo?.data?.length) { return this.buildConsumableExportHistoryEmptyRowHtml(); } return pageInfo.data .map((item, index) => this.buildConsumableExportHistoryPageRowHtml(item, pageInfo.start + index)) .join(''); } renderConsumableExportHistoryPageBody() { const tbody = document.getElementById('consumableExportHistoryPageTableBody'); if (!tbody) { return; } const pageInfo = this.getPaged( this.getFilteredConsumableExportHistories(), this.consumableExportPage, this.consumableExportPageSize ); this.consumableExportPage = pageInfo.current; tbody.innerHTML = this.buildConsumableExportHistoryPageRowsHtml(pageInfo); this.renderConsumableExportHistoryPager(pageInfo); this.setupConsumableExportHistoryPagerListeners(); this.setupConsumableReturnActionListeners(); } renderConsumableExportHistoryPager(pageInfo) { const pager = document.getElementById('consumableExportsPager'); if (!pager) { return; } pager.innerHTML = ` Hiển thị ${pageInfo.start}-${pageInfo.end} / ${pageInfo.total}
Trang ${pageInfo.current} / ${pageInfo.totalPages}
`; } async refreshConsumableExportsPage() { const tbody = document.getElementById('consumableExportHistoryPageTableBody'); if (tbody) { tbody.innerHTML = ` Đang tải lịch sử mượn / xuất / trả... `; } await Promise.all([ this.fetchConsumableExportHistories(2000), this.fetchConsumableBorrowRequests() ]); if (this.currentPage === 'consumable-exports') { const recipientFilter = document.getElementById('consumableExportRecipientFilter'); if (recipientFilter) { const recipientOptions = this.getConsumableExportRecipientOptions(); const currentValue = this.consumableExportRecipientFilter; recipientFilter.innerHTML = ` ${recipientOptions.map(name => ``).join('')} `; } const projectFilter = document.getElementById('consumableExportProjectFilter'); if (projectFilter) { const projectOptions = this.getConsumableExportProjectOptions(); const currentValue = this.consumableExportProjectFilter; projectFilter.innerHTML = ` ${projectOptions.map(name => ``).join('')} `; } this.renderConsumableExportHistoryPageBody(); } } setupConsumableExportHistoryPagerListeners() { document.querySelectorAll('.consumable-export-page-btn').forEach(btn => { if (btn.dataset.boundClick === 'true') { return; } btn.addEventListener('click', () => { const targetPage = Number(btn.dataset.page); if (!targetPage || targetPage < 1) { return; } this.consumableExportPage = targetPage; this.renderConsumableExportHistoryPageBody(); }); btn.dataset.boundClick = 'true'; }); } setupConsumableExportHistoryListeners() { const recipientFilter = document.getElementById('consumableExportRecipientFilter'); if (recipientFilter && recipientFilter.dataset.boundChange !== 'true') { recipientFilter.addEventListener('change', event => { this.consumableExportRecipientFilter = String(event.target.value || '').trim(); this.consumableExportPage = 1; this.renderConsumableExportHistoryPageBody(); }); recipientFilter.dataset.boundChange = 'true'; } const projectFilter = document.getElementById('consumableExportProjectFilter'); if (projectFilter && projectFilter.dataset.boundChange !== 'true') { projectFilter.addEventListener('change', event => { this.consumableExportProjectFilter = String(event.target.value || '').trim(); this.consumableExportPage = 1; this.renderConsumableExportHistoryPageBody(); }); projectFilter.dataset.boundChange = 'true'; } const dateFilter = document.getElementById('consumableExportDateFilter'); if (dateFilter && dateFilter.dataset.boundChange !== 'true') { dateFilter.addEventListener('change', event => { this.consumableExportDateFilter = String(event.target.value || '').trim(); this.consumableExportPage = 1; this.renderConsumableExportHistoryPageBody(); }); dateFilter.dataset.boundChange = 'true'; } const searchInput = document.getElementById('consumableExportSearch'); if (searchInput && searchInput.dataset.boundInput !== 'true') { searchInput.addEventListener('input', event => { this.consumableExportSearchTerm = String(event.target.value || '').trim(); this.consumableExportPage = 1; this.renderConsumableExportHistoryPageBody(); }); searchInput.addEventListener('focus', () => { searchInput.dataset.focused = 'true'; }); searchInput.addEventListener('blur', () => { searchInput.dataset.focused = 'false'; }); searchInput.dataset.boundInput = 'true'; } const refreshBtn = document.getElementById('refreshConsumableExportHistoryPageBtn'); if (refreshBtn && refreshBtn.dataset.boundClick !== 'true') { refreshBtn.addEventListener('click', () => this.refreshConsumableExportsPage()); refreshBtn.dataset.boundClick = 'true'; } const exportBtn = document.getElementById('exportConsumableHistoryBtn'); if (exportBtn && exportBtn.dataset.boundClick !== 'true') { exportBtn.addEventListener('click', () => this.exportConsumableHistoryToExcel()); exportBtn.dataset.boundClick = 'true'; } this.setupConsumableExportHistoryPagerListeners(); } getAssetProjectAllocations(asset) { let rows = asset?.ProjectAllocations || []; if (typeof rows === 'string') { try { rows = JSON.parse(rows); } catch { rows = []; } } const projects = new Map(); for (const row of Array.isArray(rows) ? rows : []) { const name = String(row.projectName || '').trim(); const quantity = Number(row.quantity); if (!name || !Number.isSafeInteger(quantity) || quantity <= 0) continue; const key = name.toLowerCase(); const existing = projects.get(key) || { projectName: name, quantity: 0 }; existing.quantity += quantity; projects.set(key, existing); } const result = [...projects.values()]; const allocated = result.reduce((sum, row) => sum + row.quantity, 0); const unattributed = Math.max(this.buildAssetQuantityMetrics(asset).exportInPeriod - allocated, 0); if (unattributed) result.push({ projectName: 'Chưa xác định dự án', quantity: unattributed }); return result; } formatAssetProjectsText(asset) { return this.getAssetProjectAllocations(asset) .map(row => `${row.projectName}: ${row.quantity}${asset?.Unit ? ` ${asset.Unit}` : ''}`).join('\n'); } formatAssetProjectsHtml(asset) { const rows = this.getAssetProjectAllocations(asset); return rows.length ? rows.map(row => `
${this.escapeHtml(row.projectName)}: ${row.quantity} ${this.escapeHtml(asset?.Unit || '')}
`).join('') : '-'; } getAssetAvailableForExport(asset) { const metrics = this.buildAssetQuantityMetrics(asset); const allocated = this.getAssetProjectAllocations(asset).reduce((sum, row) => sum + row.quantity, 0); return Math.min(metrics.endingBalance, Math.max(metrics.quantity + metrics.importInPeriod - Math.max(metrics.exportInPeriod, metrics.borrowerExportInPeriod, allocated), 0)); } formatAssetLocationHtml(location, constrainToContainer = false) { const value = String(location ?? '').trim(); if (!value) return '-'; const safeValue = this.escapeHtml(value); const maxWidth = constrainToContainer ? 'min(100%, 160px)' : '160px'; return `${safeValue}`; } getAssetMobileCardHtml(asset, rowNumber, canManageAssets) { const statusMeta = this.getAssetStatusMeta(asset?.Status); const assetId = Number(asset?.AssetId); const isSelected = Number.isFinite(assetId) && this.selectedAssetIds.has(assetId); const unit = String(asset?.Unit || '').trim(); const endingBalance = Number(asset?.EndingBalance) || 0; const borrowedQuantity = Number(asset?.ExportInPeriod) || 0; const borrower = this.formatBorrowerSummaryText(asset?.Borrower); const safe = value => this.escapeHtml(String(value ?? '').trim() || '-'); return `
#${rowNumber} · ${safe(asset?.AssetCode)}

${safe(asset?.AssetName)}

${statusMeta.label}
Model${safe(asset?.Model)}
Serial${safe(asset?.SerialNumber)}
Tổng tài sản${this.buildAssetQuantityMetrics(asset).quantity + this.buildAssetQuantityMetrics(asset).importInPeriod}
Tồn cuối kỳ${endingBalance}${unit ? ` ${safe(unit)}` : ''}
Đang mượn${borrowedQuantity}${unit ? ` ${safe(unit)}` : ''}
Phòng ban${safe(asset?.Department)}
Dự án
${this.formatAssetProjectsHtml(asset)}
Vị trí
${this.formatAssetLocationHtml(asset?.Location, true)}
Phụ trách${safe(asset?.Custodian)}
${borrower ? `
Người mượn ${safe(borrower)}
` : ''}
`; } getAssetsContent() { this.syncSelectedAssetIds(); const canManageAssets = this.canCurrentUserManageAssets(); const filteredAssets = this.getFilteredAssets(); const pageInfo = this.getPaged(filteredAssets, this.assetPage, this.assetPageSize); const selectedCount = this.selectedAssetIds.size; const pageAssetIds = pageInfo.data .map(asset => Number(asset.AssetId)) .filter(id => Number.isFinite(id)); const selectedOnPageCount = pageAssetIds.filter(id => this.selectedAssetIds.has(id)).length; const allOnPageSelected = pageAssetIds.length > 0 && selectedOnPageCount === pageAssetIds.length; this.assetPage = pageInfo.current; return `
Trạng thái
${selectedCount}Tài sản đã chọn
${selectedCount ? 'Chọn thao tác áp dụng cho các tài sản này.' : 'Đánh dấu ô chọn trong danh sách để thao tác.'}
${canManageAssets ? `
Khác

Với tài sản đã chọn

` : ''}
${pageInfo.data.length > 0 ? `
${pageInfo.data.map((asset, index) => { const statusMeta = this.getAssetStatusMeta(asset.Status); const assetId = Number(asset.AssetId); const isSelected = Number.isFinite(assetId) && this.selectedAssetIds.has(assetId); const rowNumber = pageInfo.start + index; return ` `; }).join('')}
STT Mã Tên tài sản Model Serial Số lượng (Tồn đầu kỳ) Tổng tài sản Nhập trong kỳ Xuất trong kỳ Tồn cuối kỳ Đơn vị Phòng ban Dự án đang xuất / SL Người phụ trách Trạng thái SL hàng mới SL đã qua sử dụng SL đang mượn Vị trí Ngày mua Người mượn Ghi chú Ngày tạo Người xuất Thao tác
${rowNumber} ${asset.AssetCode || '-'} ${asset.AssetName || '-'} ${asset.Model || '-'} ${asset.SerialNumber || '-'} ${asset.Quantity || 0} ${this.buildAssetQuantityMetrics(asset).quantity + this.buildAssetQuantityMetrics(asset).importInPeriod} ${asset.ImportInPeriod ?? 0} ${asset.ExportInPeriod ?? 0} ${asset.EndingBalance ?? 0} ${asset.Unit || '-'} ${asset.Department || '-'} ${this.formatAssetProjectsHtml(asset)} ${asset.Custodian || '-'} ${statusMeta.label} ${asset.NewQuantity ?? 0} ${asset.UsedQuantity ?? 0} ${asset.ExportInPeriod ?? 0} ${this.formatAssetLocationHtml(asset.Location)} ${this.formatDateOnly(asset.PurchaseDate)} ${this.formatBorrowerTableHtml(asset.Borrower)} ${asset.Notes || '-'} ${this.formatDateOnly(asset.CreatedDate)} ${asset.ExportedBy || '-'}
${pageInfo.data.map((asset, index) => this.getAssetMobileCardHtml(asset, pageInfo.start + index, canManageAssets)).join('')}
Hiển thị ${pageInfo.start}-${pageInfo.end} / ${pageInfo.total}
Trang ${pageInfo.current} / ${pageInfo.totalPages}
` : `

Chưa có dữ liệu tài sản. Hãy thêm tài sản đầu tiên.

`}
`; } renderAssetsTableBody() { const tbody = document.querySelector('.assets-table-body'); if (!tbody) return; this.syncSelectedAssetIds(); const canManageAssets = this.canCurrentUserManageAssets(); const pageInfo = this.getPaged(this.getFilteredAssets(), this.assetPage, this.assetPageSize); this.assetPage = pageInfo.current; tbody.innerHTML = pageInfo.data.map((asset, index) => { const statusMeta = this.getAssetStatusMeta(asset.Status); const assetId = Number(asset.AssetId); const isSelected = Number.isFinite(assetId) && this.selectedAssetIds.has(assetId); const rowNumber = pageInfo.start + index; return ` ${rowNumber} ${asset.AssetCode || '-'} ${asset.AssetName || '-'} ${asset.Model || '-'} ${asset.SerialNumber || '-'} ${asset.Quantity || 0} ${this.buildAssetQuantityMetrics(asset).quantity + this.buildAssetQuantityMetrics(asset).importInPeriod} ${asset.ImportInPeriod ?? 0} ${asset.ExportInPeriod ?? 0} ${asset.EndingBalance ?? 0} ${asset.Unit || '-'} ${asset.Department || '-'} ${this.formatAssetProjectsHtml(asset)} ${asset.Custodian || '-'} ${statusMeta.label} ${asset.NewQuantity ?? 0} ${asset.UsedQuantity ?? 0} ${asset.ExportInPeriod ?? 0} ${this.formatAssetLocationHtml(asset.Location)} ${this.formatDateOnly(asset.PurchaseDate)} ${this.formatBorrowerTableHtml(asset.Borrower)} ${asset.Notes || '-'} ${this.formatDateOnly(asset.CreatedDate)} ${asset.ExportedBy || '-'}
`; }).join(''); const mobileList = document.querySelector('.assets-mobile-list'); if (mobileList) { mobileList.innerHTML = pageInfo.data .map((asset, index) => this.getAssetMobileCardHtml(asset, pageInfo.start + index, canManageAssets)) .join(''); } const pager = document.getElementById('assetsPager'); if (pager) { pager.innerHTML = ` Hiển thị ${pageInfo.start}-${pageInfo.end} / ${pageInfo.total}
Trang ${pageInfo.current} / ${pageInfo.totalPages}
`; } this.setupAssetRowListeners(); this.setupAssetPagerListeners(); } setupAssetSelectionListeners() { document.querySelectorAll('.asset-row-checkbox').forEach(checkbox => { checkbox.addEventListener('change', () => { const assetId = Number(checkbox.dataset.assetId); if (!Number.isFinite(assetId)) { return; } document.querySelectorAll(`.asset-row-checkbox[data-asset-id="${assetId}"]`).forEach(peer => { peer.checked = checkbox.checked; }); if (checkbox.checked) { this.selectedAssetIds.add(assetId); } else { this.selectedAssetIds.delete(assetId); } this.updateAssetBulkActionState(); }); }); const selectAllCheckbox = document.getElementById('selectAllAssetsCheckbox'); if (selectAllCheckbox && !selectAllCheckbox.dataset.boundChange) { selectAllCheckbox.addEventListener('change', () => { const shouldSelect = selectAllCheckbox.checked; document.querySelectorAll('.asset-row-checkbox').forEach(checkbox => { checkbox.checked = shouldSelect; const assetId = Number(checkbox.dataset.assetId); if (!Number.isFinite(assetId)) { return; } if (shouldSelect) { this.selectedAssetIds.add(assetId); } else { this.selectedAssetIds.delete(assetId); } }); this.updateAssetBulkActionState(); }); selectAllCheckbox.dataset.boundChange = 'true'; } const bulkDeleteBtn = document.getElementById('bulkDeleteAssetsBtn'); if (bulkDeleteBtn && !bulkDeleteBtn.dataset.boundClick) { bulkDeleteBtn.addEventListener('click', async () => { await this.handleBulkDeleteAssets(); }); bulkDeleteBtn.dataset.boundClick = 'true'; } const clearSelectionButton = document.getElementById('clearAssetSelectionBtn'); if (clearSelectionButton && !clearSelectionButton.dataset.boundClick) { clearSelectionButton.addEventListener('click', () => { this.selectedAssetIds.clear(); document.querySelectorAll('.asset-row-checkbox').forEach(checkbox => { checkbox.checked = false; }); this.updateAssetBulkActionState(); }); clearSelectionButton.dataset.boundClick = 'true'; } this.updateAssetBulkActionState(); } setupAssetActionMenus() { document.querySelectorAll('.assets-page .asset-action-menu').forEach(menu => { if (menu.dataset.boundMenu) return; menu.addEventListener('toggle', () => { if (menu.open) document.querySelectorAll('.assets-page .asset-action-menu[open]').forEach(other => { if (other !== menu) other.open = false; }); }); menu.addEventListener('click', event => { const button = event.target.closest('button'); if (button && !button.disabled) menu.open = false; }); menu.addEventListener('keydown', event => { if (event.key === 'Escape' && menu.open) { menu.open = false; menu.querySelector('summary').focus(); event.stopPropagation(); } }); menu.dataset.boundMenu = 'true'; }); if (!this.assetActionMenusBound) { document.addEventListener('click', event => { document.querySelectorAll('.assets-page .asset-action-menu[open]').forEach(menu => { if (!menu.contains(event.target)) menu.open = false; }); }); this.assetActionMenusBound = true; } } updateAssetBulkActionState() { const canManageAssets = this.canCurrentUserManageAssets(); const rowCheckboxes = Array.from(document.querySelectorAll('.asset-row-checkbox')); const assetSelectionById = new Map(); rowCheckboxes.forEach(checkbox => { const assetId = Number(checkbox.dataset.assetId); if (!Number.isFinite(assetId)) { return; } assetSelectionById.set(assetId, (assetSelectionById.get(assetId) || false) || checkbox.checked); }); const selectedOnPage = Array.from(assetSelectionById.values()).filter(Boolean).length; const selectAllCheckbox = document.getElementById('selectAllAssetsCheckbox'); if (selectAllCheckbox) { const assetCountOnPage = assetSelectionById.size; const hasRows = assetCountOnPage > 0; selectAllCheckbox.checked = hasRows && selectedOnPage === assetCountOnPage; selectAllCheckbox.indeterminate = selectedOnPage > 0 && selectedOnPage < assetCountOnPage; selectAllCheckbox.disabled = !hasRows; } const selectedCount = this.selectedAssetIds.size; const selectedCountNode = document.getElementById('selectedAssetCount'); if (selectedCountNode) { selectedCountNode.textContent = String(selectedCount); } const toolbar = document.getElementById('assetSelectionToolbar'); if (toolbar) toolbar.dataset.hasSelection = String(selectedCount > 0); const hint = document.getElementById('assetSelectionHint'); if (hint) hint.textContent = selectedCount ? 'Chọn thao tác áp dụng cho các tài sản này.' : 'Đánh dấu ô chọn trong danh sách để thao tác.'; const clearSelectionButton = document.getElementById('clearAssetSelectionBtn'); if (clearSelectionButton) clearSelectionButton.hidden = selectedCount === 0; for (const [id, disabled, title] of [ ['bulkDeleteAssetsBtn', !canManageAssets || selectedCount === 0, 'Chọn ít nhất 1 tài sản để xóa.'], ['borrowAssetBtn', selectedCount === 0, 'Chọn ít nhất 1 tài sản để tạo đơn xuất dự án.'], ['exportAssetForOthersBtn', !canManageAssets || selectedCount === 0, 'Chọn ít nhất 1 tài sản để xuất cho người khác.'], ['damageAssetBtn', !canManageAssets || selectedCount !== 1, 'Chọn đúng 1 tài sản để ghi nhận hỏng hoặc thanh lý.'] ]) { const button = document.getElementById(id); if (button) { button.disabled = disabled; button.title = disabled ? title : ''; } } } async handleBulkDeleteAssets() { if (!this.ensureAssetManagePermission('xoa tai san')) { return; } const selectedIds = [...this.selectedAssetIds]; if (!selectedIds.length) { this.notifyWarning('Vui lòng chọn ít nhất 1 tài sản để xóa'); return; } const confirmed = await this.confirmBulkAssetDelete(selectedIds.length); if (!confirmed) { return; } let successCount = 0; let failedCount = 0; for (const assetId of selectedIds) { try { const response = await fetch(`${this.apiBase}/assets/${assetId}`, { method: 'DELETE', headers: this.getAuthHeaders(false) }); const data = await response.json(); if (!response.ok || !data.success) { failedCount += 1; continue; } this.selectedAssetIds.delete(assetId); successCount += 1; } catch (err) { console.error(err); failedCount += 1; } } if (successCount > 0 && failedCount === 0) { this.notifySuccess(`Đã xóa ${successCount} tài sản`); } else if (successCount > 0) { this.notifyWarning(`Đã xóa ${successCount}/${selectedIds.length} tài sản. ${failedCount} dòng xóa thất bại`); } else { this.notifyFailure('Xóa tài sản thất bại'); } await this.refreshAssetsUI(); await this.refreshAssetBorrowsUI(); } setupArchivedAssetListeners() { const archiveButton = document.getElementById('archiveAllAssetsBtn'); if (archiveButton && !archiveButton.dataset.boundClick) { archiveButton.addEventListener('click', () => this.handleArchiveAllAssets()); archiveButton.dataset.boundClick = 'true'; } const outstandingButton = document.getElementById('openArchivedAssetBorrowsBtn'); if (outstandingButton && !outstandingButton.dataset.boundClick) { outstandingButton.addEventListener('click', () => this.openArchivedAssetBorrowsModal()); outstandingButton.dataset.boundClick = 'true'; } } async handleArchiveAllAssets() { if (this.archivingAssets || !this.ensureAssetManagePermission('xóa danh mục tài sản')) return; this.archivingAssets = true; const button = document.getElementById('archiveAllAssetsBtn'); if (button) button.disabled = true; try { const confirmed = await this.confirmBulkAssetDelete(this.assets.length, true); if (!confirmed) return; const response = await fetch(`${this.apiBase}/assets/archive-all`, { method: 'POST', headers: this.getAuthHeaders(false) }); const data = await response.json(); if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Xóa danh mục tài sản thất bại'); return; } this.selectedAssetIds.clear(); this.assetPage = 1; this.assetBorrowPage = 1; this.notifySuccess(`Đã chuyển ${Number(data.data?.archivedCount) || 0} tài sản sang dữ liệu cũ. Lịch sử mượn/trả được giữ lại.`); await this.refreshAssetsUI(); await this.refreshAssetBorrowsUI(); } catch (err) { console.error(err); this.notifyFailure('Xóa danh mục tài sản thất bại'); } finally { this.archivingAssets = false; const activeButton = document.getElementById('archiveAllAssetsBtn'); if (activeButton) activeButton.disabled = !this.canCurrentUserManageAssets() || !this.assets.length; } } getFilteredArchivedAssets() { const search = String(this.archivedAssetSearchTerm || '').trim().toLowerCase(); return (this.archivedAssets || []).map(asset => { const borrowerQuantity = this.parseBorrowerEntries(asset.Borrower) .reduce((sum, entry) => sum + this.parseNonNegativeInteger(entry.quantity, 0), 0); let allocations = asset.ProjectAllocations || []; if (typeof allocations === 'string') { try { allocations = JSON.parse(allocations); } catch { allocations = []; } } const permanentQuantity = (Array.isArray(allocations) ? allocations : []) .filter(row => row.allocationType === 'export') .reduce((sum, row) => sum + this.parseNonNegativeInteger(row.quantity, 0), 0); return { ...asset, OutstandingQuantity: Math.max(borrowerQuantity, this.parseNonNegativeInteger(asset.ExportInPeriod, 0) - permanentQuantity) }; }).filter(asset => !search || [ asset.AssetId, asset.AssetCode, asset.AssetName, asset.Model, asset.SerialNumber, this.formatBorrowerSummaryText(asset.Borrower), asset.Department, asset.Project, asset.Location, asset.Custodian, asset.Notes, this.formatDateOnly(asset.ArchivedDate) ].some(value => String(value || '').toLowerCase().includes(search))); } getArchivedOutstandingAssets() { return this.getFilteredArchivedAssets().filter(asset => asset.OutstandingQuantity > 0); } renderArchivedAssetBorrowsTable() { const tableBody = document.getElementById('archivedAssetBorrowsTableBody'); if (!tableBody) return; const rows = this.getFilteredArchivedAssets(); tableBody.innerHTML = rows.length ? rows.map(asset => ` ${this.escapeHtml(asset.AssetCode || '-')}
Hồ sơ cũ #${Number(asset.AssetId) || 0}
${this.escapeHtml(asset.AssetName || '-')}
${this.escapeHtml([asset.Model, asset.SerialNumber].filter(Boolean).join(' · '))}
${this.escapeHtml(this.formatDateTime(asset.ArchivedDate))} ${this.parseNonNegativeInteger(asset.Quantity, 0)} ${this.parseNonNegativeInteger(asset.ImportInPeriod, 0)} ${this.parseNonNegativeInteger(asset.ExportInPeriod, 0)} ${this.parseNonNegativeInteger(asset.EndingBalance, 0)} ${asset.OutstandingQuantity} ${this.escapeHtml(asset.Unit || '-')} ${this.escapeHtml(this.formatBorrowerSummaryText(asset.Borrower) || '-')} ${this.escapeHtml([asset.Department, asset.Project].filter(Boolean).join(' / ') || '-')} `).join('') : 'Không có tài sản cũ phù hợp.'; const count = document.getElementById('archivedAssetBorrowsCount'); if (count) count.textContent = `${rows.length}/${(this.archivedAssets || []).length} hồ sơ tài sản cũ`; } async openArchivedAssetBorrowsModal() { const modal = document.getElementById('archivedAssetBorrowsModal'); if (!modal) { this.notifyFailure('Không tìm thấy danh sách tài sản cũ'); return; } if (!await this.fetchArchivedAssets()) { this.notifyFailure('Không tải được dữ liệu tài sản cũ. Vui lòng thử lại.'); return; } this.archivedAssetSearchTerm = ''; const search = document.getElementById('archivedAssetBorrowsSearch'); if (search) { search.value = ''; if (!search.dataset.boundInput) { search.addEventListener('input', event => { this.archivedAssetSearchTerm = event.target.value; this.renderArchivedAssetBorrowsTable(); }); search.dataset.boundInput = 'true'; } } this.renderArchivedAssetBorrowsTable(); modal.classList.add('open'); } setupAssetPagerListeners() { document.querySelectorAll('.asset-page-btn').forEach(btn => { btn.addEventListener('click', () => { const targetPage = Number(btn.dataset.page); if (!targetPage || targetPage < 1) return; this.assetPage = targetPage; this.renderAssetsTableBody(); }); }); } renderAssetDetails(asset) { const detailsContainer = document.getElementById('assetDetailsContent'); if (!detailsContainer) { return; } const borrowerSummary = this.formatBorrowerSummaryText(asset?.Borrower); const fields = [ ['Mã tài sản', asset?.AssetCode], ['Tên tài sản', asset?.AssetName], ['Model', asset?.Model], ['Sd serial', asset?.SerialNumber], ['Số lượng (Tồn đầu kỳ)', `${asset?.Quantity || 0} ${asset?.Unit || ''}`.trim()], ['Tổng tài sản', this.buildAssetQuantityMetrics(asset).quantity + this.buildAssetQuantityMetrics(asset).importInPeriod], ['Nhập trong kỳ', asset?.ImportInPeriod ?? 0], ['Xuất trong kỳ', asset?.ExportInPeriod ?? 0], ['Tồn cuối kỳ', asset?.EndingBalance ?? 0], ['SL hàng mới', asset?.NewQuantity ?? 0], ['SL đã qua sử dụng', asset?.UsedQuantity ?? 0], ['SL đang mượn', asset?.ExportInPeriod ?? 0], ['Phòng ban', asset?.Department], ['Dự án đang xuất / Số lượng', this.formatAssetProjectsHtml(asset)], ['Vị trí', asset?.Location], ['Người phụ trách', asset?.Custodian], ['Ngày mua', this.formatDateOnly(asset?.PurchaseDate)], ['Người xuất', asset?.ExportedBy], ['Trạng thái', this.getAssetStatusMeta(asset?.Status).label], ['Ghi chú', asset?.Notes] ]; detailsContainer.innerHTML = `
${borrowerSummary || '-'}
${fields.map(([label, value]) => `
${value || '-'}
`).join('')} `; } populateAssetForm(asset) { const sourceAsset = asset || {}; const borrowerEntries = this.parseBorrowerEntries(sourceAsset?.Borrower); const metrics = this.buildAssetQuantityMetrics(sourceAsset, borrowerEntries); const stockSplit = this.normalizeAssetStockSplit( metrics.endingBalance, sourceAsset?.NewQuantity ?? metrics.endingBalance, sourceAsset?.UsedQuantity ?? 0 ); const statusCode = sourceAsset?.AssetId ? this.computeAssetStatusCode(metrics.endingBalance, metrics.exportInPeriod) : 'in_stock'; const statusMeta = this.getAssetStatusMeta(statusCode); this.editingAssetBorrowerEntries = borrowerEntries; this.editingAssetStockSnapshot = { endingBalance: metrics.endingBalance, newQuantity: stockSplit.newQuantity, usedQuantity: stockSplit.usedQuantity }; this.clearAssetFormValidation(); document.getElementById('assetCodeInput').value = sourceAsset?.AssetCode || ''; document.getElementById('assetNameInput').value = sourceAsset?.AssetName || ''; const statusInput = document.getElementById('assetStatusInput'); if (statusInput) { statusInput.value = statusMeta.label; statusInput.dataset.statusCode = statusCode; } document.getElementById('assetModelInput').value = sourceAsset?.Model || ''; document.getElementById('assetSerialInput').value = sourceAsset?.SerialNumber || ''; document.getElementById('assetQuantityInput').value = metrics.quantity; document.getElementById('assetImportInPeriodInput').value = metrics.importInPeriod; document.getElementById('assetUnitInput').value = sourceAsset?.Unit || ''; this.refreshAssetDepartmentOptions(sourceAsset?.Department || ''); this.refreshAssetProjectOptions(sourceAsset?.Project || ''); document.getElementById('assetLocationInput').value = sourceAsset?.Location || ''; this.refreshAssetCustodianOptions(sourceAsset?.Custodian || ''); const borrowerSummaryInput = document.getElementById('assetBorrowerSummaryInput'); if (borrowerSummaryInput) { borrowerSummaryInput.value = this.formatBorrowerSummaryText(sourceAsset?.Borrower) || '(Chua co nguoi muon)'; borrowerSummaryInput.readOnly = true; } const exportInput = document.getElementById('assetExportInPeriodInput'); const endingInput = document.getElementById('assetEndingBalanceInput'); if (exportInput) exportInput.readOnly = true; if (endingInput) endingInput.readOnly = true; document.getElementById('assetPurchaseDateInput').value = this.toDateInputValue(sourceAsset?.PurchaseDate); document.getElementById('assetPriceInput').value = sourceAsset?.PurchasePrice || ''; document.getElementById('assetNotesInput').value = sourceAsset?.Notes || ''; this.recalculateAssetStockFields(); } openAssetModal() { if (!this.ensureAssetManagePermission('them hoac sua tai san')) { return; } if (this.editingAssetId === undefined) { this.editingAssetStockSnapshot = null; this.populateAssetForm(null); } this.refreshAssetDepartmentOptions(document.getElementById('assetDepartmentInput')?.value || ''); this.refreshAssetProjectOptions(document.getElementById('assetProjectInput')?.value || ''); if (!this.users.length) { this.fetchUsers(); } this.setAssetCodeFieldMode(this.editingAssetId !== undefined); this.clearAssetFormValidation(); document.getElementById('assetModal').classList.add('open'); } setupAssetFormValidationListeners() { const bindInput = (inputId, errorId) => { const input = document.getElementById(inputId); if (!input || input.dataset.boundValidation === 'true') { return; } input.addEventListener('input', () => this.clearAssetFieldValidation(inputId, errorId)); input.addEventListener('change', () => this.clearAssetFieldValidation(inputId, errorId)); input.dataset.boundValidation = 'true'; }; bindInput('assetCodeInput', 'assetCodeError'); bindInput('assetNameInput', 'assetNameError'); bindInput('assetModelInput', 'assetModelError'); } clearAssetFieldValidation(inputId, errorId) { const input = document.getElementById(inputId); const errorNode = document.getElementById(errorId); if (input) { input.classList.remove('border-error/30', 'ring-2', 'ring-error/20'); input.removeAttribute('aria-invalid'); } if (errorNode) { errorNode.textContent = ''; errorNode.classList.add('hidden'); } } clearAssetFormValidation() { this.clearAssetFieldValidation('assetCodeInput', 'assetCodeError'); this.clearAssetFieldValidation('assetNameInput', 'assetNameError'); this.clearAssetFieldValidation('assetModelInput', 'assetModelError'); } setAssetFieldValidationError(inputId, errorId, message) { const input = document.getElementById(inputId); const errorNode = document.getElementById(errorId); if (input) { input.classList.add('border-error/30', 'ring-2', 'ring-error/20'); input.setAttribute('aria-invalid', 'true'); } if (errorNode) { errorNode.textContent = message || ''; errorNode.classList.remove('hidden'); } } setAssetCodeFieldMode(isEdit) { const codeInput = document.getElementById('assetCodeInput'); const codeLabel = document.getElementById('assetCodeLabel'); const codeHint = document.getElementById('assetCodeHint'); if (codeInput) { codeInput.required = !!isEdit; codeInput.placeholder = isEdit ? 'Bắt buộc khi cập nhật' : 'Để trống để hệ thống tự tạo'; } if (codeLabel) { codeLabel.innerHTML = isEdit ? 'Mã tài sản *' : 'Mã tài sản'; } if (codeHint) { codeHint.textContent = isEdit ? 'Khi cập nhật, mã tài sản là bắt buộc.' : 'Để trống khi thêm mới, hệ thống sẽ tự tạo mã.'; } } generateManualAssetCodeForCreate(payload = {}) { const toToken = (value) => String(value || '') .normalize('NFD') .replace(/[\u0300-\u036f]/g, '') .replace(/[\u0111\u0110]/g, 'd') .toUpperCase() .replace(/[^A-Z0-9]+/g, '-') .replace(/^-+|-+$/g, '') .slice(0, 32); const base = toToken(payload.model) || toToken(payload.serialNumber) || toToken(payload.assetName) || 'ASSET'; const timestamp = this.formatTimestampForCode(new Date(), true); const randomSuffix = String(Math.floor(Math.random() * 100)).padStart(2, '0'); return `AST-${base}-${timestamp}${randomSuffix}`; } collectAssetFormPayload() { const quantity = this.parseNonNegativeInteger(document.getElementById('assetQuantityInput')?.value ?? 0, 0); const importInPeriod = this.parseNonNegativeInteger(document.getElementById('assetImportInPeriodInput')?.value ?? 0, 0); const exportInPeriod = this.parseNonNegativeInteger(document.getElementById('assetExportInPeriodInput')?.value ?? 0, 0); const endingBalanceInput = this.parseOptionalNonNegativeInteger(document.getElementById('assetEndingBalanceInput')?.value ?? ''); const borrowerEntries = Array.isArray(this.editingAssetBorrowerEntries) ? this.editingAssetBorrowerEntries : []; const borrower = this.formatBorrowerEntries(borrowerEntries, '; ') || null; const computedEndingBalance = Math.max(quantity + importInPeriod - exportInPeriod, 0); const endingBalance = endingBalanceInput !== null ? endingBalanceInput : computedEndingBalance; const status = this.computeAssetStatusCode(endingBalance, exportInPeriod); const previousSnapshot = this.editingAssetStockSnapshot; let nextNewQuantity = endingBalance; let nextUsedQuantity = 0; if (previousSnapshot) { const previousEnding = this.parseNonNegativeInteger(previousSnapshot.endingBalance, 0); const previousNew = this.parseNonNegativeInteger(previousSnapshot.newQuantity, previousEnding); const previousUsed = this.parseNonNegativeInteger(previousSnapshot.usedQuantity, 0); const deltaEnding = endingBalance - previousEnding; const tentativeNew = Math.max(previousNew + deltaEnding, 0); const normalized = this.normalizeAssetStockSplit(endingBalance, tentativeNew, previousUsed); nextNewQuantity = normalized.newQuantity; nextUsedQuantity = normalized.usedQuantity; } const purchasePrice = String(document.getElementById('assetPriceInput')?.value ?? '').trim(); return { assetCode: document.getElementById('assetCodeInput')?.value?.trim() || '', assetName: document.getElementById('assetNameInput')?.value?.trim() || '', status, model: document.getElementById('assetModelInput')?.value?.trim() || '', serialNumber: document.getElementById('assetSerialInput')?.value?.trim() || '', quantity, importInPeriod, exportInPeriod, endingBalance, newQuantity: nextNewQuantity, usedQuantity: nextUsedQuantity, unit: document.getElementById('assetUnitInput')?.value?.trim() || '', department: document.getElementById('assetDepartmentInput')?.value?.trim() || '', project: document.getElementById('assetProjectInput')?.value?.trim() || '', location: document.getElementById('assetLocationInput')?.value?.trim() || '', custodian: document.getElementById('assetCustodianInput')?.value?.trim() || '', borrower, purchaseDate: document.getElementById('assetPurchaseDateInput')?.value || null, purchasePrice: purchasePrice || null, notes: document.getElementById('assetNotesInput')?.value?.trim() || '' }; } buildAssetPayloadFromAsset(asset, borrowerEntriesOverride = null, fieldOverrides = {}) { if (!asset) { return null; } const quantity = this.parseNonNegativeInteger(asset?.Quantity, 0); const importInPeriod = this.parseNonNegativeInteger(asset?.ImportInPeriod, 0); const baseExportInPeriod = this.parseNonNegativeInteger(asset?.ExportInPeriod, 0); const baseEndingBalance = this.parseOptionalNonNegativeInteger(asset?.EndingBalance); const resolvedBaseEndingBalance = baseEndingBalance !== null ? baseEndingBalance : Math.max(quantity + importInPeriod - baseExportInPeriod, 0); const baseNewQuantity = this.parseOptionalNonNegativeInteger(asset?.NewQuantity); const baseUsedQuantity = this.parseOptionalNonNegativeInteger(asset?.UsedQuantity); const resolvedStockSplit = this.normalizeAssetStockSplit( resolvedBaseEndingBalance, baseNewQuantity !== null ? baseNewQuantity : resolvedBaseEndingBalance, baseUsedQuantity !== null ? baseUsedQuantity : 0 ); const borrowerEntries = Array.isArray(borrowerEntriesOverride) ? borrowerEntriesOverride : this.parseBorrowerEntries(asset?.Borrower); const borrower = this.formatBorrowerEntries(borrowerEntries, '; ') || null; let exportInPeriod = baseExportInPeriod; let endingBalance = resolvedBaseEndingBalance; let newQuantity = resolvedStockSplit.newQuantity; let usedQuantity = resolvedStockSplit.usedQuantity; if (Array.isArray(borrowerEntriesOverride)) { const existingBorrowerEntries = this.parseBorrowerEntries(asset?.Borrower); const previousBorrowerExport = existingBorrowerEntries.reduce((sum, entry) => ( sum + this.parseNonNegativeInteger(entry?.quantity, 0) ), 0); const nextBorrowerExport = this.parseBorrowerEntries(borrowerEntriesOverride).reduce((sum, entry) => ( sum + this.parseNonNegativeInteger(entry?.quantity, 0) ), 0); const exportDelta = nextBorrowerExport - previousBorrowerExport; exportInPeriod = Math.max(baseExportInPeriod + exportDelta, 0); endingBalance = Math.max(resolvedBaseEndingBalance - exportDelta, 0); const borrowFromNew = Math.min(newQuantity, Math.max(exportDelta, 0)); const borrowFromUsed = Math.max(Math.max(exportDelta, 0) - borrowFromNew, 0); newQuantity = Math.max(newQuantity - borrowFromNew, 0); usedQuantity = Math.max(usedQuantity - borrowFromUsed, 0); const normalizedSplit = this.normalizeAssetStockSplit(endingBalance, newQuantity, usedQuantity); newQuantity = normalizedSplit.newQuantity; usedQuantity = normalizedSplit.usedQuantity; } const rawPrice = asset?.PurchasePrice; const normalizedPrice = rawPrice === undefined || rawPrice === null || String(rawPrice).trim() === '' ? null : String(rawPrice).trim(); return { assetCode: String(asset?.AssetCode || '').trim(), assetName: String(asset?.AssetName || '').trim(), status: this.computeAssetStatusCode(endingBalance, exportInPeriod), model: String(asset?.Model || '').trim(), serialNumber: String(asset?.SerialNumber || '').trim(), quantity, importInPeriod, exportInPeriod, endingBalance, newQuantity, usedQuantity, unit: String(asset?.Unit || '').trim(), department: String(asset?.Department || '').trim(), project: String(fieldOverrides?.project ?? asset?.Project ?? '').trim(), location: String(asset?.Location || '').trim(), custodian: String(fieldOverrides?.custodian ?? asset?.Custodian ?? '').trim(), borrower, purchaseDate: this.toDateInputValue(asset?.PurchaseDate) || null, purchasePrice: normalizedPrice, notes: String(asset?.Notes || '').trim() }; } getSingleSelectedAssetForBorrowing(showWarning = true) { const selectedIds = [...this.selectedAssetIds]; if (!selectedIds.length) { if (showWarning) { this.notifyWarning('Vui lòng chọn 1 tài sản để xuất.'); } return null; } if (selectedIds.length > 1) { if (showWarning) { this.notifyWarning('Chỉ chọn đúng 1 tài sản cho mỗi lần xuất.'); } return null; } const assetId = Number(selectedIds[0]); const asset = this.assets.find(item => Number(item?.AssetId) === assetId) || null; if (!asset && showWarning) { this.notifyFailure('Không tìm thấy tài sản dã chọn.'); } return asset; } getSingleSelectedAssetForDamage(showWarning = true) { const selectedIds = [...this.selectedAssetIds]; if (!selectedIds.length) { if (showWarning) { this.notifyWarning('Vui lòng chọn 1 tài sản để ghi nhận hỏng/thanh lý.'); } return null; } if (selectedIds.length > 1) { if (showWarning) { this.notifyWarning('Chỉ chọn đúng 1 tài sản cho mỗi lần ghi nhận hỏng/thanh lý.'); } return null; } const assetId = Number(selectedIds[0]); const asset = this.assets.find(item => Number(item?.AssetId) === assetId) || null; if (!asset && showWarning) { this.notifyFailure('Không tìm thấy tài sản đã chọn.'); } return asset; } openAssetDamageModal() { if (!this.ensureAssetManagePermission('ghi nhan tai san hong/thanh ly')) { return; } const asset = this.getSingleSelectedAssetForDamage(true); if (!asset) { return; } const metrics = this.buildAssetQuantityMetrics(asset); if (metrics.endingBalance <= 0) { this.notifyWarning('Tài sản đã hết tồn cuối kỳ, không thể ghi nhận hỏng/thanh lý thêm.'); return; } this.pendingAssetDamageId = Number(asset.AssetId); const modal = document.getElementById('assetDamageModal'); const assetIdInput = document.getElementById('assetDamageAssetIdInput'); const assetNameInput = document.getElementById('assetDamageAssetNameInput'); const typeInput = document.getElementById('assetDamageTypeInput'); const quantityInput = document.getElementById('assetDamageQuantityInput'); const currentQuantityInput = document.getElementById('assetDamageCurrentQuantityInput'); const currentEndingInput = document.getElementById('assetDamageCurrentEndingInput'); const currentNewInput = document.getElementById('assetDamageCurrentNewInput'); const currentUsedInput = document.getElementById('assetDamageCurrentUsedInput'); const actorInput = document.getElementById('assetDamageActorInput'); const noteInput = document.getElementById('assetDamageNoteInput'); if (!modal || !assetNameInput || !quantityInput || !currentQuantityInput || !currentEndingInput) { this.notifyFailure('Không tìm thấy biểu mẫu hỏng/thanh lý tài sản.'); return; } const stockSplit = this.normalizeAssetStockSplit( metrics.endingBalance, asset?.NewQuantity ?? metrics.endingBalance, asset?.UsedQuantity ?? 0 ); if (assetIdInput) { assetIdInput.value = String(asset.AssetId || ''); } assetNameInput.value = `${asset.AssetCode || ''} - ${asset.AssetName || ''}`.trim(); currentQuantityInput.value = String(metrics.quantity); currentEndingInput.value = String(metrics.endingBalance); if (currentNewInput) currentNewInput.value = String(stockSplit.newQuantity); if (currentUsedInput) currentUsedInput.value = String(stockSplit.usedQuantity); if (typeInput) typeInput.value = 'damaged'; quantityInput.value = '1'; quantityInput.min = '1'; quantityInput.max = String(metrics.endingBalance); if (actorInput) actorInput.value = this.getCurrentUserDisplayName(); if (noteInput) noteInput.value = ''; modal.classList.add('open'); } async handleAssetDamageSubmit(e) { e.preventDefault(); if (!this.ensureAssetManagePermission('ghi nhan tai san hong/thanh ly')) { return; } const assetIdInput = document.getElementById('assetDamageAssetIdInput'); const typeInput = document.getElementById('assetDamageTypeInput'); const quantityInput = document.getElementById('assetDamageQuantityInput'); const noteInput = document.getElementById('assetDamageNoteInput'); const selectedAssetId = Number(assetIdInput?.value || this.pendingAssetDamageId); if (!Number.isFinite(selectedAssetId) || selectedAssetId <= 0) { this.notifyFailure('Không xác định được tài sản cần ghi nhận.'); return; } const asset = this.assets.find(item => Number(item?.AssetId) === selectedAssetId); if (!asset) { this.notifyFailure('Không tìm thấy tài sản cần ghi nhận.'); return; } const actionType = this.normalizeAssetDamageType(typeInput?.value || 'damaged'); const actionMeta = this.getAssetDamageTypeMeta(actionType); const actionQuantity = this.parseNonNegativeInteger(quantityInput?.value ?? 0, 0); const currentMetrics = this.buildAssetQuantityMetrics(asset); if (actionQuantity <= 0) { this.notifyWarning('Số lượng phải lớn hơn 0.'); return; } if (actionQuantity > currentMetrics.endingBalance) { this.notifyWarning(`Số lượng ${actionMeta.label.toLowerCase()} (${actionQuantity}) vượt quá tồn cuối kỳ (${currentMetrics.endingBalance}).`); return; } try { const response = await fetch(`${this.apiBase}/assets/${selectedAssetId}/damage-disposal`, { method: 'POST', headers: this.getAuthHeaders(true), body: JSON.stringify({ actionType, quantity: actionQuantity, note: String(noteInput?.value || '').trim() }) }); const data = await response.json(); if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Ghi nhận hỏng/thanh lý thất bại'); return; } this.pendingAssetDamageId = undefined; document.getElementById('assetDamageModal')?.classList.remove('open'); this.notifySuccess(data.message || `Đã ghi nhận tài sản ${actionMeta.label.toLowerCase()}`); await this.refreshAssetsUI(); const historyModal = document.getElementById('assetDamageHistoryModal'); if (historyModal?.classList.contains('open')) { await this.fetchAssetDamageHistories(); this.renderAssetDamageHistoryModal(); } } catch (err) { console.error(err); this.notifyFailure('Ghi nhận hỏng/thanh lý thất bại'); } } buildBatchAssetExportRowsHtml(assets) { const projectNames = this.getUniqueAssetProjectNames(); return assets.map(asset => { const available = this.getAssetAvailableForExport(asset); const names = [...new Set([...projectNames, String(asset.Project || '').trim()].filter(Boolean))]; return `
${this.escapeHtml(asset.AssetName || '-')}
${this.escapeHtml(asset.AssetCode || '-')} · ${this.escapeHtml(asset.Model || '-')}
${available} ${this.escapeHtml(asset.Unit || '')} `; }).join(''); } collectBatchAssetExportItems() { return [...document.querySelectorAll('#batchAssetExportTableBody .batch-asset-export-row')].map(row => ({ assetId: Number(row.dataset.assetId), allocations: [{ projectName: String(row.querySelector('.batch-export-project').value || '').trim(), quantity: Number(row.querySelector('.batch-export-quantity').value) }] })); } updateBatchAssetExportSummary() { const items = this.collectBatchAssetExportItems(); const summary = document.getElementById('batchAssetExportSummary'); if (summary) summary.textContent = `${items.length} tài sản đã chọn · 1 phiếu Excel chung`; } configureAssetExportForm(prefix, forOthers) { const form = document.getElementById(`${prefix}Form`); form.dataset.exportForOthers = String(forOthers); document.getElementById(`${prefix}RequesterField`).hidden = forOthers; document.getElementById(`${prefix}RecipientField`).hidden = !forOthers; const recipient = document.getElementById(`${prefix}RecipientInput`); recipient.disabled = !forOthers; recipient.required = forOthers; if (forOthers) this.populateUserSelectOptions(recipient.id, { emptyLabel: '-- Chọn người nhận --' }); else recipient.value = ''; const batch = prefix === 'batchAssetExport'; document.getElementById(`${prefix}Title`).textContent = forOthers ? (batch ? 'Xuất nhiều tài sản cho người khác' : 'Xuất tài sản cho người khác') : (batch ? 'Tạo đơn xuất nhiều tài sản vào dự án' : 'Tạo đơn xuất tài sản vào dự án'); document.getElementById(`${prefix}Description`).textContent = forOthers ? 'Chọn người nhận và dự án. Xác nhận sẽ trừ kho ngay và tạo phiếu Excel; tài sản xuất không hoàn lại kho.' : (batch ? 'Chọn số lượng và dự án cho từng tài sản. Chỉ trừ kho khi được duyệt; tài sản xuất không hoàn lại kho.' : 'Xuất vào dự án, không hoàn lại kho. Số lượng chỉ trừ khi admin/asset duyệt đơn.'); form.querySelector('button[type="submit"]').textContent = forOthers ? 'Xuất và tải Excel' : 'Gửi đơn chờ duyệt'; } async openBatchAssetExportModal(forOthers = false) { if (forOthers && !this.ensureAssetManagePermission('xuất tài sản cho người khác')) return; const assets = this.assets.filter(asset => this.selectedAssetIds.has(Number(asset.AssetId))); if (!assets.length || assets.length > 100) { this.notifyWarning('Vui lòng chọn từ 1 đến 100 tài sản để xuất.'); return; } const unavailable = assets.find(asset => this.getAssetAvailableForExport(asset) <= 0); if (unavailable) { this.notifyWarning(`${unavailable.AssetCode || unavailable.AssetName}: đã hết tồn, hãy bỏ chọn tài sản này.`); return; } if (forOthers && !this.users.length) await this.fetchUsers(); if (!this.assetProjects.length) await this.fetchAssetProjects(); const modal = document.getElementById('batchAssetExportModal'); const form = document.getElementById('batchAssetExportForm'); if (!modal || !form) { this.notifyFailure('Không tìm thấy biểu mẫu xuất nhiều tài sản.'); return; } this.configureAssetExportForm('batchAssetExport', forOthers); document.getElementById('batchAssetExportUserInput').value = this.getCurrentUserDisplayName(); const commonProject = document.getElementById('batchAssetExportCommonProject'); commonProject.innerHTML = '' + this.getUniqueAssetProjectNames().map(name => ``).join(''); commonProject.onchange = () => { if (!commonProject.value) return; form.querySelectorAll('.batch-export-project').forEach(select => { select.value = commonProject.value; }); }; document.getElementById('batchAssetExportNoteInput').value = ''; document.getElementById('batchAssetExportTableBody').innerHTML = this.buildBatchAssetExportRowsHtml(assets); form.onsubmit = event => this.handleBatchAssetExportSubmit(event); form.oninput = () => this.updateBatchAssetExportSummary(); form.onclick = event => { const remove = event.target.closest('.remove-batch-export-asset'); if (remove) { remove.closest('tr').remove(); this.updateBatchAssetExportSummary(); } }; this.updateBatchAssetExportSummary(); modal.classList.add('open'); } async handleBatchAssetExportSubmit(event) { event.preventDefault(); if (this.assetExportSubmitting) return; const forOthers = document.getElementById('batchAssetExportForm')?.dataset.exportForOthers === 'true'; if (forOthers && !this.ensureAssetManagePermission('xuất tài sản cho người khác')) return; const items = this.collectBatchAssetExportItems(); const borrowerName = String(document.getElementById(forOthers ? 'batchAssetExportRecipientInput' : 'batchAssetExportUserInput')?.value || '').trim(); const note = String(document.getElementById('batchAssetExportNoteInput')?.value || '').trim(); if (!borrowerName) { this.notifyWarning(forOthers ? 'Vui lòng chọn người nhận.' : 'Không xác định được người đề xuất.'); return; } if (!items.length || items.length > 100) { this.notifyWarning('Vui lòng chọn từ 1 đến 100 tài sản.'); return; } for (const item of items) { const asset = this.assets.find(asset => Number(asset.AssetId) === item.assetId); const row = item.allocations[0]; if (!asset || !row.projectName) { this.notifyWarning('Vui lòng chọn dự án cho tất cả tài sản.'); return; } if (!Number.isSafeInteger(row.quantity) || row.quantity <= 0) { this.notifyWarning('Số lượng xuất phải là số nguyên dương.'); return; } const available = this.getAssetAvailableForExport(asset); if (row.quantity > available) { this.notifyWarning(`${asset.AssetCode || asset.AssetName}: Số lượng xuất vượt quá tồn cuối kỳ (${available}).`); return; } } const button = document.querySelector('#batchAssetExportForm button[type="submit"]'); this.assetExportSubmitting = true; if (button) button.disabled = true; let exportSucceeded = false; try { const response = await fetch(`${this.apiBase}/${forOthers ? 'assets/export' : 'asset-export-requests'}`, { method: 'POST', headers: this.getAuthHeaders(true), body: JSON.stringify({ borrowerName, note, items }) }); const data = await response.json(); if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Xuất tài sản thất bại.'); return; } exportSucceeded = true; this.selectedAssetIds.clear(); if (forOthers) { this.notifySuccess(`Đã xuất ${items.length} tài sản cho ${borrowerName} và lưu phiếu Excel vào lịch sử.`); this.closeModals(); this.showExportRequestDocument(data.document); await this.refreshAssetsUI(); return; } this.notifySuccess('Đã gửi đơn xuất vào dự án, chờ admin/asset duyệt.'); this.closeModals(); this.assetExportTab = 'requests'; this.assetExportPage = 1; this.assetExportSearchTerm = ''; this.assetExportStatusFilter = ''; location.hash = '#asset-exports'; } catch (err) { console.error(err); if (exportSucceeded) this.notifyWarning(forOthers ? 'Đã xuất tài sản thành công. Hãy mở lịch sử xuất để kiểm tra hoặc tải lại phiếu Excel.' : 'Đã gửi đơn xuất. Hãy mở danh sách đơn xuất để kiểm tra.'); else this.notifyFailure('Xuất tài sản thất bại.'); } finally { this.assetExportSubmitting = false; if (button) button.disabled = false; } } collectBorrowProjectAllocations() { const rows = [{ projectName: String(document.getElementById('borrowAssetProjectInput')?.value || '').trim(), quantity: Number(document.getElementById('borrowQuantityInput')?.value) }]; document.querySelectorAll('#borrowAdditionalProjects .borrow-project-row').forEach(row => { rows.push({ projectName: row.querySelector('select').value.trim(), quantity: Number(row.querySelector('input').value) }); }); return rows; } updateBorrowAllocationSummary() { const total = this.collectBorrowProjectAllocations().reduce((sum, row) => sum + (Number.isFinite(row.quantity) ? row.quantity : 0), 0); const available = Number(document.getElementById('borrowCurrentEndingInput')?.value) || 0; const summary = document.getElementById('borrowAllocationSummary'); if (summary) { summary.textContent = `Tổng xuất: ${total} / ${available} có thể xuất · Còn lại: ${Math.max(available - total, 0)}${total > available ? ' — Vượt số lượng cho phép' : ''}`; summary.classList.toggle('text-red-600', total > available); } } addBorrowProjectRow() { const container = document.getElementById('borrowAdditionalProjects'); if (!container || container.children.length >= 99) return; const row = document.createElement('div'); row.className = 'borrow-project-row flex items-end gap-2'; row.innerHTML = ` `; row.querySelector('input').max = document.getElementById('borrowCurrentEndingInput').value; row.querySelector('button').addEventListener('click', () => { row.remove(); this.updateBorrowAllocationSummary(); }); container.appendChild(row); this.updateBorrowAllocationSummary(); } async openBorrowAssetModal(forOthers = false) { if (forOthers && !this.ensureAssetManagePermission('xuất tài sản cho người khác')) return; if (this.selectedAssetIds.size > 1) return this.openBatchAssetExportModal(forOthers); const asset = this.getSingleSelectedAssetForBorrowing(true); if (!asset) { return; } if (forOthers && !this.users.length) await this.fetchUsers(); if (!this.assetProjects.length) { await this.fetchAssetProjects(); } const metrics = this.buildAssetQuantityMetrics(asset); const available = this.getAssetAvailableForExport(asset); if (available <= 0) { this.notifyWarning('Tài sản đã hết tồn cuối kỳ, không thể xuất thêm.'); return; } this.pendingBorrowAssetId = Number(asset.AssetId); const assetIdInput = document.getElementById('borrowAssetIdInput'); const assetNameInput = document.getElementById('borrowAssetNameInput'); const endingInput = document.getElementById('borrowCurrentEndingInput'); const quantityInput = document.getElementById('borrowQuantityInput'); const projectInput = document.getElementById('borrowAssetProjectInput'); const noteInput = document.getElementById('borrowAssetNoteInput'); const modal = document.getElementById('borrowAssetModal'); if (!modal || !assetNameInput || !endingInput || !quantityInput || !projectInput || !noteInput) { this.notifyFailure('Không tìm thấy biểu mẫu xuất tài sản.'); return; } this.configureAssetExportForm('borrowAsset', forOthers); if (assetIdInput) { assetIdInput.value = String(asset.AssetId || ''); } assetNameInput.value = `${asset.AssetCode || ''} - ${asset.AssetName || ''}`.trim(); endingInput.value = String(available); quantityInput.value = '1'; quantityInput.min = '1'; quantityInput.max = String(available); document.getElementById('borrowAssetUserInput').value = this.getCurrentUserDisplayName(); this.refreshBorrowAssetProjectOptions(String(asset?.Project || '').trim()); noteInput.value = ''; document.getElementById('borrowAdditionalProjects').innerHTML = ''; document.getElementById('borrowAssetStockSummary').textContent = `Tổng tài sản: ${metrics.quantity + metrics.importInPeriod} · Đang xuất: ${metrics.exportInPeriod} · Có thể xuất: ${available}`; document.getElementById('addBorrowProjectBtn').onclick = () => this.addBorrowProjectRow(); document.getElementById('borrowAssetForm').oninput = () => this.updateBorrowAllocationSummary(); this.updateBorrowAllocationSummary(); modal.classList.add('open'); } async handleBorrowAssetSubmit(e) { e.preventDefault(); const forOthers = document.getElementById('borrowAssetForm')?.dataset.exportForOthers === 'true'; if (forOthers && !this.ensureAssetManagePermission('xuất tài sản cho người khác')) return; const assetIdInput = document.getElementById('borrowAssetIdInput'); const borrowerInput = document.getElementById(forOthers ? 'borrowAssetRecipientInput' : 'borrowAssetUserInput'); const noteInput = document.getElementById('borrowAssetNoteInput'); const selectedAssetId = Number(assetIdInput?.value || this.pendingBorrowAssetId); if (!Number.isFinite(selectedAssetId) || selectedAssetId <= 0) { this.notifyFailure('Không xác định được tài sản cần xuất.'); return; } const asset = this.assets.find(item => Number(item?.AssetId) === selectedAssetId); if (!asset) { this.notifyFailure('Không tìm thấy tài sản cần xuất.'); return; } const borrowerName = String(borrowerInput?.value || '').trim(); if (!borrowerName) { this.notifyWarning(forOthers ? 'Vui lòng chọn người nhận.' : 'Không xác định được người đề xuất.'); return; } const allocations = this.collectBorrowProjectAllocations(); if (allocations.some(row => !row.projectName)) { this.notifyWarning('Vui lòng chọn dự án cho tất cả các dòng.'); return; } if (allocations.some(row => !Number.isSafeInteger(row.quantity) || row.quantity <= 0)) { this.notifyWarning('Số lượng của mỗi dự án phải là số nguyên dương.'); return; } const borrowQuantity = allocations.reduce((sum, row) => sum + row.quantity, 0); const exportNote = String(noteInput?.value || '').trim(); const available = this.getAssetAvailableForExport(asset); if (available <= 0) { this.notifyWarning('Tài sản đã hết tồn cuối kỳ, không thể xuất thêm.'); return; } if (borrowQuantity > available) { this.notifyWarning(`Số lượng xuất (${borrowQuantity}) vượt quá tồn cuối kỳ (${available}).`); return; } if (this.assetExportSubmitting) return; this.assetExportSubmitting = true; const submitButton = document.querySelector('#borrowAssetForm button[type="submit"]'); if (submitButton) submitButton.disabled = true; let exportSucceeded = false; try { const response = await fetch(`${this.apiBase}/${forOthers ? `assets/${selectedAssetId}/export` : 'asset-export-requests'}`, { method: 'POST', headers: this.getAuthHeaders(true), body: JSON.stringify({ ...(forOthers ? { borrowerName, allocations } : { items: [{ assetId: selectedAssetId, allocations }] }), note: exportNote }) }); const data = await response.json(); if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Xuất tài sản thất bại'); return; } exportSucceeded = true; this.pendingBorrowAssetId = undefined; this.selectedAssetIds.clear(); if (forOthers) { this.notifySuccess(`Đã xuất tài sản cho ${borrowerName} và lưu phiếu Excel vào lịch sử.`); this.closeModals(); this.showExportRequestDocument(data.document); await this.refreshAssetsUI(); return; } this.notifySuccess('Đã gửi đơn xuất vào dự án, chờ admin/asset duyệt.'); this.closeModals(); this.assetExportTab = 'requests'; this.assetExportPage = 1; this.assetExportSearchTerm = ''; this.assetExportStatusFilter = ''; location.hash = '#asset-exports'; } catch (err) { console.error(err); if (exportSucceeded) this.notifyWarning(forOthers ? 'Đã xuất tài sản thành công. Hãy mở lịch sử xuất để kiểm tra hoặc tải lại phiếu Excel.' : 'Đã gửi đơn xuất. Hãy mở danh sách đơn xuất để kiểm tra.'); else this.notifyFailure('Xuất tài sản thất bại'); } finally { this.assetExportSubmitting = false; if (submitButton) submitButton.disabled = false; } } showExportRequestDocument(file) { if (!file?.contentBase64) return; this.lastExportRequestDocument = file; const modal = document.getElementById('exportRequestDocumentModal'); const name = document.getElementById('exportRequestDocumentName'); const button = document.getElementById('downloadExportRequestDocumentBtn'); if (name) name.textContent = file.fileName; if (button) button.onclick = () => this.downloadExportRequestDocument(); modal?.classList.add('open'); this.downloadExportRequestDocument(); } downloadExportRequestDocument() { const file = this.lastExportRequestDocument; if (!file?.contentBase64) return; try { const bytes = Uint8Array.from(atob(file.contentBase64), character => character.charCodeAt(0)); this.downloadBlobFile(new Blob([bytes], { type: file.mimeType }), file.fileName); } catch (err) { console.error('Download export request failed:', err); // The export has already committed. Do not report it as failed or // invite a second inventory transaction just to retry downloading. this.notifyWarning('Đã xuất thành công. Chưa tải được Excel; hãy bấm Tải lại phiếu Excel.'); } } downloadBlobFile(blob, fileName) { const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = fileName; document.body.appendChild(link); link.click(); link.remove(); setTimeout(() => URL.revokeObjectURL(url), 30000); } async handleAssetSubmit(e) { e.preventDefault(); if (!this.ensureAssetManagePermission('them hoac sua tai san')) { return; } const isEdit = this.editingAssetId !== undefined; const payload = this.collectAssetFormPayload(); this.clearAssetFormValidation(); if (!payload.model) { this.setAssetFieldValidationError('assetModelInput', 'assetModelError', 'Vui lòng nhập model.'); this.notifyWarning('Vui lòng nhập đầy đủ các trường bắt buộc.'); document.getElementById('assetModelInput')?.focus(); return; } if (!payload.assetName) { payload.assetName = payload.model; } if (isEdit && !payload.assetCode) { this.setAssetFieldValidationError('assetCodeInput', 'assetCodeError', 'Mã tài sản là bắt buộc khi cập nhật.'); this.notifyWarning('Vui lòng nhập đầy đủ các trường bắt buộc.'); document.getElementById('assetCodeInput')?.focus(); return; } if (!isEdit && !payload.assetCode) { payload.assetCode = this.generateManualAssetCodeForCreate(payload); const codeInput = document.getElementById('assetCodeInput'); if (codeInput) { codeInput.value = payload.assetCode; } } const url = isEdit ? `${this.apiBase}/assets/${this.editingAssetId}` : `${this.apiBase}/assets`; const method = isEdit ? 'PUT' : 'POST'; try { const response = await fetch(url, { method, headers: this.getAuthHeaders(true), body: JSON.stringify(payload) }); const data = await response.json(); if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Lưu tài sản thất bại'); return; } this.editingAssetId = undefined; this.editingAssetStockSnapshot = null; this.notifySuccess(isEdit ? 'Cập nhật tài sản thành công' : 'Thêm tài sản thành công'); this.closeModals(); await this.refreshAssetsUI(); } catch (err) { console.error(err); this.notifyFailure('Lưu tài sản thất bại'); } } setupConsumableStockListeners() { const openingInput = document.getElementById('consumableOpeningBalanceInput'); const importInput = document.getElementById('consumableImportInPeriodInput'); const exportInput = document.getElementById('consumableExportInPeriodInput'); const endingInput = document.getElementById('consumableEndingBalanceInput'); if (!openingInput || !importInput || !exportInput || !endingInput) { return; } const updateEnding = () => { const openingBalance = this.parseNonNegativeInteger(openingInput.value, 0); const importInPeriod = this.parseNonNegativeInteger(importInput.value, 0); const exportInPeriod = this.parseNonNegativeInteger(exportInput.value, 0); endingInput.value = String(Math.max(openingBalance + importInPeriod - exportInPeriod, 0)); }; [openingInput, importInput, exportInput].forEach(input => { if (input.dataset.boundConsumableStock === 'true') { return; } input.addEventListener('input', updateEnding); input.dataset.boundConsumableStock = 'true'; }); updateEnding(); } collectConsumableFormPayload() { const openingBalance = this.parseNonNegativeInteger(document.getElementById('consumableOpeningBalanceInput')?.value, 0); const importInPeriod = this.parseNonNegativeInteger(document.getElementById('consumableImportInPeriodInput')?.value, 0); const exportInPeriod = this.parseNonNegativeInteger(document.getElementById('consumableExportInPeriodInput')?.value, 0); return { requestMonth: String(document.getElementById('consumableRequestMonthInput')?.value || '').trim(), consumableCode: String(document.getElementById('consumableCodeInput')?.value || '').trim(), consumableName: String(document.getElementById('consumableNameInput')?.value || '').trim(), model: String(document.getElementById('consumableModelInput')?.value || '').trim(), unit: String(document.getElementById('consumableUnitInput')?.value || '').trim(), openingBalance, importInPeriod, exportInPeriod, endingBalance: Math.max(openingBalance + importInPeriod - exportInPeriod, 0), exportReason: String(document.getElementById('consumableExportReasonInput')?.value || '').trim() }; } populateConsumableForm(consumable = null) { const isEdit = Boolean(consumable); const title = document.getElementById('consumableModalTitle'); if (title) { title.textContent = isEdit ? 'Sửa vật tư tiêu hao' : 'Thêm vật tư tiêu hao'; } const setValue = (id, value) => { const input = document.getElementById(id); if (input) { input.value = value ?? ''; } }; const requestMonth = String(consumable?.RequestMonth || '').trim(); const requestMonthInput = document.getElementById('consumableRequestMonthInput'); if ( requestMonthInput?.tagName === 'SELECT' && requestMonth && !Array.from(requestMonthInput.options).some(option => option.value === requestMonth) ) { const legacyOption = document.createElement('option'); legacyOption.value = requestMonth; legacyOption.textContent = requestMonth; requestMonthInput.appendChild(legacyOption); } setValue('consumableRequestMonthInput', requestMonth); setValue('consumableCodeInput', consumable?.ConsumableCode || ''); setValue('consumableNameInput', consumable?.ConsumableName || ''); setValue('consumableModelInput', consumable?.Model || ''); setValue('consumableUnitInput', consumable?.Unit || ''); setValue('consumableOpeningBalanceInput', consumable?.OpeningBalance ?? 0); setValue('consumableImportInPeriodInput', consumable?.ImportInPeriod ?? 0); setValue('consumableExportInPeriodInput', consumable?.ExportInPeriod ?? 0); setValue('consumableEndingBalanceInput', consumable?.EndingBalance ?? 0); setValue('consumableExportReasonInput', consumable?.ExportReason || ''); this.setupConsumableStockListeners(); } openConsumableModal() { if (!this.ensureAssetManagePermission('quan ly vat tu tieu hao')) { return; } if (this.editingConsumableId === undefined) { this.populateConsumableForm(null); } document.getElementById('consumableModal')?.classList.add('open'); } openConsumableExportModal(consumable) { if (!this.ensureAssetManagePermission('xuat vat tu tieu hao')) { return; } if (!consumable) { this.notifyWarning('Vui lòng chọn vật tư cần xuất.'); return; } const endingBalance = this.parseNonNegativeInteger(consumable?.EndingBalance, 0); if (endingBalance <= 0) { this.notifyWarning('Vật tư đã hết tồn cuối kỳ, không thể xuất thêm.'); return; } this.pendingConsumableExportId = Number(consumable.ConsumableId); const idInput = document.getElementById('consumableExportConsumableIdInput'); const nameInput = document.getElementById('consumableExportConsumableNameInput'); const endingInput = document.getElementById('consumableExportCurrentEndingInput'); const quantityInput = document.getElementById('consumableExportQuantityInput'); const noteInput = document.getElementById('consumableExportNoteInput'); const actorInput = document.getElementById('consumableExportActorInput'); const roleInput = document.getElementById('consumableExportRoleInput'); const targetTypeInput = document.getElementById('consumableExportTargetTypeInput'); const projectInput = document.getElementById('consumableExportProjectInput'); const modal = document.getElementById('consumableExportModal'); if (!modal || !nameInput || !endingInput || !quantityInput || !noteInput) { this.notifyFailure('Không tìm thấy biểu mẫu xuất vật tư.'); return; } if (idInput) { idInput.value = String(consumable.ConsumableId || ''); } nameInput.value = `${consumable.ConsumableCode || ''} - ${consumable.ConsumableName || ''}`.trim(); endingInput.value = String(endingBalance); quantityInput.value = '1'; quantityInput.min = '1'; quantityInput.max = String(endingBalance); noteInput.value = ''; if (targetTypeInput) { targetTypeInput.value = 'user'; } if (projectInput) { projectInput.value = ''; } this.refreshConsumableExportUserOptions(''); this.refreshConsumableExportProjectOptions(''); this.setupConsumableExportTargetTypeListeners(); if (actorInput) { actorInput.value = this.getCurrentUserDisplayName(); } if (roleInput) { roleInput.value = String(this.getCurrentUserRoleRaw() || '').trim() || '-'; } modal.classList.add('open'); } getAvailableConsumableBorrowProducts() { return (Array.isArray(this.consumables) ? this.consumables : []) .filter(item => this.parseNonNegativeInteger(item?.EndingBalance, 0) > 0); } getFilteredConsumableBorrowProducts(queryValue = this.consumableBorrowProductQuery) { const query = String(queryValue || '').trim().toLowerCase(); return this.getAvailableConsumableBorrowProducts().filter(item => { if (!query) { return true; } return [item?.ConsumableCode, item?.ConsumableName, item?.Model, item?.Unit] .map(value => String(value || '').toLowerCase()) .some(value => value.includes(query)); }); } getConsumableBorrowProductDisplayName(consumable) { if (!consumable) { return '-- Chọn vật tư --'; } const label = [consumable.ConsumableCode, consumable.ConsumableName] .map(value => String(value || '').trim()) .filter(Boolean) .join(' - '); const stock = this.parseNonNegativeInteger(consumable?.EndingBalance, 0); const unit = String(consumable?.Unit || '').trim(); return `${label} (còn ${stock}${unit ? ` ${unit}` : ''})`; } updateConsumableBorrowProductDisplay(consumableIdValue) { const hiddenInput = document.getElementById('consumableBorrowProductInput'); const displayNode = document.getElementById('consumableBorrowProductDisplayText'); const consumableId = Number(consumableIdValue); const consumable = this.getAvailableConsumableBorrowProducts() .find(item => Number(item?.ConsumableId) === consumableId) || null; if (hiddenInput) { hiddenInput.value = consumable ? String(consumable.ConsumableId) : ''; } if (displayNode) { displayNode.textContent = this.getConsumableBorrowProductDisplayName(consumable); displayNode.classList.toggle('text-slate-600', !consumable); displayNode.classList.toggle('text-slate-700', Boolean(consumable)); } this.syncConsumableBorrowRequestSelection(); this.renderConsumableBorrowProductList(); } renderConsumableBorrowProductList() { const listNode = document.getElementById('consumableBorrowProductList'); const hiddenInput = document.getElementById('consumableBorrowProductInput'); if (!listNode) { return; } const selectedId = Number(hiddenInput?.value || 0); const rows = this.getFilteredConsumableBorrowProducts(); if (!rows.length) { listNode.innerHTML = '
Không tìm thấy vật tư phù hợp.
'; return; } listNode.innerHTML = rows.map(item => { const consumableId = Number(item?.ConsumableId); const selected = selectedId === consumableId; return ` `; }).join(''); listNode.querySelectorAll('.consumable-borrow-product-option').forEach(button => { button.addEventListener('click', () => { this.updateConsumableBorrowProductDisplay(Number(button.dataset.consumableId)); this.closeConsumableBorrowProductDropdown(); }); }); } openConsumableBorrowProductDropdown() { const dropdown = document.getElementById('consumableBorrowProductDropdown'); const searchInput = document.getElementById('consumableBorrowProductSearchInput'); dropdown?.classList.remove('hidden'); this.renderConsumableBorrowProductList(); if (searchInput) { searchInput.focus(); searchInput.select(); } } closeConsumableBorrowProductDropdown() { document.getElementById('consumableBorrowProductDropdown')?.classList.add('hidden'); } setupConsumableBorrowProductPickerListeners() { const picker = document.getElementById('consumableBorrowProductPicker'); const displayBtn = document.getElementById('consumableBorrowProductDisplayBtn'); const dropdown = document.getElementById('consumableBorrowProductDropdown'); const searchInput = document.getElementById('consumableBorrowProductSearchInput'); if (displayBtn && displayBtn.dataset.boundClick !== 'true') { displayBtn.addEventListener('click', () => { if (dropdown && !dropdown.classList.contains('hidden')) { this.closeConsumableBorrowProductDropdown(); } else { this.openConsumableBorrowProductDropdown(); } }); displayBtn.dataset.boundClick = 'true'; } if (searchInput && searchInput.dataset.boundInput !== 'true') { searchInput.addEventListener('input', () => { this.consumableBorrowProductQuery = searchInput.value; this.renderConsumableBorrowProductList(); }); searchInput.dataset.boundInput = 'true'; } if (picker && picker.dataset.boundOutsideClick !== 'true') { document.addEventListener('click', event => { if (!picker.contains(event.target)) { this.closeConsumableBorrowProductDropdown(); } }); picker.dataset.boundOutsideClick = 'true'; } } syncConsumableBorrowRequestSelection() { const productInput = document.getElementById('consumableBorrowProductInput'); const stockInput = document.getElementById('consumableBorrowCurrentStockInput'); const unitInput = document.getElementById('consumableBorrowUnitInput'); const quantityInput = document.getElementById('consumableBorrowQuantityInput'); const consumableId = Number(productInput?.value || 0); const consumable = this.consumables.find(item => Number(item?.ConsumableId) === consumableId); const endingBalance = this.parseNonNegativeInteger(consumable?.EndingBalance, 0); if (stockInput) { stockInput.value = consumable ? String(endingBalance) : ''; } if (unitInput) { unitInput.value = consumable ? String(consumable.Unit || '') : ''; } if (quantityInput) { quantityInput.max = consumable ? String(endingBalance) : ''; if (this.parseNonNegativeInteger(quantityInput.value, 0) <= 0 || Number(quantityInput.value) > endingBalance) { quantityInput.value = consumable ? '1' : ''; } } } async openConsumableBorrowRequestModal() { if (!Array.isArray(this.consumables) || !this.consumables.length) { await this.fetchConsumables(); } const modal = document.getElementById('consumableBorrowRequestModal'); const requesterInput = document.getElementById('consumableBorrowRequesterInput'); const productInput = document.getElementById('consumableBorrowProductInput'); const quantityInput = document.getElementById('consumableBorrowQuantityInput'); const dateInput = document.getElementById('consumableBorrowDateInput'); const noteInput = document.getElementById('consumableBorrowNoteInput'); if (!modal || !requesterInput || !productInput || !quantityInput || !dateInput) { this.notifyFailure('Không tìm thấy biểu mẫu tạo đơn mượn vật tư.'); return; } const availableConsumables = this.getAvailableConsumableBorrowProducts(); if (!availableConsumables.length) { this.notifyWarning('Hiện không có vật tư còn tồn để tạo đơn mượn.'); return; } requesterInput.value = this.getCurrentUserDisplayName(); this.consumableBorrowProductQuery = ''; const searchInput = document.getElementById('consumableBorrowProductSearchInput'); if (searchInput) { searchInput.value = ''; } productInput.value = ''; quantityInput.value = '1'; quantityInput.min = '1'; dateInput.value = this.toDateInputValue(new Date()); if (noteInput) { noteInput.value = ''; } this.setupConsumableBorrowProductPickerListeners(); this.updateConsumableBorrowProductDisplay(''); this.closeConsumableBorrowProductDropdown(); modal.classList.add('open'); } async handleConsumableBorrowRequestSubmit(event) { event.preventDefault(); const productInput = document.getElementById('consumableBorrowProductInput'); const quantityInput = document.getElementById('consumableBorrowQuantityInput'); const unitInput = document.getElementById('consumableBorrowUnitInput'); const dateInput = document.getElementById('consumableBorrowDateInput'); const requesterInput = document.getElementById('consumableBorrowRequesterInput'); const noteInput = document.getElementById('consumableBorrowNoteInput'); const consumableId = Number(productInput?.value || 0); const consumable = this.consumables.find(item => Number(item?.ConsumableId) === consumableId); if (!Number.isInteger(consumableId) || consumableId <= 0 || !consumable) { this.notifyWarning('Vui lòng chọn vật tư cần mượn.'); return; } const quantity = this.parseNonNegativeInteger(quantityInput?.value, 0); const endingBalance = this.parseNonNegativeInteger(consumable?.EndingBalance, 0); if (quantity <= 0) { this.notifyWarning('Số lượng mượn phải lớn hơn 0.'); return; } if (quantity > endingBalance) { this.notifyWarning(`Số lượng mượn (${quantity}) vượt quá tồn hiện tại (${endingBalance}).`); return; } try { const response = await fetch(`${this.apiBase}/consumable-borrows`, { method: 'POST', headers: this.getAuthHeaders(true), body: JSON.stringify({ consumableId, quantity, unit: String(unitInput?.value || '').trim(), borrowDate: String(dateInput?.value || '').trim(), borrowerName: String(requesterInput?.value || this.getCurrentUserDisplayName()).trim(), note: String(noteInput?.value || '').trim() }) }); const rawResponse = await response.text(); let data; try { data = rawResponse ? JSON.parse(rawResponse) : {}; } catch (_parseErr) { data = { success: false, message: `Máy chủ phản hồi lỗi HTTP ${response.status}` }; } if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Tạo đơn mượn vật tư thất bại'); return; } document.getElementById('consumableBorrowRequestModal')?.classList.remove('open'); this.notifySuccess(data.message || 'Tạo đơn mượn vật tư thành công'); await this.fetchConsumableBorrowRequests(); await this.openConsumableBorrowRequestsModal(); } catch (err) { console.error(err); this.notifyFailure('Tạo đơn mượn vật tư thất bại'); } } buildConsumableBorrowRequestRowsHtml() { const rows = Array.isArray(this.consumableBorrowRequests) ? this.consumableBorrowRequests : []; const canManageAssets = this.canCurrentUserManageAssets(); if (!rows.length) { return `Chưa có đơn mượn/trả vật tư.`; } return rows.map(item => { const requestId = Number(item?.BorrowRequestId) || 0; const typeMeta = this.getConsumableRequestTypeMeta(item?.RequestType); const statusMeta = this.getConsumableBorrowRequestStatusMeta(item?.RequestStatus); if (typeMeta.key === 'return' && statusMeta.key === 'approved') { statusMeta.label = 'Đã duyệt / đã trả'; } const isPending = statusMeta.key === 'pending'; const itemLabel = [item?.ConsumableCode, item?.ConsumableName].filter(Boolean).join(' - ') || '-'; const rejectReason = String(item?.RejectReason || '').trim(); const rejectReasonHtml = statusMeta.key === 'rejected' ? `
info ${this.escapeHtml(rejectReason || 'Chưa ghi nhận lý do từ chối')}
` : '-'; return ` #${requestId || '-'} ${this.escapeHtml(item?.BorrowerName || '-')} ${this.escapeHtml(itemLabel)} ${typeMeta.label} ${Number(item?.BorrowQuantity) || 0} ${this.escapeHtml(item?.Unit || '')} ${this.formatDateOnly(item?.BorrowDate)} ${statusMeta.label} ${this.escapeHtml(item?.RequestNote || '-')} ${this.escapeHtml(item?.ProcessedByName || '-')} ${rejectReasonHtml} ${canManageAssets && isPending ? ` ` : ''} ${isPending ? `` : '-'} `; }).join(''); } renderConsumableBorrowRequestsModal() { const tbody = document.getElementById('consumableBorrowRequestsTableBody'); const title = document.getElementById('consumableBorrowRequestsModalTitle'); const subtitle = document.getElementById('consumableBorrowRequestsModalSubtitle'); if (!tbody) { return; } if (title) { title.textContent = this.canCurrentUserManageAssets() ? 'Đơn mượn/trả vật tư / chờ duyệt' : 'Đơn mượn/trả vật tư của tôi'; } if (subtitle) { subtitle.textContent = this.canCurrentUserManageAssets() ? 'Duyệt đơn mượn sẽ trừ tồn; duyệt đơn trả sẽ cộng vật tư về kho.' : 'Theo dõi trạng thái đơn; lý do từ chối được hiển thị trực tiếp trong danh sách.'; } tbody.innerHTML = this.buildConsumableBorrowRequestRowsHtml(); this.bindConsumableBorrowRequestActions(); this.updateConsumableBorrowRequestBadges(); } bindConsumableBorrowRequestActions() { document.querySelectorAll('.consumable-borrow-approve-btn').forEach(btn => { if (btn.dataset.boundClick === 'true') return; btn.addEventListener('click', () => this.processConsumableBorrowRequest(Number(btn.dataset.requestId), 'approved')); btn.dataset.boundClick = 'true'; }); document.querySelectorAll('.consumable-borrow-reject-btn').forEach(btn => { if (btn.dataset.boundClick === 'true') return; btn.addEventListener('click', () => this.openConsumableRequestRejectModal(Number(btn.dataset.requestId))); btn.dataset.boundClick = 'true'; }); document.querySelectorAll('.consumable-borrow-cancel-btn').forEach(btn => { if (btn.dataset.boundClick === 'true') return; btn.addEventListener('click', () => this.confirmCancelConsumableBorrowRequest(Number(btn.dataset.requestId))); btn.dataset.boundClick = 'true'; }); } openConsumableRequestRejectModal(requestId) { if (!this.canCurrentUserManageAssets()) { return; } const targetId = Number(requestId); const modal = document.getElementById('consumableRequestRejectModal'); const idInput = document.getElementById('consumableRequestRejectIdInput'); const reasonInput = document.getElementById('consumableRequestRejectReasonInput'); const title = document.getElementById('consumableRequestRejectModalTitle'); if (!Number.isInteger(targetId) || targetId <= 0 || !modal || !idInput || !reasonInput) { this.notifyFailure('Không tìm thấy biểu mẫu từ chối đơn vật tư.'); return; } this.pendingConsumableRequestRejectId = targetId; idInput.value = String(targetId); reasonInput.value = ''; const request = this.consumableBorrowRequests.find(item => Number(item?.BorrowRequestId) === targetId); const requestType = this.getConsumableRequestTypeMeta(request?.RequestType); if (title) { title.textContent = `Từ chối đơn ${requestType.label.toLowerCase()} #${targetId}`; } modal.classList.add('open'); reasonInput.focus(); } async handleConsumableRequestRejectSubmit(event) { event.preventDefault(); const requestId = Number( document.getElementById('consumableRequestRejectIdInput')?.value || this.pendingConsumableRequestRejectId ); const rejectReason = String(document.getElementById('consumableRequestRejectReasonInput')?.value || '').trim(); if (!Number.isInteger(requestId) || requestId <= 0) { this.notifyWarning('Không xác định được đơn cần từ chối.'); return; } if (!rejectReason) { this.notifyWarning('Vui lòng nhập lý do từ chối để người tạo đơn được biết.'); document.getElementById('consumableRequestRejectReasonInput')?.focus(); return; } await this.processConsumableBorrowRequest(requestId, 'rejected', rejectReason); } async confirmCancelConsumableBorrowRequest(requestId) { const targetId = Number(requestId); if (!Number.isInteger(targetId) || targetId <= 0) { return; } if (!document.getElementById('assetRequestDeleteConfirmModal')) { this.notifyFailure('Không tìm thấy hộp thoại xác nhận hủy đơn.'); return; } const request = this.consumableBorrowRequests.find(item => Number(item?.BorrowRequestId) === targetId); const requestType = this.getConsumableRequestTypeMeta(request?.RequestType).label.toLowerCase(); const confirmed = await this.confirmAssetRequestDelete( `Bạn có chắc muốn hủy đơn ${requestType} #${targetId}? Thao tác này không thể hoàn tác.`, 'Hủy đơn' ); if (confirmed) { await this.cancelConsumableBorrowRequest(targetId); } } async openConsumableBorrowRequestsModal() { const modal = document.getElementById('consumableBorrowRequestsModal'); const tbody = document.getElementById('consumableBorrowRequestsTableBody'); if (!modal || !tbody) { this.notifyFailure('Không tìm thấy danh sách đơn mượn vật tư.'); return; } tbody.innerHTML = `Đang tải đơn mượn/trả...`; modal.classList.add('open'); await this.fetchConsumableBorrowRequests(); this.renderConsumableBorrowRequestsModal(); } async processConsumableBorrowRequest(requestId, action, rejectReason = '') { if (!this.ensureAssetManagePermission('xu ly don muon vat tu tieu hao')) { return; } if (!Number.isInteger(requestId) || requestId <= 0) { return; } try { const response = await fetch(`${this.apiBase}/consumable-borrows/${requestId}/process`, { method: 'POST', headers: this.getAuthHeaders(true), body: JSON.stringify({ action, rejectReason }) }); const data = await response.json(); if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Xử lý đơn mượn thất bại'); return; } if (action === 'rejected') { this.pendingConsumableRequestRejectId = undefined; closeConsumableRequestRejectModal(); } this.notifySuccess(data.message || (action === 'approved' ? 'Đã duyệt đơn' : 'Đã từ chối đơn')); await Promise.all([ this.fetchConsumableBorrowRequests(), this.fetchConsumables(), this.fetchConsumableExportHistories(300) ]); this.renderConsumableBorrowRequestsModal(); if (this.currentPage === 'consumables') { this.renderConsumablesTableBody(); } } catch (err) { console.error(err); this.notifyFailure('Xử lý đơn mượn thất bại'); } } async cancelConsumableBorrowRequest(requestId) { if (!Number.isInteger(requestId) || requestId <= 0) { return; } try { const response = await fetch(`${this.apiBase}/consumable-borrows/${requestId}`, { method: 'DELETE', headers: this.getAuthHeaders(false) }); const data = await response.json(); if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Hủy đơn mượn thất bại'); return; } this.notifySuccess(data.message || 'Đã hủy đơn mượn'); await this.fetchConsumableBorrowRequests(); this.renderConsumableBorrowRequestsModal(); } catch (err) { console.error(err); this.notifyFailure('Hủy đơn mượn thất bại'); } } setupConsumableReturnActionListeners() { document.querySelectorAll('.return-consumable-export').forEach(btn => { if (btn.dataset.boundClick === 'true') { return; } btn.addEventListener('click', () => { if (btn.disabled) { return; } const exportHistoryId = Number(btn.dataset.exportHistoryId); const history = this.consumableExportHistories.find(item => ( Number(item?.ExportHistoryId) === exportHistoryId )); if (!this.getConsumableReturnActionMeta(history)) { this.notifyWarning('Vật tư này không có hành động trả phù hợp.'); return; } this.openConsumableReturnModal(history); }); btn.dataset.boundClick = 'true'; }); } openConsumableReturnModal(history) { if (!history) { this.notifyWarning('Không tìm thấy phiếu xuất cần trả.'); return; } const remainingQuantity = this.parseNonNegativeInteger(history?.RemainingQuantity, 0); const returnAction = this.getConsumableReturnActionMeta(history); if (!returnAction) { this.notifyWarning(String(history?.ProjectName || '').trim() ? 'Vật tư xuất cho dự án không có hành động trả.' : 'Vật tư này đã hết số lượng có thể tạo đơn trả.'); return; } const modal = document.getElementById('consumableReturnModal'); const historyIdInput = document.getElementById('consumableReturnExportHistoryIdInput'); const consumableInput = document.getElementById('consumableReturnConsumableInput'); const destinationInput = document.getElementById('consumableReturnDestinationInput'); const remainingInput = document.getElementById('consumableReturnRemainingInput'); const quantityInput = document.getElementById('consumableReturnQuantityInput'); const actorInput = document.getElementById('consumableReturnActorInput'); const noteInput = document.getElementById('consumableReturnNoteInput'); const title = document.getElementById('consumableReturnModalTitle'); const quantityLabel = document.getElementById('consumableReturnQuantityLabel'); const actorLabel = document.getElementById('consumableReturnActorLabel'); const helpText = document.getElementById('consumableReturnHelpText'); const submitBtn = document.getElementById('consumableReturnSubmitBtn'); if (!modal || !historyIdInput || !consumableInput || !destinationInput || !remainingInput || !quantityInput) { this.notifyFailure('Không tìm thấy biểu mẫu hoàn trả vật tư.'); return; } this.pendingConsumableReturnHistoryId = Number(history.ExportHistoryId); historyIdInput.value = String(history.ExportHistoryId || ''); consumableInput.value = [history.ConsumableCode, history.ConsumableName].filter(Boolean).join(' - '); destinationInput.value = String(history.ProjectName || '').trim() ? `Dự án: ${String(history.ProjectName).trim()}` : `Người mượn: ${String(history.RecipientName || '-').trim() || '-'}`; remainingInput.value = String(remainingQuantity); quantityInput.value = String(returnAction.availableQuantity); quantityInput.min = '1'; quantityInput.max = String(returnAction.availableQuantity); if (actorInput) { actorInput.value = this.getCurrentUserDisplayName(); } if (noteInput) { noteInput.value = ''; } const isReturnRequest = returnAction.mode === 'request'; if (title) title.textContent = isReturnRequest ? 'Tạo đơn trả vật tư' : 'Hoàn trả vật tư về kho'; if (quantityLabel) quantityLabel.textContent = isReturnRequest ? 'Số lượng muốn trả' : 'Số lượng trả kho'; if (actorLabel) actorLabel.textContent = isReturnRequest ? 'Người tạo đơn' : 'Người nhận lại kho'; if (helpText) { helpText.textContent = isReturnRequest ? 'Vật tư chỉ được cộng lại vào tồn kho sau khi Asset/Admin duyệt đơn trả.' : 'Khi xác nhận, số lượng này sẽ được cộng lại vào tồn kho ngay.'; } if (submitBtn) submitBtn.textContent = isReturnRequest ? 'Gửi đơn trả' : 'Xác nhận trả kho'; modal.classList.add('open'); quantityInput.focus(); quantityInput.select(); } async handleConsumableReturnSubmit(e) { e.preventDefault(); const historyIdInput = document.getElementById('consumableReturnExportHistoryIdInput'); const quantityInput = document.getElementById('consumableReturnQuantityInput'); const noteInput = document.getElementById('consumableReturnNoteInput'); const exportHistoryId = Number(historyIdInput?.value || this.pendingConsumableReturnHistoryId); const history = this.consumableExportHistories.find(item => ( Number(item?.ExportHistoryId) === exportHistoryId )); if (!Number.isInteger(exportHistoryId) || exportHistoryId <= 0 || !history) { this.notifyFailure('Không xác định được phiếu xuất cần trả.'); return; } const returnAction = this.getConsumableReturnActionMeta(history); if (!returnAction) { this.notifyWarning('Vật tư này không còn số lượng có thể trả.'); return; } const returnQuantity = this.parseNonNegativeInteger(quantityInput?.value, 0); if (returnQuantity <= 0) { this.notifyWarning('Số lượng trả phải lớn hơn 0.'); return; } if (returnQuantity > returnAction.availableQuantity) { this.notifyWarning(`Số lượng trả (${returnQuantity}) vượt quá số lượng có thể trả (${returnAction.availableQuantity}).`); return; } try { const isReturnRequest = returnAction.mode === 'request'; const endpoint = isReturnRequest ? `${this.apiBase}/consumable-exports/${exportHistoryId}/return-request` : `${this.apiBase}/consumable-exports/${exportHistoryId}/return`; const response = await fetch(endpoint, { method: 'POST', headers: this.getAuthHeaders(true), body: JSON.stringify({ quantity: returnQuantity, note: String(noteInput?.value || '').trim() }) }); const rawResponse = await response.text(); let data; try { data = rawResponse ? JSON.parse(rawResponse) : {}; } catch (_parseErr) { data = { success: false, message: `Máy chủ phản hồi lỗi HTTP ${response.status}` }; } if (!response.ok || !data.success) { this.notifyFailure(data.message || (isReturnRequest ? 'Tạo đơn trả vật tư thất bại' : 'Hoàn trả vật tư thất bại')); return; } this.pendingConsumableReturnHistoryId = undefined; document.getElementById('consumableReturnModal')?.classList.remove('open'); this.notifySuccess(data.message || (isReturnRequest ? 'Tạo đơn trả vật tư thành công' : 'Hoàn trả vật tư về kho thành công')); await Promise.all([ this.fetchConsumables(), this.fetchConsumableExportHistories(this.currentPage === 'consumable-exports' ? 2000 : 300), this.fetchConsumableBorrowRequests() ]); if (isReturnRequest) { document.getElementById('consumableExportHistoryModal')?.classList.remove('open'); await this.openConsumableBorrowRequestsModal(); return; } if (this.currentPage === 'consumables') { this.renderView('consumables'); } else if (this.currentPage === 'consumable-exports') { this.renderConsumableExportHistoryPageBody(); } if (document.getElementById('consumableExportHistoryModal')?.classList.contains('open')) { this.renderConsumableExportHistoryModal(); } } catch (err) { console.error(err); this.notifyFailure('Hoàn trả vật tư thất bại'); } } async handleConsumableExportSubmit(e) { e.preventDefault(); if (!this.ensureAssetManagePermission('xuat vat tu tieu hao')) { return; } const consumableIdInput = document.getElementById('consumableExportConsumableIdInput'); const targetTypeInput = document.getElementById('consumableExportTargetTypeInput'); const recipientInput = document.getElementById('consumableExportUserInput'); const projectInput = document.getElementById('consumableExportProjectInput'); const quantityInput = document.getElementById('consumableExportQuantityInput'); const noteInput = document.getElementById('consumableExportNoteInput'); const selectedConsumableId = Number(consumableIdInput?.value || this.pendingConsumableExportId); const targetType = String(targetTypeInput?.value || 'user').trim() === 'project' ? 'project' : 'user'; if (!Number.isFinite(selectedConsumableId) || selectedConsumableId <= 0) { this.notifyFailure('Không xác định được vật tư cần xuất.'); return; } const consumable = this.consumables.find(item => Number(item?.ConsumableId) === selectedConsumableId); if (!consumable) { this.notifyFailure('Không tìm thấy vật tư cần xuất.'); return; } const recipientUserId = Number(recipientInput?.value || 0); const recipientName = String( recipientInput?.selectedOptions?.[0]?.dataset?.userName || recipientInput?.selectedOptions?.[0]?.textContent || '' ).trim(); const projectName = String(projectInput?.value || '').trim(); if (targetType === 'user' && (!Number.isInteger(recipientUserId) || recipientUserId <= 0 || !recipientName)) { this.notifyWarning('Vui lòng chọn người nhận.'); return; } if (targetType === 'project' && !projectName) { this.notifyWarning('Vui lòng chọn dự án nhận.'); return; } const exportQuantity = this.parseNonNegativeInteger(quantityInput?.value ?? 0, 0); if (exportQuantity <= 0) { this.notifyWarning('Số lượng xuất phải lớn hơn 0.'); return; } const endingBalance = this.parseNonNegativeInteger(consumable?.EndingBalance, 0); if (endingBalance <= 0) { this.notifyWarning('Vật tư đã hết tồn cuối kỳ, không thể xuất thêm.'); return; } if (exportQuantity > endingBalance) { this.notifyWarning(`Số lượng xuất (${exportQuantity}) vượt quá tồn cuối kỳ (${endingBalance}).`); return; } if (this.consumableExportSubmitting) return; this.consumableExportSubmitting = true; const submitButton = document.querySelector('#consumableExportForm button[type="submit"]'); if (submitButton) submitButton.disabled = true; let exportSucceeded = false; try { const response = await fetch(`${this.apiBase}/consumables/${selectedConsumableId}/export`, { method: 'POST', headers: this.getAuthHeaders(true), body: JSON.stringify({ quantity: exportQuantity, targetType, recipientUserId: targetType === 'user' ? recipientUserId : null, recipientName, projectName, note: String(noteInput?.value || '').trim() }) }); const data = await response.json(); if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Xuất vật tư tiêu hao thất bại'); return; } exportSucceeded = true; this.pendingConsumableExportId = undefined; this.notifySuccess(targetType === 'project' ? 'Xuất vật tư cho dự án thành công' : 'Ghi nhận mượn vật tư thành công'); this.closeModals(); this.showExportRequestDocument(data.document); await this.refreshConsumablesUI(); const exportHistoryModal = document.getElementById('consumableExportHistoryModal'); if (exportHistoryModal?.classList.contains('open')) { await this.fetchConsumableExportHistories(); this.renderConsumableExportHistoryModal(); } } catch (err) { console.error(err); if (exportSucceeded) this.notifyWarning('Đã xuất vật tư thành công. Chưa cập nhật được danh sách; hãy tải lại trang.'); else this.notifyFailure('Xuất vật tư tiêu hao thất bại'); } finally { this.consumableExportSubmitting = false; if (submitButton) submitButton.disabled = false; } } async handleConsumableSubmit(e) { e.preventDefault(); if (!this.ensureAssetManagePermission('them hoac sua vat tu tieu hao')) { return; } const isEdit = this.editingConsumableId !== undefined; const payload = this.collectConsumableFormPayload(); if (!payload.consumableName) { this.notifyWarning('Vui lòng nhập tên vật tư.'); document.getElementById('consumableNameInput')?.focus(); return; } const url = isEdit ? `${this.apiBase}/consumables/${this.editingConsumableId}` : `${this.apiBase}/consumables`; const method = isEdit ? 'PUT' : 'POST'; try { const response = await fetch(url, { method, headers: this.getAuthHeaders(true), body: JSON.stringify(payload) }); const data = await response.json(); if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Lưu vật tư tiêu hao thất bại'); return; } this.editingConsumableId = undefined; this.notifySuccess(isEdit ? 'Cập nhật vật tư thành công' : 'Thêm vật tư thành công'); this.closeModals(); await this.refreshConsumablesUI(); } catch (err) { console.error(err); this.notifyFailure('Lưu vật tư tiêu hao thất bại'); } } async refreshConsumablesUI() { await this.fetchConsumables(); if (this.currentPage === 'consumables') { this.renderView('consumables'); } } setupConsumableRowListeners() { const canManageAssets = this.canCurrentUserManageAssets(); document.querySelectorAll('.edit-consumable').forEach(btn => { if (btn.dataset.boundClick === 'true') { return; } btn.addEventListener('click', () => { if (!this.ensureAssetManagePermission('sua vat tu tieu hao')) { return; } const consumableId = Number(btn.dataset.consumableId); const consumable = this.consumables.find(item => Number(item.ConsumableId) === consumableId); this.editingConsumableId = consumable?.ConsumableId; this.populateConsumableForm(consumable); this.closeModals(); this.openConsumableModal(); }); btn.dataset.boundClick = 'true'; }); document.querySelectorAll('.export-consumable').forEach(btn => { if (btn.dataset.boundClick === 'true') { return; } btn.addEventListener('click', () => { if (!canManageAssets || !this.ensureAssetManagePermission('xuat vat tu tieu hao')) { return; } const consumableId = Number(btn.dataset.consumableId); const consumable = this.consumables.find(item => Number(item.ConsumableId) === consumableId); this.openConsumableExportModal(consumable); }); btn.dataset.boundClick = 'true'; }); document.querySelectorAll('.delete-consumable').forEach(btn => { if (btn.dataset.boundClick === 'true') { return; } btn.addEventListener('click', () => { if (!canManageAssets || !this.ensureAssetManagePermission('xoa vat tu tieu hao')) { return; } const consumableId = Number(btn.dataset.consumableId); const consumable = this.consumables.find(item => Number(item.ConsumableId) === consumableId); this.pendingDeleteConsumableId = consumableId; const label = consumable?.ConsumableName || consumable?.ConsumableCode || '-'; document.getElementById('deleteConsumableName').textContent = label; document.getElementById('deleteConsumableModal')?.classList.add('open'); }); btn.dataset.boundClick = 'true'; }); } async confirmDeleteConsumable() { if (!this.ensureAssetManagePermission('xoa vat tu tieu hao')) { return; } const consumableId = Number(this.pendingDeleteConsumableId); if (!Number.isFinite(consumableId) || consumableId <= 0) { this.notifyFailure('Không xác định được vật tư cần xóa'); return; } try { const response = await fetch(`${this.apiBase}/consumables/${consumableId}`, { method: 'DELETE', headers: this.getAuthHeaders(false) }); const data = await response.json(); if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Xóa vật tư thất bại'); return; } this.pendingDeleteConsumableId = undefined; this.notifySuccess('Xóa vật tư thành công'); this.closeModals(); await this.refreshConsumablesUI(); } catch (err) { console.error(err); this.notifyFailure('Xóa vật tư thất bại'); } } async processConsumableImportFile(event) { if (!this.ensureAssetManagePermission('nhap du lieu vat tu tieu hao')) { event.target.value = ''; return; } const file = event.target.files?.[0]; event.target.value = ''; if (!file) { return; } try { const formData = new FormData(); formData.append('file', file); const response = await fetch(`${this.apiBase}/consumables/import`, { method: 'POST', headers: this.getAuthHeaders(false), body: formData }); const data = await response.json(); if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Import vật tư tiêu hao thất bại'); return; } const inserted = data.data?.inserted ?? 0; const updated = data.data?.updated ?? 0; this.notifySuccess(`Import VTTH xong: thêm ${inserted}, cập nhật ${updated}`); await this.refreshConsumablesUI(); } catch (err) { console.error(err); this.notifyFailure('Import vật tư tiêu hao thất bại'); } } exportConsumablesToExcel() { if (!window.XLSX) { this.notifyFailure('Không tìm thấy thư viện xuất Excel'); return; } try { const exportRows = this.getFilteredConsumables().map(item => ({ 'STT': '', 'Tháng đề xuất': item.RequestMonth || '', 'Mã vật tư': item.ConsumableCode || '', 'Tên linh kiện/sp': item.ConsumableName || '', 'Model': item.Model || '', 'ĐVT': item.Unit || '', 'Tồn đầu kỳ': item.OpeningBalance ?? 0, 'Nhập trong kì': item.ImportInPeriod ?? 0, 'Xuất trong kì': item.ExportInPeriod ?? 0, 'Đã xuất': item.ExportedSummary || '', 'Người đang nhận': item.RecipientSummary || '', 'Dự án nhận': item.ProjectSummary || '', 'Tồn cuối kì': item.EndingBalance ?? 0, 'Lí do xuất': item.ExportReason || '' })).map((row, index) => ({ ...row, 'STT': index + 1 })); const worksheet = window.XLSX.utils.json_to_sheet(exportRows); const workbook = window.XLSX.utils.book_new(); window.XLSX.utils.book_append_sheet(workbook, worksheet, 'VTTH'); const timestamp = this.formatTimestampForCode(new Date()).slice(0, 8); window.XLSX.writeFile(workbook, `vat-tu-tieu-hao-${timestamp}.xlsx`); } catch (err) { console.error(err); this.notifyFailure('Xuất vật tư tiêu hao thất bại'); } } exportConsumableHistoryToExcel() { if (!window.XLSX) { this.notifyFailure('Không tìm thấy thư viện xuất Excel'); return; } try { const rows = this.getFilteredConsumableExportHistories().map((item, index) => ({ 'STT': index + 1, 'Ngày giờ': this.formatDateTime(item?.ExportedDate || item?.CreatedDate), 'Mã vật tư': item?.ConsumableCode || '', 'Tên vật tư': item?.ConsumableName || '', 'Số lượng xuất': Number(item?.ExportQuantity) || 0, 'Đã trả kho': Number(item?.ReturnedQuantity) || 0, 'Còn phải trả': Number(item?.RemainingQuantity) || 0, 'ĐVT': item?.Unit || '', 'Người nhận': item?.RecipientName || '', 'Dự án nhận': item?.ProjectName || '', 'Trạng thái': this.getConsumableReturnStatusMeta(item).label, 'Người xuất': item?.ExportedByName || '', 'Lần trả gần nhất': this.formatDateTime(item?.LastReturnedDate), 'Người nhận lại gần nhất': item?.LastReturnedByName || '', 'Ghi chú trả gần nhất': item?.LastReturnNote || '', 'Tồn trước': Number(item?.PreviousEndingBalance) || 0, 'Tồn sau': Number(item?.NextEndingBalance) || 0, 'Ghi chú': item?.ExportNote || '' })); const worksheet = window.XLSX.utils.json_to_sheet(rows); const workbook = window.XLSX.utils.book_new(); window.XLSX.utils.book_append_sheet(workbook, worksheet, 'LichSuXuatVTTH'); const timestamp = this.formatTimestampForCode(new Date()).slice(0, 8); window.XLSX.writeFile(workbook, `lich-su-xuat-vtth-${timestamp}.xlsx`); } catch (err) { console.error(err); this.notifyFailure('Xuất lịch sử vật tư tiêu hao thất bại'); } } async refreshAssetsUI() { await this.fetchAssets(); await this.fetchAssetDepartments(); await this.fetchAssetProjects(); if (this.currentPage === 'assets' || this.currentPage === 'my-borrowed-assets') { this.renderView(this.currentPage); } } setupAssetRowListeners() { this.setupArchivedAssetListeners(); this.setupAssetSelectionListeners(); this.setupAssetActionMenus(); const canManageAssets = this.canCurrentUserManageAssets(); document.querySelectorAll('.view-asset').forEach(btn => { btn.addEventListener('click', () => { const assetId = Number(btn.dataset.assetId); const asset = this.assets.find(a => a.AssetId === assetId); this.currentViewAsset = asset; this.currentViewAssetId = assetId; this.renderAssetDetails(asset); document.getElementById('viewAssetModal').classList.add('open'); }); }); document.querySelectorAll('.edit-asset').forEach(btn => { btn.addEventListener('click', () => { if (!this.ensureAssetManagePermission('sua tai san')) { return; } const assetId = Number(btn.dataset.assetId); const asset = this.assets.find(a => a.AssetId === assetId); this.editingAssetId = asset?.AssetId; this.populateAssetForm(asset); this.closeModals(); this.openAssetModal(); }); }); document.querySelectorAll('.delete-asset').forEach(btn => { btn.addEventListener('click', () => { if (!this.ensureAssetManagePermission('xoa tai san')) { return; } const assetId = Number(btn.dataset.assetId); const asset = this.assets.find(a => a.AssetId === assetId); this.pendingDeleteAssetId = assetId; document.getElementById('deleteAssetName').textContent = asset?.AssetName || asset?.AssetCode || '-'; document.getElementById('deleteAssetModal').classList.add('open'); }); }); document.querySelectorAll('.confirm-delete-asset').forEach(btn => { if (btn.dataset.boundClick) { return; } btn.addEventListener('click', async () => { if (!this.ensureAssetManagePermission('xoa tai san')) { return; } if (this.pendingDeleteAssetId === undefined) { return; } const targetDeleteId = Number(this.pendingDeleteAssetId); try { const response = await fetch(`${this.apiBase}/assets/${this.pendingDeleteAssetId}`, { method: 'DELETE', headers: this.getAuthHeaders(false) }); const data = await response.json(); if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Xóa tài sản thất bại'); return; } this.pendingDeleteAssetId = undefined; this.selectedAssetIds.delete(targetDeleteId); this.closeModals(); this.notifySuccess('Đã xóa khỏi danh mục. Lịch sử mượn/trả được giữ lại.'); await this.refreshAssetsUI(); await this.refreshAssetBorrowsUI(); } catch (err) { console.error(err); this.notifyFailure('Xóa tài sản thất bại'); } }); btn.dataset.boundClick = 'true'; }); const editFromViewBtn = document.querySelector('.edit-asset-from-view'); if (editFromViewBtn && !editFromViewBtn.dataset.boundClick) { editFromViewBtn.disabled = !canManageAssets; editFromViewBtn.classList.toggle('opacity-50', !canManageAssets); editFromViewBtn.classList.toggle('cursor-not-allowed', !canManageAssets); editFromViewBtn.addEventListener('click', () => { if (!this.ensureAssetManagePermission('sua tai san')) { return; } const asset = this.currentViewAsset; this.editingAssetId = asset?.AssetId; this.populateAssetForm(asset); this.closeModals(); this.openAssetModal(); }); editFromViewBtn.dataset.boundClick = 'true'; } } normalizeImportHeader(key) { return String(key || '') .normalize('NFD') .replace(/[\u0300-\u036f]/g, '') .replace(/[\u0111\u0110]/g, 'd') .toLowerCase() .replace(/[^a-z0-9]/g, ''); } isImportHeaderMatch(actualHeader, alias) { const normalizedHeader = this.normalizeImportHeader(actualHeader); const normalizedAlias = this.normalizeImportHeader(alias); if (!normalizedHeader || !normalizedAlias) { return false; } if (normalizedHeader === normalizedAlias) { return true; } // Avoid over-matching very short aliases such as "PN". if (normalizedAlias.length < 4 || normalizedHeader.length < 4) { return false; } return normalizedHeader.includes(normalizedAlias) || normalizedAlias.includes(normalizedHeader); } findImportValue(row, aliases) { for (const [key, value] of Object.entries(row || {})) { if (aliases.some(alias => this.isImportHeaderMatch(key, alias))) { return value; } } return ''; } isHeaderLikeImportValue(value) { const normalized = this.normalizeImportHeader(value); if (!normalized) { return false; } const knownHeaderTokens = new Set([ 'stt', 'ngayve', 'mavattu', 'mavt', 'mataisan', 'mats', 'matscd', 'tenlinhkiensp', 'tentaisan', 'tentaisanccdc', 'model', 'dvt', 'donvi', 'tondauky', 'tondauki', 'nhaptrongky', 'nhaptrongki', 'xuattrongky', 'xuattrongki', 'toncuoiky', 'toncuoiki', 'lidoxuat', 'lydoxuat', 'tinhtrang', 'vitri', 'duan', 'assetcode', 'assetname', 'quantity', 'importinperiod', 'exportinperiod', 'endingbalance', 'unit', 'location', 'department', 'project', 'status', 'notes' ]); return knownHeaderTokens.has(normalized); } isLikelyHeaderArtifactAssetRow(mappedRow) { const row = mappedRow || {}; const fields = [ row.assetCode, row.assetName, row.model, row.unit, row.status, row.location, row.department, row.project, row.importInPeriod, row.exportInPeriod, row.endingBalance, row.notes ]; const headerLikeCount = fields.reduce((count, value) => { return count + (this.isHeaderLikeImportValue(value) ? 1 : 0); }, 0); if (headerLikeCount >= 2) { return true; } return this.isHeaderLikeImportValue(row.assetName) && this.isHeaderLikeImportValue(row.model); } hasImportAliasInRow(row, aliases) { const normalizedRow = (Array.isArray(row) ? row : []) .map(cell => this.normalizeImportHeader(cell)) .filter(Boolean); if (!normalizedRow.length) { return false; } return aliases.some(alias => { const normalizedAlias = this.normalizeImportHeader(alias); return normalizedRow.some(headerValue => this.isImportHeaderMatch(headerValue, normalizedAlias)); }); } isLikelyAssetHeaderRow(row) { const codeAliases = ['Asset Code', 'Ma tai san', 'Ma TS', 'Ma TSCD', 'Ma vat tu', 'Ma VT', 'Ma linh kien', 'Code', 'SKU', 'Part Number', 'PN', 'So the', 'So hieu', 'Ma tai san/CCDC']; const nameAliases = ['Asset Name', 'Ten tai san', 'Ten TS', 'Ten TSCD', 'Ten CCDC', 'Ten vat tu', 'Ten linh kien', 'Ten linh kien/sp', 'Ten linh kien sp', 'Ten sp', 'Name', 'Dien giai', 'Mo ta', 'Ten tai san/CCDC']; const modelAliases = ['Model', 'Dong may']; const quantityAliases = ['Ton dau ky', 'Ton dau ki', 'Quantity', 'So luong', 'SL', 'Nhap trong ky', 'Nhap trong ki', 'Xuat trong ky', 'Xuat trong ki']; const unitAliases = ['Unit', 'Don vi', 'DVT']; const sttAliases = ['STT', 'So thu tu']; const hasCode = this.hasImportAliasInRow(row, codeAliases); const hasName = this.hasImportAliasInRow(row, nameAliases); const hasModel = this.hasImportAliasInRow(row, modelAliases); const hasQty = this.hasImportAliasInRow(row, quantityAliases); const hasUnit = this.hasImportAliasInRow(row, unitAliases); const hasStt = this.hasImportAliasInRow(row, sttAliases); if (hasStt && hasName && (hasModel || hasQty || hasUnit || hasCode)) { return true; } if (hasCode && hasName) { return true; } return hasName && (hasModel || hasQty || hasUnit); } findAssetImportHeaderRowIndex(matrixRows) { const codeAliases = ['Asset Code', 'Ma tai san', 'Ma TS', 'Ma TSCD', 'Ma vat tu', 'Ma VT', 'Ma linh kien', 'Code', 'SKU', 'Part Number', 'PN', 'So the', 'So hieu', 'Ma tai san/CCDC']; const nameAliases = ['Asset Name', 'Ten tai san', 'Ten TS', 'Ten TSCD', 'Ten CCDC', 'Ten vat tu', 'Ten linh kien', 'Ten linh kien/sp', 'Ten linh kien sp', 'Ten sp', 'Name', 'Dien giai', 'Mo ta', 'Ten tai san/CCDC']; const modelAliases = ['Model', 'Dong may']; const quantityAliases = ['Ton dau ky', 'Ton dau ki', 'Quantity', 'So luong', 'SL', 'Nhap trong ky', 'Nhap trong ki', 'Xuat trong ky', 'Xuat trong ki']; const unitAliases = ['Unit', 'Don vi', 'DVT']; const sttAliases = ['STT', 'So thu tu']; const maxScanRows = Math.min(Array.isArray(matrixRows) ? matrixRows.length : 0, 50); let bestIndex = -1; let bestScore = 0; for (let rowIndex = 0; rowIndex < maxScanRows; rowIndex += 1) { const row = Array.isArray(matrixRows[rowIndex]) ? matrixRows[rowIndex] : []; const normalizedRow = row .map(cell => this.normalizeImportHeader(cell)) .filter(Boolean); if (!normalizedRow.length) { continue; } const normalizedSet = new Set(normalizedRow); const hasAnyAlias = aliasList => aliasList.some(alias => { const normalizedAlias = this.normalizeImportHeader(alias); for (const headerValue of normalizedSet) { if (this.isImportHeaderMatch(headerValue, normalizedAlias)) { return true; } } return false; }); const hasCode = hasAnyAlias(codeAliases); const hasName = hasAnyAlias(nameAliases); const hasModel = hasAnyAlias(modelAliases); const hasQty = hasAnyAlias(quantityAliases); const hasUnit = hasAnyAlias(unitAliases); const hasStt = hasAnyAlias(sttAliases); if (hasStt && hasName && (hasModel || hasQty || hasUnit || hasCode)) { return rowIndex; } if (hasCode && hasName) { return rowIndex; } let score = 0; if (hasName) score += 4; if (hasCode) score += 3; if (hasStt) score += 3; if (hasModel) score += 2; if (hasQty) score += 1; if (hasUnit) score += 1; if (score > bestScore) { bestScore = score; bestIndex = rowIndex; } } // Fallback for inventory templates that omit one canonical column name. if (bestScore >= 4) { return bestIndex; } return -1; } mapImportedAssetRowsFromMatrix(matrixRows, headerRowIndex) { const headerRow = Array.isArray(matrixRows[headerRowIndex]) ? matrixRows[headerRowIndex] : []; if (!headerRow.length) { return []; } const sttAliases = ['STT', 'So thu tu']; return matrixRows .slice(headerRowIndex + 1) .filter(row => Array.isArray(row) && row.some(cell => String(cell ?? '').trim() !== '')) .map((row, rowOffset) => { const rowObject = {}; headerRow.forEach((header, index) => { const headerText = String(header ?? '').trim(); if (!headerText) { return; } rowObject[headerText] = row[index] ?? ''; }); const sttValue = String(this.findImportValue(rowObject, sttAliases)).trim(); if (sttValue && Number.isNaN(Number(sttValue))) { return null; } if (sttValue === '' && this.findImportValue(rowObject, sttAliases) !== '') { return null; } return this.mapImportedAssetRow(rowObject, headerRowIndex + rowOffset + 2); }) .filter(Boolean) .filter(row => !this.isLikelyHeaderArtifactAssetRow(row)) .filter(row => row.assetCode && row.assetName); } inferImportColumnIndex(headerRow, aliases) { const headers = Array.isArray(headerRow) ? headerRow : []; for (let index = 0; index < headers.length; index += 1) { if (aliases.some(alias => this.isImportHeaderMatch(headers[index], alias))) { return index; } } return -1; } parseImportNumericValue(value, fallback = 1) { if (value === undefined || value === null || value === '') { return fallback; } const normalized = String(value).trim().replace(/,/g, ''); if (!normalized) { return fallback; } const parsed = Number(normalized); return Number.isFinite(parsed) ? parsed : fallback; } buildAssetImportIndexMap(headerRow) { const indexMap = { stt: this.inferImportColumnIndex(headerRow, ['STT', 'So thu tu']), assetCode: this.inferImportColumnIndex(headerRow, ['Asset Code', 'Ma tai san', 'Ma TS', 'Ma TSCD', 'Ma vat tu', 'Ma VT', 'Ma linh kien', 'Code', 'SKU', 'Part Number', 'PN', 'So the', 'So hieu', 'Ma tai san/CCDC']), assetName: this.inferImportColumnIndex(headerRow, ['Asset Name', 'Ten tai san', 'Ten TS', 'Ten TSCD', 'Ten CCDC', 'Ten vat tu', 'Ten linh kien', 'Ten linh kien/sp', 'Ten linh kien sp', 'Ten sp', 'Name', 'Dien giai', 'Mo ta', 'Ten tai san/CCDC']), model: this.inferImportColumnIndex(headerRow, ['Model', 'Dong may']), serialNumber: this.inferImportColumnIndex(headerRow, ['Serial Number', 'Serial', 'So serial', 'So seri']), quantity: this.inferImportColumnIndex(headerRow, ['Ton dau ky', 'Ton dau ki', 'Opening Balance', 'Quantity', 'So luong', 'SL']), importInPeriod: this.inferImportColumnIndex(headerRow, ['Nhap trong ky', 'Nhap trong ki', 'Nhap ky', 'Nhap']), exportInPeriod: this.inferImportColumnIndex(headerRow, ['Xuat trong ky', 'Xuat trong ki', 'Xuat ky', 'Xuat']), endingBalance: this.inferImportColumnIndex(headerRow, ['Ton cuoi ki', 'Ton cuoi ky', 'Ton cuoi', 'Ending Balance']), unit: this.inferImportColumnIndex(headerRow, ['Unit', 'Don vi', 'DVT']), department: this.inferImportColumnIndex(headerRow, ['Department', 'Bo phan', 'Phong ban']), project: this.inferImportColumnIndex(headerRow, ['Project', 'Du an', 'Cong trinh']), location: this.inferImportColumnIndex(headerRow, ['Location', 'Vi tri', 'Noi dat']), custodian: this.inferImportColumnIndex(headerRow, ['Custodian', 'Nguoi quan ly', 'Nguoi su dung']), purchaseDate: this.inferImportColumnIndex(headerRow, ['Purchase Date', 'Ngay mua', 'Ngay nhap', 'Ngay ve']), purchasePrice: this.inferImportColumnIndex(headerRow, ['Purchase Price', 'Gia mua', 'Don gia']), status: this.inferImportColumnIndex(headerRow, ['Status', 'Trang thai', 'Tinh trang']), notes: this.inferImportColumnIndex(headerRow, ['Notes', 'Ghi chu', 'Li do xuat', 'Ly do xuat']) }; // Fallback by relative offsets for common inventory sheets: // STT | Ngay ve | Ma vat tu | Ten linh kien/sp | Model | DVT | ... | Ton cuoi ki | Ly do xuat | Tinh trang | Vi tri | Du an if (indexMap.stt >= 0) { if (indexMap.purchaseDate < 0) indexMap.purchaseDate = indexMap.stt + 1; if (indexMap.assetCode < 0) indexMap.assetCode = indexMap.stt + 2; if (indexMap.assetName < 0) indexMap.assetName = indexMap.stt + 3; if (indexMap.model < 0) indexMap.model = indexMap.stt + 4; if (indexMap.unit < 0) indexMap.unit = indexMap.stt + 5; if (indexMap.quantity < 0) indexMap.quantity = indexMap.stt + 6; if (indexMap.importInPeriod < 0) indexMap.importInPeriod = indexMap.stt + 7; if (indexMap.exportInPeriod < 0) indexMap.exportInPeriod = indexMap.stt + 8; if (indexMap.endingBalance < 0) indexMap.endingBalance = indexMap.stt + 9; if (indexMap.notes < 0) indexMap.notes = indexMap.stt + 10; if (indexMap.status < 0) indexMap.status = indexMap.stt + 11; if (indexMap.location < 0) indexMap.location = indexMap.stt + 12; if (indexMap.project < 0) indexMap.project = indexMap.stt + 13; } return indexMap; } mapImportedAssetRowsByColumnIndex(matrixRows, headerRowIndex) { const headerRow = Array.isArray(matrixRows[headerRowIndex]) ? matrixRows[headerRowIndex] : []; if (!headerRow.length) { return []; } const indexMap = this.buildAssetImportIndexMap(headerRow); if (indexMap.assetCode < 0 && indexMap.assetName < 0 && indexMap.model < 0 && indexMap.stt < 0) { return []; } const pick = (row, index) => { if (index < 0 || !Array.isArray(row)) { return ''; } return row[index] ?? ''; }; return matrixRows .slice(headerRowIndex + 1) .filter(row => Array.isArray(row) && row.some(cell => String(cell ?? '').trim() !== '')) .map((row, rowOffset) => { const sttRaw = String(pick(row, indexMap.stt)).trim(); if (indexMap.stt >= 0) { const normalizedStt = sttRaw.replace(/\.$/, ''); if (!normalizedStt || Number.isNaN(Number(normalizedStt))) { return null; } } const endingBalance = this.parseImportNumericValue( pick(row, indexMap.endingBalance), 0 ); const mapped = { assetCode: String(pick(row, indexMap.assetCode)).trim(), assetName: String(pick(row, indexMap.assetName)).trim(), model: String(pick(row, indexMap.model)).trim(), serialNumber: String(pick(row, indexMap.serialNumber)).trim(), quantity: this.parseImportNumericValue(pick(row, indexMap.quantity), 0), importInPeriod: this.parseImportNumericValue(pick(row, indexMap.importInPeriod), 0), exportInPeriod: this.parseImportNumericValue(pick(row, indexMap.exportInPeriod), 0), endingBalance, unit: String(pick(row, indexMap.unit)).trim(), department: String(pick(row, indexMap.department)).trim(), project: String(pick(row, indexMap.project)).trim(), location: String(pick(row, indexMap.location)).trim(), custodian: String(pick(row, indexMap.custodian)).trim(), purchaseDate: pick(row, indexMap.purchaseDate), purchasePrice: pick(row, indexMap.purchasePrice), status: String(pick(row, indexMap.status)).trim(), notes: String(pick(row, indexMap.notes)).trim() }; return this.finalizeImportedAssetRow(mapped, headerRowIndex + rowOffset + 2); }) .filter(Boolean) .filter(row => !this.isLikelyHeaderArtifactAssetRow(row)) .filter(row => row.assetCode && row.assetName); } findBestAssetImportRowsFromMatrix(matrixRows) { const maxScanRows = Math.min(Array.isArray(matrixRows) ? matrixRows.length : 0, 60); let bestRows = []; for (let rowIndex = 0; rowIndex < maxScanRows; rowIndex += 1) { const row = Array.isArray(matrixRows[rowIndex]) ? matrixRows[rowIndex] : []; if (!this.isLikelyAssetHeaderRow(row)) { continue; } const candidateRows = this.mapImportedAssetRowsByColumnIndex(matrixRows, rowIndex); if (candidateRows.length > bestRows.length) { bestRows = candidateRows; } if (bestRows.length >= 10) { break; } } return bestRows; } mapImportedAssetRowsBySttPattern(matrixRows) { const rows = Array.isArray(matrixRows) ? matrixRows : []; const maxScanRows = Math.min(rows.length, 60); // Prefer dynamic header detection with STT, then parse by resolved column indexes. let bestRows = []; for (let rowIndex = 0; rowIndex < maxScanRows; rowIndex += 1) { const row = Array.isArray(rows[rowIndex]) ? rows[rowIndex] : []; const hasStt = this.hasImportAliasInRow(row, ['STT', 'So thu tu']); const hasName = this.hasImportAliasInRow(row, ['Asset Name', 'Ten tai san', 'Ten TS', 'Ten TSCD', 'Ten CCDC', 'Ten vat tu', 'Ten linh kien', 'Ten linh kien/sp', 'Ten linh kien sp', 'Ten sp', 'Name', 'Dien giai', 'Mo ta', 'Ten tai san/CCDC']); const hasModel = this.hasImportAliasInRow(row, ['Model', 'Dong may']); const hasQty = this.hasImportAliasInRow(row, ['Ton dau ky', 'Ton dau ki', 'Quantity', 'So luong', 'SL', 'Nhap trong ky', 'Nhap trong ki', 'Xuat trong ky', 'Xuat trong ki']); if (!hasStt || (!hasName && !hasModel && !hasQty)) { continue; } const candidateRows = this.mapImportedAssetRowsByColumnIndex(rows, rowIndex); if (candidateRows.length > bestRows.length) { bestRows = candidateRows; } } if (bestRows.length >= 3) { return bestRows; } // Last-resort fallback for shifted templates where columns are still in STT-order. let detectedSttCol = -1; for (let rowIndex = 0; rowIndex < maxScanRows; rowIndex += 1) { const row = Array.isArray(rows[rowIndex]) ? rows[rowIndex] : []; const sttCol = row.findIndex(cell => this.isImportHeaderMatch(cell, 'STT') || this.isImportHeaderMatch(cell, 'So thu tu')); if (sttCol >= 0) { detectedSttCol = sttCol; break; } } if (detectedSttCol < 0) { detectedSttCol = 0; } const sttDataRows = rows.filter(row => { if (!Array.isArray(row)) { return false; } const stt = String(row[detectedSttCol] ?? '').trim().replace(/\.$/, ''); if (!/^\d+$/.test(stt)) { return false; } const hasCoreValue = [2, 3, 4, 5, 9, 12] .map(offset => detectedSttCol + offset) .some(index => String(row[index] ?? '').trim() !== ''); return hasCoreValue; }); if (sttDataRows.length < 3) { return []; } return sttDataRows .map((row, idx) => { const endingBalance = this.parseImportNumericValue(row[detectedSttCol + 9] ?? '', 0); const mapped = { assetCode: String(row[detectedSttCol + 2] ?? '').trim() || String(row[detectedSttCol + 4] ?? '').trim(), assetName: String(row[detectedSttCol + 3] ?? '').trim() || String(row[detectedSttCol + 2] ?? '').trim() || String(row[detectedSttCol + 4] ?? '').trim(), model: String(row[detectedSttCol + 4] ?? '').trim(), serialNumber: '', quantity: this.parseImportNumericValue(row[detectedSttCol + 6] ?? '', 0), importInPeriod: this.parseImportNumericValue(row[detectedSttCol + 7] ?? '', 0), exportInPeriod: this.parseImportNumericValue(row[detectedSttCol + 8] ?? '', 0), endingBalance, unit: String(row[detectedSttCol + 5] ?? '').trim(), department: '', project: String(row[detectedSttCol + 13] ?? '').trim(), location: String(row[detectedSttCol + 12] ?? '').trim(), custodian: '', purchaseDate: row[detectedSttCol + 1] ?? '', purchasePrice: '', status: String(row[detectedSttCol + 11] ?? '').trim(), notes: String(row[detectedSttCol + 10] ?? '').trim() }; return this.finalizeImportedAssetRow(mapped, idx + 2); }) .filter(row => !this.isLikelyHeaderArtifactAssetRow(row)) .filter(row => row.assetCode && row.assetName); } sanitizeAssetCodeToken(value) { return String(value || '') .normalize('NFD') .replace(/[\u0300-\u036f]/g, '') .toUpperCase() .replace(/[^A-Z0-9]+/g, '-') .replace(/^-+|-+$/g, '') .slice(0, 40); } generateImportAssetCode(mapped, rowNumber = 0) { const fromModel = this.sanitizeAssetCodeToken(mapped.model); const fromSerial = this.sanitizeAssetCodeToken(mapped.serialNumber); const fromName = this.sanitizeAssetCodeToken(mapped.assetName); const base = fromModel || fromSerial || fromName || 'ASSET'; const suffix = String(rowNumber || 0).padStart(4, '0'); return `IMP-${base}-${suffix}`; } finalizeImportedAssetRow(mapped, rowNumber = 0) { const result = { ...mapped }; if (!result.assetName) { const fallbackName = String(result.model || result.serialNumber || result.assetCode || '').trim(); result.assetName = fallbackName; } if (!result.assetCode && result.assetName) { result.assetCode = this.generateImportAssetCode(result, rowNumber); } return result; } mapImportedAssetRow(row, rowNumber = 0) { const endingBalance = this.parseImportNumericValue( this.findImportValue(row, ['Ton cuoi ki', 'Ton cuoi ky', 'Ton cuoi', 'Ending Balance']), this.parseImportNumericValue(this.findImportValue(row, ['Quantity', 'So luong', 'SL']), 0) ); const mapped = { assetCode: String(this.findImportValue(row, ['Asset Code', 'Ma tai san', 'Ma TS', 'Ma TSCD', 'Ma vat tu', 'Ma VT', 'Ma linh kien', 'Code', 'SKU', 'Part Number', 'PN', 'So the', 'So hieu', 'Ma tai san/CCDC'])).trim(), assetName: String(this.findImportValue(row, ['Asset Name', 'Ten tai san', 'Ten TS', 'Ten TSCD', 'Ten CCDC', 'Ten vat tu', 'Ten linh kien', 'Ten linh kien/sp', 'Ten linh kien sp', 'Ten sp', 'Name', 'Dien giai', 'Mo ta', 'Ten tai san/CCDC'])).trim(), model: String(this.findImportValue(row, ['Model', 'Dong may'])).trim(), serialNumber: String(this.findImportValue(row, ['Serial Number', 'Serial', 'So serial', 'So seri'])).trim(), quantity: this.parseImportNumericValue(this.findImportValue(row, ['Ton dau ky', 'Ton dau ki', 'Opening Balance', 'Quantity', 'So luong', 'SL']), 0), importInPeriod: this.parseImportNumericValue(this.findImportValue(row, ['Nhap trong ky', 'Nhap trong ki', 'Nhap ky', 'Nhap']), 0), exportInPeriod: this.parseImportNumericValue(this.findImportValue(row, ['Xuat trong ky', 'Xuat trong ki', 'Xuat ky', 'Xuat']), 0), endingBalance, unit: String(this.findImportValue(row, ['Unit', 'Don vi', 'DVT'])).trim(), department: String(this.findImportValue(row, ['Department', 'Bo phan', 'Phong ban'])).trim(), project: String(this.findImportValue(row, ['Project', 'Du an', 'Cong trinh'])).trim(), location: String(this.findImportValue(row, ['Location', 'Vi tri', 'Noi dat'])).trim(), custodian: String(this.findImportValue(row, ['Custodian', 'Nguoi quan ly', 'Nguoi su dung'])).trim(), purchaseDate: this.findImportValue(row, ['Purchase Date', 'Ngay mua', 'Ngay nhap', 'Ngay ve']), purchasePrice: this.findImportValue(row, ['Purchase Price', 'Gia mua', 'Don gia']), status: String(this.findImportValue(row, ['Status', 'Trang thai', 'Tinh trang'])).trim(), notes: String(this.findImportValue(row, ['Notes', 'Ghi chu', 'Li do xuat', 'Ly do xuat'])).trim() }; const finalized = this.finalizeImportedAssetRow(mapped, rowNumber); return this.isLikelyHeaderArtifactAssetRow(finalized) ? null : finalized; } shouldFallbackToClientAssetImport(statusCode, message = '') { const normalizedMessage = String(message || '') .normalize('NFD') .replace(/[\u0300-\u036f]/g, '') .toLowerCase(); const parserErrorHints = [ 'khong tim thay dong du lieu hop le', 'khong tim thay dong hop le', 'khong tim thay dong', 'cannot parse import file', 'excel file does not contain a worksheet', 'import data is empty', 'no valid rows found' ]; const isParserRelatedError = parserErrorHints.some(hint => normalizedMessage.includes(hint)); return (statusCode === 400 || statusCode === 422) && isParserRelatedError; } async importAssetsByFileUpload(file) { if (!this.ensureAssetManagePermission('nhap du lieu tai san')) { return { uploaded: false, shouldFallback: false, message: 'No permission' }; } const formData = new FormData(); formData.append('file', file); try { const response = await fetch(`${this.apiBase}/assets/import`, { method: 'POST', headers: this.getAuthHeaders(false), body: formData }); let data = null; try { data = await response.json(); } catch (parseErr) { data = null; } if (!response.ok || !data?.success) { const message = data?.message || 'Nhập Excel thất bại'; const shouldFallback = this.shouldFallbackToClientAssetImport(response.status, message); console.warn('Asset file-upload import failed', { status: response.status, message, diagnostics: data?.diagnostics || null, shouldFallback }); if (!shouldFallback) { this.notifyFailure(message); } return { uploaded: false, shouldFallback, message }; } this.notifySuccess(data.message || 'Nhập Excel thành công'); await this.refreshAssetsUI(); return { uploaded: true, shouldFallback: false }; } catch (err) { console.warn('Asset file-upload import network error, fallback to client parser', err); return { uploaded: false, shouldFallback: true }; } } async processAssetImportFile(event) { if (!this.ensureAssetManagePermission('nhap du lieu tai san')) { event.target.value = ''; return; } const file = event.target?.files?.[0]; if (!file) { return; } try { const uploadResult = await this.importAssetsByFileUpload(file); if (uploadResult.uploaded || !uploadResult.shouldFallback) { event.target.value = ''; return; } } catch (uploadErr) { // Ignore and continue with client-side parser fallback. } if (!window.XLSX) { this.notifyFailure('Chưa tải được thư viện xử lý Excel'); event.target.value = ''; return; } try { const buffer = await file.arrayBuffer(); const workbook = window.XLSX.read(buffer, { type: 'array' }); const sheetNames = Array.isArray(workbook.SheetNames) ? workbook.SheetNames : []; if (!sheetNames.length) { this.notifyFailure('Tệp Excel không có sheet dữ liệu'); return; } let mappedRows = []; let debugSheetName = ''; let debugHeaderRowIndex = -1; let debugMatrixRows = []; for (const sheetName of sheetNames) { const sheet = workbook.Sheets[sheetName]; if (!sheet) { continue; } const matrixRows = window.XLSX.utils.sheet_to_json(sheet, { header: 1, defval: '', raw: false }); const headerRowIndex = this.findAssetImportHeaderRowIndex(matrixRows); if (!debugMatrixRows.length) { debugSheetName = sheetName; debugHeaderRowIndex = headerRowIndex; debugMatrixRows = matrixRows; } let candidateRows = []; if (headerRowIndex >= 0) { candidateRows = this.mapImportedAssetRowsFromMatrix(matrixRows, headerRowIndex); if (!candidateRows.length) { candidateRows = this.mapImportedAssetRowsByColumnIndex(matrixRows, headerRowIndex); } } const bestRowsFromMatrix = this.findBestAssetImportRowsFromMatrix(matrixRows); if (bestRowsFromMatrix.length > candidateRows.length) { candidateRows = bestRowsFromMatrix; } const sttPatternRows = this.mapImportedAssetRowsBySttPattern(matrixRows); if (sttPatternRows.length > candidateRows.length) { candidateRows = sttPatternRows; console.info('Asset import switched to STT-pattern parser', { sheetName, parsedRows: sttPatternRows.length }); } if (candidateRows.length > mappedRows.length) { mappedRows = candidateRows; debugSheetName = sheetName; debugHeaderRowIndex = headerRowIndex; debugMatrixRows = matrixRows; } } if (!mappedRows.length) { const headerPreview = debugMatrixRows .slice(0, 8) .map((row, rowIndex) => ({ rowIndex, values: (Array.isArray(row) ? row : []).slice(0, 14).map(cell => String(cell ?? '').trim()) })) .filter(item => item.values.some(value => value)); console.warn('Asset import parser could not find valid rows', { sheetName: debugSheetName || sheetNames[0] || '', headerRowIndex: debugHeaderRowIndex, headerPreview }); this.notifyWarning('Không tìm thấy dòng hợp lệ. Vui lòng kiểm tra dòng tiêu đề có cột mã/tên tài sản hoặc mã/tên vật tư.'); return; } const response = await fetch(`${this.apiBase}/assets/import`, { method: 'POST', headers: this.getAuthHeaders(true), body: JSON.stringify({ rows: mappedRows }) }); const data = await response.json(); if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Nhập Excel thất bại'); return; } this.notifySuccess(data.message || 'Nhập Excel thành công'); await this.refreshAssetsUI(); } catch (err) { console.error(err); this.notifyFailure('Nhập Excel thất bại'); } finally { event.target.value = ''; } } exportAssetsToExcel() { if (!window.XLSX) { this.notifyFailure('Chưa tải được thư viện xử lý Excel'); return; } const exportRows = this.assets.map(asset => ({ 'Asset Code': asset.AssetCode || '', 'Asset Name': asset.AssetName || '', 'Model': asset.Model || '', 'Serial Number': asset.SerialNumber || '', 'Quantity': asset.Quantity || 0, 'Tổng tài sản': this.buildAssetQuantityMetrics(asset).quantity + this.buildAssetQuantityMetrics(asset).importInPeriod, 'Import In Period': asset.ImportInPeriod ?? 0, 'Export In Period': asset.ExportInPeriod ?? 0, 'Ending Balance': asset.EndingBalance ?? 0, 'Unit': asset.Unit || '', 'Department': asset.Department || '', 'Project': asset.Project || '', 'Dự án đang xuất / Số lượng': this.formatAssetProjectsText(asset), 'Location': asset.Location || '', 'Custodian': asset.Custodian || '', 'Borrower': asset.Borrower || '', 'Exported By': asset.ExportedBy || '', 'Purchase Date': this.toDateInputValue(asset.PurchaseDate), 'Purchase Price': asset.PurchasePrice || '', 'Status': asset.Status || '', 'Notes': asset.Notes || '' })); const worksheet = window.XLSX.utils.json_to_sheet(exportRows); const workbook = window.XLSX.utils.book_new(); window.XLSX.utils.book_append_sheet(workbook, worksheet, 'TaiSan'); const timestamp = this.formatTimestampForCode(new Date()).slice(0, 8); window.XLSX.writeFile(workbook, `danh-sach-tai-san-${timestamp}.xlsx`); this.notifySuccess('Xuất Excel thành công'); } renderAccountsTableBody() { const tbody = document.querySelector('.accounts-table-body'); if (!tbody) return; const currentUserId = this.getUserId(); const pageInfo = this.getPaged(this.getFilteredAccounts(), this.accountPage, this.accountPageSize); this.accountPage = pageInfo.current; tbody.innerHTML = pageInfo.data.map(acc => { const isOwnAccount = acc.UserId == currentUserId; const canAccessAccount = isOwnAccount || this.getCurrentUserRole() === 'admin'; const accountUsername = acc.AccountUsername || '-'; const displayAccountUsername = canAccessAccount ? accountUsername : this.maskForeignAccountUsername(accountUsername); const createdDate = this.formatDateTime(acc.CreatedDate); const updatedDate = this.formatDateTime(acc.UpdatedDate); const actionContent = canAccessAccount ? ` ` : '-'; return ` ${acc.Email || '-'} ${displayAccountUsername} ${acc.AppName || '-'} ${createdDate} ${updatedDate} ${actionContent} `; }).join(''); const pager = document.getElementById('accountsPager'); if (pager) { pager.innerHTML = ` Showing ${pageInfo.start}-${pageInfo.end} of ${pageInfo.total}
Page ${pageInfo.current} / ${pageInfo.totalPages}
`; } this.setupAccountRowListeners(); this.setupAccountPagerListeners(); } renderApplicationsTableBody() { const tbody = document.querySelector('.apps-table-body'); if (!tbody) return; const pageInfo = this.getPaged(this.getFilteredApplications(), this.appPage, this.appPageSize); this.appPage = pageInfo.current; tbody.innerHTML = pageInfo.data.map(app => `
${app.Icon || 'apps'}
${app.Name}
${app.Type} ${app.Description || '-'} ${(app.Url || app.url) ? `${app.Url || app.url}` : '-'}
${(app.Status || app.status) === 'online' ? 'Online' : 'Offline'}
`).join(''); const pager = document.getElementById('appsPager'); if (pager) { pager.innerHTML = ` Showing ${pageInfo.start}-${pageInfo.end} of ${pageInfo.total}
Page ${pageInfo.current} / ${pageInfo.totalPages}
`; } this.setupAccountRowListeners(); this.setupAppPagerListeners(); } setupAccountPagerListeners() { document.querySelectorAll('.account-page-btn').forEach(btn => { btn.addEventListener('click', () => { const targetPage = Number(btn.dataset.page); if (!targetPage || targetPage < 1) return; this.accountPage = targetPage; this.renderAccountsTableBody(); }); }); } setupAppPagerListeners() { document.querySelectorAll('.app-page-btn').forEach(btn => { btn.addEventListener('click', () => { const targetPage = Number(btn.dataset.page); if (!targetPage || targetPage < 1) return; this.appPage = targetPage; this.renderApplicationsTableBody(); }); }); } setupAccountRowListeners() { // View Account listeners document.querySelectorAll('.view-account').forEach(btn => { btn.addEventListener('click', (e) => { if (btn.disabled) return; // Only view own accounts const accountId = Number(btn.dataset.accountId); const account = this.accounts.find(a => a.AccountId === accountId); this.currentViewAccountId = accountId; this.currentViewAccount = account; document.getElementById('viewAccountService').textContent = account?.AppName || '-'; document.getElementById('viewAccountOwner').textContent = account?.Email || '-'; document.getElementById('viewAccountUsername').textContent = account?.AccountUsername || '-'; const passwordEl = document.getElementById('viewAccountPassword'); const toggleBtn = document.querySelector('.toggle-password'); const toggleIcon = document.getElementById('toggleIcon'); passwordEl.dataset.password = ''; passwordEl.dataset.loaded = 'false'; passwordEl.textContent = account?.PasswordAvailable ? '********' : '(no password stored)'; passwordEl.dataset.visible = 'false'; if (toggleIcon) toggleIcon.textContent = 'visibility'; // Rebind toggle each time modal opens to keep state fresh if (toggleBtn) { toggleBtn.disabled = !account?.PasswordAvailable; toggleBtn.onclick = async () => { if (passwordEl.dataset.loaded !== 'true') { try { toggleBtn.disabled = true; const password = await this.fetchAccountSecret(accountId); passwordEl.dataset.password = password; passwordEl.dataset.loaded = 'true'; this.currentViewAccount = { ...account, AccountPassword: password }; } catch (error) { this.notifyFailure(error.message || 'Unable to reveal stored credential'); return; } finally { toggleBtn.disabled = false; } } const currentPwd = passwordEl.dataset.password || ''; const isVisible = passwordEl.dataset.visible === 'true'; if (isVisible) { passwordEl.textContent = currentPwd ? '********' : '(no password stored)'; passwordEl.dataset.visible = 'false'; if (toggleIcon) toggleIcon.textContent = 'visibility'; } else { passwordEl.textContent = currentPwd || '(no password stored)'; passwordEl.dataset.visible = 'true'; if (toggleIcon) toggleIcon.textContent = 'visibility_off'; } }; } document.getElementById('viewAccountModal').classList.add('open'); }); }); // Delete Account listeners - show confirmation modal document.querySelectorAll('.delete-account').forEach(btn => { btn.addEventListener('click', (e) => { if (btn.disabled) return; // Don't delete others' accounts const accountId = Number(btn.dataset.accountId); const account = this.accounts.find(a => a.AccountId === accountId); this.pendingDeleteAccountId = accountId; document.getElementById('deleteAccountUsername').textContent = account?.AccountUsername || ''; document.getElementById('deleteAccountModal').classList.add('open'); }); }); // Confirm Delete Account document.querySelectorAll('.confirm-delete-account').forEach(btn => { btn.addEventListener('click', () => { if (this.pendingDeleteAccountId !== undefined) { fetch(`${this.apiBase}/accounts/${this.pendingDeleteAccountId}`, { method: 'DELETE' }) .then(res => res.json()) .then(data => { if (data.success) { this.notifySuccess('Account deleted successfully'); this.closeModals(); this.refreshAccountsUI(); } else { this.notifyFailure(data.message || 'Delete account failed'); } }) .catch(err => { console.error(err); this.notifyFailure('Delete account failed'); }); } }); }); // Edit Account listeners document.querySelectorAll('.edit-account').forEach(btn => { btn.addEventListener('click', async (e) => { if (btn.disabled) return; // Don't edit others' accounts const accountId = Number(btn.dataset.accountId); const account = this.accounts.find(a => a.AccountId === accountId); let accountPassword = ''; try { accountPassword = account?.PasswordAvailable ? await this.fetchAccountSecret(accountId) : ''; } catch (error) { this.notifyFailure(error.message || 'Unable to load stored credential'); return; } // Populate form with existing data const form = document.getElementById('accountForm'); if (form) { const userInput = form.querySelector('#accountUsername'); const passInput = form.querySelector('#accountPassword'); const ownerInput = form.querySelector('#accountOwner'); const serviceSelect = form.querySelector('#accountService'); if (userInput) userInput.value = account?.AccountUsername || ''; if (passInput) passInput.value = accountPassword; if (ownerInput) ownerInput.value = this.currentUser?.Username || this.currentUser?.username || ''; if (serviceSelect) serviceSelect.value = account?.AppId || ''; } this.pendingAccountAppId = account?.AppId; this.editingAccountId = account?.AccountId; this.closeModals(); this.openAccountModal(); }); }); // Edit from View modal document.querySelectorAll('.edit-account-from-view').forEach(btn => { btn.addEventListener('click', async () => { let account = this.currentViewAccount; if (account?.PasswordAvailable && !account?.AccountPassword) { try { const password = await this.fetchAccountSecret(account.AccountId); account = { ...account, AccountPassword: password }; this.currentViewAccount = account; } catch (error) { this.notifyFailure(error.message || 'Unable to load stored credential'); return; } } const form = document.getElementById('accountForm'); if (form) { const userInput = form.querySelector('#accountUsername'); const passInput = form.querySelector('#accountPassword'); const ownerInput = form.querySelector('#accountOwner'); const serviceSelect = form.querySelector('#accountService'); if (userInput) userInput.value = account?.AccountUsername || ''; if (passInput) passInput.value = account?.AccountPassword || ''; if (ownerInput) ownerInput.value = this.currentUser?.Username || this.currentUser?.username || ''; if (serviceSelect) serviceSelect.value = account?.AppId || ''; } this.pendingAccountAppId = account?.AppId; this.editingAccountId = account?.AccountId; this.closeModals(); this.openAccountModal(); }); }); // View App listeners document.querySelectorAll('.view-app').forEach(btn => { btn.addEventListener('click', (e) => { const appId = Number(btn.dataset.appId); const app = this.applications.find(a => a.AppId === appId); this.currentViewAppId = appId; document.getElementById('viewAppName').textContent = app?.Name || '-'; document.getElementById('viewAppType').textContent = app?.Type || '-'; const iconVal = app?.Icon || app?.icon || 'apps'; const iconSymbolEl = document.getElementById('viewAppIconSymbol'); const iconNameEl = document.getElementById('viewAppIconName'); if (iconSymbolEl) iconSymbolEl.textContent = iconVal; if (iconNameEl) iconNameEl.textContent = iconVal; document.getElementById('viewAppDescription').textContent = app?.Description || '-'; const urlEl = document.getElementById('viewAppUrl'); const urlVal = app?.Url || app?.url; if (urlEl) { if (urlVal) { try { const parsedUrl = new URL(urlVal); if (!['http:', 'https:'].includes(parsedUrl.protocol)) { throw new Error('Unsupported URL protocol'); } const link = document.createElement('a'); link.href = parsedUrl.toString(); link.target = '_blank'; link.rel = 'noopener noreferrer'; link.className = 'text-primary underline'; link.textContent = urlVal; urlEl.replaceChildren(link); } catch (error) { urlEl.textContent = '-'; } } else { urlEl.textContent = '-'; } } const statusValue = app?.Status || app?.status; document.getElementById('viewAppStatus').textContent = statusValue === 'online' ? 'Online' : 'Offline'; document.getElementById('viewAppModal').classList.add('open'); }); }); // Delete App listeners - show confirmation modal document.querySelectorAll('.delete-app').forEach(btn => { btn.addEventListener('click', (e) => { const appId = Number(btn.dataset.appId); const app = this.applications.find(a => a.AppId === appId); this.pendingDeleteAppId = appId; document.getElementById('deleteAppName').textContent = app?.Name || ''; document.getElementById('deleteAppModal').classList.add('open'); }); }); // Confirm Delete App document.querySelectorAll('.confirm-delete-app').forEach(btn => { btn.addEventListener('click', () => { if (this.pendingDeleteAppId !== undefined) { fetch(`${this.apiBase}/applications/${this.pendingDeleteAppId}`, { method: 'DELETE' }) .then(res => res.json()) .then(data => { if (data.success) { this.notifySuccess('Application deleted successfully'); this.closeModals(); this.refreshApplicationsUI(); } else { this.notifyFailure(data.message || 'Delete application failed'); } }) .catch(err => { console.error(err); this.notifyFailure('Delete application failed'); }); } }); }); // Edit App listeners document.querySelectorAll('.edit-app').forEach(btn => { btn.addEventListener('click', (e) => { const appId = Number(btn.dataset.appId); const app = this.applications.find(a => a.AppId === appId); document.getElementById('appName').value = app?.Name || ''; document.getElementById('appType').value = app?.Type || ''; document.getElementById('appStatus').value = app?.Status || 'online'; document.getElementById('appDescription').value = app?.Description || ''; document.getElementById('appIcon').value = app?.Icon || app?.icon || ''; document.getElementById('appUrl').value = app?.Url || app?.url || ''; this.editingAppId = app?.AppId; this.closeModals(); this.openAppModal(); }); }); // Edit App from View modal document.querySelectorAll('.edit-app-from-view').forEach(btn => { btn.addEventListener('click', () => { const appId = this.currentViewAppId; const app = this.applications.find(a => a.AppId === appId); document.getElementById('appName').value = app?.Name || ''; document.getElementById('appType').value = app?.Type || ''; document.getElementById('appStatus').value = app?.Status || 'online'; document.getElementById('appDescription').value = app?.Description || ''; document.getElementById('appIcon').value = app?.Icon || app?.icon || ''; document.getElementById('appUrl').value = app?.Url || ''; this.editingAppId = app?.AppId; this.closeModals(); this.openAppModal(); }); }); } setupAddButtonListeners() { // Add Account button document.querySelectorAll('#addAccountBtn').forEach(btn => { btn.addEventListener('click', () => { this.editingAccountId = undefined; this.pendingAccountAppId = undefined; this.openAccountModal(); }); }); // Add Application button document.querySelectorAll('#addAppBtn').forEach(btn => { btn.addEventListener('click', () => { this.editingAppId = undefined; this.openAppModal(); }); }); // Add Asset button document.querySelectorAll('#addAssetBtn').forEach(btn => { btn.addEventListener('click', () => { this.editingAssetId = undefined; this.openAssetModal(); }); }); document.querySelectorAll('#addConsumableBtn').forEach(btn => { if (btn.dataset.boundClick === 'true') { return; } btn.addEventListener('click', () => { this.editingConsumableId = undefined; this.openConsumableModal(); }); btn.dataset.boundClick = 'true'; }); const createConsumableBorrowRequestBtn = document.getElementById('createConsumableBorrowRequestBtn'); if (createConsumableBorrowRequestBtn && !createConsumableBorrowRequestBtn.dataset.boundClick) { createConsumableBorrowRequestBtn.addEventListener('click', () => this.openConsumableBorrowRequestModal()); createConsumableBorrowRequestBtn.dataset.boundClick = 'true'; } const openConsumableBorrowRequestsBtn = document.getElementById('openConsumableBorrowRequestsBtn'); if (openConsumableBorrowRequestsBtn && !openConsumableBorrowRequestsBtn.dataset.boundClick) { openConsumableBorrowRequestsBtn.addEventListener('click', () => this.openConsumableBorrowRequestsModal()); openConsumableBorrowRequestsBtn.dataset.boundClick = 'true'; } const openRejectedConsumableReturnRequestsBtn = document.getElementById('openRejectedConsumableReturnRequestsBtn'); if (openRejectedConsumableReturnRequestsBtn && !openRejectedConsumableReturnRequestsBtn.dataset.boundClick) { openRejectedConsumableReturnRequestsBtn.addEventListener('click', () => this.openConsumableBorrowRequestsModal()); openRejectedConsumableReturnRequestsBtn.dataset.boundClick = 'true'; } const addAssetDepartmentBtn = document.getElementById('addAssetDepartmentBtn'); if (addAssetDepartmentBtn && !addAssetDepartmentBtn.dataset.boundClick) { addAssetDepartmentBtn.addEventListener('click', () => this.handleCreateAssetDepartment()); addAssetDepartmentBtn.dataset.boundClick = 'true'; } const addAssetProjectBtn = document.getElementById('addAssetProjectBtn'); if (addAssetProjectBtn && !addAssetProjectBtn.dataset.boundClick) { addAssetProjectBtn.addEventListener('click', () => this.handleCreateAssetProject()); addAssetProjectBtn.dataset.boundClick = 'true'; } const addAssetBorrowRequestBtn = document.getElementById('addAssetBorrowRequestBtn'); if (addAssetBorrowRequestBtn && !addAssetBorrowRequestBtn.dataset.boundClick) { addAssetBorrowRequestBtn.addEventListener('click', () => this.openAssetBorrowRequestModal('borrow')); addAssetBorrowRequestBtn.dataset.boundClick = 'true'; } const addAssetReturnRequestBtn = document.getElementById('addAssetReturnRequestBtn'); if (addAssetReturnRequestBtn && !addAssetReturnRequestBtn.dataset.boundClick) { addAssetReturnRequestBtn.addEventListener('click', () => this.openAssetBorrowRequestModal('return')); addAssetReturnRequestBtn.dataset.boundClick = 'true'; } const openPendingAssetBorrowsBtn = document.getElementById('openPendingAssetBorrowsBtn'); if (openPendingAssetBorrowsBtn && !openPendingAssetBorrowsBtn.dataset.boundClick) { openPendingAssetBorrowsBtn.addEventListener('click', () => this.openPendingAssetRequestsModal()); openPendingAssetBorrowsBtn.dataset.boundClick = 'true'; } const borrowAssetBtn = document.getElementById('borrowAssetBtn'); if (borrowAssetBtn && !borrowAssetBtn.dataset.boundClick) { borrowAssetBtn.addEventListener('click', () => this.openBorrowAssetModal()); borrowAssetBtn.dataset.boundClick = 'true'; } const exportForOthersButton = document.getElementById('exportAssetForOthersBtn'); if (exportForOthersButton && !exportForOthersButton.dataset.boundClick) { exportForOthersButton.addEventListener('click', () => this.openBorrowAssetModal(true)); exportForOthersButton.dataset.boundClick = 'true'; } const damageAssetBtn = document.getElementById('damageAssetBtn'); if (damageAssetBtn && !damageAssetBtn.dataset.boundClick) { damageAssetBtn.addEventListener('click', () => this.openAssetDamageModal()); damageAssetBtn.dataset.boundClick = 'true'; } const importAssetBtn = document.getElementById('importAssetBtn'); const assetImportInput = document.getElementById('assetImportInput'); const exportAssetBtn = document.getElementById('exportAssetBtn'); const importConsumableBtn = document.getElementById('importConsumableBtn'); const consumableImportInput = document.getElementById('consumableImportInput'); const exportConsumableBtn = document.getElementById('exportConsumableBtn'); const openConsumableExportHistoryBtn = document.getElementById('openConsumableExportHistoryBtn'); const refreshConsumableExportHistoryPageBtn = document.getElementById('refreshConsumableExportHistoryPageBtn'); const openAssetExportHistoryBtn = document.getElementById('openAssetExportHistoryBtn'); const openAssetDamageHistoryBtn = document.getElementById('openAssetDamageHistoryBtn'); if (importAssetBtn && assetImportInput && !importAssetBtn.dataset.boundClick) { importAssetBtn.addEventListener('click', () => { if (!this.ensureAssetManagePermission('nhap du lieu tai san')) { return; } assetImportInput.click(); }); importAssetBtn.dataset.boundClick = 'true'; } if (assetImportInput && !assetImportInput.dataset.boundChange) { assetImportInput.addEventListener('change', (event) => this.processAssetImportFile(event)); assetImportInput.dataset.boundChange = 'true'; } if (exportAssetBtn && !exportAssetBtn.dataset.boundClick) { exportAssetBtn.addEventListener('click', () => this.exportAssetsToExcel()); exportAssetBtn.dataset.boundClick = 'true'; } if (importConsumableBtn && consumableImportInput && !importConsumableBtn.dataset.boundClick) { importConsumableBtn.addEventListener('click', () => { if (!this.ensureAssetManagePermission('nhap du lieu vat tu tieu hao')) { return; } consumableImportInput.click(); }); importConsumableBtn.dataset.boundClick = 'true'; } if (consumableImportInput && !consumableImportInput.dataset.boundChange) { consumableImportInput.addEventListener('change', (event) => this.processConsumableImportFile(event)); consumableImportInput.dataset.boundChange = 'true'; } if (exportConsumableBtn && !exportConsumableBtn.dataset.boundClick) { exportConsumableBtn.addEventListener('click', () => this.exportConsumablesToExcel()); exportConsumableBtn.dataset.boundClick = 'true'; } if (openConsumableExportHistoryBtn && !openConsumableExportHistoryBtn.dataset.boundClick) { openConsumableExportHistoryBtn.addEventListener('click', () => this.openConsumableExportHistoryModal()); openConsumableExportHistoryBtn.dataset.boundClick = 'true'; } if (refreshConsumableExportHistoryPageBtn && !refreshConsumableExportHistoryPageBtn.dataset.boundClick) { refreshConsumableExportHistoryPageBtn.addEventListener('click', () => this.refreshConsumableExportsPage()); refreshConsumableExportHistoryPageBtn.dataset.boundClick = 'true'; } if (openAssetExportHistoryBtn && !openAssetExportHistoryBtn.dataset.boundClick) { openAssetExportHistoryBtn.addEventListener('click', () => this.openAssetExportsPage()); openAssetExportHistoryBtn.dataset.boundClick = 'true'; } if (openAssetDamageHistoryBtn && !openAssetDamageHistoryBtn.dataset.boundClick) { openAssetDamageHistoryBtn.addEventListener('click', () => this.openAssetDamageHistoryModal()); openAssetDamageHistoryBtn.dataset.boundClick = 'true'; } } setupFilters() { const serviceFilter = document.getElementById('serviceFilter'); if (serviceFilter) { serviceFilter.value = this.accountServiceFilter || ''; serviceFilter.addEventListener('change', (e) => { this.accountServiceFilter = e.target.value; this.renderAccountsTableBody(); }); } const accountSearch = document.getElementById('accountSearch'); if (accountSearch) { accountSearch.value = this.accountSearchTerm; const handleAccountSearch = event => { this.accountSearchTerm = event.target.value.toLowerCase(); this.renderAccountsTableBody(); }; accountSearch.addEventListener('input', handleAccountSearch); // Restore focus/selection after renders to avoid typing interruptions accountSearch.addEventListener('focus', () => { accountSearch.dataset.focused = 'true'; }); accountSearch.addEventListener('blur', () => { accountSearch.dataset.focused = 'false'; }); } const appSearch = document.getElementById('appSearch'); if (appSearch) { appSearch.value = this.applicationSearchTerm; const handleApplicationSearch = event => { this.applicationSearchTerm = event.target.value.toLowerCase(); this.renderApplicationsTableBody(); }; appSearch.addEventListener('input', handleApplicationSearch); // Restore focus/selection after renders to avoid typing interruptions appSearch.addEventListener('focus', () => { appSearch.dataset.focused = 'true'; }); appSearch.addEventListener('blur', () => { appSearch.dataset.focused = 'false'; }); } const assetStatusFilter = document.getElementById('assetStatusFilter'); if (assetStatusFilter) { assetStatusFilter.value = this.assetStatusFilter || ''; assetStatusFilter.addEventListener('change', (e) => { this.assetStatusFilter = String(e.target.value || '').toLowerCase(); this.assetPage = 1; this.renderAssetsTableBody(); }); } const assetSearch = document.getElementById('assetSearch'); if (assetSearch) { assetSearch.value = this.assetSearchTerm; const handleAssetSearch = event => { this.assetSearchTerm = event.target.value.toLowerCase(); this.assetPage = 1; this.renderAssetsTableBody(); }; assetSearch.addEventListener('input', handleAssetSearch); assetSearch.addEventListener('focus', () => { assetSearch.dataset.focused = 'true'; }); assetSearch.addEventListener('blur', () => { assetSearch.dataset.focused = 'false'; }); } const consumableMonthFilter = document.getElementById('consumableMonthFilter'); if (consumableMonthFilter) { consumableMonthFilter.value = this.consumableMonthFilter || ''; consumableMonthFilter.addEventListener('change', (e) => { this.consumableMonthFilter = String(e.target.value || ''); this.consumablePage = 1; this.renderConsumablesTableBody(); }); } const consumableStatusFilter = document.getElementById('consumableStatusFilter'); if (consumableStatusFilter) { consumableStatusFilter.value = this.consumableStatusFilter || ''; consumableStatusFilter.addEventListener('change', (e) => { this.consumableStatusFilter = String(e.target.value || ''); this.consumablePage = 1; this.renderConsumablesTableBody(); }); } const consumableSearch = document.getElementById('consumableSearch'); if (consumableSearch) { consumableSearch.value = this.consumableSearchTerm; const handleConsumableSearch = event => { this.consumableSearchTerm = event.target.value.toLowerCase(); this.consumablePage = 1; this.renderConsumablesTableBody(); }; consumableSearch.addEventListener('input', handleConsumableSearch); consumableSearch.addEventListener('focus', () => { consumableSearch.dataset.focused = 'true'; }); consumableSearch.addEventListener('blur', () => { consumableSearch.dataset.focused = 'false'; }); } } async handleAccountSubmit(e) { e.preventDefault(); const accountForm = document.getElementById('accountForm'); const userId = this.getUserId(); const appId = Number(accountForm?.querySelector('#accountService')?.value || 0); const accountUsername = (accountForm?.querySelector('#accountUsername')?.value || '').trim(); const accountPassword = (accountForm?.querySelector('#accountPassword')?.value || '').trim(); const accountEmail = ((accountForm?.querySelector('#accountOwner')?.value || '').trim()) || this.currentUser?.Username || this.currentUser?.username || ''; if (!accountForm) { this.notifyFailure('Account form not found.'); return; } if (!userId) { this.notifyFailure('User is not authenticated. Please login again.'); return; } if (!appId) { this.notifyWarning('Please select a service.'); return; } if (!accountUsername) { this.notifyWarning('Please enter a username.'); return; } if (!accountPassword) { this.notifyWarning('Please enter a password.'); return; } const payload = { userId, appId, accountUsername, accountPassword, email: accountEmail, accessLevel: 'user', notes: '' }; const isEdit = this.editingAccountId !== undefined; const url = isEdit ? `${this.apiBase}/accounts/${this.editingAccountId}` : `${this.apiBase}/accounts`; const method = isEdit ? 'PUT' : 'POST'; fetch(url, { method, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }).then(res => res.json()).then(data => { if (data.success) { this.editingAccountId = undefined; this.pendingAccountAppId = undefined; this.notifySuccess(isEdit ? 'Account updated successfully' : 'Account created successfully'); this.closeModals(); this.refreshAccountsUI(); } else { this.notifyFailure(data.message || 'Save account failed'); } }).catch(err => { console.error(err); this.notifyFailure('Save account failed'); }); } async refreshAccountsUI() { await this.fetchAccounts(); if (this.currentPage === 'accounts') { this.renderView('accounts'); } } async handleAppSubmit(e) { e.preventDefault(); const payload = { name: document.getElementById('appName').value, type: document.getElementById('appType').value, status: document.getElementById('appStatus').value, icon: (document.getElementById('appIcon')?.value || 'apps').trim() || 'apps', description: document.getElementById('appDescription')?.value || '', url: (document.getElementById('appUrl')?.value || '').trim() }; const isEdit = this.editingAppId !== undefined; const url = isEdit ? `${this.apiBase}/applications/${this.editingAppId}` : `${this.apiBase}/applications`; const method = isEdit ? 'PUT' : 'POST'; fetch(url, { method, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }).then(res => res.json()).then(data => { if (data.success) { this.editingAppId = undefined; this.notifySuccess(isEdit ? 'Application updated successfully' : 'Application created successfully'); this.closeModals(); this.refreshApplicationsUI(); } else { this.notifyFailure(data.message || 'Save application failed'); } }).catch(err => { console.error(err); this.notifyFailure('Save application failed'); }); } async refreshApplicationsUI() { await this.fetchApplications(); if (this.currentPage === 'applications') { this.renderView('applications'); } } openAccountModal() { // Refresh service options so newly added applications appear const serviceSelect = document.getElementById('accountService'); if (serviceSelect) { serviceSelect.innerHTML = `` + this.applications.map(app => ``).join(''); if (this.editingAccountId !== undefined && this.pendingAccountAppId) { serviceSelect.value = this.pendingAccountAppId; } } if (this.editingAccountId === undefined) { const form = document.getElementById('accountForm'); if (form) { const serviceSelect = form.querySelector('#accountService'); const ownerInput = form.querySelector('#accountOwner'); const userInput = form.querySelector('#accountUsername'); const passInput = form.querySelector('#accountPassword'); if (serviceSelect) serviceSelect.value = ''; if (ownerInput) ownerInput.value = this.currentUser?.Username || this.currentUser?.username || ''; if (userInput) userInput.value = ''; if (passInput) passInput.value = ''; } } document.getElementById('accountModal').classList.add('open'); } openAppModal() { if (this.editingAppId === undefined) { document.getElementById('appName').value = ''; document.getElementById('appType').value = ''; document.getElementById('appStatus').value = 'online'; const iconInput = document.getElementById('appIcon'); const desc = document.getElementById('appDescription'); const url = document.getElementById('appUrl'); if (iconInput) iconInput.value = ''; if (desc) desc.value = ''; if (url) url.value = ''; } document.getElementById('appModal').classList.add('open'); } setupModalCloseListeners() { if (this.modalCloseListenersBound) return; // Modals are loaded asynchronously and some forms are recreated on each open. // Delegate clicks so both work without inline handlers blocked by CSP. document.addEventListener('click', event => { const closeButton = event.target.closest?.('[data-close-modal]'); if (closeButton) { event.preventDefault(); const modalId = closeButton.dataset.closeModal || closeButton.closest('.modal-backdrop')?.id; if (modalId) this.closeModal(modalId); return; } if (event.target.classList?.contains('modal-backdrop')) { this.closeModal(event.target.id); } }); this.modalCloseListenersBound = true; } closeModal(modalId) { // These dialogs also own temporary state, child dialogs, or pending promises. const closeHandlers = { consumableBorrowRequestModal: closeConsumableBorrowRequestModal, consumableBorrowRequestsModal: closeConsumableBorrowRequestsModal, consumableRequestRejectModal: closeConsumableRequestRejectModal, consumableReturnModal: closeConsumableReturnModal, bulkDeleteAssetsConfirmModal: closeBulkDeleteAssetsConfirmModal, assetBorrowRequestModal: closeAssetBorrowRequestModal, assetPendingRequestsModal: closeAssetPendingRequestsModal, assetRequestDeleteConfirmModal: closeAssetRequestDeleteConfirmModal, userModal: closeUserModal, roleModal: closeRoleModal, userDetailsModal: closeUserDetailsModal, profileModal: closeProfileModal, notificationComposerModal: () => document.getElementById('notificationComposerModal')?.remove() }; if (Object.hasOwn(closeHandlers, modalId)) { closeHandlers[modalId](); } else { document.getElementById(modalId)?.classList.remove('open'); } } closeModals() { if (this.pendingAssetRequestDeleteConfirmResolver) { this.resolveAssetRequestDeleteConfirm(false); } if (this.pendingBulkAssetDeleteConfirmResolver) { this.resolveBulkAssetDeleteConfirm(false); } document.querySelectorAll('.modal-backdrop.open').forEach(modal => { this.closeModal(modal.id); }); this.pendingConsumableRequestRejectId = undefined; } async openProfileModal() { try { const response = await fetch(`${this.apiBase}/users/me`, { headers: this.getAuthHeaders(false) }); const data = await response.json(); if (!data.success || !data.data) { this.notifyFailure(data.message || 'Cannot load profile'); return; } this.renderProfileModal(data.data); } catch (err) { console.error(err); this.notifyFailure('Cannot load profile'); } } renderProfileModal(profile) { const isVerified = profile?.EmailVerified === true || profile?.EmailVerified === 1; const html = ` `; const containerId = 'profileModalContainer'; let container = document.getElementById(containerId); if (!container) { container = document.createElement('div'); container.id = containerId; document.body.appendChild(container); } container.innerHTML = html; const form = document.getElementById('profileForm'); if (form) { form.addEventListener('submit', (e) => this.saveProfile(e)); } this.setupProfilePasswordToggles(); } setupProfilePasswordToggles() { document.querySelectorAll('[data-password-toggle]').forEach((toggleBtn) => { if (toggleBtn.dataset.bound === 'true') { return; } toggleBtn.addEventListener('click', () => { const inputId = toggleBtn.dataset.passwordToggle; if (!inputId) return; const input = document.getElementById(inputId); const icon = document.getElementById(`${inputId}Icon`); if (!input) return; const isHidden = input.type === 'password'; input.type = isHidden ? 'text' : 'password'; if (icon) { icon.textContent = isHidden ? 'visibility_off' : 'visibility'; } }); toggleBtn.dataset.bound = 'true'; }); } async saveProfile(e) { e.preventDefault(); const fullname = document.getElementById('profileFullName')?.value.trim() || ''; const email = document.getElementById('profileEmail')?.value.trim() || ''; const currentPassword = document.getElementById('profileCurrentPassword')?.value || ''; const newPassword = document.getElementById('profileNewPassword')?.value || ''; const confirmPassword = document.getElementById('profileConfirmPassword')?.value || ''; if (!fullname || !email) { this.notifyFailure('Full name and email are required'); return; } if (newPassword && newPassword !== confirmPassword) { this.notifyFailure('New password and confirm password do not match'); return; } if (newPassword && (newPassword.length < 6 || newPassword.length > 256)) { this.notifyFailure('New password must be between 6 and 256 characters'); return; } const emailChanged = email.toLowerCase() !== String(this.currentUser?.Email || '').toLowerCase(); if ((newPassword || emailChanged) && !currentPassword) { this.notifyFailure('Current password is required to change password or email'); return; } try { const response = await fetch(`${this.apiBase}/users/me`, { method: 'PUT', headers: this.getAuthHeaders(true), body: JSON.stringify({ fullname, email, currentPassword, newPassword }) }); const data = await response.json(); if (!data.success) { this.notifyFailure(data.message || 'Update profile failed'); return; } if (data.sessionEnded) { localStorage.removeItem('currentUser'); this.notifySuccess(data.message || 'Profile updated. Please sign in again.'); setTimeout(() => window.location.replace('../pages/login.html'), 700); return; } if (data.user) { this.currentUser = { ...this.currentUser, ...data.user, role: data.user.role || data.user.Role || this.currentUser.role || this.currentUser.Role }; this.saveToStorage('currentUser', this.currentUser); this.updateAccountDisplay(); } closeProfileModal(); this.notifySuccess(data.message || 'Profile updated'); if (data.verificationRequired && data.emailSent === false) { if (data.verificationPreviewUrl) { this.notifyWarning(`Email confirmation link (dev): ${data.verificationPreviewUrl}`); } else { this.notifyWarning('Email changed but verification email could not be sent.'); } } } catch (err) { console.error(err); this.notifyFailure('Update profile failed'); } } loadFromStorage(key) { const data = localStorage.getItem(key); return data ? JSON.parse(data) : null; } saveToStorage(key, data) { localStorage.setItem(key, JSON.stringify(data)); } formatDateTime(value) { if (!value) return '-'; const date = new Date(value); if (Number.isNaN(date.getTime())) { return String(value); } return APP_DATE_TIME_FORMATTER.format(date); } // ========== Users Management ========== getUsersContent() { const filteredUsers = this.getFilteredUsers(); const pageInfo = this.getPaged(filteredUsers, this.userPage, this.userPageSize); this.userPage = pageInfo.current; return `
search
${pageInfo.data.length === 0 ? ` ` : pageInfo.data.map(user => ` `).join('')}
Username Full Name Email Role Status Actions
No users found
${user.Username} ${user.FullName || '-'} ${user.Email || '-'} ${user.RoleName || user.Role || 'N/A'} ${user.IsActive ? 'Active' : 'Inactive'}
Showing ${pageInfo.start}-${pageInfo.end} of ${pageInfo.total}
Page ${pageInfo.current} / ${pageInfo.totalPages}
`; } setupUsersRowListeners() { const userRows = document.querySelectorAll('.user-row'); userRows.forEach(row => { const viewBtn = row.querySelector('.view-user-btn'); const editBtn = row.querySelector('.edit-user-btn'); const deleteBtn = row.querySelector('.delete-user-btn'); const userId = row.dataset.userId; if (viewBtn) { viewBtn.addEventListener('click', () => this.viewUserDetails(userId)); } if (editBtn) { editBtn.addEventListener('click', () => this.editUser(userId)); } if (deleteBtn && !deleteBtn.disabled) { deleteBtn.addEventListener('click', () => this.deleteUserConfirm(userId)); } }); // Search and Filter const searchInput = document.getElementById('userSearch'); const roleFilter = document.getElementById('roleFilter'); if (searchInput) { searchInput.oninput = (e) => { this.userSearchTerm = (e.target.value || '').toLowerCase(); this.userPage = 1; this.renderUsersTableBody(); }; } if (roleFilter) { roleFilter.value = this.userRoleFilter || ''; roleFilter.onchange = (e) => { this.userRoleFilter = e.target.value; this.userPage = 1; this.renderUsersTableBody(); }; } // Add User Button const addBtn = document.getElementById('addUserBtn'); if (addBtn) { addBtn.onclick = () => this.openUserModal(); } const addRoleBtn = document.getElementById('addRoleBtn'); if (addRoleBtn) { addRoleBtn.onclick = () => this.openRoleModal(); } } getFilteredUsers() { const search = (this.userSearchTerm || '').toLowerCase(); const roleId = this.userRoleFilter || ''; return this.users.filter(user => { const matchesSearch = !search || [user.Username, user.FullName, user.Email].some(val => (val || '').toLowerCase().includes(search)); const matchesRole = !roleId || String(user.RoleId) === String(roleId) || String(user.RoleID) === String(roleId); return matchesSearch && matchesRole; }); } renderUsersTableBody() { const tbody = document.querySelector('.users-table-body'); if (!tbody) return; const pageInfo = this.getPaged(this.getFilteredUsers(), this.userPage, this.userPageSize); this.userPage = pageInfo.current; tbody.innerHTML = pageInfo.data.length === 0 ? ` No users found ` : pageInfo.data.map(user => ` ${user.Username} ${user.FullName || '-'} ${user.Email || '-'} ${user.RoleName || user.Role || 'N/A'} ${user.IsActive ? 'Active' : 'Inactive'}
`).join(''); const pager = document.getElementById('usersPager'); if (pager) { pager.innerHTML = ` Showing ${pageInfo.start}-${pageInfo.end} of ${pageInfo.total}
Page ${pageInfo.current} / ${pageInfo.totalPages}
`; } this.setupUsersRowListeners(); this.setupUsersPagerListeners(); } setupUsersPagerListeners() { document.querySelectorAll('.user-page-btn').forEach(btn => { btn.addEventListener('click', () => { const targetPage = Number(btn.dataset.page); if (!targetPage || targetPage < 1) return; this.userPage = targetPage; this.renderUsersTableBody(); }); }); } openUserModal() { this.showUserFormModal(null); } openRoleModal() { this.showRoleFormModal(); } showRoleFormModal() { const html = ` `; const editingContainer = document.getElementById('roleModalContainer'); if (editingContainer) { editingContainer.innerHTML = html; } else { const container = document.createElement('div'); container.id = 'roleModalContainer'; document.body.appendChild(container); container.innerHTML = html; } const form = document.getElementById('roleForm'); if (form) { form.addEventListener('submit', (e) => this.saveRole(e)); } } async saveRole(e) { e.preventDefault(); const roleName = document.getElementById('roleName')?.value.trim(); const description = document.getElementById('roleDescription')?.value.trim(); if (!roleName) { this.notifyFailure('Role name is required'); return; } const roleExists = this.roles.some(role => String(role.RoleName || '').trim().toLowerCase() === roleName.toLowerCase() ); if (roleExists) { this.notifyWarning('Role already exists'); return; } try { const response = await fetch(`${this.apiBase}/roles`, { method: 'POST', headers: this.getAuthHeaders(true), body: JSON.stringify({ roleName, description: description || null }) }); const data = await response.json(); if (!response.ok || !data.success) { this.notifyFailure(data.message || 'Create role failed'); return; } this.notifySuccess('Role created'); closeRoleModal(); await this.fetchRoles(); if (this.currentPage === 'users') { this.renderView('users'); } } catch (err) { console.error(err); this.notifyFailure('Create role failed'); } } showUserFormModal(userId) { const user = userId ? this.users.find(u => u.UserId == userId) : null; const html = ` `; // Insert modal in DOM const editingContainer = document.getElementById('userModalContainer'); if (editingContainer) { editingContainer.innerHTML = html; } else { const container = document.createElement('div'); container.id = 'userModalContainer'; document.body.appendChild(container); container.innerHTML = html; } // Add form submit listener const form = document.getElementById('userForm'); if (form) { form.addEventListener('submit', (e) => this.saveUser(e, userId)); } const passwordInput = document.getElementById('userPassword'); const passwordToggleBtn = document.getElementById('userPasswordToggle'); const passwordToggleIcon = document.getElementById('userPasswordToggleIcon'); if (passwordInput && passwordToggleBtn) { passwordToggleBtn.addEventListener('click', () => { const isHidden = passwordInput.type === 'password'; passwordInput.type = isHidden ? 'text' : 'password'; if (passwordToggleIcon) { passwordToggleIcon.textContent = isHidden ? 'visibility_off' : 'visibility'; } }); } } async saveUser(e, userId) { e.preventDefault(); const username = document.getElementById('userUsername').value.trim(); const fullname = document.getElementById('userFullName').value.trim(); const email = document.getElementById('userEmail').value.trim(); const password = document.getElementById('userPassword')?.value.trim(); const roleId = document.getElementById('userRole').value; const isActive = document.getElementById('userActive').checked; // Validate required fields if (!username || !fullname) { this.notifyFailure('Username and Full Name are required'); return; } // Password required for new user if (!userId && !password) { this.notifyFailure('Password is required for new user'); return; } if (password && (password.length < 6 || password.length > 256)) { this.notifyFailure('Password must be between 6 and 256 characters'); return; } const method = userId ? 'PUT' : 'POST'; const url = userId ? `${this.apiBase}/users/${userId}` : `${this.apiBase}/users`; const payload = userId ? { email: email || null, fullname, roleId: parseInt(roleId), status: 'Active', isActive, ...(password ? { password } : {}) } : { username, password, email: email || null, fullname, roleId: parseInt(roleId) }; try { const response = await fetch(url, { method, headers: this.getAuthHeaders(true), body: JSON.stringify(payload) }); const data = await response.json(); if (data.success) { this.notifySuccess(userId ? 'User updated' : 'User created'); closeUserModal(); this.refreshUsersUI(); } else { this.notifyFailure(data.message || 'Save failed'); } } catch (err) { console.error(err); this.notifyFailure('Save failed'); } } async editUser(userId) { this.showUserFormModal(userId); } async viewUserDetails(userId) { try { const response = await fetch(`${this.apiBase}/users/${userId}`, { headers: this.getAuthHeaders(false) }); const data = await response.json(); if (!data.success || !data.data) { this.notifyFailure(data.message || 'Cannot load user details'); return; } this.showUserDetailsModal(data.data); } catch (err) { console.error(err); this.notifyFailure('Cannot load user details'); } } showUserDetailsModal(user) { const html = ` `; const detailsContainer = document.getElementById('userDetailsModalContainer'); if (detailsContainer) { detailsContainer.innerHTML = html; } else { const container = document.createElement('div'); container.id = 'userDetailsModalContainer'; container.innerHTML = html; document.body.appendChild(container); } const usernameEl = document.getElementById('userDetailUsername'); const fullNameEl = document.getElementById('userDetailFullName'); const emailEl = document.getElementById('userDetailEmail'); const roleEl = document.getElementById('userDetailRole'); const statusEl = document.getElementById('userDetailStatus'); const createdDateEl = document.getElementById('userDetailCreatedDate'); const lastLoginEl = document.getElementById('userDetailLastLogin'); const editBtn = document.getElementById('userDetailEditBtn'); if (usernameEl) usernameEl.textContent = user?.Username || '-'; if (fullNameEl) fullNameEl.textContent = user?.FullName || '-'; if (emailEl) emailEl.textContent = user?.Email || '-'; if (roleEl) roleEl.textContent = user?.RoleName || user?.Role || '-'; if (statusEl) statusEl.textContent = user?.IsActive ? 'Active' : 'Inactive'; if (createdDateEl) createdDateEl.textContent = this.formatDateTime(user?.CreatedDate); if (lastLoginEl) lastLoginEl.textContent = this.formatDateTime(user?.LastLogin); if (editBtn) { editBtn.addEventListener('click', () => { closeUserDetailsModal(); this.editUser(user?.UserId); }); } } async deleteUserConfirm(userId) { const user = this.users.find(u => u.UserId == userId); if (!user) return; if (confirm(`Are you sure you want to delete user "${user.Username}"?`)) { await this.deleteUser(userId); } } async deleteUser(userId) { try { const response = await fetch(`${this.apiBase}/users/${userId}`, { method: 'DELETE', headers: this.getAuthHeaders(false) }); const data = await response.json(); if (data.success) { this.notifySuccess('User deleted'); this.refreshUsersUI(); } else { this.notifyFailure(data.message || 'Delete failed'); } } catch (err) { console.error(err); this.notifyFailure('Delete failed'); } } async refreshUsersUI() { await this.fetchUsers(); if (this.currentPage === 'users') { this.renderView('users'); } } } // Global modal close functions function closeAllModals() { if (app?.pendingAssetRequestDeleteConfirmResolver) { app.resolveAssetRequestDeleteConfirm(false); } if (app?.pendingBulkAssetDeleteConfirmResolver) { app.resolveBulkAssetDeleteConfirm(false); } document.querySelectorAll('.modal-backdrop').forEach(modal => { modal.classList.remove('open'); }); if (app) { app.pendingConsumableRequestRejectId = undefined; } } function closeAccountModal() { document.getElementById('accountModal').classList.remove('open'); } function closeViewAccountModal() { document.getElementById('viewAccountModal').classList.remove('open'); } function closeDeleteAccountModal() { document.getElementById('deleteAccountModal').classList.remove('open'); } function closeAppModal() { document.getElementById('appModal').classList.remove('open'); } function closeViewAppModal() { document.getElementById('viewAppModal').classList.remove('open'); } function closeDeleteAppModal() { document.getElementById('deleteAppModal').classList.remove('open'); } function closeAssetModal() { document.getElementById('assetModal').classList.remove('open'); } function closeConsumableModal() { document.getElementById('consumableModal').classList.remove('open'); } function closeDeleteConsumableModal() { document.getElementById('deleteConsumableModal').classList.remove('open'); } function closeConsumableExportModal() { const modal = document.getElementById('consumableExportModal'); if (modal) { modal.classList.remove('open'); } } function closeConsumableBorrowRequestModal() { const modal = document.getElementById('consumableBorrowRequestModal'); if (modal) { modal.classList.remove('open'); } if (typeof app !== 'undefined') { app.closeConsumableBorrowProductDropdown(); } } function closeConsumableBorrowRequestsModal() { const modal = document.getElementById('consumableBorrowRequestsModal'); if (modal) { modal.classList.remove('open'); } closeConsumableRequestRejectModal(); } function closeConsumableRequestRejectModal() { const modal = document.getElementById('consumableRequestRejectModal'); const idInput = document.getElementById('consumableRequestRejectIdInput'); const reasonInput = document.getElementById('consumableRequestRejectReasonInput'); if (modal) { modal.classList.remove('open'); } if (idInput) { idInput.value = ''; } if (reasonInput) { reasonInput.value = ''; } if (app) { app.pendingConsumableRequestRejectId = undefined; } } function closeConsumableReturnModal() { const modal = document.getElementById('consumableReturnModal'); if (modal) { modal.classList.remove('open'); } if (app) { app.pendingConsumableReturnHistoryId = undefined; } } function closeConsumableExportHistoryModal() { const modal = document.getElementById('consumableExportHistoryModal'); if (modal) { modal.classList.remove('open'); } } function closeViewAssetModal() { document.getElementById('viewAssetModal').classList.remove('open'); } function closeDeleteAssetModal() { document.getElementById('deleteAssetModal').classList.remove('open'); } function closeBulkDeleteAssetsConfirmModal() { if (app?.pendingBulkAssetDeleteConfirmResolver) { app.resolveBulkAssetDeleteConfirm(false); return; } const modal = document.getElementById('bulkDeleteAssetsConfirmModal'); if (modal) { modal.classList.remove('open'); } } function closeBorrowAssetModal() { const modal = document.getElementById('borrowAssetModal'); if (modal) { modal.classList.remove('open'); } } function closeAssetDamageModal() { const modal = document.getElementById('assetDamageModal'); if (modal) { modal.classList.remove('open'); } } function closeAssetDamageHistoryModal() { const modal = document.getElementById('assetDamageHistoryModal'); if (modal) { modal.classList.remove('open'); } } function closeAssetBorrowRequestModal() { const modal = document.getElementById('assetBorrowRequestModal'); const dropdown = document.getElementById('assetBorrowProductDropdown'); if (modal) { modal.classList.remove('open'); } if (dropdown) { dropdown.classList.add('hidden'); } } function closeAssetPendingRequestsModal() { const modal = document.getElementById('assetPendingRequestsModal'); if (modal) { modal.classList.remove('open'); } closeAssetRequestDeleteConfirmModal(); const rejectModal = document.getElementById('assetRequestRejectModal'); if (rejectModal) { rejectModal.classList.remove('open'); } } function closeAssetBorrowDetailsModal() { const modal = document.getElementById('assetBorrowDetailsModal'); if (modal) { modal.classList.remove('open'); } } function closeAssetRequestRejectModal() { const modal = document.getElementById('assetRequestRejectModal'); if (modal) { modal.classList.remove('open'); } } function closeAssetRequestDeleteConfirmModal() { if (app?.pendingAssetRequestDeleteConfirmResolver) { app.resolveAssetRequestDeleteConfirm(false); return; } const modal = document.getElementById('assetRequestDeleteConfirmModal'); if (modal) { modal.classList.remove('open'); } } function closeAssetDepartmentModal() { const modal = document.getElementById('assetDepartmentModal'); if (modal) { modal.classList.remove('open'); } } function closeDeleteAssetDepartmentModal() { const modal = document.getElementById('deleteAssetDepartmentModal'); if (modal) { modal.classList.remove('open'); } } function closeAssetProjectModal() { const modal = document.getElementById('assetProjectModal'); if (modal) { modal.classList.remove('open'); } } function closeDeleteAssetProjectModal() { const modal = document.getElementById('deleteAssetProjectModal'); if (modal) { modal.classList.remove('open'); } } function closeUserModal() { const userModalContainer = document.getElementById('userModalContainer'); if (userModalContainer) { userModalContainer.innerHTML = ''; } } function closeRoleModal() { const roleModalContainer = document.getElementById('roleModalContainer'); if (roleModalContainer) { roleModalContainer.innerHTML = ''; } } function closeUserDetailsModal() { const detailsContainer = document.getElementById('userDetailsModalContainer'); if (detailsContainer) { detailsContainer.innerHTML = ''; } } function closeProfileModal() { const profileContainer = document.getElementById('profileModalContainer'); if (profileContainer) { profileContainer.innerHTML = ''; } } // Initialize app when DOM is ready let app; document.addEventListener('DOMContentLoaded', async () => { try { const response = await fetch('/api/auth/session', { cache: 'no-store' }); const data = await response.json(); if (!response.ok || !data.success || !data.user) { throw new Error('Session is not valid'); } localStorage.setItem('currentUser', JSON.stringify(data.user)); app = new AccountManager(); } catch (error) { localStorage.removeItem('currentUser'); window.location.replace('../pages/login.html?reason=session-expired'); } });