// public/js/utils/dropdownHelpers.js import * as departmentManager from '../managers/departmentManager.js'; import * as userManager from '../managers/userManager.js'; import { getDepartmentsData } from '../managers/departmentManager.js'; import { getAllUsersFromCache } from '../managers/userManager.js'; /** * Hàm để đổ dữ liệu phòng ban vào các dropdown */ export async function populateDepartmentDropdown(dropdownId, selectedUid = '') { console.log('populateDepartmentDropdown called for ID:', dropdownId); const dropdown = document.getElementById(dropdownId); if (!dropdown) { console.error(`Department dropdown element with ID "${dropdownId}" not found.`); return; } const currentValue = dropdown.value; // Store current value to try and re-select after repopulating dropdown.innerHTML = ''; // Default option try { // Lấy dữ liệu phòng ban từ manager (đã được cache) console.log('populateDepartmentDropdown: Calling getDepartmentsData from departmentManager'); const departments = departmentManager.getDepartmentsData(); // Use manager to get cached data console.log('populateDepartmentDropdown: Fetched', departments.length, 'departments for dropdown.'); departments.forEach(department => { console.log('populateDepartmentDropdown: department option value:', department.id, 'name:', department.name); const option = document.createElement('option'); option.value = department.id; option.textContent = department.name; // Prioritize the originally selectedUid passed to the function // If not provided, try to select the previously selected value from the dropdown if (selectedUid && department.id === selectedUid) { option.selected = true; } else if (!selectedUid && department.id === currentValue) { option.selected = true; } dropdown.appendChild(option); }); console.log('populateDepartmentDropdown: Dropdown populated.'); } catch (err) { console.error('populateDepartmentDropdown error:', err); } } /** * Hàm để đổ dữ liệu người dùng vào các dropdown */ export async function populateUserDropdown(dropdownId, selectedUid = '') { const dropdown = document.getElementById(dropdownId); if (!dropdown) { console.error(`User dropdown element with ID "${dropdownId}" not found.`); return; } const currentValue = dropdown.value; // Store current value dropdown.innerHTML = ''; // Default option try { console.log('populateUserDropdown: Calling getUsersData from userManager'); // Lấy dữ liệu người dùng từ manager (đã được cache) const users = userManager.getAllUsersFromCache(); // Use manager to get cached data console.log('populateUserDropdown: Fetched', users.length, 'users for dropdown.'); users.forEach(user => { const option = document.createElement('option'); option.value = user.id; option.textContent = user.name; // Prioritize the originally selectedUid passed to the function // If not provided, try to select the previously selected value from the dropdown if (selectedUid && user.id === selectedUid) { option.selected = true; } else if (!selectedUid && user.id === currentValue) { option.selected = true; } dropdown.appendChild(option); }); console.log('populateUserDropdown: Dropdown populated'); } catch (err) { console.error('populateUserDropdown error:', err); // Note: This error will likely be caught by the caller, but logging here is also fine. } } // Optional: Helper to populate multiple known department dropdowns export function populateAllDepartmentDropdowns() { console.log('Populating all department dropdowns.'); // List all IDs of dropdowns that need department data const dropdownIds = ['addUserDepartment', 'editUserDepartment', 'taskDepartment']; // Add other IDs if necessary dropdownIds.forEach(dropdownId => { // We don't have the selected value easily here, so it will default to the first option. // If preserving the selected value is critical, the component managing that dropdown // would need to re-select based on its own state after this repopulation happens. populateDepartmentDropdown(dropdownId); }); } // Optional: Helper to populate multiple known user dropdowns export function populateAllUserDropdowns() { console.log('Populating all user dropdowns.'); // List all IDs of dropdowns that need user data const dropdownIds = ['taskAssignedTo', 'commentUser', 'otherUserDropdownId']; // Add other IDs if necessary dropdownIds.forEach(dropdownId => { populateUserDropdown(dropdownId); }); }