12188 lines
604 KiB
JavaScript
12188 lines
604 KiB
JavaScript
// VaultSentinel - Account Management Application
|
|
// Main JavaScript functionality
|
|
|
|
const authenticatedFetch = window.fetch.bind(window);
|
|
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.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.notificationPollTimer = 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.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.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.fetchNotificationPreview();
|
|
|
|
if (this.canCurrentUserManageAssets()) {
|
|
await this.fetchUsers();
|
|
}
|
|
|
|
// 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));
|
|
}
|
|
|
|
handleRoute(hash) {
|
|
const route = (hash || '#dashboard').replace('#', '') || 'dashboard';
|
|
if (this.isMobileViewport()) {
|
|
this.closeMobileNav();
|
|
}
|
|
this.renderView(route);
|
|
}
|
|
|
|
renderView(page) {
|
|
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 === '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 === 'documents') {
|
|
mainContent.innerHTML = this.getDocumentsContent();
|
|
this.setupDocumentListeners();
|
|
} else if (page === 'notifications') {
|
|
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();
|
|
}
|
|
|
|
if (page === 'asset-borrows') {
|
|
this.startAssetBorrowAutoRefresh();
|
|
} else {
|
|
this.stopAssetBorrowAutoRefresh();
|
|
}
|
|
|
|
this.restoreSearchFocus();
|
|
this.enhanceResponsiveTables(mainContent);
|
|
this.updatePendingAssetRequestsBadge();
|
|
this.setActiveNav(page);
|
|
}
|
|
|
|
setActiveNav(page) {
|
|
document.querySelectorAll('[data-nav]').forEach(link => {
|
|
const isActive = link.dataset.nav === page;
|
|
link.classList.toggle('border-l-4', isActive);
|
|
link.classList.toggle('border-blue-600', isActive);
|
|
link.classList.toggle('bg-slate-200/80', isActive);
|
|
link.classList.toggle('dark:bg-slate-800', isActive);
|
|
link.classList.toggle('text-slate-900', isActive);
|
|
link.classList.toggle('dark:text-slate-50', isActive);
|
|
link.classList.toggle('font-bold', isActive);
|
|
});
|
|
}
|
|
|
|
isMobileViewport() {
|
|
return window.matchMedia(`(max-width: ${this.mobileBreakpoint}px)`).matches;
|
|
}
|
|
|
|
setupResponsiveShell() {
|
|
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 fetchNotificationPreview() {
|
|
try {
|
|
const response = await fetch(`${this.apiBase}/notifications?page=1&pageSize=5&filter=all`, { 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 báo');
|
|
}
|
|
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(() => this.fetchNotificationPreview(), 60000);
|
|
}
|
|
|
|
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', borderClass: 'border-l-emerald-500' },
|
|
warning: { icon: 'warning', iconClass: 'bg-amber-100 text-amber-700', borderClass: 'border-l-amber-500' },
|
|
error: { icon: 'error', iconClass: 'bg-red-100 text-red-700', borderClass: 'border-l-red-500' },
|
|
info: { icon: 'info', iconClass: 'bg-blue-100 text-blue-700', borderClass: 'border-l-blue-500' }
|
|
};
|
|
const source = String(item.Source || 'system').toLowerCase();
|
|
return {
|
|
...(severityMap[severity] || severityMap.info),
|
|
sourceLabel: source === 'admin' ? 'Quản trị viê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 = `
|
|
<div class="flex flex-col items-center px-6 py-10 text-center">
|
|
<span class="material-symbols-outlined text-4xl text-slate-300">notifications_off</span>
|
|
<p class="mt-2 text-sm font-semibold text-slate-700 dark:text-slate-200">Chưa có thông báo</p>
|
|
<p class="mt-1 text-xs text-slate-500">Thông báo mới sẽ xuất hiện tại đây.</p>
|
|
</div>`;
|
|
return;
|
|
}
|
|
|
|
list.innerHTML = this.notificationPreviewItems.map(item => {
|
|
const meta = this.getNotificationMeta(item);
|
|
const isRead = item.IsRead === true || item.IsRead === 1;
|
|
return `
|
|
<button type="button" class="notification-preview-item w-full flex gap-3 px-4 py-3 text-left border-b border-slate-100 dark:border-slate-800 hover:bg-slate-50 dark:hover:bg-slate-800 transition-colors ${isRead ? '' : 'bg-blue-50/60 dark:bg-blue-950/20'}" data-notification-id="${item.NotificationId}">
|
|
<span class="shrink-0 inline-flex items-center justify-center w-9 h-9 rounded-xl ${meta.iconClass}">
|
|
<span class="material-symbols-outlined text-lg">${meta.icon}</span>
|
|
</span>
|
|
<span class="min-w-0 flex-1">
|
|
<span class="flex items-start gap-2">
|
|
<span class="flex-1 text-sm font-bold text-slate-900 dark:text-slate-50 line-clamp-1">${this.escapeHtml(item.Title)}</span>
|
|
${isRead ? '' : '<span class="mt-1.5 w-2 h-2 rounded-full bg-blue-600 shrink-0"></span>'}
|
|
</span>
|
|
<span class="mt-0.5 block text-xs text-slate-600 dark:text-slate-400 line-clamp-2">${this.escapeHtml(item.Message)}</span>
|
|
<span class="mt-1 block text-[10px] text-slate-400">${meta.sourceLabel} · ${this.formatNotificationTime(item.CreatedDate)}</span>
|
|
</span>
|
|
</button>`;
|
|
}).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 response = await fetch(`${this.apiBase}/notifications?${params.toString()}`, { 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 thông báo');
|
|
this.notificationItems = Array.isArray(data.data) ? data.data : [];
|
|
this.notificationPagination = data.pagination || this.notificationPagination;
|
|
}
|
|
|
|
async refreshNotificationsPage() {
|
|
this.notificationLoading = true;
|
|
this.renderNotificationsPage();
|
|
try {
|
|
await this.fetchNotifications();
|
|
} catch (err) {
|
|
console.error('Fetch notifications error:', err);
|
|
this.notifyFailure(err.message || 'Không thể tải thông báo');
|
|
} finally {
|
|
this.notificationLoading = false;
|
|
this.renderNotificationsPage();
|
|
}
|
|
}
|
|
|
|
renderNotificationsPage() {
|
|
if (this.currentPage !== 'notifications') 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 canWithdraw = this.isCurrentUserAdmin() && String(item.Source).toLowerCase() === 'admin';
|
|
const author = String(item.Source).toLowerCase() === 'admin'
|
|
? (item.CreatedByName || 'Quản trị viên')
|
|
: 'Hệ thống';
|
|
return `
|
|
<article class="rounded-2xl border border-slate-200 dark:border-slate-700 border-l-4 ${meta.borderClass} bg-white dark:bg-slate-900 p-4 shadow-sm ${isRead ? 'opacity-80' : ''}">
|
|
<div class="flex gap-4">
|
|
<span class="shrink-0 inline-flex items-center justify-center w-11 h-11 rounded-2xl ${meta.iconClass}">
|
|
<span class="material-symbols-outlined">${meta.icon}</span>
|
|
</span>
|
|
<div class="min-w-0 flex-1">
|
|
<div class="flex flex-wrap items-start justify-between gap-2">
|
|
<div class="min-w-0">
|
|
<div class="flex items-center gap-2">
|
|
<h3 class="text-sm font-extrabold text-slate-900 dark:text-slate-50">${this.escapeHtml(item.Title)}</h3>
|
|
${isRead ? '' : '<span class="inline-flex w-2 h-2 rounded-full bg-blue-600" title="Chưa đọc"></span>'}
|
|
</div>
|
|
<p class="mt-1 text-[11px] text-slate-500">${this.escapeHtml(author)} · ${this.formatNotificationTime(item.CreatedDate)}</p>
|
|
</div>
|
|
<span class="inline-flex items-center gap-1 rounded-full bg-slate-100 dark:bg-slate-800 px-2.5 py-1 text-[10px] font-bold text-slate-600 dark:text-slate-300">
|
|
<span class="material-symbols-outlined text-sm">${meta.sourceIcon}</span>${meta.sourceLabel}
|
|
</span>
|
|
</div>
|
|
<p class="mt-3 whitespace-pre-line text-sm leading-6 text-slate-700 dark:text-slate-300">${this.escapeHtml(item.Message)}</p>
|
|
<div class="mt-4 flex flex-wrap items-center gap-2">
|
|
${item.ActionUrl ? `<button type="button" class="notification-action-btn inline-flex items-center gap-1.5 rounded-lg bg-primary px-3 py-2 text-xs font-bold text-on-primary hover:bg-primary-dim" data-notification-id="${item.NotificationId}">Xem nội dung<span class="material-symbols-outlined text-base">arrow_forward</span></button>` : ''}
|
|
${!isRead ? `<button type="button" class="notification-read-btn rounded-lg border border-slate-200 dark:border-slate-700 px-3 py-2 text-xs font-bold text-slate-600 dark:text-slate-300 hover:bg-slate-50 dark:hover:bg-slate-800" data-notification-id="${item.NotificationId}">Đánh dấu đã đọc</button>` : ''}
|
|
${canWithdraw ? `<button type="button" class="notification-withdraw-btn ml-auto inline-flex items-center gap-1 rounded-lg px-3 py-2 text-xs font-bold text-red-600 hover:bg-red-50 dark:hover:bg-red-950/30" data-notification-id="${item.NotificationId}"><span class="material-symbols-outlined text-base">cancel</span>Thu hồi</button>` : ''}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</article>`;
|
|
}
|
|
|
|
getNotificationsContent() {
|
|
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
|
|
? `<div class="flex flex-col items-center justify-center rounded-2xl border border-dashed border-slate-300 dark:border-slate-700 py-16"><span class="material-symbols-outlined animate-spin text-3xl text-primary">progress_activity</span><p class="mt-3 text-sm text-slate-500">Đang tải thông báo...</p></div>`
|
|
: this.notificationItems.length
|
|
? this.notificationItems.map(item => this.getNotificationCardHtml(item)).join('')
|
|
: `<div class="flex flex-col items-center justify-center rounded-2xl border border-dashed border-slate-300 dark:border-slate-700 bg-white/50 dark:bg-slate-900/50 py-16 text-center"><span class="material-symbols-outlined text-5xl text-slate-300">notifications_off</span><h3 class="mt-3 text-base font-bold text-slate-700 dark:text-slate-200">Không có thông báo phù hợp</h3><p class="mt-1 text-sm text-slate-500">Hãy thử thay đổi bộ lọc.</p></div>`;
|
|
|
|
return `
|
|
<div class="notifications-page h-full overflow-y-auto bg-slate-50/60 dark:bg-slate-950 p-4 md:p-6">
|
|
<div class="mx-auto max-w-5xl">
|
|
<header class="flex flex-col gap-4 sm:flex-row sm:items-end sm:justify-between">
|
|
<div>
|
|
<div class="inline-flex items-center gap-2 rounded-full bg-blue-100 px-3 py-1 text-xs font-bold text-blue-700 dark:bg-blue-950/50 dark:text-blue-300"><span class="material-symbols-outlined text-base">notifications</span>Trung tâm thông báo</div>
|
|
<h1 class="mt-3 text-2xl font-extrabold text-slate-900 dark:text-slate-50">Thông tin cần bạn chú ý</h1>
|
|
<p class="mt-1 text-sm text-slate-500">Thông báo từ quản trị viên và các sự kiện hệ thống quan trọng — không gồm lịch sử chỉnh sửa tài sản.</p>
|
|
</div>
|
|
${this.isCurrentUserAdmin() ? `<button id="composeNotificationBtn" type="button" class="inline-flex items-center justify-center gap-2 rounded-xl bg-primary px-4 py-2.5 text-sm font-bold text-on-primary shadow-sm hover:bg-primary-dim"><span class="material-symbols-outlined">campaign</span>Gửi thông báo</button>` : ''}
|
|
</header>
|
|
|
|
<section class="mt-6 grid grid-cols-1 gap-3 sm:grid-cols-2">
|
|
<div class="rounded-2xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 p-4"><p class="text-xs font-bold uppercase tracking-wider text-slate-400">Chưa đọc</p><p class="mt-1 text-3xl font-extrabold text-slate-900 dark:text-slate-50">${this.notificationUnreadCount}</p></div>
|
|
<div class="rounded-2xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 p-4"><p class="text-xs font-bold uppercase tracking-wider text-slate-400">Theo bộ lọc</p><p class="mt-1 text-3xl font-extrabold text-slate-900 dark:text-slate-50">${total}</p></div>
|
|
</section>
|
|
|
|
<section class="mt-4 flex flex-col gap-3 rounded-2xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 p-3 sm:flex-row sm:items-center sm:justify-between">
|
|
<div class="flex flex-wrap gap-2">
|
|
<button type="button" data-notification-filter="all" class="notification-filter-btn rounded-lg px-3 py-2 text-xs font-bold ${this.notificationFilter === 'all' ? 'bg-slate-900 text-white dark:bg-slate-100 dark:text-slate-900' : 'bg-slate-100 text-slate-600 dark:bg-slate-800 dark:text-slate-300'}">Tất cả</button>
|
|
<button type="button" data-notification-filter="unread" class="notification-filter-btn rounded-lg px-3 py-2 text-xs font-bold ${this.notificationFilter === 'unread' ? 'bg-slate-900 text-white dark:bg-slate-100 dark:text-slate-900' : 'bg-slate-100 text-slate-600 dark:bg-slate-800 dark:text-slate-300'}">Chưa đọc</button>
|
|
<select id="notificationSourceFilter" class="rounded-lg border border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-800 px-3 py-2 text-xs font-bold text-slate-600 dark:text-slate-300">
|
|
<option value="" ${this.notificationSourceFilter === '' ? 'selected' : ''}>Mọi nguồn</option>
|
|
<option value="admin" ${this.notificationSourceFilter === 'admin' ? 'selected' : ''}>Quản trị viên</option>
|
|
<option value="system" ${this.notificationSourceFilter === 'system' ? 'selected' : ''}>Hệ thống</option>
|
|
</select>
|
|
</div>
|
|
<button id="notificationsMarkAllPageBtn" type="button" class="inline-flex items-center justify-center gap-1 rounded-lg px-3 py-2 text-xs font-bold text-primary hover:bg-blue-50 dark:hover:bg-blue-950/30 ${this.notificationUnreadCount ? '' : 'invisible'}"><span class="material-symbols-outlined text-base">done_all</span>Đọc tất cả</button>
|
|
</section>
|
|
|
|
<div class="mt-4 space-y-3">${listHtml}</div>
|
|
|
|
${!this.notificationLoading && totalPages > 1 ? `<nav class="mt-5 flex items-center justify-between rounded-xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 px-4 py-3"><span class="text-xs text-slate-500">Trang ${currentPage}/${totalPages}</span><div class="flex gap-2"><button class="notification-page-btn rounded-lg border border-slate-200 dark:border-slate-700 px-3 py-1.5 text-xs font-bold disabled:opacity-40" data-page="${currentPage - 1}" ${currentPage <= 1 ? 'disabled' : ''}>Trước</button><button class="notification-page-btn rounded-lg border border-slate-200 dark:border-slate-700 px-3 py-1.5 text-xs font-bold disabled:opacity-40" data-page="${currentPage + 1}" ${currentPage >= totalPages ? 'disabled' : ''}>Sau</button></div></nav>` : ''}
|
|
</div>
|
|
</div>`;
|
|
}
|
|
|
|
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());
|
|
}
|
|
|
|
findNotification(notificationId) {
|
|
return this.notificationItems.find(item => Number(item.NotificationId) === Number(notificationId))
|
|
|| this.notificationPreviewItems.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 {
|
|
const response = await fetch(`${this.apiBase}/notifications/${notificationId}/read`, { method: 'PUT' });
|
|
const data = await response.json();
|
|
if (!response.ok || !data.success) throw new Error(data.message || 'Không thể cập nhật thông báo');
|
|
[...this.notificationItems, ...this.notificationPreviewItems].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 {
|
|
const response = await fetch(`${this.apiBase}/notifications/read-all`, { method: 'PUT' });
|
|
const data = await response.json();
|
|
if (!response.ok || !data.success) throw new Error(data.message || 'Không thể cập nhật thông báo');
|
|
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');
|
|
}
|
|
}
|
|
|
|
async withdrawNotification(notificationId) {
|
|
if (!window.confirm('Thu hồi thông báo này khỏi tất cả người dùng?')) return;
|
|
try {
|
|
const response = await fetch(`${this.apiBase}/notifications/${notificationId}`, { method: 'DELETE' });
|
|
const data = await response.json();
|
|
if (!response.ok || !data.success) throw new Error(data.message || 'Không thể thu hồi thông báo');
|
|
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');
|
|
}
|
|
}
|
|
|
|
openNotificationComposer() {
|
|
if (!this.isCurrentUserAdmin()) 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 = `
|
|
<div class="w-full max-w-xl overflow-hidden rounded-2xl bg-white dark:bg-slate-900 shadow-2xl" role="dialog" aria-modal="true" aria-labelledby="notificationComposerTitle">
|
|
<div class="flex items-start justify-between border-b border-slate-100 dark:border-slate-800 px-5 py-4">
|
|
<div><h2 id="notificationComposerTitle" class="text-lg font-extrabold text-slate-900 dark:text-slate-50">Gửi thông báo tới mọi người</h2><p class="mt-1 text-xs text-slate-500">Tất cả tài khoản đang hoạt động đều có thể nhận thông báo này.</p></div>
|
|
<button id="closeNotificationComposerBtn" type="button" class="rounded-lg p-2 text-slate-500 hover:bg-slate-100 dark:hover:bg-slate-800" aria-label="Đóng"><span class="material-symbols-outlined">close</span></button>
|
|
</div>
|
|
<form id="notificationComposerForm" class="space-y-4 p-5">
|
|
<div><label class="mb-1.5 block text-xs font-bold text-slate-700 dark:text-slate-200">Tiêu đề <span class="text-red-500">*</span></label><input id="notificationTitleInput" maxlength="160" required class="w-full rounded-xl border border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-800 px-3 py-2.5 text-sm" placeholder="Ví dụ: Lịch bảo trì hệ thống" /></div>
|
|
<div><div class="mb-1.5 flex justify-between"><label class="text-xs font-bold text-slate-700 dark:text-slate-200">Nội dung <span class="text-red-500">*</span></label><span id="notificationMessageCounter" class="text-[10px] text-slate-400">0/2000</span></div><textarea id="notificationMessageInput" maxlength="2000" required rows="5" class="w-full resize-y rounded-xl border border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-800 px-3 py-2.5 text-sm" placeholder="Viết ngắn gọn điều mọi người cần biết..."></textarea></div>
|
|
<div class="grid grid-cols-1 gap-4 sm:grid-cols-2"><div><label class="mb-1.5 block text-xs font-bold text-slate-700 dark:text-slate-200">Mức độ</label><select id="notificationSeverityInput" class="w-full rounded-xl border border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-800 px-3 py-2.5 text-sm"><option value="info">Thông tin</option><option value="success">Thành công</option><option value="warning">Cần chú ý</option><option value="error">Khẩn cấp</option></select></div><div><label class="mb-1.5 block text-xs font-bold text-slate-700 dark:text-slate-200">Liên kết khi bấm</label><select id="notificationActionInput" class="w-full rounded-xl border border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-800 px-3 py-2.5 text-sm"><option value="">Không có</option><option value="#dashboard">Tổng quan</option><option value="#assets">Tài sản</option><option value="#asset-borrows">Mượn/Trả tài sản</option><option value="#consumables">Vật tư tiêu hao</option><option value="#consumable-exports">Mượn/Trả vật tư</option><option value="#documents">Tài liệu</option></select></div></div>
|
|
<div><label class="mb-1.5 block text-xs font-bold text-slate-700 dark:text-slate-200">Tự hết hạn <span class="font-normal text-slate-400">(không bắt buộc)</span></label><input id="notificationExpiryInput" type="datetime-local" class="w-full rounded-xl border border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-800 px-3 py-2.5 text-sm" /></div>
|
|
<div class="flex items-center justify-end gap-2 border-t border-slate-100 dark:border-slate-800 pt-4"><button id="cancelNotificationComposerBtn" type="button" class="rounded-xl px-4 py-2.5 text-sm font-bold text-slate-600 hover:bg-slate-100 dark:text-slate-300 dark:hover:bg-slate-800">Hủy</button><button id="sendNotificationBtn" type="submit" class="inline-flex items-center gap-2 rounded-xl bg-primary px-4 py-2.5 text-sm font-bold text-on-primary hover:bg-primary-dim"><span class="material-symbols-outlined text-lg">send</span>Gửi tới mọi người</button></div>
|
|
</form>
|
|
</div>`;
|
|
document.body.appendChild(container);
|
|
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();
|
|
const expiryValue = document.getElementById('notificationExpiryInput')?.value || '';
|
|
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: expiryValue ? new Date(expiryValue).toISOString() : null
|
|
};
|
|
const sendButton = document.getElementById('sendNotificationBtn');
|
|
if (sendButton) sendButton.disabled = true;
|
|
try {
|
|
const response = await fetch(`${this.apiBase}/notifications`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(payload)
|
|
});
|
|
const data = await response.json();
|
|
if (!response.ok || !data.success) throw new Error(data.message || 'Không thể gửi thông báo');
|
|
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 = '') {
|
|
this.populateUserSelectOptions('borrowAssetUserInput', {
|
|
selectedValue,
|
|
emptyLabel: '-- Chọn người mượn --'
|
|
});
|
|
}
|
|
|
|
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 = '<option value="">-- Chọn người nhận --</option>';
|
|
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 = `
|
|
<div class="px-3 py-3 text-sm text-slate-500">Không tìm thấy tài sản phù hợp.</div>
|
|
`;
|
|
} 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 `
|
|
<button
|
|
type="button"
|
|
class="asset-borrow-product-option block min-w-full w-max text-left px-3 py-2 hover:bg-slate-50 transition-colors ${isSelected ? 'bg-primary/10 text-primary' : 'text-slate-700'}"
|
|
data-asset-id="${assetId}"
|
|
>
|
|
<div class="block text-sm font-medium whitespace-nowrap pr-6">${this.escapeHtml(displayName)}</div>
|
|
</button>
|
|
`;
|
|
}).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 ? 'Xác nhận xuất dự án' : 'Xác nhận mượn';
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
|
|
async fetchAssetExportHistories(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-export-history?limit=${safeLimit}`, {
|
|
headers: this.getAuthHeaders(false)
|
|
});
|
|
const data = await res.json();
|
|
if (data.success) {
|
|
this.assetExportHistories = Array.isArray(data.data) ? data.data : [];
|
|
} else {
|
|
console.error('Load asset export history failed:', data.message);
|
|
}
|
|
} catch (err) {
|
|
console.error('Fetch asset export history error:', err);
|
|
}
|
|
}
|
|
|
|
buildAssetExportHistoryRowsHtml(rows = []) {
|
|
if (!Array.isArray(rows) || rows.length === 0) {
|
|
return `
|
|
<tr>
|
|
<td colspan="7" class="px-4 py-8 text-sm text-center text-slate-500">Chưa có dữ liệu lịch sử xuất.</td>
|
|
</tr>
|
|
`;
|
|
}
|
|
|
|
return rows.map(item => {
|
|
const assetLabel = [String(item?.AssetCode || '').trim(), String(item?.AssetName || '').trim()]
|
|
.filter(Boolean)
|
|
.join(' - ') || '-';
|
|
return `
|
|
<tr class="hover:bg-slate-50/80 transition-colors">
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${this.formatDateTime(item?.ExportedDate || item?.CreatedDate)}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700">${this.escapeHtml(assetLabel)}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700 font-semibold">${Number(item?.ExportQuantity) || 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${this.escapeHtml(item?.ProjectName || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${this.escapeHtml(item?.CustodianName || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${this.escapeHtml(item?.ExportedByName || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-pre-line">${this.escapeHtml(item?.ExportNote || '-')}</td>
|
|
</tr>
|
|
`;
|
|
}).join('');
|
|
}
|
|
|
|
renderAssetExportHistoryModal() {
|
|
const tbody = document.getElementById('assetExportHistoryTableBody');
|
|
if (!tbody) {
|
|
return;
|
|
}
|
|
|
|
tbody.innerHTML = this.buildAssetExportHistoryRowsHtml(this.assetExportHistories);
|
|
}
|
|
|
|
async openAssetExportHistoryModal() {
|
|
if (!this.ensureAssetManagePermission('xem lich su xuat tai san')) {
|
|
return;
|
|
}
|
|
|
|
const modal = document.getElementById('assetExportHistoryModal');
|
|
const tbody = document.getElementById('assetExportHistoryTableBody');
|
|
if (!modal || !tbody) {
|
|
this.notifyFailure('Không tìm thấy biểu mẫu lịch sử xuất tài sản.');
|
|
return;
|
|
}
|
|
|
|
tbody.innerHTML = `
|
|
<tr>
|
|
<td colspan="7" class="px-4 py-8 text-sm text-center text-slate-500">Đang tải lịch sử xuất...</td>
|
|
</tr>
|
|
`;
|
|
modal.classList.add('open');
|
|
|
|
await this.fetchAssetExportHistories();
|
|
this.renderAssetExportHistoryModal();
|
|
}
|
|
|
|
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 `
|
|
<tr>
|
|
<td colspan="11" class="px-4 py-8 text-sm text-center text-slate-500">Chưa có dữ liệu lịch sử mượn / xuất vật tư.</td>
|
|
</tr>
|
|
`;
|
|
}
|
|
|
|
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 `
|
|
<tr class="hover:bg-slate-50/80 transition-colors">
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${this.formatDateTime(item?.ExportedDate || item?.CreatedDate)}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700">${this.escapeHtml(consumableLabel)}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${this.escapeHtml(destination)}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700 font-semibold text-right">${exportedQuantity}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 text-right">${returnedQuantity}</td>
|
|
<td class="px-4 py-3 text-sm font-semibold text-slate-700 text-right">${remainingQuantity}</td>
|
|
<td class="px-4 py-3 text-sm align-top"><span class="inline-block px-2 py-1 rounded text-xs font-semibold whitespace-nowrap ${statusMeta.className}">${statusMeta.label}</span>${returnRequestNotice}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${this.escapeHtml(item?.ExportedByName || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${this.escapeHtml(balanceLabel)}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-pre-line">${this.escapeHtml(historyNote)}</td>
|
|
<td class="px-4 py-3 text-right">
|
|
${returnAction ? `
|
|
<button
|
|
class="return-consumable-export inline-flex items-center gap-1 px-2.5 py-1.5 rounded-lg text-xs font-bold transition-colors bg-primary/10 text-primary hover:bg-primary/5"
|
|
data-export-history-id="${item?.ExportHistoryId || ''}"
|
|
data-return-mode="${returnAction.mode}"
|
|
title="${this.escapeHtml(returnAction.title)}"
|
|
>
|
|
<span class="material-symbols-outlined text-base">assignment_return</span>
|
|
${returnAction.label}
|
|
</button>
|
|
` : '<span class="text-xs text-slate-400">-</span>'}
|
|
</td>
|
|
</tr>
|
|
`;
|
|
}).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 = `
|
|
<tr>
|
|
<td colspan="11" class="px-4 py-8 text-sm text-center text-slate-500">Đang tải lịch sử mượn / xuất...</td>
|
|
</tr>
|
|
`;
|
|
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 `
|
|
<tr>
|
|
<td colspan="10" class="px-4 py-8 text-sm text-center text-slate-500">Chưa có dữ liệu tài sản hỏng/thanh lý.</td>
|
|
</tr>
|
|
`;
|
|
}
|
|
|
|
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 `
|
|
<tr class="hover:bg-slate-50/80 transition-colors">
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${this.formatDateTime(item?.ActionDate || item?.CreatedDate)}</td>
|
|
<td class="px-4 py-3 text-sm">
|
|
<span class="inline-flex items-center px-2 py-1 rounded text-xs font-semibold ${typeMeta.className}">${typeMeta.label}</span>
|
|
</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700">${this.escapeHtml(assetLabel)}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700 font-semibold">${this.escapeHtml(quantityLabel)}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${Number(item?.PreviousQuantity) || 0} -> ${Number(item?.NextQuantity) || 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${Number(item?.PreviousEndingBalance) || 0} -> ${Number(item?.NextEndingBalance) || 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${Number(item?.PreviousNewQuantity) || 0} -> ${Number(item?.NextNewQuantity) || 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${Number(item?.PreviousUsedQuantity) || 0} -> ${Number(item?.NextUsedQuantity) || 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${this.escapeHtml(item?.CreatedByName || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-pre-line">${this.escapeHtml(item?.ActionNote || '-')}</td>
|
|
</tr>
|
|
`;
|
|
}).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 = `
|
|
<tr>
|
|
<td colspan="10" class="px-4 py-8 text-sm text-center text-slate-500">Đang tải dữ liệu tài sản hỏng/thanh lý...</td>
|
|
</tr>
|
|
`;
|
|
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,
|
|
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 `
|
|
<div class="mt-1.5 max-w-[280px] rounded-lg border border-red-200 bg-red-50 px-2.5 py-2 text-xs text-red-700 whitespace-normal">
|
|
<div class="font-extrabold">Đơn trả #${requestId} đã bị từ chối</div>
|
|
<div class="mt-0.5"><span class="font-bold">Lý do:</span> ${this.escapeHtml(rejectReason)}</div>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
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');
|
|
}
|
|
}
|
|
}
|
|
|
|
renderDashboard() {
|
|
return `
|
|
<div class="dashboard-page flex-1 flex flex-col p-4 md:p-6 space-y-6 min-h-0 overflow-auto">
|
|
<!-- Title and Stats -->
|
|
<div class="dashboard-header flex items-end justify-between shrink-0">
|
|
<div>
|
|
<h1 class="text-2xl font-extrabold text-on-surface tracking-tight leading-none">System Overview</h1>
|
|
<p class="text-xs text-on-surface-variant font-medium mt-1">Account & Service Management</p>
|
|
</div>
|
|
<div class="dashboard-actions flex gap-2">
|
|
<a href="#accounts" class="py-1.5 px-3 bg-primary text-on-primary rounded-lg text-xs font-bold flex items-center gap-1.5 shadow-sm active:scale-95 duration-100">
|
|
<span class="material-symbols-outlined text-sm">add</span>
|
|
Add Account
|
|
</a>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Metric Grid -->
|
|
<div class="dashboard-stats grid grid-cols-4 gap-4 shrink-0">
|
|
<div class="bg-surface-container-lowest p-4 rounded-xl border border-outline-variant/15 flex flex-col">
|
|
<span class="text-[10px] font-bold text-on-surface-variant uppercase tracking-wider mb-2">Applications</span>
|
|
<div class="flex items-baseline justify-between">
|
|
<span class="text-2xl font-black text-on-surface">${this.applications.length}</span>
|
|
<span class="text-[10px] font-bold text-on-surface-variant">${this.applications.filter(a => (a.Status || a.status) === 'online').length} Active</span>
|
|
</div>
|
|
</div>
|
|
<div class="bg-surface-container-lowest p-4 rounded-xl border border-outline-variant/15 flex flex-col">
|
|
<span class="text-[10px] font-bold text-on-surface-variant uppercase tracking-wider mb-2">Total Accounts</span>
|
|
<div class="flex items-baseline justify-between">
|
|
<span class="text-2xl font-black text-on-surface">${this.accounts.length}</span>
|
|
<span class="text-[10px] font-bold text-on-surface-variant/40">Managed</span>
|
|
</div>
|
|
</div>
|
|
<div class="bg-surface-container-lowest p-4 rounded-xl border border-outline-variant/15 flex flex-col">
|
|
<span class="text-[10px] font-bold text-on-surface-variant uppercase tracking-wider mb-2">Last Updated</span>
|
|
<div class="flex items-baseline justify-between">
|
|
<span class="text-sm font-black text-on-surface">${APP_DATE_FORMATTER.format(new Date())}</span>
|
|
</div>
|
|
</div>
|
|
<div class="bg-primary-container/10 p-4 rounded-xl border border-primary/20 flex flex-col">
|
|
<span class="text-[10px] font-bold text-primary uppercase tracking-wider mb-2">Status</span>
|
|
<div class="flex items-baseline justify-between">
|
|
<span class="text-lg font-black text-primary">Operational</span>
|
|
<span class="material-symbols-outlined text-primary text-sm">check_circle</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Recent Activity -->
|
|
<div class="bg-surface-container-lowest rounded-xl p-5 border border-outline-variant/15 flex flex-col flex-1 min-h-0">
|
|
<div class="flex items-center justify-between mb-4 shrink-0">
|
|
<h3 class="text-sm font-bold text-on-surface flex items-center gap-2">
|
|
<span class="material-symbols-outlined text-primary text-base">history</span>
|
|
Recent Accounts
|
|
</h3>
|
|
</div>
|
|
${this.accounts.length > 0 ? `
|
|
<div class="flex-1 overflow-y-auto space-y-2">
|
|
${this.accounts.slice(-5).reverse().map(acc => {
|
|
const username = acc.AccountUsername || acc.username || '-';
|
|
const service = acc.AppName || acc.service || '-';
|
|
const owner = acc.Email || acc.owner || this.currentUser?.Username || this.currentUser?.username || '-';
|
|
return `
|
|
<div class="flex gap-3 p-3 bg-surface-container-low/50 rounded-lg border-l-2 border-primary/50">
|
|
<div class="flex-1 min-w-0">
|
|
<p class="text-[11px] font-bold text-on-surface truncate">${username}</p>
|
|
<p class="text-[9px] text-on-surface-variant">${service} ? ${owner}</p>
|
|
</div>
|
|
</div>
|
|
`;}).join('')}
|
|
</div>
|
|
` : `
|
|
<div class="flex-1 flex items-center justify-center text-center">
|
|
<p class="text-sm text-on-surface-variant">No accounts yet. <a href="#accounts" class="text-primary font-bold">Create one</a></p>
|
|
</div>
|
|
`}
|
|
</div>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
getAccountsContent() {
|
|
const filteredAccounts = this.getFilteredAccounts();
|
|
const currentUserId = this.getUserId();
|
|
const pageInfo = this.getPaged(filteredAccounts, this.accountPage, this.accountPageSize);
|
|
this.accountPage = pageInfo.current;
|
|
return `
|
|
<div class="accounts-page p-4 md:p-6 flex flex-col h-full overflow-hidden">
|
|
<!-- Page Header -->
|
|
<div class="page-header flex items-center justify-between mb-4 shrink-0">
|
|
<div>
|
|
<h1 class="text-xl font-extrabold text-on-surface tracking-tight">Accounts Management</h1>
|
|
<p class="text-[10px] text-on-surface-variant uppercase font-semibold tracking-widest mt-0.5">Administrative Access Control</p>
|
|
</div>
|
|
<button id="addAccountBtn" class="bg-primary hover:bg-primary-dim text-on-primary px-3 py-1.5 rounded-lg text-xs font-bold shadow-sm flex items-center gap-1.5 transition-all active:scale-95">
|
|
<span class="material-symbols-outlined text-base">person_add</span>
|
|
Add Account
|
|
</button>
|
|
</div>
|
|
|
|
<!-- Filters -->
|
|
<div class="page-filters flex items-center gap-3 mb-4">
|
|
<div class="flex items-center gap-1.5">
|
|
<span class="text-[10px] font-bold uppercase text-on-surface-variant">Service</span>
|
|
<select id="serviceFilter" class="bg-surface-container-low border-slate-200 rounded-md text-[11px] py-1 px-2 pr-6 focus:ring-1 focus:ring-primary shadow-sm">
|
|
<option value="">All Services</option>
|
|
${this.applications.map(app => `<option value="${app.AppId}">${app.Name}</option>`).join('')}
|
|
</select>
|
|
</div>
|
|
<div class="flex items-center gap-1.5 flex-1">
|
|
<span class="text-[10px] font-bold uppercase text-on-surface-variant">Search</span>
|
|
<input id="accountSearch" class="flex-1 bg-surface-container-low border-slate-200 rounded-md text-[11px] py-1 px-2 focus:ring-1 focus:ring-primary shadow-sm" placeholder="Search by owner, username, service">
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Accounts Table -->
|
|
<div class="flex-1 bg-white rounded-xl border border-slate-200 shadow-sm flex flex-col overflow-hidden min-h-0">
|
|
${pageInfo.data.length > 0 ? `
|
|
<div class="table-wrap overflow-y-auto overflow-x-auto flex-1">
|
|
<table class="w-full text-left border-collapse w-full">
|
|
<thead class="sticky top-0 z-10">
|
|
<tr class="bg-slate-50 border-b border-slate-200">
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Owner</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Username</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Service</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Created Date</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Last Updated</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500 text-right">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody class="divide-y divide-slate-100 accounts-table-body">
|
|
${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
|
|
? `<button class="p-1.5 text-slate-400 transition-colors view-account hover:text-slate-600" data-account-id="${acc.AccountId}" title="View Details">
|
|
<span class="material-symbols-outlined text-lg">info</span>
|
|
</button>
|
|
<button class="p-1.5 text-slate-400 transition-colors edit-account hover:text-primary" data-account-id="${acc.AccountId}" title="Edit">
|
|
<span class="material-symbols-outlined text-lg">edit</span>
|
|
</button>
|
|
<button class="p-1.5 text-slate-400 transition-colors delete-account hover:text-error" data-account-id="${acc.AccountId}" title="Delete">
|
|
<span class="material-symbols-outlined text-lg">delete</span>
|
|
</button>`
|
|
: '<span class="text-slate-400 text-xs">-</span>';
|
|
return `
|
|
<tr class="hover:bg-slate-50/80 transition-colors group account-row" data-account-id="${acc.AccountId}" data-user-id="${acc.UserId}">
|
|
<td class="px-4 py-3 text-sm text-slate-600">${acc.Email || '-'}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${displayAccountUsername}</td>
|
|
<td class="px-4 py-3 text-sm">
|
|
<span class="px-2 py-1 bg-blue-100 text-blue-700 rounded text-xs font-semibold">${acc.AppName || '-'}</span>
|
|
</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${createdDate}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${updatedDate}</td>
|
|
<td class="px-4 py-3 text-right">
|
|
${actionContent}
|
|
</td>
|
|
</tr>
|
|
`;
|
|
}).join('')}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div class="page-pager flex items-center justify-between px-4 py-2 border-t border-slate-200 bg-slate-50 text-xs text-slate-600" id="accountsPager">
|
|
<span>Showing ${pageInfo.start}-${pageInfo.end} of ${pageInfo.total}</span>
|
|
<div class="flex items-center gap-2">
|
|
<button class="account-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === 1 ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current - 1}" ${pageInfo.current === 1 ? 'disabled' : ''}>Prev</button>
|
|
<span class="text-[11px]">Page ${pageInfo.current} / ${pageInfo.totalPages}</span>
|
|
<button class="account-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === pageInfo.totalPages ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current + 1}" ${pageInfo.current === pageInfo.totalPages ? 'disabled' : ''}>Next</button>
|
|
</div>
|
|
</div>
|
|
` : `
|
|
<div class="flex-1 flex items-center justify-center text-center">
|
|
<div>
|
|
<p class="text-sm text-on-surface-variant mb-4">No accounts yet. Create one to get started.</p>
|
|
<button id="addAccountBtn" class="bg-primary hover:bg-primary-dim text-on-primary px-4 py-2 rounded-lg text-sm font-bold">
|
|
<span class="material-symbols-outlined text-base inline mr-2">person_add</span>
|
|
Add First Account
|
|
</button>
|
|
</div>
|
|
</div>
|
|
`}
|
|
</div>
|
|
</div>
|
|
|
|
`;
|
|
}
|
|
|
|
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 `
|
|
<div class="documents-page p-4 md:p-6 w-full h-full overflow-y-auto">
|
|
<div class="page-header flex items-end justify-between gap-4 mb-5">
|
|
<div>
|
|
<div class="flex items-center gap-2 text-red-600 mb-1">
|
|
<span class="material-symbols-outlined">picture_as_pdf</span>
|
|
<span class="text-xs font-extrabold uppercase tracking-widest">Kho tài liệu</span>
|
|
</div>
|
|
<h1 class="text-2xl font-extrabold text-slate-900 dark:text-slate-50">Tài liệu PDF</h1>
|
|
<p class="text-sm text-slate-500 mt-1">Lưu trữ, xem và tải xuống tài liệu dùng chung.</p>
|
|
</div>
|
|
<div class="document-header-actions flex flex-wrap items-center justify-end gap-3">
|
|
<div class="inline-flex h-10 items-center gap-2 whitespace-nowrap px-1 text-xs text-slate-500" aria-label="${this.documents.length} tài liệu, tổng dung lượng ${this.formatFileSize(totalSize)}">
|
|
<span class="material-symbols-outlined text-lg text-slate-400">folder</span>
|
|
<span><strong class="font-extrabold text-slate-700">${this.documents.length}</strong> tài liệu</span>
|
|
<span class="text-slate-300" aria-hidden="true">•</span>
|
|
<span class="font-semibold text-slate-600">${this.formatFileSize(totalSize)}</span>
|
|
</div>
|
|
<button id="openDocumentUploadDialog" type="button" class="inline-flex items-center justify-center gap-2 rounded-xl bg-red-600 px-4 py-2 text-sm font-bold text-white shadow-sm transition hover:bg-red-700 active:scale-95">
|
|
<span class="material-symbols-outlined text-base">upload_file</span>
|
|
<span>Thêm tài liệu</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="rounded-xl border border-slate-200 bg-white shadow-sm overflow-hidden">
|
|
<div class="page-filters flex items-center gap-3 border-b border-slate-200 p-4">
|
|
<div class="relative flex-1">
|
|
<span class="material-symbols-outlined absolute left-3 top-1/2 -translate-y-1/2 text-slate-400">search</span>
|
|
<input id="documentSearch" type="search" value="${this.escapeHtml(this.documentSearchTerm)}" placeholder="Tìm theo tên tài liệu, tên file hoặc người tải lên..."
|
|
class="w-full rounded-lg border-slate-200 py-2 pl-10 pr-3 text-sm focus:border-primary focus:ring-primary" />
|
|
</div>
|
|
<span class="text-xs font-semibold text-slate-500 whitespace-nowrap">${documents.length} kết quả</span>
|
|
</div>
|
|
|
|
${documents.length ? `
|
|
<div class="grid grid-cols-1 xl:grid-cols-2 gap-4 p-4">
|
|
${documents.map(document => {
|
|
const documentId = Number(document?.DocumentId);
|
|
const canDelete = Boolean(document?.CanDelete);
|
|
return `
|
|
<article class="flex min-w-0 gap-4 rounded-xl border border-slate-200 p-4 transition hover:border-red-200 hover:shadow-sm">
|
|
<div class="flex h-12 w-12 shrink-0 items-center justify-center rounded-xl bg-red-100 text-red-600">
|
|
<span class="material-symbols-outlined text-3xl">picture_as_pdf</span>
|
|
</div>
|
|
<div class="min-w-0 flex-1">
|
|
<h2 class="truncate font-extrabold text-slate-800" title="${this.escapeHtml(document?.Title || '')}">${this.escapeHtml(document?.Title || 'Tài liệu PDF')}</h2>
|
|
<p class="mt-0.5 truncate text-xs text-slate-500" title="${this.escapeHtml(document?.OriginalFileName || '')}">${this.escapeHtml(document?.OriginalFileName || '-')}</p>
|
|
<div class="mt-2 flex flex-wrap gap-x-3 gap-y-1 text-[11px] text-slate-500">
|
|
<span class="inline-flex items-center gap-1"><span class="material-symbols-outlined text-sm">hard_drive</span>${this.formatFileSize(document?.FileSize)}</span>
|
|
<span class="inline-flex items-center gap-1"><span class="material-symbols-outlined text-sm">person</span>${this.escapeHtml(document?.UploadedByName || '-')}</span>
|
|
<span class="inline-flex items-center gap-1"><span class="material-symbols-outlined text-sm">schedule</span>${this.formatDateTime(document?.UploadedDate)}</span>
|
|
</div>
|
|
<div class="mt-3 flex flex-wrap items-center gap-2">
|
|
<a href="${this.apiBase}/documents/${documentId}/file" target="_blank" rel="noopener noreferrer" class="inline-flex items-center gap-1.5 rounded-lg bg-primary px-3 py-1.5 text-xs font-bold text-white hover:bg-primary-dim">
|
|
<span class="material-symbols-outlined text-base">visibility</span>Xem PDF
|
|
</a>
|
|
<a href="${this.apiBase}/documents/${documentId}/file?download=1" class="inline-flex items-center gap-1.5 rounded-lg border border-slate-200 px-3 py-1.5 text-xs font-bold text-slate-700 hover:bg-slate-50">
|
|
<span class="material-symbols-outlined text-base">download</span>Tải xuống
|
|
</a>
|
|
${canDelete ? `
|
|
<button type="button" class="delete-document ml-auto inline-flex items-center gap-1 rounded-lg px-2 py-1.5 text-xs font-bold text-red-600 hover:bg-red-50" data-document-id="${documentId}" data-document-title="${this.escapeHtml(document?.Title || document?.OriginalFileName || 'Tài liệu')}">
|
|
<span class="material-symbols-outlined text-base">delete</span>Xóa
|
|
</button>
|
|
` : ''}
|
|
</div>
|
|
</div>
|
|
</article>
|
|
`;
|
|
}).join('')}
|
|
</div>
|
|
` : `
|
|
<div class="flex flex-col items-center justify-center px-6 py-16 text-center">
|
|
<div class="flex h-16 w-16 items-center justify-center rounded-full bg-slate-100 text-slate-400">
|
|
<span class="material-symbols-outlined text-4xl">folder_open</span>
|
|
</div>
|
|
<h2 class="mt-4 font-extrabold text-slate-700">${this.documentSearchTerm ? 'Không tìm thấy tài liệu' : 'Chưa có tài liệu PDF'}</h2>
|
|
<p class="mt-1 max-w-md text-sm text-slate-500">${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.'}</p>
|
|
</div>
|
|
`}
|
|
</div>
|
|
</div>
|
|
|
|
<dialog id="documentUploadDialog" aria-labelledby="documentUploadDialogTitle" class="m-auto w-[min(92vw,34rem)] max-w-none overflow-hidden rounded-2xl border-0 bg-white p-0 text-slate-800 shadow-2xl backdrop:bg-slate-950/50 backdrop:backdrop-blur-sm">
|
|
<form id="documentUploadForm">
|
|
<div class="flex items-start justify-between gap-4 border-b border-slate-200 bg-red-50 px-5 py-4">
|
|
<div class="flex min-w-0 items-start gap-3">
|
|
<div class="flex h-10 w-10 shrink-0 items-center justify-center rounded-xl bg-red-100 text-red-600">
|
|
<span class="material-symbols-outlined">upload_file</span>
|
|
</div>
|
|
<div class="min-w-0">
|
|
<h2 id="documentUploadDialogTitle" class="text-lg font-extrabold text-slate-900">Thêm tài liệu PDF</h2>
|
|
<p class="mt-0.5 text-xs text-slate-500">Tải tài liệu lên kho dùng chung.</p>
|
|
</div>
|
|
</div>
|
|
<button type="button" data-close-document-dialog class="inline-flex h-9 w-9 shrink-0 items-center justify-center rounded-lg text-slate-500 transition hover:bg-white hover:text-slate-800 disabled:cursor-not-allowed disabled:opacity-50" aria-label="Đóng hộp thoại">
|
|
<span class="material-symbols-outlined">close</span>
|
|
</button>
|
|
</div>
|
|
|
|
<div class="space-y-4 px-5 py-5">
|
|
<div>
|
|
<label for="documentFile" class="mb-1.5 block text-sm font-bold text-slate-700">Chọn file PDF <span class="text-red-600">*</span></label>
|
|
<input id="documentFile" name="file" type="file" accept="application/pdf,.pdf" required
|
|
class="block h-11 w-full rounded-lg border border-slate-200 bg-white p-0 pr-3 text-sm file:mr-4 file:h-full file:border-0 file:bg-slate-100 file:px-4 file:font-bold file:text-slate-700 hover:file:bg-slate-200" />
|
|
<p class="mt-1.5 text-xs text-slate-500">Chỉ nhận định dạng PDF, tối đa ${this.documentMaxFileSizeMb} MB.</p>
|
|
</div>
|
|
<div>
|
|
<label for="documentTitle" class="mb-1.5 block text-sm font-bold text-slate-700">Tên hiển thị <span class="font-normal text-slate-400">(không bắt buộc)</span></label>
|
|
<input id="documentTitle" name="title" maxlength="255" placeholder="Mặc định lấy theo tên file"
|
|
class="h-11 w-full rounded-lg border-slate-200 bg-white text-sm focus:border-red-400 focus:ring-red-400" />
|
|
</div>
|
|
</div>
|
|
|
|
<div class="flex flex-col-reverse gap-2 border-t border-slate-200 bg-slate-50 px-5 py-4 sm:flex-row sm:justify-end">
|
|
<button type="button" data-close-document-dialog class="inline-flex h-10 items-center justify-center rounded-lg border border-slate-300 bg-white px-4 text-sm font-bold text-slate-700 transition hover:bg-slate-100 disabled:cursor-not-allowed disabled:opacity-50">Hủy</button>
|
|
<button id="documentUploadSubmit" type="submit" class="inline-flex h-10 items-center justify-center gap-2 rounded-lg bg-red-600 px-5 text-sm font-bold text-white shadow-sm transition hover:bg-red-700 disabled:cursor-not-allowed disabled:opacity-60">
|
|
<span class="material-symbols-outlined text-base">upload_file</span>
|
|
<span>Tải lên</span>
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</dialog>
|
|
`;
|
|
}
|
|
|
|
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)
|
|
? '<div class="mt-1 text-[11px] font-bold text-amber-700">Tài sản cũ</div>'
|
|
: '';
|
|
}
|
|
|
|
async handleDocumentUpload(event) {
|
|
event.preventDefault();
|
|
const form = event.currentTarget;
|
|
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 file PDF');
|
|
return;
|
|
}
|
|
|
|
const extensionIsPdf = String(file.name || '').toLowerCase().endsWith('.pdf');
|
|
if (!extensionIsPdf || (file.type && file.type !== 'application/pdf')) {
|
|
this.notifyFailure('Chỉ chấp nhận file PDF');
|
|
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 PDF');
|
|
} 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 `
|
|
<div class="apps-page flex flex-col p-4 md:p-6 overflow-hidden h-full">
|
|
<!-- Header Section -->
|
|
<div class="page-header flex items-center justify-between gap-6 mb-6 shrink-0">
|
|
<div>
|
|
<h1 class="text-2xl font-extrabold text-on-surface tracking-tight">Applications</h1>
|
|
<p class="text-sm text-on-surface-variant">Manage and monitor active infrastructure services.</p>
|
|
</div>
|
|
<button id="addAppBtn" class="bg-primary hover:bg-primary-dim text-on-primary px-4 py-2 rounded-xl font-bold flex items-center gap-2 transition-all active:scale-95 shadow-sm">
|
|
<span class="material-symbols-outlined">add</span>
|
|
Add New
|
|
</button>
|
|
</div>
|
|
|
|
|
|
|
|
<!-- Applications List -->
|
|
<div class="bg-surface-container-lowest rounded-xl shadow-sm border border-outline-variant/10 overflow-hidden flex flex-col flex-1 min-h-0">
|
|
<div class="page-filters px-4 py-3 border-b border-outline-variant/10 flex items-center gap-2 bg-surface-container-low/40">
|
|
<span class="text-[10px] font-bold uppercase text-on-surface-variant">Search</span>
|
|
<input id="appSearch" class="flex-1 bg-white border border-slate-200 rounded-md text-[11px] py-1 px-2 focus:ring-1 focus:ring-primary shadow-sm" placeholder="Search by name, type, description, url">
|
|
</div>
|
|
<div class="table-wrap overflow-y-auto overflow-x-auto flex-1">
|
|
<table class="w-full text-left border-collapse" style="min-width: 1500px;">
|
|
<thead class="sticky top-0 bg-surface-container-lowest z-10">
|
|
<tr class="bg-surface-container-low/30 border-b border-outline-variant/10">
|
|
<th class="px-6 py-2.5 text-[10px] font-bold text-on-surface-variant uppercase tracking-widest">Name</th>
|
|
<th class="px-6 py-2.5 text-[10px] font-bold text-on-surface-variant uppercase tracking-widest">Type</th>
|
|
<th class="px-6 py-2.5 text-[10px] font-bold text-on-surface-variant uppercase tracking-widest">Description</th>
|
|
<th class="px-6 py-2.5 text-[10px] font-bold text-on-surface-variant uppercase tracking-widest">URL</th>
|
|
<th class="px-6 py-2.5 text-[10px] font-bold text-on-surface-variant uppercase tracking-widest">Status</th>
|
|
<th class="px-6 py-2.5 text-[10px] font-bold text-on-surface-variant uppercase tracking-widest text-right">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody class="divide-y divide-outline-variant/5 apps-table-body">
|
|
${pageInfo.data.map(app => `
|
|
<tr class="hover:bg-surface-container-low/30 transition-colors group">
|
|
<td class="px-6 py-3">
|
|
<div class="flex items-center gap-3">
|
|
<div class="w-8 h-8 rounded bg-surface-container-highest flex items-center justify-center text-primary">
|
|
<span class="material-symbols-outlined text-sm">${app.Icon || 'apps'}</span>
|
|
</div>
|
|
<span class="font-bold text-sm text-on-surface">${app.Name}</span>
|
|
</div>
|
|
</td>
|
|
<td class="px-6 py-3">
|
|
<span class="px-2 py-0.5 rounded-full text-[9px] font-black uppercase bg-surface-container-highest text-on-surface-variant">${app.Type}</span>
|
|
</td>
|
|
<td class="px-6 py-3 text-sm text-on-surface-variant max-w-xs truncate" title="${app.Description || ''}">${app.Description || '-'}</td>
|
|
<td class="px-6 py-3 text-sm text-primary max-w-xs truncate">${(app.Url || app.url) ? `<a href="${app.Url || app.url}" target="_blank" class="underline">${app.Url || app.url}</a>` : '-'}</td>
|
|
<td class="px-6 py-3">
|
|
<div class="flex items-center gap-1.5">
|
|
<div class="w-1.5 h-1.5 rounded-full ${(app.Status || app.status) === 'online' ? 'bg-primary' : 'bg-error'} ring-2 ${(app.Status || app.status) === 'online' ? 'ring-primary/20' : 'ring-error/20'}"></div>
|
|
<span class="text-xs font-medium ${(app.Status || app.status) === 'online' ? 'text-on-primary-fixed-variant' : 'text-error'}">${(app.Status || app.status) === 'online' ? 'Online' : 'Offline'}</span>
|
|
</div>
|
|
</td>
|
|
<td class="px-6 py-3 text-right">
|
|
<div class="flex items-center justify-end gap-1">
|
|
<button class="p-1.5 text-on-surface-variant hover:text-on-surface transition-colors view-app" data-app-id="${app.AppId}" title="View Details">
|
|
<span class="material-symbols-outlined text-lg">info</span>
|
|
</button>
|
|
<button class="p-1.5 text-on-surface-variant hover:text-primary transition-colors edit-app" data-app-id="${app.AppId}">
|
|
<span class="material-symbols-outlined text-lg">edit</span>
|
|
</button>
|
|
<button class="p-1.5 text-on-surface-variant hover:text-error transition-colors delete-app" data-app-id="${app.AppId}">
|
|
<span class="material-symbols-outlined text-lg">delete</span>
|
|
</button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
`).join('')}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div class="page-pager flex items-center justify-between px-4 py-2 border-t border-outline-variant/10 bg-surface-container-low/60 text-xs text-on-surface-variant" id="appsPager">
|
|
<span>Showing ${pageInfo.start}-${pageInfo.end} of ${pageInfo.total}</span>
|
|
<div class="flex items-center gap-2">
|
|
<button class="app-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === 1 ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current - 1}" ${pageInfo.current === 1 ? 'disabled' : ''}>Prev</button>
|
|
<span class="text-[11px]">Page ${pageInfo.current} / ${pageInfo.totalPages}</span>
|
|
<button class="app-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === pageInfo.totalPages ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current + 1}" ${pageInfo.current === pageInfo.totalPages ? 'disabled' : ''}>Next</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
`;
|
|
}
|
|
|
|
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, '"')
|
|
.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 => `<div class="leading-5 whitespace-nowrap">${this.escapeHtml(item)}</div>`)
|
|
.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 `
|
|
<tr>
|
|
<td colspan="4" class="px-4 py-8 text-sm text-center text-slate-500">Chưa có phòng ban nào.</td>
|
|
</tr>
|
|
`;
|
|
}
|
|
|
|
return departments.map((item, index) => {
|
|
const departmentId = Number(item?.DepartmentId);
|
|
const assetCount = Number(item?.AssetCount) || 0;
|
|
const departmentName = this.escapeHtml(item?.DepartmentName || '-');
|
|
|
|
return `
|
|
<tr class="hover:bg-slate-50/80 transition-colors">
|
|
<td class="px-4 py-3 text-sm text-slate-600">${index + 1}</td>
|
|
<td class="px-4 py-3 text-sm font-semibold text-slate-700">${departmentName}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${assetCount}</td>
|
|
<td class="px-4 py-3 text-right">
|
|
<div class="inline-flex items-center gap-1.5">
|
|
<button
|
|
class="p-1.5 text-slate-400 transition-colors edit-asset-department ${canManageAssets ? 'hover:text-primary' : 'opacity-40 cursor-not-allowed'}"
|
|
data-department-id="${departmentId}"
|
|
${canManageAssets ? '' : 'disabled'}
|
|
title="${canManageAssets ? 'Sửa phòng ban' : 'Chỉ xem'}"
|
|
>
|
|
<span class="material-symbols-outlined text-lg">edit</span>
|
|
</button>
|
|
<button
|
|
class="p-1.5 text-slate-400 transition-colors delete-asset-department ${canManageAssets ? 'hover:text-error' : 'opacity-40 cursor-not-allowed'}"
|
|
data-department-id="${departmentId}"
|
|
${canManageAssets ? '' : 'disabled'}
|
|
title="${canManageAssets ? 'Xóa phòng ban' : 'Chỉ xem'}"
|
|
>
|
|
<span class="material-symbols-outlined text-lg">delete</span>
|
|
</button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
`;
|
|
}).join('');
|
|
}
|
|
|
|
getAssetDepartmentsContent() {
|
|
const filteredDepartments = this.getFilteredAssetDepartments();
|
|
const canManageAssets = this.canCurrentUserManageAssets();
|
|
|
|
return `
|
|
<div class="asset-departments-page flex flex-col p-4 md:p-6 overflow-hidden h-full">
|
|
<div class="page-header flex items-center justify-between gap-4 mb-5 shrink-0">
|
|
<div>
|
|
<h1 class="text-2xl font-extrabold text-on-surface tracking-tight">Quản Lý Phòng Ban</h1>
|
|
<p class="text-sm text-on-surface-variant">Thêm, sửa, xóa danh mục phòng ban sử dụng trong tài sản.</p>
|
|
</div>
|
|
<button
|
|
id="addAssetDepartmentBtn"
|
|
class="bg-primary text-on-primary px-4 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 transition-all active:scale-95 ${canManageAssets ? 'hover:bg-primary-dim' : 'opacity-50 cursor-not-allowed'}"
|
|
${canManageAssets ? '' : 'disabled'}
|
|
>
|
|
<span class="material-symbols-outlined text-base">add_home_work</span>
|
|
Thêm phòng ban
|
|
</button>
|
|
</div>
|
|
|
|
<div class="page-filters flex items-center gap-3 mb-4 shrink-0">
|
|
<div class="flex items-center gap-1.5 flex-1">
|
|
<span class="text-[10px] font-bold uppercase text-on-surface-variant">Tìm kiếm</span>
|
|
<input
|
|
id="assetDepartmentSearch"
|
|
value="${this.escapeHtml(this.assetDepartmentSearchTerm)}"
|
|
class="flex-1 bg-surface-container-low border-slate-200 rounded-md text-[11px] py-1 px-2 focus:ring-1 focus:ring-primary shadow-sm"
|
|
placeholder="Nhập tên phòng ban..."
|
|
>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="flex-1 bg-white rounded-xl border border-slate-200 shadow-sm flex flex-col overflow-hidden min-h-0">
|
|
<div class="table-wrap overflow-y-auto overflow-x-auto flex-1">
|
|
<table class="w-full text-left border-collapse">
|
|
<thead class="sticky top-0 z-10 bg-slate-50 border-b border-slate-200">
|
|
<tr>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">STT</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Phòng ban</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Sd tài sản</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500 text-right">Thao tác</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody class="divide-y divide-slate-100 asset-departments-table-body">
|
|
${this.buildAssetDepartmentsRowsHtml(filteredDepartments)}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div class="px-4 py-2 border-t border-slate-200 bg-slate-50 text-xs text-slate-600">
|
|
Tổng phòng ban: <span id="assetDepartmentCount">${filteredDepartments.length}</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
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 `
|
|
<tr>
|
|
<td colspan="4" class="px-4 py-8 text-sm text-center text-slate-500">Chưa có dự án nào.</td>
|
|
</tr>
|
|
`;
|
|
}
|
|
|
|
return projects.map((item, index) => {
|
|
const projectId = Number(item?.ProjectId);
|
|
const assetCount = Number(item?.AssetCount) || 0;
|
|
const projectName = this.escapeHtml(item?.ProjectName || '-');
|
|
|
|
return `
|
|
<tr class="hover:bg-slate-50/80 transition-colors">
|
|
<td class="px-4 py-3 text-sm text-slate-600">${index + 1}</td>
|
|
<td class="px-4 py-3 text-sm font-semibold text-slate-700">${projectName}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${assetCount}</td>
|
|
<td class="px-4 py-3 text-right">
|
|
<div class="inline-flex items-center gap-1.5">
|
|
<button
|
|
class="p-1.5 text-slate-400 transition-colors edit-asset-project ${canManageAssets ? 'hover:text-primary' : 'opacity-40 cursor-not-allowed'}"
|
|
data-project-id="${projectId}"
|
|
${canManageAssets ? '' : 'disabled'}
|
|
title="${canManageAssets ? 'Sửa dự án' : 'Chỉ xem'}"
|
|
>
|
|
<span class="material-symbols-outlined text-lg">edit</span>
|
|
</button>
|
|
<button
|
|
class="p-1.5 text-slate-400 transition-colors delete-asset-project ${canManageAssets ? 'hover:text-error' : 'opacity-40 cursor-not-allowed'}"
|
|
data-project-id="${projectId}"
|
|
${canManageAssets ? '' : 'disabled'}
|
|
title="${canManageAssets ? 'Xóa dự án' : 'Chỉ xem'}"
|
|
>
|
|
<span class="material-symbols-outlined text-lg">delete</span>
|
|
</button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
`;
|
|
}).join('');
|
|
}
|
|
|
|
getAssetProjectsContent() {
|
|
const filteredProjects = this.getFilteredAssetProjects();
|
|
const canManageAssets = this.canCurrentUserManageAssets();
|
|
|
|
return `
|
|
<div class="asset-projects-page flex flex-col p-4 md:p-6 overflow-hidden h-full">
|
|
<div class="page-header flex items-center justify-between gap-4 mb-5 shrink-0">
|
|
<div>
|
|
<h1 class="text-2xl font-extrabold text-on-surface tracking-tight">Quản Lý Dự Án</h1>
|
|
<p class="text-sm text-on-surface-variant">Thêm, sửa, xóa danh mục dự án sử dụng trong tài sản.</p>
|
|
</div>
|
|
<button
|
|
id="addAssetProjectBtn"
|
|
class="bg-primary text-on-primary px-4 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 transition-all active:scale-95 ${canManageAssets ? 'hover:bg-primary-dim' : 'opacity-50 cursor-not-allowed'}"
|
|
${canManageAssets ? '' : 'disabled'}
|
|
>
|
|
<span class="material-symbols-outlined text-base">workspaces</span>
|
|
Thêm dự án
|
|
</button>
|
|
</div>
|
|
|
|
<div class="page-filters flex items-center gap-3 mb-4 shrink-0">
|
|
<div class="flex items-center gap-1.5 flex-1">
|
|
<span class="text-[10px] font-bold uppercase text-on-surface-variant">Tìm kiếm</span>
|
|
<input
|
|
id="assetProjectSearch"
|
|
value="${this.escapeHtml(this.assetProjectSearchTerm)}"
|
|
class="flex-1 bg-surface-container-low border-slate-200 rounded-md text-[11px] py-1 px-2 focus:ring-1 focus:ring-primary shadow-sm"
|
|
placeholder="Nhập tên dự án..."
|
|
>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="flex-1 bg-white rounded-xl border border-slate-200 shadow-sm flex flex-col overflow-hidden min-h-0">
|
|
<div class="table-wrap overflow-y-auto overflow-x-auto flex-1">
|
|
<table class="w-full text-left border-collapse">
|
|
<thead class="sticky top-0 z-10 bg-slate-50 border-b border-slate-200">
|
|
<tr>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">STT</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Dự án</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Sd tài sản</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500 text-right">Thao tác</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody class="divide-y divide-slate-100 asset-projects-table-body">
|
|
${this.buildAssetProjectsRowsHtml(filteredProjects)}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div class="px-4 py-2 border-t border-slate-200 bg-slate-50 text-xs text-slate-600">
|
|
Tổng dự án: <span id="assetProjectCount">${filteredProjects.length}</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
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 ? `<div class="text-[11px] text-slate-500">${this.escapeHtml(item.AssetCode)}</div>` : '';
|
|
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'
|
|
? `<div class="mt-1 text-[11px] text-slate-500 whitespace-nowrap">Đã trả ${returnedQuantity}/${borrowQuantity}, còn ${remainingQuantity}</div>`
|
|
: '';
|
|
const detailActionHtml = `
|
|
<button
|
|
class="asset-borrow-detail-btn inline-flex h-8 w-8 items-center justify-center rounded-md border border-slate-200 text-slate-500 hover:bg-slate-50 hover:text-primary transition-colors"
|
|
data-request-id="${requestId}"
|
|
title="Xem chi tiết"
|
|
aria-label="Xem chi tiết đơn"
|
|
>
|
|
<span class="material-symbols-outlined text-base">info</span>
|
|
</button>
|
|
`;
|
|
const returnActionHtml = canCreateReturn
|
|
? `<button class="asset-borrow-return-btn inline-flex items-center gap-1.5 px-3 py-1.5 rounded-md bg-emerald-600 hover:bg-emerald-700 text-white text-xs font-bold whitespace-nowrap" data-request-id="${requestId}" title="Tạo đơn trả từ đơn mượn này">
|
|
<span class="material-symbols-outlined text-sm">assignment_return</span>
|
|
Trả tài sản
|
|
</button>`
|
|
: '';
|
|
const cancelActionHtml = canCancel
|
|
? `<button class="asset-borrow-cancel-btn px-3 py-1.5 rounded-md bg-red-600 hover:bg-red-700 text-white text-xs font-bold whitespace-nowrap" data-request-id="${requestId}">Hủy đơn</button>`
|
|
: '';
|
|
const actionHtml = returnActionHtml || cancelActionHtml
|
|
? `<div class="flex items-center gap-2">${returnActionHtml}${cancelActionHtml}</div>`
|
|
: `<span class="text-xs text-slate-400">-</span>`;
|
|
|
|
return `
|
|
<tr class="hover:bg-slate-50/80 transition-colors">
|
|
<td class="px-4 py-3 text-sm text-slate-600">${rowNumber}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700 font-semibold">${this.escapeHtml(item.BorrowerName || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700">
|
|
<div>${this.escapeHtml(assetName)}</div>
|
|
${assetCode}
|
|
${this.getArchivedAssetBadgeHtml(item)}
|
|
</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">
|
|
<span class="inline-flex items-center px-2 py-1 rounded-md text-xs font-bold whitespace-nowrap ${typeMeta.className}">${typeMeta.label}</span>
|
|
</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">
|
|
<span class="inline-flex items-center px-2 py-1 rounded-md text-xs font-bold whitespace-nowrap ${statusMeta.className}">${statusMeta.label}</span>
|
|
${returnProgressHtml}
|
|
</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${this.escapeHtml(item.Unit || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${Number(item.BorrowQuantity) || 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${this.formatDateOnly(item.BorrowDate)}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 max-w-xs">${this.escapeHtml(note || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 max-w-xs">${this.escapeHtml(rejectReason || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 text-center">${detailActionHtml}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${actionHtml}</td>
|
|
</tr>
|
|
`;
|
|
}
|
|
|
|
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 `
|
|
<tr>
|
|
<td colspan="12" class="px-4 py-8 text-sm text-center text-slate-500">Chưa có đơn mượn/trả tài sản nào.</td>
|
|
</tr>
|
|
`;
|
|
}
|
|
|
|
renderAssetBorrowsPager(pageInfo) {
|
|
const pager = document.getElementById('assetBorrowsPager');
|
|
if (!pager) {
|
|
return;
|
|
}
|
|
|
|
pager.innerHTML = `
|
|
<span>Hiển thị ${pageInfo.start}-${pageInfo.end} / ${pageInfo.total}</span>
|
|
<div class="flex items-center gap-2">
|
|
<button class="asset-borrow-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === 1 ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current - 1}" ${pageInfo.current === 1 ? 'disabled' : ''}>Trước</button>
|
|
<span class="text-[11px]">Trang ${pageInfo.current} / ${pageInfo.totalPages}</span>
|
|
<button class="asset-borrow-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === pageInfo.totalPages ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current + 1}" ${pageInfo.current === pageInfo.totalPages ? 'disabled' : ''}>Tiếp</button>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
renderMyBorrowedAssetsPager(pageInfo) {
|
|
const pager = document.getElementById('myBorrowedAssetsPager');
|
|
if (!pager) {
|
|
return;
|
|
}
|
|
|
|
pager.innerHTML = `
|
|
<span>Hiển thị ${pageInfo.start}-${pageInfo.end} / ${pageInfo.total}</span>
|
|
<div class="flex items-center gap-2">
|
|
<button class="my-borrowed-asset-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === 1 ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current - 1}" ${pageInfo.current === 1 ? 'disabled' : ''}>Trước</button>
|
|
<span class="text-[11px]">Trang ${pageInfo.current} / ${pageInfo.totalPages}</span>
|
|
<button class="my-borrowed-asset-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === pageInfo.totalPages ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current + 1}" ${pageInfo.current === pageInfo.totalPages ? 'disabled' : ''}>Tiếp</button>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
getMyBorrowedAssetsContent() {
|
|
const filteredAssets = this.getFilteredMyBorrowedAssets();
|
|
const pageInfo = this.getPaged(filteredAssets, this.myBorrowedAssetPage, this.myBorrowedAssetPageSize);
|
|
this.myBorrowedAssetPage = pageInfo.current;
|
|
|
|
return `
|
|
<div class="my-borrowed-assets-page flex flex-col p-4 md:p-6 overflow-hidden h-full">
|
|
<div class="page-header flex items-center justify-between gap-4 mb-5 shrink-0">
|
|
<div>
|
|
<h1 class="text-2xl font-extrabold text-on-surface tracking-tight">Tài sản đang mượn</h1>
|
|
<p class="text-sm text-on-surface-variant">Danh sách tài sản bạn đang mượn theo tài khoản đăng nhập.</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="page-filters flex items-center gap-3 mb-4 shrink-0">
|
|
<div class="flex items-center gap-1.5 flex-1">
|
|
<span class="text-[10px] font-bold uppercase text-on-surface-variant">Tìm kiếm</span>
|
|
<input
|
|
id="myBorrowedAssetSearch"
|
|
value="${this.escapeHtml(this.myBorrowedAssetSearchTerm)}"
|
|
class="flex-1 bg-surface-container-low border-slate-200 rounded-md text-[11px] py-1 px-2 focus:ring-1 focus:ring-primary shadow-sm"
|
|
placeholder="Mã, tên tài sản, dự án, vị trí..."
|
|
>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="flex-1 bg-white rounded-xl border border-slate-200 shadow-sm flex flex-col overflow-hidden min-h-0">
|
|
<div class="table-wrap overflow-y-auto overflow-x-auto flex-1">
|
|
<table class="w-full text-left border-collapse" style="min-width: 1500px;">
|
|
<thead class="sticky top-0 z-10 bg-slate-50 border-b border-slate-200">
|
|
<tr>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">STT</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Mã tài sản</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Tên tài sản</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Số lượng đang mượn</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Đơn vị</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Dự án</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Vị trí</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500 whitespace-nowrap">Trạng thái</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Ghi chú</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody class="divide-y divide-slate-100 my-borrowed-assets-table-body">
|
|
${pageInfo.data.length > 0 ? pageInfo.data.map((asset, index) => {
|
|
const statusMeta = this.getAssetStatusMeta(asset.Status);
|
|
return `
|
|
<tr class="hover:bg-slate-50/80 transition-colors">
|
|
<td class="px-4 py-3 text-sm text-slate-600">${pageInfo.start + index}</td>
|
|
<td class="px-4 py-3 text-sm font-semibold text-slate-700">${this.escapeHtml(asset.AssetCode || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700">${this.escapeHtml(asset.AssetName || '-')}${this.getArchivedAssetBadgeHtml(asset)}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700 font-bold">${Number(asset.BorrowedQuantityByCurrentUser) || 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${this.escapeHtml(asset.Unit || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${this.escapeHtml(asset.Project || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${this.escapeHtml(asset.Location || '-')}</td>
|
|
<td class="px-4 py-3 text-sm">
|
|
<span class="inline-block px-2 py-1 rounded text-xs font-semibold ${statusMeta.className}">${statusMeta.label}</span>
|
|
</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 max-w-xs truncate" title="${this.escapeHtml(asset.Notes || '')}">${this.escapeHtml(asset.Notes || '-')}</td>
|
|
</tr>
|
|
`;
|
|
}).join('') : `
|
|
<tr>
|
|
<td colspan="9" class="px-4 py-8 text-sm text-center text-slate-500">Hiện tại bạn chưa mượn tài sản nào.</td>
|
|
</tr>
|
|
`}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div class="page-pager flex items-center justify-between px-4 py-2 border-t border-slate-200 bg-slate-50 text-xs text-slate-600" id="myBorrowedAssetsPager">
|
|
<span>Hiển thị ${pageInfo.start}-${pageInfo.end} / ${pageInfo.total}</span>
|
|
<div class="flex items-center gap-2">
|
|
<button class="my-borrowed-asset-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === 1 ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current - 1}" ${pageInfo.current === 1 ? 'disabled' : ''}>Trước</button>
|
|
<span class="text-[11px]">Trang ${pageInfo.current} / ${pageInfo.totalPages}</span>
|
|
<button class="my-borrowed-asset-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === pageInfo.totalPages ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current + 1}" ${pageInfo.current === pageInfo.totalPages ? 'disabled' : ''}>Tiếp</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
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 = `
|
|
<tr>
|
|
<td colspan="9" class="px-4 py-8 text-sm text-center text-slate-500">Hiện tại bạn chưa mượn tài sản nào.</td>
|
|
</tr>
|
|
`;
|
|
} else {
|
|
tbody.innerHTML = pageInfo.data.map((asset, index) => {
|
|
const statusMeta = this.getAssetStatusMeta(asset.Status);
|
|
return `
|
|
<tr class="hover:bg-slate-50/80 transition-colors">
|
|
<td class="px-4 py-3 text-sm text-slate-600">${pageInfo.start + index}</td>
|
|
<td class="px-4 py-3 text-sm font-semibold text-slate-700">${this.escapeHtml(asset.AssetCode || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700">${this.escapeHtml(asset.AssetName || '-')}${this.getArchivedAssetBadgeHtml(asset)}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700 font-bold">${Number(asset.BorrowedQuantityByCurrentUser) || 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${this.escapeHtml(asset.Unit || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${this.escapeHtml(asset.Project || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${this.escapeHtml(asset.Location || '-')}</td>
|
|
<td class="px-4 py-3 text-sm">
|
|
<span class="inline-block px-2 py-1 rounded text-xs font-semibold ${statusMeta.className}">${statusMeta.label}</span>
|
|
</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 max-w-xs truncate" title="${this.escapeHtml(asset.Notes || '')}">${this.escapeHtml(asset.Notes || '-')}</td>
|
|
</tr>
|
|
`;
|
|
}).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 `
|
|
<div class="asset-borrows-page flex flex-col p-4 md:p-6 overflow-hidden h-full">
|
|
<div class="page-header flex items-center justify-between gap-4 mb-5 shrink-0">
|
|
<div>
|
|
<h1 class="text-2xl font-extrabold text-on-surface tracking-tight">Mượn/Trả tài sản</h1>
|
|
<p class="text-sm text-on-surface-variant">Lịch sử mượn/trả gồm cả tài sản cũ đã xóa khỏi danh mục.</p>
|
|
</div>
|
|
<div class="compact-page-actions flex flex-wrap items-center gap-2">
|
|
<button
|
|
id="openArchivedAssetBorrowsBtn"
|
|
class="border border-amber-300 text-amber-700 px-4 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 hover:bg-amber-50"
|
|
>
|
|
<span class="material-symbols-outlined text-base">history</span>
|
|
Tài sản cũ
|
|
</button>
|
|
<button
|
|
id="addAssetBorrowRequestBtn"
|
|
class="bg-primary text-on-primary px-4 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 transition-all active:scale-95 hover:bg-primary-dim"
|
|
>
|
|
<span class="material-symbols-outlined text-base">assignment_returned</span>
|
|
Tạo đơn mượn
|
|
</button>
|
|
<button
|
|
id="addAssetReturnRequestBtn"
|
|
class="border border-emerald-300 text-emerald-700 px-4 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 transition-all active:scale-95 hover:bg-emerald-50"
|
|
>
|
|
<span class="material-symbols-outlined text-base">assignment_return</span>
|
|
Tạo đơn trả
|
|
</button>
|
|
${canManageAssets ? `
|
|
<button
|
|
id="openPendingAssetBorrowsBtn"
|
|
class="relative border border-amber-300 text-amber-700 px-4 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 transition-all active:scale-95 hover:bg-amber-50"
|
|
>
|
|
<span class="material-symbols-outlined text-base">notifications_active</span>
|
|
Đơn chờ
|
|
<span id="pendingAssetBorrowsCountBadge" class="${pendingCount > 0 ? '' : 'hidden'} absolute -top-2.5 -right-2.5 min-w-[22px] h-[22px] px-1.5 rounded-[999px] bg-red-600 text-white text-xs font-extrabold leading-[22px] text-center ring-2 ring-white">${pendingCount > 99 ? '99+' : pendingCount}</span>
|
|
</button>
|
|
` : ''}
|
|
</div>
|
|
</div>
|
|
|
|
<div class="page-filters compact-page-filters asset-borrow-filter-bar flex items-center gap-3 mb-4 shrink-0">
|
|
<div class="flex items-center gap-1.5">
|
|
<span class="text-[10px] font-bold uppercase text-on-surface-variant">Đơn</span>
|
|
<select id="assetBorrowTypeFilter" class="bg-surface-container-low border-slate-200 rounded-md text-[11px] py-1 px-2 pr-6 focus:ring-1 focus:ring-primary shadow-sm">
|
|
<option value="" ${!this.assetBorrowTypeFilter ? 'selected' : ''}>Tất cả</option>
|
|
<option value="outstanding" ${this.assetBorrowTypeFilter === 'outstanding' ? 'selected' : ''}>Còn đang mượn</option>
|
|
<option value="borrow" ${this.assetBorrowTypeFilter === 'borrow' ? 'selected' : ''}>Mượn tài sản</option>
|
|
<option value="return" ${this.assetBorrowTypeFilter === 'return' ? 'selected' : ''}>Trả tài sản</option>
|
|
</select>
|
|
</div>
|
|
<div class="flex items-center gap-1.5">
|
|
<label for="assetBorrowCatalogFilter" class="text-[10px] font-bold uppercase text-on-surface-variant">Tài sản</label>
|
|
<select id="assetBorrowCatalogFilter" class="bg-surface-container-low border-slate-200 rounded-md text-[11px] py-1 px-2 pr-6 focus:ring-1 focus:ring-primary shadow-sm">
|
|
<option value="" ${!this.assetBorrowCatalogFilter ? 'selected' : ''}>Tất cả</option>
|
|
<option value="archived" ${this.assetBorrowCatalogFilter === 'archived' ? 'selected' : ''}>Tài sản cũ</option>
|
|
<option value="current" ${this.assetBorrowCatalogFilter === 'current' ? 'selected' : ''}>Danh mục hiện tại</option>
|
|
</select>
|
|
</div>
|
|
<div class="compact-filter-search flex items-center gap-1.5 flex-1">
|
|
<span class="text-[10px] font-bold uppercase text-on-surface-variant">Tìm kiếm</span>
|
|
<input
|
|
id="assetBorrowSearch"
|
|
value="${this.escapeHtml(this.assetBorrowSearchTerm)}"
|
|
class="flex-1 bg-surface-container-low border-slate-200 rounded-md text-[11px] py-1 px-2 focus:ring-1 focus:ring-primary shadow-sm"
|
|
placeholder="Người tạo, tài sản, danh mục, trạng thái, lý do..."
|
|
>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="flex-1 bg-white rounded-xl border border-slate-200 shadow-sm flex flex-col overflow-hidden min-h-0">
|
|
<div class="table-wrap overflow-y-auto overflow-x-auto flex-1">
|
|
<table class="w-full text-left border-collapse">
|
|
<thead class="sticky top-0 z-10 bg-slate-50 border-b border-slate-200">
|
|
<tr>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">STT</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Tên đầy đủ</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Tài sản</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500 whitespace-nowrap">Danh mục</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500 whitespace-nowrap">Trạng thái</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Đơn vị</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Số lượng</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Ngày</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Ghi chú</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Lý do</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500 text-center">Chi tiết</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Hành động</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody class="divide-y divide-slate-100 asset-borrows-table-body">
|
|
${pageInfo.data.length > 0 ? pageInfo.data.map((item, index) => this.buildAssetBorrowRowHtml(item, pageInfo.start + index)).join('') : this.buildAssetBorrowEmptyRowHtml()}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div class="page-pager flex items-center justify-between px-4 py-2 border-t border-slate-200 bg-slate-50 text-xs text-slate-600" id="assetBorrowsPager">
|
|
<span>Hiển thị ${pageInfo.start}-${pageInfo.end} / ${pageInfo.total}</span>
|
|
<div class="flex items-center gap-2">
|
|
<button class="asset-borrow-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === 1 ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current - 1}" ${pageInfo.current === 1 ? 'disabled' : ''}>Trước</button>
|
|
<span class="text-[11px]">Trang ${pageInfo.current} / ${pageInfo.totalPages}</span>
|
|
<button class="asset-borrow-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === pageInfo.totalPages ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current + 1}" ${pageInfo.current === pageInfo.totalPages ? 'disabled' : ''}>Tiếp</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
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
|
|
? `<div><span class="font-bold">Đã trả:</span> ${returnedQuantity}/${borrowQuantity}${remainingQuantity > 0 ? `, còn ${remainingQuantity}` : ''}</div>`
|
|
: '';
|
|
|
|
return `
|
|
<div class="w-full min-w-0 overflow-hidden rounded-lg border border-slate-200 bg-white p-4 text-sm text-slate-700 space-y-2 break-words">
|
|
<div class="flex flex-wrap items-center gap-2 min-w-0">
|
|
<span class="inline-flex items-center px-2 py-1 rounded-md text-xs font-bold ${typeMeta.className}">${typeMeta.label}</span>
|
|
<span class="inline-flex items-center px-2 py-1 rounded-md text-xs font-bold ${statusMeta.className}">${statusMeta.label}</span>
|
|
<span class="ml-auto text-xs font-extrabold text-slate-500">#${Number(item?.BorrowId) || '-'}</span>
|
|
</div>
|
|
<div class="grid grid-cols-1 md:grid-cols-2 gap-x-4 gap-y-1 min-w-0">
|
|
<div class="min-w-0"><span class="font-bold">Người tạo:</span> ${this.escapeHtml(item?.BorrowerName || '-')}</div>
|
|
<div class="min-w-0"><span class="font-bold">${dateLabel}:</span> ${this.formatDateOnly(item?.BorrowDate)}</div>
|
|
<div class="md:col-span-2 min-w-0"><span class="font-bold">Tài sản:</span> ${this.escapeHtml(assetName)}</div>
|
|
<div class="min-w-0"><span class="font-bold">Số lượng:</span> ${Number(item?.BorrowQuantity) || 0}</div>
|
|
<div class="min-w-0"><span class="font-bold">Đơn vị:</span> ${this.escapeHtml(item?.Unit || '-')}</div>
|
|
${returnSummary}
|
|
<div class="min-w-0"><span class="font-bold">Ngày xử lý:</span> ${this.formatDateTime(item?.ProcessedDate)}</div>
|
|
<div class="min-w-0"><span class="font-bold">Người xử lý:</span> ${this.escapeHtml(item?.ProcessedByName || '-')}</div>
|
|
<div class="md:col-span-2 min-w-0"><span class="font-bold">Ghi chú:</span> ${this.escapeHtml(note || '-')}</div>
|
|
<div class="md:col-span-2 min-w-0"><span class="font-bold">Lý do:</span> ${this.escapeHtml(rejectReason || '-')}</div>
|
|
</div>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
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('')
|
|
: `<div class="rounded-lg border border-dashed border-slate-300 px-4 py-8 text-sm text-center text-slate-500">${emptyText}</div>`;
|
|
|
|
let container = document.getElementById('assetBorrowDetailsModalContainer');
|
|
if (!container) {
|
|
container = document.createElement('div');
|
|
container.id = 'assetBorrowDetailsModalContainer';
|
|
document.body.appendChild(container);
|
|
}
|
|
|
|
container.innerHTML = `
|
|
<div class="fixed inset-0 bg-black/40 backdrop-blur-sm modal-backdrop open" id="assetBorrowDetailsModal" style="z-index:1000;display:flex;align-items:center;justify-content:center;padding:16px;overflow:hidden;">
|
|
<div class="modal-content bg-white rounded-xl shadow-2xl border border-slate-200 overflow-hidden flex flex-col" style="position:relative;z-index:1001;width:min(1080px, calc(100vw - 32px));max-height:calc(100vh - 32px);box-sizing:border-box;">
|
|
<div class="px-6 py-4 border-b border-slate-100 flex items-center justify-between gap-3 bg-slate-50">
|
|
<div class="min-w-0">
|
|
<h3 class="text-base font-extrabold text-slate-900">Chi tiết mượn/trả tài sản</h3>
|
|
<div class="mt-1 flex flex-wrap items-center gap-2 text-xs text-slate-500 min-w-0">
|
|
<span class="font-bold text-slate-700 break-words min-w-0">${this.escapeHtml(assetName)}${this.getArchivedAssetBadgeHtml(selected)}</span>
|
|
<span class="inline-flex items-center px-2 py-0.5 rounded ${selectedTypeMeta.className}">${selectedTypeMeta.label}</span>
|
|
<span class="inline-flex items-center px-2 py-0.5 rounded ${selectedStatusMeta.className}">${selectedStatusMeta.label}</span>
|
|
</div>
|
|
</div>
|
|
<button type="button" class="p-1.5 rounded-lg hover:bg-slate-200 text-slate-400 transition-colors" data-close-modal="assetBorrowDetailsModal" title="Đóng">
|
|
<span class="material-symbols-outlined">close</span>
|
|
</button>
|
|
</div>
|
|
<div class="p-4 md:p-6 overflow-y-auto overflow-x-hidden bg-slate-50/60">
|
|
<div class="grid grid-cols-1 lg:grid-cols-2 gap-4 min-w-0">
|
|
<section class="space-y-3 min-w-0">
|
|
<div class="flex items-center justify-between">
|
|
<h4 class="text-sm font-extrabold text-blue-700">Lịch sử đơn mượn</h4>
|
|
<span class="text-xs font-bold text-slate-500">${borrowRequests.length}</span>
|
|
</div>
|
|
${renderList(borrowRequests, 'Không tìm thấy đơn mượn liên quan.')}
|
|
</section>
|
|
<section class="space-y-3 min-w-0">
|
|
<div class="flex items-center justify-between">
|
|
<h4 class="text-sm font-extrabold text-emerald-700">Lịch sử đơn trả</h4>
|
|
<span class="text-xs font-bold text-slate-500">${returnRequests.length}</span>
|
|
</div>
|
|
${renderList(returnRequests, 'Chưa có đơn trả liên quan.')}
|
|
</section>
|
|
</div>
|
|
</div>
|
|
<div class="p-4 border-t border-slate-100 bg-white flex justify-end">
|
|
<button type="button" class="px-4 py-2 text-xs font-bold text-slate-600 border border-slate-200 rounded-lg hover:bg-slate-50" data-close-modal="assetBorrowDetailsModal">Đóng</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
`;
|
|
|
|
}
|
|
|
|
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 `
|
|
<div class="rounded-lg border border-slate-200 bg-white p-3 space-y-2 shadow-sm">
|
|
<div class="flex items-center justify-between gap-2">
|
|
<span class="inline-flex items-center px-2 py-0.5 rounded text-[11px] font-bold ${typeMeta.className}">${typeMeta.label}</span>
|
|
<span class="inline-flex items-center justify-center min-w-[30px] h-6 px-2 rounded-full bg-slate-100 text-slate-700 text-xs font-extrabold">#${requestId || '-'}</span>
|
|
</div>
|
|
<div class="text-xs text-slate-700 space-y-1">
|
|
<div><span class="font-bold">Tên đầy đủ:</span> ${this.escapeHtml(item?.BorrowerName || '-')}</div>
|
|
<div><span class="font-bold">Tên tài sản:</span> ${this.escapeHtml(item?.AssetCode || '')} ${item?.AssetCode ? '- ' : ''}${this.escapeHtml(item?.AssetName || '-')}</div>
|
|
${this.getArchivedAssetBadgeHtml(item)}
|
|
${blockedArchivedBorrow ? '<p class="text-amber-700">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.</p>' : ''}
|
|
<div><span class="font-bold">Số lượng:</span> ${Number(item?.BorrowQuantity) || 0} ${this.escapeHtml(item?.Unit || '')}</div>
|
|
<div><span class="font-bold">Ghi chú:</span> ${this.escapeHtml(note || '-')}</div>
|
|
<div><span class="font-bold">${dateLabel}:</span> ${this.formatDateOnly(item?.BorrowDate)}</div>
|
|
</div>
|
|
<div class="flex items-center gap-2 pt-1">
|
|
${blockedArchivedBorrow ? '' : `<button class="asset-request-approve-btn flex-1 min-w-0 px-3 py-1.5 rounded-md bg-primary hover:bg-primary-dim text-on-primary text-xs font-bold whitespace-nowrap" data-request-id="${requestId}">Chấp nhận</button>`}
|
|
<button class="asset-request-reject-btn flex-1 min-w-0 px-3 py-1.5 rounded-md bg-red-600 hover:bg-red-700 text-white text-xs font-bold whitespace-nowrap" data-request-id="${requestId}">Từ chối</button>
|
|
<button class="asset-request-delete-btn flex-1 min-w-0 px-3 py-1.5 rounded-md bg-slate-700 hover:bg-slate-800 text-white text-xs font-bold whitespace-nowrap" data-request-id="${requestId}">Xóa đơn</button>
|
|
</div>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
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('')
|
|
: `<div class="rounded-lg border border-dashed border-slate-300 px-3 py-6 text-center text-xs text-slate-500">Không có đơn mượn nào đang chờ.</div>`;
|
|
|
|
returnList.innerHTML = pendingReturnRequests.length
|
|
? pendingReturnRequests.map(item => this.buildPendingAssetRequestCardHtml(item)).join('')
|
|
: `<div class="rounded-lg border border-dashed border-slate-300 px-3 py-6 text-center text-xs text-slate-500">Không có đơn trả nào đang chờ.</div>`;
|
|
|
|
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 `
|
|
<tr class="group hover:bg-slate-50/80 transition-colors">
|
|
<td class="px-4 py-3 text-sm text-slate-600">${rowNumber}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${this.escapeHtml(item.RequestMonth || '-')}</td>
|
|
<td class="px-4 py-3 text-sm font-semibold text-slate-700 whitespace-nowrap">${this.escapeHtml(item.ConsumableCode || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700 min-w-[360px]">${this.escapeHtml(item.ConsumableName || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${this.escapeHtml(item.Model || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${this.escapeHtml(item.Unit || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 text-right">${item.OpeningBalance ?? 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 text-right">${item.ImportInPeriod ?? 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 text-right">${item.ExportInPeriod ?? 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 max-w-[220px] truncate" title="${this.escapeHtml(exportedSummary)}">${this.escapeHtml(exportedSummary || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 max-w-[220px] truncate" title="${this.escapeHtml(recipientSummary)}">${this.escapeHtml(recipientSummary || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 max-w-[220px] truncate" title="${this.escapeHtml(projectSummary)}">${this.escapeHtml(projectSummary || '-')}</td>
|
|
<td class="px-4 py-3 text-sm font-semibold text-slate-700 text-right">${item.EndingBalance ?? 0}</td>
|
|
<td class="px-4 py-3 text-sm whitespace-nowrap">
|
|
<span class="inline-block px-2 py-1 rounded text-xs font-semibold ${statusMeta.className}">${statusMeta.label}</span>
|
|
</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 max-w-sm truncate" title="${this.escapeHtml(reason)}">${this.escapeHtml(reason || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${this.formatDateOnly(item.UpdatedDate || item.CreatedDate)}</td>
|
|
<td class="sticky right-0 z-10 bg-white group-hover:bg-slate-50/80 px-4 py-3 text-right shadow-[-10px_0_12px_-12px_rgba(15,23,42,0.45)]" style="width: 140px; min-width: 140px;">
|
|
<div class="ml-auto flex items-center justify-end gap-1.5 whitespace-nowrap">
|
|
<button class="p-1.5 text-slate-400 transition-colors export-consumable ${canManageAssets ? 'hover:text-emerald-600' : 'opacity-40 cursor-not-allowed'}" data-consumable-id="${item.ConsumableId}" title="${canManageAssets ? 'Mượn / xuất vật tư' : 'Chỉ xem'}" ${canManageAssets ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-lg">outbox</span>
|
|
</button>
|
|
<button class="p-1.5 text-slate-400 transition-colors edit-consumable ${canManageAssets ? 'hover:text-primary' : 'opacity-40 cursor-not-allowed'}" data-consumable-id="${item.ConsumableId}" title="${canManageAssets ? 'Sửa' : 'Chỉ xem'}" ${canManageAssets ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-lg">edit</span>
|
|
</button>
|
|
<button class="p-1.5 text-slate-400 transition-colors delete-consumable ${canManageAssets ? 'hover:text-error' : 'opacity-40 cursor-not-allowed'}" data-consumable-id="${item.ConsumableId}" title="${canManageAssets ? 'Xóa' : 'Chỉ xem'}" ${canManageAssets ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-lg">delete</span>
|
|
</button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
`;
|
|
}).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 `
|
|
<article class="consumable-mobile-card rounded-xl border border-slate-200 bg-white p-3 shadow-sm">
|
|
<div class="flex items-start justify-between gap-3">
|
|
<div class="min-w-0">
|
|
<div class="text-[10px] font-bold uppercase tracking-wider text-slate-400">#${rowNumber} · ${safe(item?.ConsumableCode)}</div>
|
|
<h3 class="mt-0.5 text-sm font-extrabold text-slate-800">${safe(item?.ConsumableName)}</h3>
|
|
<div class="mt-1 flex flex-wrap items-center gap-x-2 gap-y-1 text-[11px] text-slate-500">
|
|
<span>Tháng: <strong class="text-slate-700">${safe(item?.RequestMonth)}</strong></span>
|
|
<span>Model: <strong class="text-slate-700">${safe(item?.Model)}</strong></span>
|
|
<span>ĐVT: <strong class="text-slate-700">${safe(unit)}</strong></span>
|
|
</div>
|
|
</div>
|
|
<span class="shrink-0 inline-block px-2 py-1 rounded text-[10px] font-semibold ${statusMeta.className}">${statusMeta.label}</span>
|
|
</div>
|
|
|
|
<div class="mt-3 grid grid-cols-4 gap-1.5 border-y border-slate-100 py-3 text-center">
|
|
<div class="rounded-lg bg-slate-50 px-1 py-2">
|
|
<span class="block text-[9px] font-bold uppercase text-slate-400">Tồn đầu</span>
|
|
<strong class="text-sm text-slate-700">${number(item?.OpeningBalance)}</strong>
|
|
</div>
|
|
<div class="rounded-lg bg-emerald-50 px-1 py-2">
|
|
<span class="block text-[9px] font-bold uppercase text-emerald-600">Nhập</span>
|
|
<strong class="text-sm text-emerald-700">${number(item?.ImportInPeriod)}</strong>
|
|
</div>
|
|
<div class="rounded-lg bg-amber-50 px-1 py-2">
|
|
<span class="block text-[9px] font-bold uppercase text-amber-600">Xuất</span>
|
|
<strong class="text-sm text-amber-700">${number(item?.ExportInPeriod)}</strong>
|
|
</div>
|
|
<div class="rounded-lg bg-blue-50 px-1 py-2">
|
|
<span class="block text-[9px] font-bold uppercase text-blue-600">Tồn cuối</span>
|
|
<strong class="text-sm text-blue-700">${number(item?.EndingBalance)}</strong>
|
|
</div>
|
|
</div>
|
|
|
|
${(exportedSummary || recipientSummary || projectSummary) ? `
|
|
<div class="mt-3 grid grid-cols-1 gap-2 rounded-lg bg-slate-50 px-3 py-2 text-xs text-slate-600">
|
|
${exportedSummary ? `<div><span class="font-bold text-slate-500">Đã xuất:</span> <span class="whitespace-pre-line">${safe(exportedSummary)}</span></div>` : ''}
|
|
${recipientSummary ? `<div><span class="font-bold text-slate-500">Người đang nhận:</span> <span class="whitespace-pre-line">${safe(recipientSummary)}</span></div>` : ''}
|
|
${projectSummary ? `<div><span class="font-bold text-slate-500">Dự án nhận:</span> <span class="whitespace-pre-line">${safe(projectSummary)}</span></div>` : ''}
|
|
</div>
|
|
` : ''}
|
|
|
|
<div class="mt-3 flex items-end justify-between gap-3 text-xs">
|
|
<div class="min-w-0 text-slate-600">
|
|
${reason ? `<div><span class="font-bold text-slate-500">Lý do:</span> ${safe(reason)}</div>` : ''}
|
|
<div class="mt-0.5 text-[10px] text-slate-400">Cập nhật: ${this.formatDateOnly(item?.UpdatedDate || item?.CreatedDate)}</div>
|
|
</div>
|
|
<div class="flex shrink-0 items-center gap-1.5">
|
|
<button class="export-consumable inline-flex items-center gap-1 rounded-md border border-emerald-200 px-2.5 py-2 text-xs font-bold text-emerald-700 ${canManageAssets ? '' : 'opacity-40 cursor-not-allowed'}" data-consumable-id="${item?.ConsumableId}" ${canManageAssets ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-base">outbox</span>
|
|
Xuất
|
|
</button>
|
|
<button class="edit-consumable inline-flex items-center gap-1 rounded-md border border-primary/30 px-2.5 py-2 text-xs font-bold text-primary ${canManageAssets ? '' : 'opacity-40 cursor-not-allowed'}" data-consumable-id="${item?.ConsumableId}" ${canManageAssets ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-base">edit</span>
|
|
Sửa
|
|
</button>
|
|
<button class="delete-consumable inline-flex items-center justify-center rounded-md border border-red-200 p-2 text-red-600 ${canManageAssets ? '' : 'opacity-40 cursor-not-allowed'}" data-consumable-id="${item?.ConsumableId}" title="Xóa" ${canManageAssets ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-base">delete</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</article>
|
|
`;
|
|
}
|
|
|
|
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 `
|
|
<div class="consumables-page flex flex-col p-4 md:p-6 overflow-hidden h-full">
|
|
<div class="page-header flex items-center justify-between gap-4 mb-5 shrink-0">
|
|
<div>
|
|
<h1 class="text-2xl font-extrabold text-on-surface tracking-tight">Vật tư tiêu hao</h1>
|
|
<p class="text-sm text-on-surface-variant">Quản lý xuất nhập tồn vật tư tiêu hao theo tháng.</p>
|
|
</div>
|
|
<div class="consumable-header-actions flex flex-wrap items-center gap-2">
|
|
<button id="createConsumableBorrowRequestBtn" class="bg-primary text-on-primary px-4 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 transition-all active:scale-95 hover:bg-primary-dim">
|
|
<span class="material-symbols-outlined text-base">assignment_add</span>
|
|
Tạo đơn mượn
|
|
</button>
|
|
<button id="openConsumableBorrowRequestsBtn" class="relative border border-blue-300 text-blue-700 px-3 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 transition-all active:scale-95 hover:bg-blue-50">
|
|
<span class="material-symbols-outlined text-base">assignment</span>
|
|
${canManageAssets ? 'Đơn mượn/trả / chờ duyệt' : 'Đơn mượn/trả của tôi'}
|
|
<span id="consumableBorrowRequestsCountBadge" class="${requestNotificationCount > 0 ? '' : 'hidden'} absolute -top-2.5 -right-2.5 min-w-[22px] h-[22px] px-1.5 rounded-[999px] bg-red-600 text-white text-xs font-extrabold leading-[22px] text-center ring-2 ring-white">${requestNotificationCount > 99 ? '99+' : requestNotificationCount}</span>
|
|
</button>
|
|
<button id="importConsumableBtn" class="border border-slate-300 text-slate-700 px-3 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 transition-all active:scale-95 ${canManageAssets ? 'hover:bg-slate-100' : 'opacity-50 cursor-not-allowed'}" ${canManageAssets ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-base">upload_file</span>
|
|
Nhập Excel
|
|
</button>
|
|
<button id="exportConsumableBtn" class="border border-slate-300 hover:bg-slate-100 text-slate-700 px-3 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 transition-all active:scale-95">
|
|
<span class="material-symbols-outlined text-base">download</span>
|
|
Xuất Excel
|
|
</button>
|
|
<button id="openConsumableExportHistoryBtn" class="border border-amber-300 text-amber-700 px-3 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 transition-all active:scale-95 hover:bg-amber-50">
|
|
<span class="material-symbols-outlined text-base">history</span>
|
|
${canManageAssets ? 'Mượn / trả' : 'Vật tư đang nhận / trả'}
|
|
</button>
|
|
<button id="addConsumableBtn" class="bg-primary text-on-primary px-4 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 transition-all active:scale-95 ${canManageAssets ? 'hover:bg-primary-dim' : 'opacity-50 cursor-not-allowed'}" ${canManageAssets ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-base">add_box</span>
|
|
Thêm vật tư
|
|
</button>
|
|
<input id="consumableImportInput" type="file" accept=".xlsx,.xls" class="hidden" />
|
|
</div>
|
|
</div>
|
|
|
|
${latestRejectedReturn ? `
|
|
<div class="mb-4 shrink-0 rounded-xl border border-red-200 bg-red-50 px-4 py-3 flex flex-col md:flex-row md:items-center gap-3">
|
|
<span class="material-symbols-outlined text-red-600 text-2xl shrink-0">notification_important</span>
|
|
<div class="flex-1 min-w-0 text-sm text-red-800">
|
|
<div class="font-extrabold">${rejectedReturnRequests.length > 1 ? `Có ${rejectedReturnRequests.length} đơn trả bị từ chối` : `Đơn trả #${Number(latestRejectedReturn?.BorrowRequestId) || '-'} đã bị từ chối`}</div>
|
|
<div class="mt-0.5 break-words"><span class="font-bold">${this.escapeHtml(latestRejectedItemLabel)} — Lý do:</span> ${this.escapeHtml(latestRejectReason)}</div>
|
|
</div>
|
|
<button id="openRejectedConsumableReturnRequestsBtn" class="shrink-0 px-3 py-2 rounded-lg bg-red-600 hover:bg-red-700 text-white text-xs font-bold">Xem đơn</button>
|
|
</div>
|
|
` : ''}
|
|
|
|
<div class="page-filters compact-page-filters consumable-filter-bar flex items-center gap-3 mb-4">
|
|
<div class="flex items-center gap-1.5">
|
|
<span class="text-[10px] font-bold uppercase text-on-surface-variant">Tháng</span>
|
|
<select id="consumableMonthFilter" class="bg-surface-container-low border-slate-200 rounded-md text-[11px] py-1 px-2 pr-6 focus:ring-1 focus:ring-primary shadow-sm">
|
|
<option value="">Tất cả</option>
|
|
${monthOptions.map(month => `<option value="${this.escapeHtml(month)}" ${this.consumableMonthFilter === month ? 'selected' : ''}>${this.escapeHtml(month)}</option>`).join('')}
|
|
</select>
|
|
</div>
|
|
<div class="flex items-center gap-1.5">
|
|
<span class="text-[10px] font-bold uppercase text-on-surface-variant">Trạng thái</span>
|
|
<select id="consumableStatusFilter" class="bg-surface-container-low border-slate-200 rounded-md text-[11px] py-1 px-2 pr-6 focus:ring-1 focus:ring-primary shadow-sm">
|
|
<option value="" ${!this.consumableStatusFilter ? 'selected' : ''}>Tất cả</option>
|
|
<option value="in_stock" ${this.consumableStatusFilter === 'in_stock' ? 'selected' : ''}>Còn tồn</option>
|
|
<option value="out_of_stock" ${this.consumableStatusFilter === 'out_of_stock' ? 'selected' : ''}>Hết tồn</option>
|
|
</select>
|
|
</div>
|
|
<div class="compact-filter-search flex items-center gap-1.5 flex-1">
|
|
<span class="text-[10px] font-bold uppercase text-on-surface-variant">Tìm kiếm</span>
|
|
<input id="consumableSearch" class="flex-1 bg-surface-container-low border-slate-200 rounded-md text-[11px] py-1 px-2 focus:ring-1 focus:ring-primary shadow-sm" placeholder="Mã, tên, model, lý do xuất..." value="${this.escapeHtml(this.consumableSearchTerm)}">
|
|
</div>
|
|
</div>
|
|
|
|
<div class="flex-1 bg-white rounded-xl border border-slate-200 shadow-sm flex flex-col overflow-hidden min-h-0">
|
|
${pageInfo.data.length > 0 ? `
|
|
<div class="consumables-desktop-table table-wrap overflow-y-auto overflow-x-auto flex-1">
|
|
<table class="w-full text-left border-collapse" style="min-width: 2120px; border-collapse: separate; border-spacing: 0;">
|
|
<thead class="sticky top-0 z-50 bg-slate-50 border-b border-slate-200">
|
|
<tr>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">STT</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Tháng đề xuất</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Mã vật tư</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Tên linh kiện/sp</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Model</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">ĐVT</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500 text-right">Tồn đầu</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500 text-right">Nhập</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500 text-right">Xuất</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Đã xuất</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Người đang nhận</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Dự án nhận</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500 text-right">Tồn cuối</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Trạng thái</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Lý do xuất</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Cập nhật</th>
|
|
<th class="sticky top-0 right-0 z-[100] bg-slate-50 px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500 text-right shadow-[-10px_0_12px_-12px_rgba(15,23,42,0.45)]" style="width: 140px; min-width: 140px;">Thao tác</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody class="divide-y divide-slate-100 consumables-table-body">
|
|
${this.buildConsumableTableRows(pageInfo)}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div class="consumables-mobile-list flex-1 flex-col gap-3 overflow-y-auto bg-slate-50/70 p-3">
|
|
${pageInfo.data.map((item, index) => this.getConsumableMobileCardHtml(item, pageInfo.start + index, canManageAssets)).join('')}
|
|
</div>
|
|
<div class="page-pager flex items-center justify-between px-4 py-2 border-t border-slate-200 bg-slate-50 text-xs text-slate-600" id="consumablesPager">
|
|
<span>Hiển thị ${pageInfo.start}-${pageInfo.end} / ${pageInfo.total}</span>
|
|
<div class="flex items-center gap-2">
|
|
<button class="consumable-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === 1 ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current - 1}" ${pageInfo.current === 1 ? 'disabled' : ''}>Trước</button>
|
|
<span class="text-[11px]">Trang ${pageInfo.current} / ${pageInfo.totalPages}</span>
|
|
<button class="consumable-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === pageInfo.totalPages ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current + 1}" ${pageInfo.current === pageInfo.totalPages ? 'disabled' : ''}>Sau</button>
|
|
</div>
|
|
</div>
|
|
` : `
|
|
<div class="flex-1 flex items-center justify-center text-center p-6">
|
|
<div>
|
|
<p class="text-sm text-on-surface-variant mb-4">Chưa có dữ liệu vật tư tiêu hao.</p>
|
|
<button id="addConsumableBtn" class="bg-primary text-on-primary px-4 py-2 rounded-lg text-sm font-bold ${canManageAssets ? 'hover:bg-primary-dim' : 'opacity-50 cursor-not-allowed'}" ${canManageAssets ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-base inline mr-2">add_box</span>
|
|
Thêm vật tư đầu tiên
|
|
</button>
|
|
</div>
|
|
</div>
|
|
`}
|
|
</div>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
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 = `
|
|
<span>Hiển thị ${pageInfo.start}-${pageInfo.end} / ${pageInfo.total}</span>
|
|
<div class="flex items-center gap-2">
|
|
<button class="consumable-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === 1 ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current - 1}" ${pageInfo.current === 1 ? 'disabled' : ''}>Trước</button>
|
|
<span class="text-[11px]">Trang ${pageInfo.current} / ${pageInfo.totalPages}</span>
|
|
<button class="consumable-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === pageInfo.totalPages ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current + 1}" ${pageInfo.current === pageInfo.totalPages ? 'disabled' : ''}>Sau</button>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
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 `
|
|
<div class="consumable-exports-page flex flex-col p-4 md:p-6 overflow-hidden h-full">
|
|
<div class="page-header flex items-center justify-between gap-4 mb-5 shrink-0">
|
|
<div>
|
|
<h1 class="text-2xl font-extrabold text-on-surface tracking-tight">Mượn / xuất / trả VTTH</h1>
|
|
<p class="text-sm text-on-surface-variant">${pageDescription}</p>
|
|
</div>
|
|
<div class="compact-page-actions flex flex-wrap items-center gap-2">
|
|
<button id="exportConsumableHistoryBtn" class="border border-slate-300 text-slate-700 px-3 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 transition-all active:scale-95 ${canManageAssets ? 'hover:bg-slate-100' : 'opacity-50 cursor-not-allowed'}" ${canManageAssets ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-base">download</span>
|
|
Xuất Excel
|
|
</button>
|
|
<button id="refreshConsumableExportHistoryPageBtn" class="border border-slate-300 text-slate-700 px-3 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 transition-all active:scale-95 hover:bg-slate-100">
|
|
<span class="material-symbols-outlined text-base">refresh</span>
|
|
Làm mới
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="page-filters compact-page-filters consumable-export-filter-bar flex items-center gap-3 mb-4 shrink-0">
|
|
<div class="flex items-center gap-1.5">
|
|
<span class="text-[10px] font-bold uppercase text-on-surface-variant">Người nhận</span>
|
|
<select id="consumableExportRecipientFilter" class="bg-surface-container-low border-slate-200 rounded-md text-[11px] py-1 px-2 pr-6 focus:ring-1 focus:ring-primary shadow-sm">
|
|
<option value="" ${!this.consumableExportRecipientFilter ? 'selected' : ''}>Tất cả</option>
|
|
${recipientOptions.map(name => `<option value="${this.escapeHtml(name)}" ${this.consumableExportRecipientFilter === name ? 'selected' : ''}>${this.escapeHtml(name)}</option>`).join('')}
|
|
</select>
|
|
</div>
|
|
<div class="flex items-center gap-1.5">
|
|
<span class="text-[10px] font-bold uppercase text-on-surface-variant">Dự án</span>
|
|
<select id="consumableExportProjectFilter" class="bg-surface-container-low border-slate-200 rounded-md text-[11px] py-1 px-2 pr-6 focus:ring-1 focus:ring-primary shadow-sm">
|
|
<option value="" ${!this.consumableExportProjectFilter ? 'selected' : ''}>Tất cả</option>
|
|
${projectOptions.map(name => `<option value="${this.escapeHtml(name)}" ${this.consumableExportProjectFilter === name ? 'selected' : ''}>${this.escapeHtml(name)}</option>`).join('')}
|
|
</select>
|
|
</div>
|
|
<div class="flex items-center gap-1.5">
|
|
<span class="text-[10px] font-bold uppercase text-on-surface-variant">Ngày</span>
|
|
<input id="consumableExportDateFilter" type="date" value="${this.escapeHtml(this.consumableExportDateFilter)}" class="bg-surface-container-low border-slate-200 rounded-md text-[11px] py-1 px-2 focus:ring-1 focus:ring-primary shadow-sm">
|
|
</div>
|
|
<div class="compact-filter-search flex items-center gap-1.5 flex-1">
|
|
<span class="text-[10px] font-bold uppercase text-on-surface-variant">Tìm kiếm</span>
|
|
<input
|
|
id="consumableExportSearch"
|
|
value="${this.escapeHtml(this.consumableExportSearchTerm)}"
|
|
class="flex-1 bg-surface-container-low border-slate-200 rounded-md text-[11px] py-1 px-2 focus:ring-1 focus:ring-primary shadow-sm"
|
|
placeholder="Mã, tên vật tư, người nhận, dự án, người xuất, ghi chú..."
|
|
>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="flex-1 bg-white rounded-xl border border-slate-200 shadow-sm flex flex-col overflow-hidden min-h-0">
|
|
<div class="table-wrap overflow-y-auto overflow-x-auto flex-1">
|
|
<table class="w-full text-left border-collapse" style="min-width: 1850px; border-collapse: separate; border-spacing: 0;">
|
|
<thead class="sticky top-0 z-50 bg-slate-50 border-b border-slate-200">
|
|
<tr>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">STT</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Ngày giờ</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Mã vật tư</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Tên vật tư</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500 text-right">Đã xuất</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500 text-right">Đã trả</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500 text-right">Còn lại</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">ĐVT</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Người nhận</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Dự án nhận</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Trạng thái</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Người xuất</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Tồn trước/sau</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Ghi chú</th>
|
|
<th class="sticky top-0 right-0 z-[100] bg-slate-50 px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500 text-right">Thao tác</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody id="consumableExportHistoryPageTableBody" class="divide-y divide-slate-100">
|
|
${this.buildConsumableExportHistoryPageRowsHtml(pageInfo)}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div class="page-pager flex items-center justify-between px-4 py-2 border-t border-slate-200 bg-slate-50 text-xs text-slate-600" id="consumableExportsPager">
|
|
<span>Hiển thị ${pageInfo.start}-${pageInfo.end} / ${pageInfo.total}</span>
|
|
<div class="flex items-center gap-2">
|
|
<button class="consumable-export-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === 1 ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current - 1}" ${pageInfo.current === 1 ? 'disabled' : ''}>Trước</button>
|
|
<span class="text-[11px]">Trang ${pageInfo.current} / ${pageInfo.totalPages}</span>
|
|
<button class="consumable-export-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === pageInfo.totalPages ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current + 1}" ${pageInfo.current === pageInfo.totalPages ? 'disabled' : ''}>Tiếp</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
buildConsumableExportHistoryEmptyRowHtml() {
|
|
return `
|
|
<tr>
|
|
<td colspan="15" class="px-4 py-8 text-sm text-center text-slate-500">Chưa có dữ liệu lịch sử mượn / xuất vật tư.</td>
|
|
</tr>
|
|
`;
|
|
}
|
|
|
|
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 `
|
|
<tr class="hover:bg-slate-50/80 transition-colors">
|
|
<td class="px-4 py-3 text-sm text-slate-600">${rowNumber}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${this.formatDateTime(item?.ExportedDate || item?.CreatedDate)}</td>
|
|
<td class="px-4 py-3 text-sm font-semibold text-slate-700 whitespace-nowrap">${this.escapeHtml(item?.ConsumableCode || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700 min-w-[220px]">${this.escapeHtml(item?.ConsumableName || '-')}</td>
|
|
<td class="px-4 py-3 text-sm font-semibold text-slate-700 text-right">${exportedQuantity}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 text-right">${returnedQuantity}</td>
|
|
<td class="px-4 py-3 text-sm font-semibold text-slate-700 text-right">${remainingQuantity}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${this.escapeHtml(item?.Unit || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${this.escapeHtml(item?.RecipientName || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${this.escapeHtml(item?.ProjectName || '-')}</td>
|
|
<td class="px-4 py-3 text-sm align-top"><span class="inline-block px-2 py-1 rounded text-xs font-semibold whitespace-nowrap ${statusMeta.className}">${statusMeta.label}</span>${returnRequestNotice}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${this.escapeHtml(item?.ExportedByName || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${this.escapeHtml(balanceLabel)}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 max-w-sm whitespace-pre-line">${this.escapeHtml(historyNote)}</td>
|
|
<td class="sticky right-0 z-10 bg-white px-4 py-3 text-right">
|
|
${returnAction ? `
|
|
<button
|
|
class="return-consumable-export inline-flex items-center gap-1 px-2.5 py-1.5 rounded-lg text-xs font-bold transition-colors bg-primary/10 text-primary hover:bg-primary/5"
|
|
data-export-history-id="${item?.ExportHistoryId || ''}"
|
|
data-return-mode="${returnAction.mode}"
|
|
title="${this.escapeHtml(returnAction.title)}"
|
|
>
|
|
<span class="material-symbols-outlined text-base">assignment_return</span>
|
|
${returnAction.label}
|
|
</button>
|
|
` : '<span class="text-xs text-slate-400">-</span>'}
|
|
</td>
|
|
</tr>
|
|
`;
|
|
}
|
|
|
|
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 = `
|
|
<span>Hiển thị ${pageInfo.start}-${pageInfo.end} / ${pageInfo.total}</span>
|
|
<div class="flex items-center gap-2">
|
|
<button class="consumable-export-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === 1 ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current - 1}" ${pageInfo.current === 1 ? 'disabled' : ''}>Trước</button>
|
|
<span class="text-[11px]">Trang ${pageInfo.current} / ${pageInfo.totalPages}</span>
|
|
<button class="consumable-export-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === pageInfo.totalPages ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current + 1}" ${pageInfo.current === pageInfo.totalPages ? 'disabled' : ''}>Tiếp</button>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
async refreshConsumableExportsPage() {
|
|
const tbody = document.getElementById('consumableExportHistoryPageTableBody');
|
|
if (tbody) {
|
|
tbody.innerHTML = `
|
|
<tr>
|
|
<td colspan="15" class="px-4 py-8 text-sm text-center text-slate-500">Đang tải lịch sử mượn / xuất / trả...</td>
|
|
</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 = `
|
|
<option value="" ${!currentValue ? 'selected' : ''}>Tất cả</option>
|
|
${recipientOptions.map(name => `<option value="${this.escapeHtml(name)}" ${currentValue === name ? 'selected' : ''}>${this.escapeHtml(name)}</option>`).join('')}
|
|
`;
|
|
}
|
|
|
|
const projectFilter = document.getElementById('consumableExportProjectFilter');
|
|
if (projectFilter) {
|
|
const projectOptions = this.getConsumableExportProjectOptions();
|
|
const currentValue = this.consumableExportProjectFilter;
|
|
projectFilter.innerHTML = `
|
|
<option value="" ${!currentValue ? 'selected' : ''}>Tất cả</option>
|
|
${projectOptions.map(name => `<option value="${this.escapeHtml(name)}" ${currentValue === name ? 'selected' : ''}>${this.escapeHtml(name)}</option>`).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();
|
|
}
|
|
|
|
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 `
|
|
<article class="asset-mobile-card rounded-xl border border-slate-200 bg-white p-3 shadow-sm">
|
|
<div class="flex items-start justify-between gap-3">
|
|
<div class="flex min-w-0 items-start gap-2">
|
|
<input type="checkbox" class="asset-row-checkbox mt-1 w-4 h-4 shrink-0" data-asset-id="${asset?.AssetId}" ${isSelected ? 'checked' : ''} ${canManageAssets ? '' : 'disabled'} aria-label="Chọn tài sản ${safe(asset?.AssetCode)}">
|
|
<div class="min-w-0">
|
|
<div class="text-[10px] font-bold uppercase tracking-wider text-slate-400">#${rowNumber} · ${safe(asset?.AssetCode)}</div>
|
|
<h3 class="mt-0.5 text-sm font-extrabold text-slate-800">${safe(asset?.AssetName)}</h3>
|
|
</div>
|
|
</div>
|
|
<span class="shrink-0 inline-block px-2 py-1 rounded text-[10px] font-semibold ${statusMeta.className}">${statusMeta.label}</span>
|
|
</div>
|
|
|
|
<div class="mt-3 grid grid-cols-2 gap-x-3 gap-y-2 border-t border-slate-100 pt-3 text-xs">
|
|
<div><span class="block text-[10px] font-bold uppercase text-slate-400">Model</span><span class="text-slate-700">${safe(asset?.Model)}</span></div>
|
|
<div><span class="block text-[10px] font-bold uppercase text-slate-400">Serial</span><span class="text-slate-700">${safe(asset?.SerialNumber)}</span></div>
|
|
<div><span class="block text-[10px] font-bold uppercase text-slate-400">Tồn cuối kỳ</span><span class="font-bold text-slate-800">${endingBalance}${unit ? ` ${safe(unit)}` : ''}</span></div>
|
|
<div><span class="block text-[10px] font-bold uppercase text-slate-400">Đang mượn</span><span class="font-bold text-slate-800">${borrowedQuantity}${unit ? ` ${safe(unit)}` : ''}</span></div>
|
|
<div><span class="block text-[10px] font-bold uppercase text-slate-400">Phòng ban</span><span class="text-slate-700">${safe(asset?.Department)}</span></div>
|
|
<div><span class="block text-[10px] font-bold uppercase text-slate-400">Dự án</span><span class="text-slate-700">${safe(asset?.Project)}</span></div>
|
|
<div><span class="block text-[10px] font-bold uppercase text-slate-400">Vị trí</span><span class="text-slate-700">${safe(asset?.Location)}</span></div>
|
|
<div><span class="block text-[10px] font-bold uppercase text-slate-400">Phụ trách</span><span class="text-slate-700">${safe(asset?.Custodian)}</span></div>
|
|
</div>
|
|
|
|
${borrower ? `
|
|
<div class="mt-3 rounded-lg bg-slate-50 px-3 py-2 text-xs text-slate-600">
|
|
<span class="block text-[10px] font-bold uppercase text-slate-400">Người mượn</span>
|
|
<span class="whitespace-pre-line">${safe(borrower)}</span>
|
|
</div>
|
|
` : ''}
|
|
|
|
<div class="mt-3 flex items-center justify-end gap-2 border-t border-slate-100 pt-3">
|
|
<button class="view-asset inline-flex items-center gap-1 rounded-md border border-slate-200 px-3 py-2 text-xs font-bold text-slate-600" data-asset-id="${asset?.AssetId}">
|
|
<span class="material-symbols-outlined text-base">info</span>
|
|
Chi tiết
|
|
</button>
|
|
<button class="edit-asset inline-flex items-center gap-1 rounded-md border border-primary/30 px-3 py-2 text-xs font-bold text-primary ${canManageAssets ? '' : 'opacity-40 cursor-not-allowed'}" data-asset-id="${asset?.AssetId}" ${canManageAssets ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-base">edit</span>
|
|
Sửa
|
|
</button>
|
|
<button class="delete-asset inline-flex items-center gap-1 rounded-md border border-red-200 px-3 py-2 text-xs font-bold text-red-600 ${canManageAssets ? '' : 'opacity-40 cursor-not-allowed'}" data-asset-id="${asset?.AssetId}" ${canManageAssets ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-base">delete</span>
|
|
Xóa
|
|
</button>
|
|
</div>
|
|
</article>
|
|
`;
|
|
}
|
|
|
|
getAssetsContent() {
|
|
this.syncSelectedAssetIds();
|
|
const canManageAssets = this.canCurrentUserManageAssets();
|
|
const filteredAssets = this.getFilteredAssets();
|
|
const pageInfo = this.getPaged(filteredAssets, this.assetPage, this.assetPageSize);
|
|
const selectedCount = canManageAssets ? this.selectedAssetIds.size : 0;
|
|
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 = canManageAssets && pageAssetIds.length > 0 && selectedOnPageCount === pageAssetIds.length;
|
|
this.assetPage = pageInfo.current;
|
|
|
|
return `
|
|
<div class="assets-page flex flex-col p-4 md:p-6 overflow-hidden h-full">
|
|
<div class="page-header flex items-center justify-between gap-4 mb-5 shrink-0">
|
|
<div>
|
|
<h1 class="text-2xl font-extrabold text-on-surface tracking-tight">Quản lý tài sản</h1>
|
|
<p class="text-sm text-on-surface-variant">Theo dõi tài sản, kho và trạng thái sử dụng.</p>
|
|
</div>
|
|
<div class="asset-header-actions flex flex-wrap items-center gap-2">
|
|
<button id="archiveAllAssetsBtn" class="border border-red-200 text-red-700 px-3 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 ${canManageAssets && this.assets.length ? 'hover:bg-red-50' : 'opacity-50 cursor-not-allowed'}" ${canManageAssets && this.assets.length ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-base">archive</span>
|
|
Xóa danh mục hiện tại
|
|
</button>
|
|
<button id="openArchivedAssetBorrowsBtn" class="border border-amber-300 text-amber-700 px-3 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 hover:bg-amber-50">
|
|
<span class="material-symbols-outlined text-base">history</span>
|
|
Tài sản cũ
|
|
</button>
|
|
<button id="importAssetBtn" class="border border-slate-300 text-slate-700 px-3 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 transition-all active:scale-95 ${canManageAssets ? 'hover:bg-slate-100' : 'opacity-50 cursor-not-allowed'}" ${canManageAssets ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-base">upload_file</span>
|
|
Nhập Excel
|
|
</button>
|
|
<button id="exportAssetBtn" class="border border-slate-300 hover:bg-slate-100 text-slate-700 px-3 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 transition-all active:scale-95">
|
|
<span class="material-symbols-outlined text-base">download</span>
|
|
Xuất Excel
|
|
</button>
|
|
<button id="openAssetExportHistoryBtn" class="border border-amber-300 text-amber-700 px-3 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 transition-all active:scale-95 ${canManageAssets ? 'hover:bg-amber-50' : 'opacity-50 cursor-not-allowed'}" ${canManageAssets ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-base">history</span>
|
|
Lịch sử xuất
|
|
</button>
|
|
<button id="openAssetDamageHistoryBtn" class="border border-red-200 text-red-700 px-3 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 transition-all active:scale-95 ${canManageAssets ? 'hover:bg-red-50' : 'opacity-50 cursor-not-allowed'}" ${canManageAssets ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-base">inventory_2</span>
|
|
DS hỏng/TL
|
|
</button>
|
|
<button id="addAssetBtn" class="bg-primary text-on-primary px-4 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 transition-all active:scale-95 ${canManageAssets ? 'hover:bg-primary-dim' : 'opacity-50 cursor-not-allowed'}" ${canManageAssets ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-base">add_box</span>
|
|
Thêm tài sản
|
|
</button>
|
|
<button id="borrowAssetBtn" class="border border-primary text-primary px-4 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 transition-all active:scale-95 ${(canManageAssets && selectedCount === 1) ? 'hover:bg-primary/5' : 'opacity-50 cursor-not-allowed'}" ${(canManageAssets && selectedCount === 1) ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-base">handshake</span>
|
|
Xuất tài sản
|
|
</button>
|
|
<button id="damageAssetBtn" class="border border-red-300 text-red-700 px-4 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 transition-all active:scale-95 ${(canManageAssets && selectedCount === 1) ? 'hover:bg-red-50' : 'opacity-50 cursor-not-allowed'}" ${(canManageAssets && selectedCount === 1) ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-base">broken_image</span>
|
|
Hỏng/Thanh lý
|
|
</button>
|
|
<input id="assetImportInput" type="file" accept=".xlsx,.xls" class="hidden" />
|
|
</div>
|
|
</div>
|
|
|
|
<div class="page-filters compact-page-filters asset-filter-bar flex items-center gap-3 mb-4">
|
|
<div class="flex items-center gap-1.5">
|
|
<span class="text-[10px] font-bold uppercase text-on-surface-variant">Trạng thái</span>
|
|
<select id="assetStatusFilter" class="bg-surface-container-low border-slate-200 rounded-md text-[11px] py-1 px-2 pr-6 focus:ring-1 focus:ring-primary shadow-sm">
|
|
<option value="">Tất cả</option>
|
|
<option value="in_use">Đang sử dụng</option>
|
|
<option value="in_stock">Trong kho</option>
|
|
<option value="exported">Đã xuất</option>
|
|
</select>
|
|
</div>
|
|
<div class="compact-filter-search flex items-center gap-1.5 flex-1">
|
|
<span class="text-[10px] font-bold uppercase text-on-surface-variant">Tìm kiếm</span>
|
|
<input id="assetSearch" class="flex-1 bg-surface-container-low border-slate-200 rounded-md text-[11px] py-1 px-2 focus:ring-1 focus:ring-primary shadow-sm" placeholder="Mã, tên, model, serial, dự án, vị trí...">
|
|
</div>
|
|
<button id="bulkDeleteAssetsBtn" class="compact-filter-action border border-red-200 text-red-600 px-3 py-1.5 rounded-md text-[11px] font-bold flex items-center gap-1.5 transition-colors ${(selectedCount === 0 || !canManageAssets) ? 'opacity-50 cursor-not-allowed' : 'hover:bg-red-50'}" ${(selectedCount === 0 || !canManageAssets) ? 'disabled' : ''}>
|
|
<span class="material-symbols-outlined text-base">delete_sweep</span>
|
|
Xóa đã chọn (<span id="selectedAssetCount">${selectedCount}</span>)
|
|
</button>
|
|
</div>
|
|
|
|
<div class="flex-1 bg-white rounded-xl border border-slate-200 shadow-sm flex flex-col overflow-hidden min-h-0">
|
|
${pageInfo.data.length > 0 ? `
|
|
<div class="assets-desktop-table table-wrap overflow-y-auto overflow-x-auto flex-1">
|
|
<table class="w-full text-left border-collapse" style="min-width: 2720px; border-collapse: separate; border-spacing: 0;">
|
|
<thead class="sticky top-0 z-50 bg-slate-50 border-b border-slate-200">
|
|
<tr>
|
|
<th class="px-4 py-2.5 text-center">
|
|
<input type="checkbox" id="selectAllAssetsCheckbox" class="w-4 h-4" ${allOnPageSelected ? 'checked' : ''} ${canManageAssets ? '' : 'disabled'} title="Chọn tất cả dòng trong trang hiện tại">
|
|
</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">STT</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Mã</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Tên tài sản</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Model</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Serial</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Số lượng (Tồn đầu kỳ)</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Nhập trong kỳ</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Xuất trong kỳ</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Tồn cuối kỳ</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Đơn vị</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Phòng ban</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Dự án</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Người phụ trách</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500" style="width: 132px; min-width: 132px; white-space: nowrap;">Trạng thái</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">SL hàng mới</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">SL đã qua sử dụng</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">SL đang mượn</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Vị trí</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Ngày mua</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500" style="width: 240px; min-width: 240px;">Người mượn</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Ghi chú</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Ngày tạo</th>
|
|
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Người xuất</th>
|
|
<th class="sticky top-0 right-0 z-[100] bg-slate-50 px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500 text-right shadow-[-10px_0_12px_-12px_rgba(15,23,42,0.45)]" style="z-index: 100; width: 140px; min-width: 140px; white-space: nowrap;">Thao tác</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody class="divide-y divide-slate-100 assets-table-body">
|
|
${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 `
|
|
<tr class="group hover:bg-slate-50/80 transition-colors">
|
|
<td class="px-4 py-3 text-center">
|
|
<input type="checkbox" class="asset-row-checkbox w-4 h-4" data-asset-id="${asset.AssetId}" ${isSelected ? 'checked' : ''} ${canManageAssets ? '' : 'disabled'}>
|
|
</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${rowNumber}</td>
|
|
<td class="px-4 py-3 text-sm font-semibold text-slate-700">${asset.AssetCode || '-'}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700">${asset.AssetName || '-'}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.Model || '-'}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.SerialNumber || '-'}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.Quantity || 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.ImportInPeriod ?? 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.ExportInPeriod ?? 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.EndingBalance ?? 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.Unit || '-'}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.Department || '-'}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.Project || '-'}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.Custodian || '-'}</td>
|
|
<td class="px-4 py-3 text-sm" style="width: 132px; min-width: 132px; white-space: nowrap;">
|
|
<span class="inline-block px-2 py-1 rounded text-xs font-semibold ${statusMeta.className}" style="display: inline-block; white-space: nowrap; word-break: keep-all; overflow-wrap: normal;">${statusMeta.label}</span>
|
|
</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.NewQuantity ?? 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.UsedQuantity ?? 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.ExportInPeriod ?? 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.Location || '-'}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${this.formatDateOnly(asset.PurchaseDate)}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-normal" style="width: 240px; min-width: 240px;">${this.formatBorrowerTableHtml(asset.Borrower)}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 max-w-xs truncate" title="${asset.Notes || ''}">${asset.Notes || '-'}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${this.formatDateOnly(asset.CreatedDate)}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.ExportedBy || '-'}</td>
|
|
<td class="sticky right-0 z-10 bg-white group-hover:bg-slate-50/80 px-4 py-3 text-right shadow-[-10px_0_12px_-12px_rgba(15,23,42,0.45)]" style="z-index: 1; width: 140px; min-width: 140px; white-space: nowrap;">
|
|
<div class="ml-auto" style="margin-left: auto; display: flex; flex-wrap: nowrap; align-items: center; justify-content: flex-end; gap: 6px; white-space: nowrap;">
|
|
<button class="p-1.5 text-slate-400 transition-colors view-asset hover:text-slate-600" style="display: inline-flex; align-items: center; justify-content: center; flex: 0 0 auto;" data-asset-id="${asset.AssetId}" title="Xem chi tiết">
|
|
<span class="material-symbols-outlined text-lg">info</span>
|
|
</button>
|
|
<button class="p-1.5 text-slate-400 transition-colors edit-asset ${canManageAssets ? 'hover:text-primary' : 'opacity-40 cursor-not-allowed'}" style="display: inline-flex; align-items: center; justify-content: center; flex: 0 0 auto;" data-asset-id="${asset.AssetId}" title="${canManageAssets ? 'Sửa' : 'Chi xem'}" ${canManageAssets ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-lg">edit</span>
|
|
</button>
|
|
<button class="p-1.5 text-slate-400 transition-colors delete-asset ${canManageAssets ? 'hover:text-error' : 'opacity-40 cursor-not-allowed'}" style="display: inline-flex; align-items: center; justify-content: center; flex: 0 0 auto;" data-asset-id="${asset.AssetId}" title="${canManageAssets ? 'Xóa' : 'Chi xem'}" ${canManageAssets ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-lg">delete</span>
|
|
</button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
`;
|
|
}).join('')}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div class="assets-mobile-list flex-1 flex-col gap-3 overflow-y-auto bg-slate-50/70 p-3">
|
|
${pageInfo.data.map((asset, index) => this.getAssetMobileCardHtml(asset, pageInfo.start + index, canManageAssets)).join('')}
|
|
</div>
|
|
<div class="page-pager flex items-center justify-between px-4 py-2 border-t border-slate-200 bg-slate-50 text-xs text-slate-600" id="assetsPager">
|
|
<span>Hiển thị ${pageInfo.start}-${pageInfo.end} / ${pageInfo.total}</span>
|
|
<div class="flex items-center gap-2">
|
|
<button class="asset-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === 1 ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current - 1}" ${pageInfo.current === 1 ? 'disabled' : ''}>Trước</button>
|
|
<span class="text-[11px]">Trang ${pageInfo.current} / ${pageInfo.totalPages}</span>
|
|
<button class="asset-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === pageInfo.totalPages ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current + 1}" ${pageInfo.current === pageInfo.totalPages ? 'disabled' : ''}>Sau</button>
|
|
</div>
|
|
</div>
|
|
` : `
|
|
<div class="flex-1 flex items-center justify-center text-center p-6">
|
|
<div>
|
|
<p class="text-sm text-on-surface-variant mb-4">Chưa có dữ liệu tài sản. Hãy thêm tài sản đầu tiên.</p>
|
|
<button id="addAssetBtn" class="bg-primary text-on-primary px-4 py-2 rounded-lg text-sm font-bold ${canManageAssets ? 'hover:bg-primary-dim' : 'opacity-50 cursor-not-allowed'}" ${canManageAssets ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-base inline mr-2">add_box</span>
|
|
Thêm tài sản dầu tiên
|
|
</button>
|
|
</div>
|
|
</div>
|
|
`}
|
|
</div>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
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 `
|
|
<tr class="group hover:bg-slate-50/80 transition-colors">
|
|
<td class="px-4 py-3 text-center">
|
|
<input type="checkbox" class="asset-row-checkbox w-4 h-4" data-asset-id="${asset.AssetId}" ${isSelected ? 'checked' : ''} ${canManageAssets ? '' : 'disabled'}>
|
|
</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${rowNumber}</td>
|
|
<td class="px-4 py-3 text-sm font-semibold text-slate-700">${asset.AssetCode || '-'}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700">${asset.AssetName || '-'}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.Model || '-'}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.SerialNumber || '-'}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.Quantity || 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.ImportInPeriod ?? 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.ExportInPeriod ?? 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.EndingBalance ?? 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.Unit || '-'}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.Department || '-'}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.Project || '-'}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.Custodian || '-'}</td>
|
|
<td class="px-4 py-3 text-sm" style="width: 132px; min-width: 132px; white-space: nowrap;">
|
|
<span class="inline-block px-2 py-1 rounded text-xs font-semibold ${statusMeta.className}" style="display: inline-block; white-space: nowrap; word-break: keep-all; overflow-wrap: normal;">${statusMeta.label}</span>
|
|
</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.NewQuantity ?? 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.UsedQuantity ?? 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.ExportInPeriod ?? 0}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.Location || '-'}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${this.formatDateOnly(asset.PurchaseDate)}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-normal" style="width: 240px; min-width: 240px;">${this.formatBorrowerTableHtml(asset.Borrower)}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 max-w-xs truncate" title="${asset.Notes || ''}">${asset.Notes || '-'}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${this.formatDateOnly(asset.CreatedDate)}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${asset.ExportedBy || '-'}</td>
|
|
<td class="sticky right-0 z-10 bg-white group-hover:bg-slate-50/80 px-4 py-3 text-right shadow-[-10px_0_12px_-12px_rgba(15,23,42,0.45)]" style="z-index: 1; width: 140px; min-width: 140px; white-space: nowrap;">
|
|
<div class="ml-auto" style="margin-left: auto; display: flex; flex-wrap: nowrap; align-items: center; justify-content: flex-end; gap: 6px; white-space: nowrap;">
|
|
<button class="p-1.5 text-slate-400 transition-colors view-asset hover:text-slate-600" style="display: inline-flex; align-items: center; justify-content: center; flex: 0 0 auto;" data-asset-id="${asset.AssetId}" title="Xem chi tiết">
|
|
<span class="material-symbols-outlined text-lg">info</span>
|
|
</button>
|
|
<button class="p-1.5 text-slate-400 transition-colors edit-asset ${canManageAssets ? 'hover:text-primary' : 'opacity-40 cursor-not-allowed'}" style="display: inline-flex; align-items: center; justify-content: center; flex: 0 0 auto;" data-asset-id="${asset.AssetId}" title="${canManageAssets ? 'Sửa' : 'Chi xem'}" ${canManageAssets ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-lg">edit</span>
|
|
</button>
|
|
<button class="p-1.5 text-slate-400 transition-colors delete-asset ${canManageAssets ? 'hover:text-error' : 'opacity-40 cursor-not-allowed'}" style="display: inline-flex; align-items: center; justify-content: center; flex: 0 0 auto;" data-asset-id="${asset.AssetId}" title="${canManageAssets ? 'Xóa' : 'Chi xem'}" ${canManageAssets ? '' : 'disabled'}>
|
|
<span class="material-symbols-outlined text-lg">delete</span>
|
|
</button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
`;
|
|
}).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 = `
|
|
<span>Hiển thị ${pageInfo.start}-${pageInfo.end} / ${pageInfo.total}</span>
|
|
<div class="flex items-center gap-2">
|
|
<button class="asset-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === 1 ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current - 1}" ${pageInfo.current === 1 ? 'disabled' : ''}>Trước</button>
|
|
<span class="text-[11px]">Trang ${pageInfo.current} / ${pageInfo.totalPages}</span>
|
|
<button class="asset-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === pageInfo.totalPages ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current + 1}" ${pageInfo.current === pageInfo.totalPages ? 'disabled' : ''}>Sau</button>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
this.setupAssetRowListeners();
|
|
this.setupAssetPagerListeners();
|
|
}
|
|
|
|
setupAssetSelectionListeners() {
|
|
if (!this.canCurrentUserManageAssets()) {
|
|
this.selectedAssetIds.clear();
|
|
document.querySelectorAll('.asset-row-checkbox').forEach(checkbox => {
|
|
checkbox.checked = false;
|
|
checkbox.disabled = true;
|
|
});
|
|
const selectAllCheckbox = document.getElementById('selectAllAssetsCheckbox');
|
|
if (selectAllCheckbox) {
|
|
selectAllCheckbox.checked = false;
|
|
selectAllCheckbox.indeterminate = false;
|
|
selectAllCheckbox.disabled = true;
|
|
}
|
|
this.updateAssetBulkActionState();
|
|
return;
|
|
}
|
|
|
|
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';
|
|
}
|
|
|
|
this.updateAssetBulkActionState();
|
|
}
|
|
|
|
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;
|
|
|
|
if (!canManageAssets) {
|
|
this.selectedAssetIds.clear();
|
|
}
|
|
rowCheckboxes.forEach(checkbox => {
|
|
checkbox.disabled = !canManageAssets;
|
|
});
|
|
|
|
const selectAllCheckbox = document.getElementById('selectAllAssetsCheckbox');
|
|
if (selectAllCheckbox) {
|
|
const assetCountOnPage = assetSelectionById.size;
|
|
const hasRows = assetCountOnPage > 0;
|
|
selectAllCheckbox.checked = canManageAssets && hasRows && selectedOnPage === assetCountOnPage;
|
|
selectAllCheckbox.indeterminate = canManageAssets && selectedOnPage > 0 && selectedOnPage < assetCountOnPage;
|
|
selectAllCheckbox.disabled = !canManageAssets;
|
|
}
|
|
|
|
const selectedCount = canManageAssets ? this.selectedAssetIds.size : 0;
|
|
const selectedCountNode = document.getElementById('selectedAssetCount');
|
|
if (selectedCountNode) {
|
|
selectedCountNode.textContent = String(selectedCount);
|
|
}
|
|
|
|
const bulkDeleteBtn = document.getElementById('bulkDeleteAssetsBtn');
|
|
if (bulkDeleteBtn) {
|
|
const disabled = !canManageAssets || selectedCount === 0;
|
|
bulkDeleteBtn.disabled = disabled;
|
|
bulkDeleteBtn.classList.toggle('opacity-50', disabled);
|
|
bulkDeleteBtn.classList.toggle('cursor-not-allowed', disabled);
|
|
}
|
|
|
|
const borrowAssetBtn = document.getElementById('borrowAssetBtn');
|
|
if (borrowAssetBtn) {
|
|
const disabled = !canManageAssets || selectedCount !== 1;
|
|
borrowAssetBtn.disabled = disabled;
|
|
borrowAssetBtn.classList.toggle('opacity-50', disabled);
|
|
borrowAssetBtn.classList.toggle('cursor-not-allowed', disabled);
|
|
borrowAssetBtn.classList.toggle('hover:bg-primary/5', !disabled);
|
|
}
|
|
|
|
const damageAssetBtn = document.getElementById('damageAssetBtn');
|
|
if (damageAssetBtn) {
|
|
const disabled = !canManageAssets || selectedCount !== 1;
|
|
damageAssetBtn.disabled = disabled;
|
|
damageAssetBtn.classList.toggle('opacity-50', disabled);
|
|
damageAssetBtn.classList.toggle('cursor-not-allowed', disabled);
|
|
damageAssetBtn.classList.toggle('hover:bg-red-50', !disabled);
|
|
}
|
|
}
|
|
|
|
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);
|
|
return {
|
|
...asset,
|
|
OutstandingQuantity: Math.max(borrowerQuantity, this.parseNonNegativeInteger(asset.ExportInPeriod, 0))
|
|
};
|
|
}).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 => `
|
|
<tr class="border-b border-slate-100">
|
|
<td class="px-4 py-3 text-sm text-slate-700">${this.escapeHtml(asset.AssetCode || '-')}<div class="text-[11px] text-slate-500">Hồ sơ cũ #${Number(asset.AssetId) || 0}</div></td>
|
|
<td class="px-4 py-3 text-sm text-slate-700">${this.escapeHtml(asset.AssetName || '-')}<div class="text-[11px] text-slate-500">${this.escapeHtml([asset.Model, asset.SerialNumber].filter(Boolean).join(' · '))}</div></td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${this.escapeHtml(this.formatDateTime(asset.ArchivedDate))}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700">${this.parseNonNegativeInteger(asset.Quantity, 0)}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700">${this.parseNonNegativeInteger(asset.ImportInPeriod, 0)}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700">${this.parseNonNegativeInteger(asset.ExportInPeriod, 0)}</td>
|
|
<td class="px-4 py-3 text-sm font-bold text-slate-700">${this.parseNonNegativeInteger(asset.EndingBalance, 0)}</td>
|
|
<td class="px-4 py-3 text-sm font-bold text-slate-700">${asset.OutstandingQuantity}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700">${this.escapeHtml(asset.Unit || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700 whitespace-pre-line">${this.escapeHtml(this.formatBorrowerSummaryText(asset.Borrower) || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700">${this.escapeHtml([asset.Department, asset.Project].filter(Boolean).join(' / ') || '-')}</td>
|
|
</tr>
|
|
`).join('') : '<tr><td colspan="11" class="px-4 py-8 text-center text-sm text-slate-500">Không có tài sản cũ phù hợp.</td></tr>';
|
|
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()],
|
|
['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', asset?.Project],
|
|
['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 = `
|
|
<div class="md:col-span-2">
|
|
<label class="text-[10px] font-bold uppercase text-slate-500 tracking-widest block mb-1">Người mượn</label>
|
|
<div class="w-full border border-slate-200 rounded-lg text-sm py-2.5 px-3 bg-slate-50 text-slate-600 whitespace-pre-line">${borrowerSummary || '-'}</div>
|
|
</div>
|
|
${fields.map(([label, value]) => `
|
|
<div>
|
|
<label class="text-[10px] font-bold uppercase text-slate-500 tracking-widest block mb-1">${label}</label>
|
|
<div class="w-full border border-slate-200 rounded-lg text-sm py-2.5 px-3 bg-slate-50 text-slate-600 break-words">${value || '-'}</div>
|
|
</div>
|
|
`).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 <span class="text-red-600">*</span>'
|
|
: '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');
|
|
}
|
|
}
|
|
|
|
async openBorrowAssetModal() {
|
|
if (!this.ensureAssetManagePermission('xuat tai san')) {
|
|
return;
|
|
}
|
|
|
|
const asset = this.getSingleSelectedAssetForBorrowing(true);
|
|
if (!asset) {
|
|
return;
|
|
}
|
|
|
|
if (!this.users.length) {
|
|
await this.fetchUsers();
|
|
}
|
|
if (!this.assetProjects.length) {
|
|
await this.fetchAssetProjects();
|
|
}
|
|
|
|
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ể 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 borrowByInput = document.getElementById('borrowByInput');
|
|
const borrowRoleInput = document.getElementById('borrowRoleInput');
|
|
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;
|
|
}
|
|
|
|
if (assetIdInput) {
|
|
assetIdInput.value = String(asset.AssetId || '');
|
|
}
|
|
|
|
assetNameInput.value = `${asset.AssetCode || ''} - ${asset.AssetName || ''}`.trim();
|
|
endingInput.value = String(metrics.endingBalance);
|
|
quantityInput.value = '1';
|
|
quantityInput.min = '1';
|
|
quantityInput.max = String(metrics.endingBalance);
|
|
|
|
this.refreshBorrowAssetUserOptions('');
|
|
this.refreshBorrowAssetProjectOptions(String(asset?.Project || '').trim());
|
|
noteInput.value = '';
|
|
|
|
if (borrowByInput) {
|
|
borrowByInput.value = this.getCurrentUserDisplayName();
|
|
}
|
|
|
|
if (borrowRoleInput) {
|
|
borrowRoleInput.value = String(this.getCurrentUserRoleRaw() || '').trim() || '-';
|
|
}
|
|
|
|
modal.classList.add('open');
|
|
}
|
|
|
|
async handleBorrowAssetSubmit(e) {
|
|
e.preventDefault();
|
|
|
|
if (!this.ensureAssetManagePermission('xuat tai san')) {
|
|
return;
|
|
}
|
|
|
|
const assetIdInput = document.getElementById('borrowAssetIdInput');
|
|
const borrowerInput = document.getElementById('borrowAssetUserInput');
|
|
const projectInput = document.getElementById('borrowAssetProjectInput');
|
|
const quantityInput = document.getElementById('borrowQuantityInput');
|
|
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('Vui lòng chọn người mượn.');
|
|
return;
|
|
}
|
|
|
|
const projectName = String(projectInput?.value || '').trim();
|
|
if (!projectName) {
|
|
this.notifyWarning('Vui lòng chọn dự án cần xuất.');
|
|
return;
|
|
}
|
|
|
|
const borrowQuantity = this.parseNonNegativeInteger(quantityInput?.value ?? 0, 0);
|
|
if (borrowQuantity <= 0) {
|
|
this.notifyWarning('Số lượng xuất phải lớn hơn 0.');
|
|
return;
|
|
}
|
|
const exportNote = String(noteInput?.value || '').trim();
|
|
|
|
const currentMetrics = this.buildAssetQuantityMetrics(asset);
|
|
if (currentMetrics.endingBalance <= 0) {
|
|
this.notifyWarning('Tài sản đã hết tồn cuối kỳ, không thể xuất thêm.');
|
|
return;
|
|
}
|
|
|
|
if (borrowQuantity > currentMetrics.endingBalance) {
|
|
this.notifyWarning(`Số lượng xuất (${borrowQuantity}) vượt quá tồn cuối kỳ (${currentMetrics.endingBalance}).`);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const response = await fetch(`${this.apiBase}/assets/${selectedAssetId}/export`, {
|
|
method: 'POST',
|
|
headers: this.getAuthHeaders(true),
|
|
body: JSON.stringify({
|
|
quantity: borrowQuantity,
|
|
borrowerName,
|
|
custodianName: borrowerName,
|
|
projectName,
|
|
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;
|
|
}
|
|
|
|
this.pendingBorrowAssetId = undefined;
|
|
this.notifySuccess('Xuất tài sản thành công');
|
|
this.closeModals();
|
|
await this.refreshAssetsUI();
|
|
const exportHistoryModal = document.getElementById('assetExportHistoryModal');
|
|
if (exportHistoryModal?.classList.contains('open')) {
|
|
await this.fetchAssetExportHistories();
|
|
this.renderAssetExportHistoryModal();
|
|
}
|
|
} catch (err) {
|
|
console.error(err);
|
|
this.notifyFailure('Xuất tài sản thất bại');
|
|
}
|
|
}
|
|
|
|
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 = '<div class="px-3 py-3 text-sm text-slate-500">Không tìm thấy vật tư phù hợp.</div>';
|
|
return;
|
|
}
|
|
|
|
listNode.innerHTML = rows.map(item => {
|
|
const consumableId = Number(item?.ConsumableId);
|
|
const selected = selectedId === consumableId;
|
|
return `
|
|
<button
|
|
type="button"
|
|
class="consumable-borrow-product-option block min-w-full w-max text-left px-3 py-2 hover:bg-slate-50 transition-colors ${selected ? 'bg-primary/10 text-primary' : 'text-slate-700'}"
|
|
data-consumable-id="${consumableId}"
|
|
>
|
|
<div class="text-sm font-medium whitespace-nowrap pr-6">${this.escapeHtml(this.getConsumableBorrowProductDisplayName(item))}</div>
|
|
${item?.Model ? `<div class="text-xs text-slate-500 mt-0.5 whitespace-nowrap pr-6">Model: ${this.escapeHtml(item.Model)}</div>` : ''}
|
|
</button>
|
|
`;
|
|
}).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 `<tr><td colspan="11" class="px-4 py-8 text-sm text-center text-slate-500">Chưa có đơn mượn/trả vật tư.</td></tr>`;
|
|
}
|
|
|
|
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'
|
|
? `<div class="inline-flex items-start gap-1.5 rounded-lg border border-red-200 bg-red-50 px-2.5 py-2 text-red-700">
|
|
<span class="material-symbols-outlined text-base shrink-0">info</span>
|
|
<span>${this.escapeHtml(rejectReason || 'Chưa ghi nhận lý do từ chối')}</span>
|
|
</div>`
|
|
: '<span class="text-slate-400">-</span>';
|
|
|
|
return `
|
|
<tr class="hover:bg-slate-50/80 transition-colors">
|
|
<td class="px-4 py-3 text-sm font-semibold text-slate-700 whitespace-nowrap">#${requestId || '-'}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700">${this.escapeHtml(item?.BorrowerName || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-700 min-w-[260px]">${this.escapeHtml(itemLabel)}</td>
|
|
<td class="px-4 py-3 text-sm whitespace-nowrap"><span class="inline-block px-2 py-1 rounded text-xs font-semibold ${typeMeta.className}">${typeMeta.label}</span></td>
|
|
<td class="px-4 py-3 text-sm font-semibold text-slate-700 text-right whitespace-nowrap">${Number(item?.BorrowQuantity) || 0} ${this.escapeHtml(item?.Unit || '')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${this.formatDateOnly(item?.BorrowDate)}</td>
|
|
<td class="px-4 py-3 text-sm whitespace-nowrap"><span class="inline-block px-2 py-1 rounded text-xs font-semibold ${statusMeta.className}">${statusMeta.label}</span></td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 max-w-[240px] whitespace-pre-line">${this.escapeHtml(item?.RequestNote || '-')}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${this.escapeHtml(item?.ProcessedByName || '-')}</td>
|
|
<td class="px-4 py-3 text-sm max-w-[280px] whitespace-pre-line">${rejectReasonHtml}</td>
|
|
<td class="px-4 py-3 text-right whitespace-nowrap">
|
|
${canManageAssets && isPending ? `
|
|
<button class="consumable-borrow-approve-btn px-2.5 py-1.5 rounded-lg bg-primary text-on-primary hover:bg-primary-dim text-xs font-bold" data-request-id="${requestId}">Duyệt ${typeMeta.key === 'return' ? 'trả' : 'mượn'}</button>
|
|
<button class="consumable-borrow-reject-btn ml-1 px-2.5 py-1.5 rounded-lg bg-red-600 text-white hover:bg-red-700 text-xs font-bold" data-request-id="${requestId}">Từ chối</button>
|
|
` : ''}
|
|
${isPending ? `<button class="consumable-borrow-cancel-btn ml-1 px-2.5 py-1.5 rounded-lg bg-slate-100 text-slate-700 hover:bg-slate-200 text-xs font-bold" data-request-id="${requestId}">Hủy đơn</button>` : '-'}
|
|
</td>
|
|
</tr>
|
|
`;
|
|
}).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 = `<tr><td colspan="11" class="px-4 py-8 text-sm text-center text-slate-500">Đang tải đơn mượn/trả...</td></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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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();
|
|
await this.refreshConsumablesUI();
|
|
|
|
const exportHistoryModal = document.getElementById('consumableExportHistoryModal');
|
|
if (exportHistoryModal?.classList.contains('open')) {
|
|
await this.fetchConsumableExportHistories();
|
|
this.renderConsumableExportHistoryModal();
|
|
}
|
|
} catch (err) {
|
|
console.error(err);
|
|
this.notifyFailure('Xuất vật tư tiêu hao thất bại');
|
|
}
|
|
}
|
|
|
|
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();
|
|
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,
|
|
'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 || '',
|
|
'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
|
|
? `<button class="p-1.5 text-slate-400 transition-colors view-account hover:text-slate-600" data-account-id="${acc.AccountId}" title="View Details">
|
|
<span class="material-symbols-outlined text-lg">info</span>
|
|
</button>
|
|
<button class="p-1.5 text-slate-400 transition-colors edit-account hover:text-primary" data-account-id="${acc.AccountId}" title="Edit">
|
|
<span class="material-symbols-outlined text-lg">edit</span>
|
|
</button>
|
|
<button class="p-1.5 text-slate-400 transition-colors delete-account hover:text-error" data-account-id="${acc.AccountId}" title="Delete">
|
|
<span class="material-symbols-outlined text-lg">delete</span>
|
|
</button>`
|
|
: '<span class="text-slate-400 text-xs">-</span>';
|
|
return `
|
|
<tr class="hover:bg-slate-50/80 transition-colors group account-row" data-account-id="${acc.AccountId}" data-user-id="${acc.UserId}">
|
|
<td class="px-4 py-3 text-sm text-slate-600">${acc.Email || '-'}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600">${displayAccountUsername}</td>
|
|
<td class="px-4 py-3 text-sm">
|
|
<span class="px-2 py-1 bg-blue-100 text-blue-700 rounded text-xs font-semibold">${acc.AppName || '-'}</span>
|
|
</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${createdDate}</td>
|
|
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${updatedDate}</td>
|
|
<td class="px-4 py-3 text-right">
|
|
${actionContent}
|
|
</td>
|
|
</tr>
|
|
`;
|
|
}).join('');
|
|
|
|
const pager = document.getElementById('accountsPager');
|
|
if (pager) {
|
|
pager.innerHTML = `
|
|
<span>Showing ${pageInfo.start}-${pageInfo.end} of ${pageInfo.total}</span>
|
|
<div class="flex items-center gap-2">
|
|
<button class="account-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === 1 ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current - 1}" ${pageInfo.current === 1 ? 'disabled' : ''}>Prev</button>
|
|
<span class="text-[11px]">Page ${pageInfo.current} / ${pageInfo.totalPages}</span>
|
|
<button class="account-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === pageInfo.totalPages ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current + 1}" ${pageInfo.current === pageInfo.totalPages ? 'disabled' : ''}>Next</button>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
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 => `
|
|
<tr class="hover:bg-surface-container-low/30 transition-colors group">
|
|
<td class="px-6 py-3">
|
|
<div class="flex items-center gap-3">
|
|
<div class="w-8 h-8 rounded bg-surface-container-highest flex items-center justify-center text-primary">
|
|
<span class="material-symbols-outlined text-sm">${app.Icon || 'apps'}</span>
|
|
</div>
|
|
<span class="font-bold text-sm text-on-surface">${app.Name}</span>
|
|
</div>
|
|
</td>
|
|
<td class="px-6 py-3">
|
|
<span class="px-2 py-0.5 rounded-full text-[9px] font-black uppercase bg-surface-container-highest text-on-surface-variant">${app.Type}</span>
|
|
</td>
|
|
<td class="px-6 py-3 text-sm text-on-surface-variant max-w-xs truncate" title="${app.Description || ''}">${app.Description || '-'}</td>
|
|
<td class="px-6 py-3 text-sm text-primary max-w-xs truncate">${(app.Url || app.url) ? `<a href="${app.Url || app.url}" target="_blank" class="underline">${app.Url || app.url}</a>` : '-'}</td>
|
|
<td class="px-6 py-3">
|
|
<div class="flex items-center gap-1.5">
|
|
<div class="w-1.5 h-1.5 rounded-full ${(app.Status || app.status) === 'online' ? 'bg-primary' : 'bg-error'} ring-2 ${(app.Status || app.status) === 'online' ? 'ring-primary/20' : 'ring-error/20'}"></div>
|
|
<span class="text-xs font-medium ${(app.Status || app.status) === 'online' ? 'text-on-primary-fixed-variant' : 'text-error'}">${(app.Status || app.status) === 'online' ? 'Online' : 'Offline'}</span>
|
|
</div>
|
|
</td>
|
|
<td class="px-6 py-3 text-right">
|
|
<div class="flex items-center justify-end gap-1">
|
|
<button class="p-1.5 text-on-surface-variant hover:text-on-surface transition-colors view-app" data-app-id="${app.AppId}" title="View Details">
|
|
<span class="material-symbols-outlined text-lg">info</span>
|
|
</button>
|
|
<button class="p-1.5 text-on-surface-variant hover:text-primary transition-colors edit-app" data-app-id="${app.AppId}">
|
|
<span class="material-symbols-outlined text-lg">edit</span>
|
|
</button>
|
|
<button class="p-1.5 text-on-surface-variant hover:text-error transition-colors delete-app" data-app-id="${app.AppId}">
|
|
<span class="material-symbols-outlined text-lg">delete</span>
|
|
</button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
`).join('');
|
|
|
|
const pager = document.getElementById('appsPager');
|
|
if (pager) {
|
|
pager.innerHTML = `
|
|
<span>Showing ${pageInfo.start}-${pageInfo.end} of ${pageInfo.total}</span>
|
|
<div class="flex items-center gap-2">
|
|
<button class="app-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === 1 ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current - 1}" ${pageInfo.current === 1 ? 'disabled' : ''}>Prev</button>
|
|
<span class="text-[11px]">Page ${pageInfo.current} / ${pageInfo.totalPages}</span>
|
|
<button class="app-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === pageInfo.totalPages ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current + 1}" ${pageInfo.current === pageInfo.totalPages ? 'disabled' : ''}>Next</button>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
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 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.openAssetExportHistoryModal());
|
|
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 = `<option value="">Select a service</option>` +
|
|
this.applications.map(app => `<option value="${app.AppId}">${app.Name}</option>`).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 = `
|
|
<div class="fixed inset-0 bg-black/50 flex items-center justify-center z-50 modal-backdrop open" id="profileModal" style="display:flex;align-items:center;justify-content:center;padding:16px;">
|
|
<div class="bg-white dark:bg-slate-900 rounded-lg p-6 modal-content" style="width:min(720px,calc(100vw - 32px));max-height:calc(100vh - 32px);overflow:auto;">
|
|
<h2 class="text-xl font-bold text-slate-900 dark:text-slate-50">My Profile</h2>
|
|
<p class="text-xs text-slate-500 mt-1 mb-4">Update personal info and password in one place.</p>
|
|
<form id="profileForm" class="space-y-5">
|
|
<div class="rounded-xl border border-outline-variant/20 bg-surface-container-low/40 dark:bg-slate-800/40 p-4 space-y-3">
|
|
<div>
|
|
<label class="block text-sm font-medium mb-1">Username</label>
|
|
<input type="text" value="${profile?.Username || ''}" readonly class="w-full px-3 py-2 border border-outline-variant/30 rounded-lg bg-surface-container-low dark:bg-slate-800 opacity-80">
|
|
</div>
|
|
|
|
<div>
|
|
<label class="block text-sm font-medium mb-1">Full Name</label>
|
|
<input type="text" id="profileFullName" value="${profile?.FullName || ''}" class="w-full px-3 py-2 border border-outline-variant/30 rounded-lg bg-white dark:bg-slate-900" required>
|
|
</div>
|
|
|
|
<div>
|
|
<label class="block text-sm font-medium mb-1">Email</label>
|
|
<input type="email" id="profileEmail" value="${profile?.Email || ''}" class="w-full px-3 py-2 border border-outline-variant/30 rounded-lg bg-white dark:bg-slate-900" required>
|
|
<p class="text-xs mt-1 ${isVerified ? 'text-green-600' : 'text-amber-600'}">
|
|
${isVerified ? 'Email is confirmed' : 'Email is not confirmed yet'}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="rounded-xl border border-outline-variant/20 bg-surface-container-low/40 dark:bg-slate-800/40 p-4">
|
|
<label class="block text-sm font-semibold mb-3">Change Password</label>
|
|
<div class="space-y-3">
|
|
<div>
|
|
<label class="block text-xs font-semibold text-slate-600 dark:text-slate-300 mb-1">Current password</label>
|
|
<div class="flex items-center gap-2">
|
|
<input type="password" id="profileCurrentPassword" placeholder="Enter current password" class="flex-1 px-3 py-2 border border-outline-variant/30 rounded-lg bg-white dark:bg-slate-900">
|
|
<button type="button" data-password-toggle="profileCurrentPassword" class="shrink-0 px-2.5 py-2 border border-outline-variant/30 rounded-lg text-slate-500 hover:text-slate-700 hover:bg-slate-100 dark:hover:text-slate-200 dark:hover:bg-slate-800" aria-label="Show current password">
|
|
<span class="material-symbols-outlined text-base" id="profileCurrentPasswordIcon">visibility</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<label class="block text-xs font-semibold text-slate-600 dark:text-slate-300 mb-1">New password</label>
|
|
<div class="flex items-center gap-2">
|
|
<input type="password" id="profileNewPassword" placeholder="At least 12 characters" minlength="12" maxlength="256" class="flex-1 px-3 py-2 border border-outline-variant/30 rounded-lg bg-white dark:bg-slate-900">
|
|
<button type="button" data-password-toggle="profileNewPassword" class="shrink-0 px-2.5 py-2 border border-outline-variant/30 rounded-lg text-slate-500 hover:text-slate-700 hover:bg-slate-100 dark:hover:text-slate-200 dark:hover:bg-slate-800" aria-label="Show new password">
|
|
<span class="material-symbols-outlined text-base" id="profileNewPasswordIcon">visibility</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<label class="block text-xs font-semibold text-slate-600 dark:text-slate-300 mb-1">Confirm new password</label>
|
|
<div class="flex items-center gap-2">
|
|
<input type="password" id="profileConfirmPassword" placeholder="Confirm new password" minlength="12" maxlength="256" class="flex-1 px-3 py-2 border border-outline-variant/30 rounded-lg bg-white dark:bg-slate-900">
|
|
<button type="button" data-password-toggle="profileConfirmPassword" class="shrink-0 px-2.5 py-2 border border-outline-variant/30 rounded-lg text-slate-500 hover:text-slate-700 hover:bg-slate-100 dark:hover:text-slate-200 dark:hover:bg-slate-800" aria-label="Show confirm password">
|
|
<span class="material-symbols-outlined text-base" id="profileConfirmPasswordIcon">visibility</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<p class="text-xs text-slate-500">Leave password fields empty if you only want to update profile info.</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="flex gap-3">
|
|
<button type="button" data-close-modal="profileModal" class="flex-1 px-4 py-2 bg-outline/10 hover:bg-outline/20 rounded-lg transition-colors">Cancel</button>
|
|
<button type="submit" class="flex-1 px-4 py-2 bg-primary hover:bg-primary-dim text-on-primary font-bold rounded-lg transition-colors">Save</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
`;
|
|
|
|
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 < 12 || newPassword.length > 256)) {
|
|
this.notifyFailure('New password must be between 12 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 `
|
|
<div class="users-page p-4 md:p-6 w-full h-full flex flex-col overflow-hidden">
|
|
<div class="page-header flex justify-between items-center mb-6 shrink-0">
|
|
<h1 class="text-2xl font-bold text-slate-900 dark:text-slate-50">Users Management</h1>
|
|
<div class="flex items-center gap-2">
|
|
<button id="addRoleBtn" class="border border-outline-variant/40 hover:bg-slate-100 dark:hover:bg-slate-800 text-slate-700 dark:text-slate-200 font-bold py-2 px-4 rounded-lg transition-all active:scale-95 flex items-center gap-2">
|
|
<span class="material-symbols-outlined text-base">badge</span>
|
|
<span>Add Role</span>
|
|
</button>
|
|
<button id="addUserBtn" class="bg-primary hover:bg-primary-dim text-on-primary font-bold py-2 px-4 rounded-lg transition-all active:scale-95 flex items-center gap-2">
|
|
<span class="material-symbols-outlined text-base">add</span>
|
|
<span>Add User</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Search and Filter -->
|
|
<div class="users-controls mb-4 flex gap-3 shrink-0">
|
|
<div class="flex-1 relative">
|
|
<input type="text" id="userSearch" placeholder="Search users..." value="${this.userSearchTerm}" class="w-full px-4 py-2 border border-outline-variant/30 rounded-lg bg-surface-container-low dark:bg-slate-800 focus:ring-2 focus:ring-primary focus:border-transparent transition-all">
|
|
<span class="absolute right-3 top-1/2 -translate-y-1/2 text-slate-400 material-symbols-outlined">search</span>
|
|
</div>
|
|
<select id="roleFilter" class="px-4 py-2 border border-outline-variant/30 rounded-lg bg-surface-container-low dark:bg-slate-800 text-sm">
|
|
<option value="">All Roles</option>
|
|
${this.roles.map(r => `<option value="${r.RoleId}" ${String(this.userRoleFilter) === String(r.RoleId) ? 'selected' : ''}>${r.RoleName}</option>`).join('')}
|
|
</select>
|
|
</div>
|
|
|
|
<!-- Users Table -->
|
|
<div class="flex-1 overflow-hidden border border-outline-variant/20 rounded-lg flex flex-col min-h-0">
|
|
<div class="table-wrap overflow-auto flex-1">
|
|
<table class="w-full text-sm">
|
|
<thead class="bg-slate-100 dark:bg-slate-800 border-b border-outline-variant/20 sticky top-0">
|
|
<tr>
|
|
<th class="px-4 py-3 text-left font-semibold">Username</th>
|
|
<th class="px-4 py-3 text-left font-semibold">Full Name</th>
|
|
<th class="px-4 py-3 text-left font-semibold">Email</th>
|
|
<th class="px-4 py-3 text-left font-semibold">Role</th>
|
|
<th class="px-4 py-3 text-left font-semibold">Status</th>
|
|
<th class="px-4 py-3 text-center font-semibold">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody class="users-table-body">
|
|
${pageInfo.data.length === 0 ? `
|
|
<tr>
|
|
<td colspan="6" class="px-4 py-8 text-center text-slate-500">No users found</td>
|
|
</tr>
|
|
` : pageInfo.data.map(user => `
|
|
<tr class="border-b border-outline-variant/10 hover:bg-slate-50 dark:hover:bg-slate-800/50 transition-colors user-row" data-user-id="${user.UserId}">
|
|
<td class="px-4 py-3 font-medium text-slate-900 dark:text-slate-50">${user.Username}</td>
|
|
<td class="px-4 py-3 text-slate-700 dark:text-slate-300">${user.FullName || '-'}</td>
|
|
<td class="px-4 py-3 text-slate-600 dark:text-slate-400">${user.Email || '-'}</td>
|
|
<td class="px-4 py-3">
|
|
<span class="px-3 py-1 rounded-full text-xs font-semibold ${user.RoleName === 'Admin' ? 'bg-blue-100 text-blue-700 dark:bg-blue-900 dark:text-blue-200' : 'bg-gray-100 text-gray-700 dark:bg-gray-700 dark:text-gray-200'}">
|
|
${user.RoleName || user.Role || 'N/A'}
|
|
</span>
|
|
</td>
|
|
<td class="px-4 py-3">
|
|
<span class="px-3 py-1 rounded-full text-xs font-semibold ${user.IsActive ? 'bg-green-100 text-green-700 dark:bg-green-900 dark:text-green-200' : 'bg-red-100 text-red-700 dark:bg-red-900 dark:text-red-200'}">
|
|
${user.IsActive ? 'Active' : 'Inactive'}
|
|
</span>
|
|
</td>
|
|
<td class="px-4 py-3 text-center">
|
|
<div class="flex gap-2 justify-center">
|
|
<button class="view-user-btn p-1.5 rounded hover:bg-slate-100 dark:hover:bg-slate-700 text-slate-600 dark:text-slate-300 transition-colors" title="View Details">
|
|
<span class="material-symbols-outlined text-base">info</span>
|
|
</button>
|
|
<button class="edit-user-btn p-1.5 rounded hover:bg-blue-100 dark:hover:bg-blue-900 text-blue-600 dark:text-blue-400 transition-colors" title="Edit">
|
|
<span class="material-symbols-outlined text-base">edit</span>
|
|
</button>
|
|
<button class="delete-user-btn p-1.5 rounded hover:bg-red-100 dark:hover:bg-red-900 text-red-600 dark:text-red-400 transition-colors" title="Delete" ${user.UserId === 1 ? 'disabled' : ''}>
|
|
<span class="material-symbols-outlined text-base">delete</span>
|
|
</button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
`).join('')}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div class="page-pager flex items-center justify-between px-4 py-2 border-t border-outline-variant/20 bg-slate-50 dark:bg-slate-800/40 text-xs text-on-surface-variant" id="usersPager">
|
|
<span>Showing ${pageInfo.start}-${pageInfo.end} of ${pageInfo.total}</span>
|
|
<div class="flex items-center gap-2">
|
|
<button class="user-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === 1 ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current - 1}" ${pageInfo.current === 1 ? 'disabled' : ''}>Prev</button>
|
|
<span class="text-[11px]">Page ${pageInfo.current} / ${pageInfo.totalPages}</span>
|
|
<button class="user-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === pageInfo.totalPages ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current + 1}" ${pageInfo.current === pageInfo.totalPages ? 'disabled' : ''}>Next</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
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 ? `
|
|
<tr>
|
|
<td colspan="6" class="px-4 py-8 text-center text-slate-500">No users found</td>
|
|
</tr>
|
|
` : pageInfo.data.map(user => `
|
|
<tr class="border-b border-outline-variant/10 hover:bg-slate-50 dark:hover:bg-slate-800/50 transition-colors user-row" data-user-id="${user.UserId}">
|
|
<td class="px-4 py-3 font-medium text-slate-900 dark:text-slate-50">${user.Username}</td>
|
|
<td class="px-4 py-3 text-slate-700 dark:text-slate-300">${user.FullName || '-'}</td>
|
|
<td class="px-4 py-3 text-slate-600 dark:text-slate-400">${user.Email || '-'}</td>
|
|
<td class="px-4 py-3">
|
|
<span class="px-3 py-1 rounded-full text-xs font-semibold ${user.RoleName === 'Admin' ? 'bg-blue-100 text-blue-700 dark:bg-blue-900 dark:text-blue-200' : 'bg-gray-100 text-gray-700 dark:bg-gray-700 dark:text-gray-200'}">
|
|
${user.RoleName || user.Role || 'N/A'}
|
|
</span>
|
|
</td>
|
|
<td class="px-4 py-3">
|
|
<span class="px-3 py-1 rounded-full text-xs font-semibold ${user.IsActive ? 'bg-green-100 text-green-700 dark:bg-green-900 dark:text-green-200' : 'bg-red-100 text-red-700 dark:bg-red-900 dark:text-red-200'}">
|
|
${user.IsActive ? 'Active' : 'Inactive'}
|
|
</span>
|
|
</td>
|
|
<td class="px-4 py-3 text-center">
|
|
<div class="flex gap-2 justify-center">
|
|
<button class="view-user-btn p-1.5 rounded hover:bg-slate-100 dark:hover:bg-slate-700 text-slate-600 dark:text-slate-300 transition-colors" title="View Details">
|
|
<span class="material-symbols-outlined text-base">info</span>
|
|
</button>
|
|
<button class="edit-user-btn p-1.5 rounded hover:bg-blue-100 dark:hover:bg-blue-900 text-blue-600 dark:text-blue-400 transition-colors" title="Edit">
|
|
<span class="material-symbols-outlined text-base">edit</span>
|
|
</button>
|
|
<button class="delete-user-btn p-1.5 rounded hover:bg-red-100 dark:hover:bg-red-900 text-red-600 dark:text-red-400 transition-colors" title="Delete" ${user.UserId === 1 ? 'disabled' : ''}>
|
|
<span class="material-symbols-outlined text-base">delete</span>
|
|
</button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
`).join('');
|
|
|
|
const pager = document.getElementById('usersPager');
|
|
if (pager) {
|
|
pager.innerHTML = `
|
|
<span>Showing ${pageInfo.start}-${pageInfo.end} of ${pageInfo.total}</span>
|
|
<div class="flex items-center gap-2">
|
|
<button class="user-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === 1 ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current - 1}" ${pageInfo.current === 1 ? 'disabled' : ''}>Prev</button>
|
|
<span class="text-[11px]">Page ${pageInfo.current} / ${pageInfo.totalPages}</span>
|
|
<button class="user-page-btn px-2 py-1 border border-slate-200 rounded text-xs ${pageInfo.current === pageInfo.totalPages ? 'opacity-50 cursor-not-allowed' : ''}" data-page="${pageInfo.current + 1}" ${pageInfo.current === pageInfo.totalPages ? 'disabled' : ''}>Next</button>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
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 = `
|
|
<div class="fixed inset-0 bg-black/50 flex items-center justify-center z-50 modal-backdrop open" id="roleModal">
|
|
<div class="bg-white dark:bg-slate-900 rounded-lg p-6 w-full max-w-md modal-content">
|
|
<h2 class="text-xl font-bold mb-4 text-slate-900 dark:text-slate-50">Add New Role</h2>
|
|
<form id="roleForm" class="space-y-4">
|
|
<div>
|
|
<label class="block text-sm font-medium mb-1">Role Name</label>
|
|
<input type="text" id="roleName" placeholder="Manager" class="w-full px-3 py-2 border border-outline-variant/30 rounded-lg bg-surface-container-low dark:bg-slate-800" required>
|
|
</div>
|
|
<div>
|
|
<label class="block text-sm font-medium mb-1">Description</label>
|
|
<textarea id="roleDescription" placeholder="Describe this role permissions" class="w-full px-3 py-2 border border-outline-variant/30 rounded-lg bg-surface-container-low dark:bg-slate-800 min-h-[90px]"></textarea>
|
|
</div>
|
|
<div class="flex gap-3 pt-2">
|
|
<button type="button" data-close-modal="roleModal" class="flex-1 px-4 py-2 bg-outline/10 hover:bg-outline/20 rounded-lg transition-colors">Cancel</button>
|
|
<button type="submit" class="flex-1 px-4 py-2 bg-primary hover:bg-primary-dim text-on-primary font-bold rounded-lg transition-colors">Save Role</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
`;
|
|
|
|
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 = `
|
|
<div class="fixed inset-0 bg-black/50 flex items-center justify-center z-50 modal-backdrop open" id="userModal">
|
|
<div class="bg-white dark:bg-slate-900 rounded-lg p-6 w-full max-w-md modal-content">
|
|
<h2 class="text-xl font-bold mb-4 text-slate-900 dark:text-slate-50">${user ? 'Edit User' : 'Add New User'}</h2>
|
|
<form id="userForm" class="space-y-4">
|
|
<div>
|
|
<label class="block text-sm font-medium mb-1">Username</label>
|
|
<input type="text" id="userUsername" placeholder="Username" class="w-full px-3 py-2 border border-outline-variant/30 rounded-lg bg-surface-container-low dark:bg-slate-800" ${user ? 'readonly' : ''} value="${user?.Username || ''}">
|
|
</div>
|
|
<div>
|
|
<label class="block text-sm font-medium mb-1">Full Name</label>
|
|
<input type="text" id="userFullName" placeholder="Full Name" class="w-full px-3 py-2 border border-outline-variant/30 rounded-lg bg-surface-container-low dark:bg-slate-800" value="${user?.FullName || ''}">
|
|
</div>
|
|
<div>
|
|
<label class="block text-sm font-medium mb-1">Email</label>
|
|
<input type="email" id="userEmail" placeholder="Email" class="w-full px-3 py-2 border border-outline-variant/30 rounded-lg bg-surface-container-low dark:bg-slate-800" value="${user?.Email || ''}">
|
|
</div>
|
|
<div>
|
|
<label class="block text-sm font-medium mb-1">${user ? 'New Password' : 'Password'}</label>
|
|
<div class="flex items-center gap-2">
|
|
<input type="password" id="userPassword" placeholder="${user ? 'Leave blank to keep current password' : 'At least 12 characters'}" minlength="12" maxlength="256" class="flex-1 px-3 py-2 border border-outline-variant/30 rounded-lg bg-surface-container-low dark:bg-slate-800" ${user ? '' : 'required'}>
|
|
<button type="button" id="userPasswordToggle" class="p-2 rounded-lg border border-outline-variant/30 hover:bg-slate-100 dark:hover:bg-slate-700" title="Show/Hide password">
|
|
<span class="material-symbols-outlined text-base" id="userPasswordToggleIcon">visibility</span>
|
|
</button>
|
|
</div>
|
|
<p class="text-xs text-slate-500 mt-1">${user ? 'Enter a new password only when you want to reset it.' : 'Password is required for new user.'}</p>
|
|
</div>
|
|
<div>
|
|
<label class="block text-sm font-medium mb-1">Role</label>
|
|
<select id="userRole" class="w-full px-3 py-2 border border-outline-variant/30 rounded-lg bg-surface-container-low dark:bg-slate-800">
|
|
${this.roles.map(r => `<option value="${r.RoleId}" ${user?.RoleId === r.RoleId ? 'selected' : ''}>${r.RoleName}</option>`).join('')}
|
|
</select>
|
|
</div>
|
|
<div>
|
|
<label class="flex items-center gap-2">
|
|
<input type="checkbox" id="userActive" class="w-4 h-4" ${user && user.IsActive ? 'checked' : 'checked'}>
|
|
<span class="text-sm">Active</span>
|
|
</label>
|
|
</div>
|
|
<div class="flex gap-3 pt-4">
|
|
<button type="button" data-close-modal="userModal" class="flex-1 px-4 py-2 bg-outline/10 hover:bg-outline/20 rounded-lg transition-colors">Cancel</button>
|
|
<button type="submit" class="flex-1 px-4 py-2 bg-primary hover:bg-primary-dim text-on-primary font-bold rounded-lg transition-colors">Save</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
`;
|
|
|
|
// 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 < 12 || password.length > 256)) {
|
|
this.notifyFailure('Password must be between 12 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 = `
|
|
<div class="fixed inset-0 bg-black/50 flex items-center justify-center z-50 modal-backdrop open" id="userDetailsModal">
|
|
<div class="bg-white dark:bg-slate-900 rounded-lg p-6 w-full max-w-md modal-content">
|
|
<h2 class="text-xl font-bold mb-4 text-slate-900 dark:text-slate-50">User Details</h2>
|
|
<div class="space-y-4">
|
|
<div>
|
|
<label class="block text-sm font-medium mb-1">Username</label>
|
|
<div id="userDetailUsername" class="w-full px-3 py-2 border border-outline-variant/30 rounded-lg bg-surface-container-low dark:bg-slate-800"></div>
|
|
</div>
|
|
<div>
|
|
<label class="block text-sm font-medium mb-1">Full Name</label>
|
|
<div id="userDetailFullName" class="w-full px-3 py-2 border border-outline-variant/30 rounded-lg bg-surface-container-low dark:bg-slate-800"></div>
|
|
</div>
|
|
<div>
|
|
<label class="block text-sm font-medium mb-1">Email</label>
|
|
<div id="userDetailEmail" class="w-full px-3 py-2 border border-outline-variant/30 rounded-lg bg-surface-container-low dark:bg-slate-800"></div>
|
|
</div>
|
|
<div>
|
|
<label class="block text-sm font-medium mb-1">Role</label>
|
|
<div id="userDetailRole" class="w-full px-3 py-2 border border-outline-variant/30 rounded-lg bg-surface-container-low dark:bg-slate-800"></div>
|
|
</div>
|
|
<div>
|
|
<label class="block text-sm font-medium mb-1">Status</label>
|
|
<div id="userDetailStatus" class="w-full px-3 py-2 border border-outline-variant/30 rounded-lg bg-surface-container-low dark:bg-slate-800"></div>
|
|
</div>
|
|
<div>
|
|
<label class="block text-sm font-medium mb-1">Password</label>
|
|
<div class="px-3 py-2 border border-outline-variant/30 rounded-lg bg-surface-container-low dark:bg-slate-800 text-sm text-slate-500">
|
|
Passwords are one-way hashed and cannot be displayed. Use Edit to reset the password.
|
|
</div>
|
|
</div>
|
|
<div class="grid grid-cols-2 gap-3">
|
|
<div>
|
|
<label class="block text-sm font-medium mb-1">Created Date</label>
|
|
<div id="userDetailCreatedDate" class="w-full px-3 py-2 border border-outline-variant/30 rounded-lg bg-surface-container-low dark:bg-slate-800"></div>
|
|
</div>
|
|
<div>
|
|
<label class="block text-sm font-medium mb-1">Last Login</label>
|
|
<div id="userDetailLastLogin" class="w-full px-3 py-2 border border-outline-variant/30 rounded-lg bg-surface-container-low dark:bg-slate-800"></div>
|
|
</div>
|
|
</div>
|
|
<div class="flex gap-3 pt-2">
|
|
<button type="button" data-close-modal="userDetailsModal" class="flex-1 px-4 py-2 bg-outline/10 hover:bg-outline/20 rounded-lg transition-colors">Close</button>
|
|
<button type="button" id="userDetailEditBtn" class="flex-1 px-4 py-2 bg-primary hover:bg-primary-dim text-on-primary font-bold rounded-lg transition-colors">Edit</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
`;
|
|
|
|
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 closeAssetExportHistoryModal() {
|
|
const modal = document.getElementById('assetExportHistoryModal');
|
|
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');
|
|
}
|
|
});
|