This commit is contained in:
2026-11-02 15:09:59 +07:00
parent c441c809ca
commit 6a872e8c8b
16 changed files with 1706 additions and 7 deletions

View File

@@ -317,6 +317,14 @@ class AccountManager {
handleRoute(hash) {
const route = (hash || '#dashboard').replace('#', '') || 'dashboard';
if (route !== this.currentPage) document.getElementById('robotDeleteDialog')?.close();
if (this.currentPage === 'robot-bom' && route !== 'robot-bom') {
if (this.robotBom?.dirty && !this.robotBom.discardChanges()) {
history.replaceState(null, '', '#robot-bom');
return;
}
this.robotBom?.unmount();
}
if (this.isMobileViewport()) {
this.closeMobileNav();
}
@@ -381,6 +389,10 @@ class AccountManager {
} else if (page === 'robot-types') {
mainContent.innerHTML = this.getRobotTypesContent();
this.setupRobotTypeListeners();
} else if (page === 'robot-bom') {
this.robotBom ||= new window.RobotBomController(this);
this.robotBom.mount(mainContent, this.pendingBomModel);
this.pendingBomModel = null;
} else if (page === 'documents') {
mainContent.innerHTML = this.getDocumentsContent();
this.setupDocumentListeners();
@@ -3467,9 +3479,12 @@ class AccountManager {
}
async deleteRobotType(type) {
if (!this.canCurrentUserManageAssets() || Number(type.RobotCount) > 0) return;
if (!window.confirm(`Xóa thể loại “${type.Name}”?`)) return;
if (!this.canCurrentUserManageAssets() || Number(type.RobotCount) > 0 || this.robotDeletionPending) return;
this.robotDeletionPending = true;
try {
if (!await this.confirmRobotDeletion({ title: 'Xóa thể loại robot?', name: type.Name,
description: 'Thể loại này sẽ được gỡ khỏi danh mục robot.', label: 'Xóa thể loại',
note: 'Chỉ xóa được thể loại chưa có robot hoặc BOM sử dụng.' })) return;
const response = await fetch(`${this.apiBase}/robots/types/${Number(type.TypeId)}`, { method: 'DELETE' });
const data = await response.json();
if (!response.ok || !data.success) throw new Error(data.message || 'Không thể xóa thể loại');
@@ -3477,6 +3492,7 @@ class AccountManager {
if (this.currentPage === 'robot-types') this.renderView('robot-types');
this.notifySuccess(data.message || 'Đã xóa thể loại robot');
} catch (error) { this.notifyFailure(error.message); }
finally { this.robotDeletionPending = false; }
}
async fetchRobots() {
@@ -3531,6 +3547,7 @@ class AccountManager {
<div class="mt-4 flex items-center gap-2 border-t border-slate-100 pt-3">
<span class="mr-auto inline-flex items-center gap-1 text-xs text-slate-500"><span class="material-symbols-outlined text-base">attach_file</span>${Number(robot.DocumentCount) || 0} tài liệu</span>
<button type="button" data-robot-action="view" data-robot-id="${Number(robot.RobotId)}" class="rounded-lg border border-slate-200 px-3 py-2 text-xs font-bold text-primary hover:bg-slate-50">Chi tiết</button>
<button type="button" data-robot-action="bom" data-robot-id="${Number(robot.RobotId)}" class="rounded-lg border border-slate-200 px-3 py-2 text-xs font-bold text-primary hover:bg-slate-50">BOM</button>
${canManage ? `<button type="button" data-robot-action="edit" data-robot-id="${Number(robot.RobotId)}" class="rounded-lg p-2 text-slate-500 hover:bg-slate-100 hover:text-primary" aria-label="Sửa ${this.escapeHtml(robot.Name)}"><span class="material-symbols-outlined text-lg">edit</span></button>
<button type="button" data-robot-action="delete" data-robot-id="${Number(robot.RobotId)}" class="rounded-lg p-2 text-red-600 hover:bg-red-50" aria-label="Xóa ${this.escapeHtml(robot.Name)}"><span class="material-symbols-outlined text-lg">delete</span></button>` : ''}
</div>
@@ -3592,6 +3609,11 @@ class AccountManager {
try {
const id = Number(button.dataset.robotId);
if (button.dataset.robotAction === 'view') await this.openRobotDetails(id);
if (button.dataset.robotAction === 'bom') {
const robot = this.robots.find(robot => Number(robot.RobotId) === id);
this.pendingBomModel = robot ? { Model: robot.Model, TypeId: robot.TypeId } : null;
location.hash = '#robot-bom';
}
if (button.dataset.robotAction === 'edit') await this.openRobotForm(id);
if (button.dataset.robotAction === 'delete') await this.deleteRobot(id);
} finally { button.disabled = false; }
@@ -3788,11 +3810,43 @@ class AccountManager {
finally { delete dialog.dataset.saving; controls.forEach(control => { control.disabled = false; }); saveButton.textContent = 'Lưu robot'; }
}
confirmRobotDeletion({ title, name, description, note, label, details = [] }) {
if (document.getElementById('robotDeleteDialog')) return Promise.resolve(false);
const dialog = document.createElement('dialog');
dialog.id = 'robotDeleteDialog';
dialog.className = 'bom-dialog bom-confirm-dialog robot-delete-dialog';
dialog.setAttribute('aria-labelledby', 'robotDeleteTitle');
dialog.setAttribute('aria-describedby', 'robotDeleteDescription');
dialog.innerHTML = `<header class="bom-dialog-header"><div class="bom-confirm-heading"><span class="bom-confirm-icon material-symbols-outlined" aria-hidden="true">delete</span><div><p class="bom-eyebrow">QUẢN LÝ ROBOT</p><h2 id="robotDeleteTitle">${this.escapeHtml(title)}</h2></div></div><button type="button" data-cancel-robot-delete aria-label="Đóng"><span class="material-symbols-outlined">close</span></button></header>
<div class="bom-confirm-body"><p id="robotDeleteDescription">${this.escapeHtml(description)}</p>
<div class="robot-delete-card"><strong class="robot-delete-name">${this.escapeHtml(name)}</strong>${details.length ? `<dl class="robot-delete-details">${details.map(([key, value]) => `<div><dt>${this.escapeHtml(key)}</dt><dd>${this.escapeHtml(value || '—')}</dd></div>`).join('')}</dl>` : ''}</div>
<div class="bom-publish-note"><span class="material-symbols-outlined" aria-hidden="true">info</span><p>${this.escapeHtml(note)}</p></div></div>
<footer class="bom-dialog-footer"><div><button type="button" data-cancel-robot-delete class="bom-btn">Hủy</button><button type="button" data-confirm-robot-delete class="bom-btn bom-btn-danger"><span class="material-symbols-outlined" aria-hidden="true">delete</span>${this.escapeHtml(label)}</button></div></footer>`;
return new Promise(resolve => {
dialog.addEventListener('click', event => {
if (event.target.closest('[data-confirm-robot-delete]')) dialog.close('confirm');
else if (event.target.closest('[data-cancel-robot-delete]')) dialog.close();
else if (event.target === dialog) {
const rect = dialog.getBoundingClientRect();
if (event.clientX < rect.left || event.clientX > rect.right || event.clientY < rect.top || event.clientY > rect.bottom) dialog.close();
}
});
dialog.addEventListener('close', () => { dialog.remove(); resolve(dialog.returnValue === 'confirm'); }, { once: true });
document.body.appendChild(dialog); dialog.showModal();
dialog.querySelector('.bom-dialog-footer [data-cancel-robot-delete]').focus({ preventScroll: true });
});
}
async deleteRobot(robotId) {
if (!this.canCurrentUserManageAssets()) return;
if (!this.canCurrentUserManageAssets() || this.robotDeletionPending) return;
const robot = this.robots.find(item => Number(item.RobotId) === Number(robotId));
if (!robot || !window.confirm(`Xóa robot “${robot.Name}”? Các tài liệu vẫn được giữ trong kho.`)) return;
if (!robot) return;
this.robotDeletionPending = true;
try {
if (!await this.confirmRobotDeletion({ title: 'Xóa robot này?', name: robot.Name,
description: 'Robot này sẽ được gỡ khỏi danh sách quản lý.', label: 'Xóa robot',
details: [['Model', robot.Model], ['Thể loại', robot.Type]],
note: 'Tài liệu đính kèm vẫn được giữ trong kho tài liệu.' })) return;
const response = await fetch(`${this.apiBase}/robots/${Number(robotId)}`, { method: 'DELETE' });
const data = await response.json();
if (!response.ok || !data.success) throw new Error(data.message || 'Không thể xóa robot');
@@ -3800,6 +3854,7 @@ class AccountManager {
if (this.currentPage === 'robots') this.renderView('robots');
this.notifySuccess(data.message || 'Đã xóa robot');
} catch (error) { this.notifyFailure(error.message); }
finally { this.robotDeletionPending = false; }
}
isSupportedDocumentName(fileName) {