-
Notifications
You must be signed in to change notification settings - Fork 0
/
department.html
99 lines (91 loc) · 3.85 KB
/
department.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Departments</title>
<link rel="stylesheet" href="styles.css">
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<header>
<h1>Departments</h1>
<button onclick="window.location.href='department-register.html'">Register Department</button>
<button onclick="window.location.href='purchase-orders.html'">Purchase Orders</button>
</header>
<main>
<table border="1" cellpadding="10" cellspacing="0">
<thead>
<tr>
<th>Department Name</th>
<th>Budget Allocated</th>
<th>Action</th>
</tr>
</thead>
<tbody id="departments-table">
<!-- Rows will be dynamically populated here -->
</tbody>
</table>
</main>
<script>
// Fetch and display department data
function fetchDepartments() {
axios.get('/api/departments')
.then(response => {
const departments = response.data;
const tableBody = document.getElementById('departments-table');
tableBody.innerHTML = ''; // Clear existing rows
departments.forEach(department => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${department.DepartmentName}</td>
<td>
<input
type="number"
value="${department.Budget_Allocated}"
id="budget-${department.Department_ID}"
style="width: 100px;"
/>
</td>
<td>
<button onclick="updateBudget(${department.Department_ID})">Save</button>
<button onclick="navigateToBudgetAllocation(${department.Department_ID})">Budget Allocation</button>
</td>
`;
tableBody.appendChild(row);
});
})
.catch(error => {
console.error('Error fetching departments:', error);
});
}
// Update budget allocated for a department
function updateBudget(departmentId) {
const budgetInput = document.getElementById(`budget-${departmentId}`);
const newBudget = parseFloat(budgetInput.value);
if (isNaN(newBudget) || newBudget <= 0) {
alert('Please enter a valid budget.');
return;
}
axios.post('/api/update-department-budget', {
departmentId: departmentId,
newBudget: newBudget
})
.then(response => {
alert('Budget updated successfully!');
fetchDepartments(); // Refresh the table
})
.catch(error => {
console.error('Error updating budget:', error);
alert('Failed to update budget.');
});
}
// Navigate to the budget allocation page
function navigateToBudgetAllocation(departmentId) {
window.location.href = `budget-allocation.html?departmentId=${departmentId}`;
}
// Fetch departments on page load
document.addEventListener('DOMContentLoaded', fetchDepartments);
</script>
</body>
</html>