mp/Sean/views/inusers.ejs
2023-12-17 20:47:13 +08:00

252 lines
6.3 KiB
Plaintext

<!-- views/inusers.ejs -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>In-House Users</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.4/xlsx.full.min.js"></script>
<style>
body {
margin: 0;
font-family: 'Arial', sans-serif;
}
#sidebar {
height: 100%;
width: 250px;
position: fixed;
background-color: #333;
padding-top: 60px;
transition: 0.5s;
}
#sidebar img {
width: 100%;
margin-bottom: 20px;
}
#sidebar a {
padding: 10px 15px;
text-decoration: none;
font-size: 18px;
color: white;
display: block;
transition: 0.3s;
}
#sidebar a:hover {
background-color: #555;
}
#content {
margin-left: 250px;
padding: 16px;
}
@media screen and (max-width: 600px) {
#sidebar {
width: 0;
overflow-x: hidden;
}
#content {
margin-left: 0;
}
}
#sidebarCollapse {
width: 40px;
height: 40px;
position: absolute;
top: 10px;
left: 10px;
cursor: pointer;
z-index: 1;
}
#sidebarCollapse span {
display: block;
background: white;
height: 5px;
width: 30px;
margin: 6px auto;
transition: 0.3s;
}
#sidebarCollapse:hover span:nth-child(1) {
transform: rotate(-45deg) translate(-5px, 6px);
}
#sidebarCollapse:hover span:nth-child(2) {
opacity: 0;
}
#sidebarCollapse:hover span:nth-child(3) {
transform: rotate(45deg) translate(-5px, -6px);
}
/* Add additional styles specific to inusers.ejs below */
#userDataContainer {
margin-top: 20px;
}
table {
border-collapse: collapse;
width: 100%;
margin-top: 20px;
}
th, td {
border: 1px solid #dddddd;
text-align: left;
padding: 12px;
}
th {
background-color: #f2f2f2;
}
tr:hover {
background-color: #f5f5f5;
}
td {
white-space: nowrap;
}
</style>
</head>
<body>
<div id="sidebar">
<img src="LOGO.PNG" alt="Custom Image">
<div id="sidebarCollapse">
<span></span>
<span></span>
<span></span>
</div>
<a href="#" id="userDataLink">User Data</a>
<a href="/inusers/edituserdata">Edit User Data</a>
<a href="/home" id="homeLink">Home</a>
</div>
<div id="content">
<h2>Welcome to the In-House Users Page</h2>
<div id="downloadButtonContainer">
<button id="downloadButton">Download as Excel</button>
</div>
<div id="userDataContainer">
<h3>All Users</h3>
<table>
<thead>
<tr>
<th>Name</th>
<th>Username</th>
<th>Email</th>
<th>Password</th>
<th>Last Login</th>
<th>Job Title</th>
<!-- Add more table headers as needed -->
</tr>
</thead>
<tbody>
<!-- Data will be populated dynamically using JavaScript -->
</tbody>
</table>
</div>
<!-- Additional content for In-House Users page goes here -->
<script>
document.getElementById('sidebarCollapse').addEventListener('click', function () {
document.getElementById('sidebar').style.width = (document.getElementById('sidebar').style.width === '250px') ? '0' : '250px';
});
// Function to download data as Excel
function downloadExcel() {
// Assuming 'allUsers' is defined in the global scope
if (window.allUsers && window.allUsers.length > 0) {
const data = window.allUsers.map(user => [
user.name,
user.username,
user.email,
user.password,
new Date(user.lastLogin).toLocaleString('en-US', { timeZone: 'Asia/Singapore' }),
user.jobTitle
// Add more data fields as needed
]);
const ws = XLSX.utils.aoa_to_sheet([['Name', 'Username', 'Email', 'Password', 'Last Login', 'Job Title'], ...data]);
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'All Users');
XLSX.writeFile(wb, 'user_data.xlsx');
} else {
console.error('No data available for download.');
// Handle the case where there is no data
}
}
// Function to display user data
function displayUserData() {
$.ajax({
url: '/inusers/userdata',
method: 'GET',
dataType: 'json', // Assuming the response is JSON
success: function (data) {
window.allUsers = data; // Assuming the response contains user data
populateTable(data);
},
error: function (error) {
console.error('Error fetching user data:', error);
// Handle error as needed
}
});
}
// Function to populate the table with user data
function populateTable(users) {
const tbody = document.querySelector('tbody');
tbody.innerHTML = ''; // Clear existing data
if (users && users.length > 0) {
users.forEach(user => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${user.name}</td>
<td>${user.username}</td>
<td>${user.email}</td>
<td>${user.password}</td>
<td>${new Date(user.lastLogin).toLocaleString('en-US', { timeZone: 'Asia/Singapore' })}</td>
<td>${user.jobTitle}</td>
<!-- Add more table data cells as needed -->
`;
tbody.appendChild(row);
});
} else {
const row = document.createElement('tr');
row.innerHTML = `<td colspan="6">No users available.</td>`;
tbody.appendChild(row);
}
}
// Call the function when the page loads
displayUserData();
// Attach click event to the download button
document.getElementById('downloadButton').addEventListener('click', function () {
downloadExcel();
});
</script>
</div>
</body>
</html>