1 | function updateSearchInputVisibility(data) {
|
---|
2 | const searchInputContainer = document.getElementById("search-input-container");
|
---|
3 | if (data !== 'defaultValue') {
|
---|
4 | searchInputContainer.style.display = 'block';
|
---|
5 | } else {
|
---|
6 | searchInputContainer.style.display = 'none';
|
---|
7 | }
|
---|
8 | }
|
---|
9 |
|
---|
10 | function setInitialSelectValue(selectedElement){
|
---|
11 | selectedElement.selectedIndex=0;
|
---|
12 | }
|
---|
13 | function calculateAge(dateBirth){
|
---|
14 | const [year, month, day] = dateBirth.split('-').map(Number);
|
---|
15 | const birthDate = new Date(year, month - 1, day);
|
---|
16 | const currentDate = new Date();
|
---|
17 | let age = currentDate.getFullYear() - birthDate.getFullYear();
|
---|
18 | const currentMonth = currentDate.getMonth();
|
---|
19 | const currentDay = currentDate.getDate();
|
---|
20 |
|
---|
21 | if (currentMonth < (birthDate.getMonth()) || (currentMonth === birthDate.getMonth() && currentDay < birthDate.getDate())) {
|
---|
22 | age--;
|
---|
23 | }
|
---|
24 |
|
---|
25 | return age;
|
---|
26 | }
|
---|
27 |
|
---|
28 | function renderTable(filteredUsers) {
|
---|
29 |
|
---|
30 | const tbody = document.querySelector("#users-table tbody");
|
---|
31 | tbody.innerHTML = ""; // Clear existing rows
|
---|
32 | if(filteredUsers.length===0){
|
---|
33 | return;
|
---|
34 | }
|
---|
35 |
|
---|
36 | filteredUsers.forEach(user => {
|
---|
37 | const row = document.createElement("tr");
|
---|
38 |
|
---|
39 | for (const key of ["username", "name", "surname", "dateBirth", "phone"]) {
|
---|
40 | const cell = document.createElement("td");
|
---|
41 | if(key==='dateBirth'){
|
---|
42 | let ageTemp=calculateAge(user[key]);
|
---|
43 | cell.textContent=ageTemp.toString();
|
---|
44 | }
|
---|
45 | else{
|
---|
46 | cell.textContent = user[key];
|
---|
47 | }
|
---|
48 | row.appendChild(cell);
|
---|
49 | }
|
---|
50 |
|
---|
51 | const buttonCell = document.createElement("td");
|
---|
52 | const button = document.createElement("button");
|
---|
53 | button.textContent = "Преглед на корисник";
|
---|
54 | button.addEventListener('click',()=>{
|
---|
55 | let temp=user.username;
|
---|
56 | const params = new URLSearchParams({ param1: 'ADMIN', param2: temp }).toString();
|
---|
57 | window.location.href = `editUser.html?${params}`;
|
---|
58 | })
|
---|
59 | buttonCell.appendChild(button);
|
---|
60 | row.appendChild(buttonCell);
|
---|
61 | tbody.appendChild(row);
|
---|
62 | });
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | async function filterUsers(byParam,selectedValue) {
|
---|
67 | let url=`api/users/getUsersByParameter?parameter=${byParam}&filter=${selectedValue}`;
|
---|
68 | try{
|
---|
69 | const response=await fetch(url);
|
---|
70 | if(!response.ok){
|
---|
71 | console.log('Network response was not ok');
|
---|
72 | }
|
---|
73 | else{
|
---|
74 | const users=await response.json();
|
---|
75 | renderTable(users);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | catch(error){
|
---|
79 | console.log("Error fetching users");
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | let userStatusElement=document.getElementById("users-status");
|
---|
84 | let userParameterElement=document.getElementById("users-parameters");
|
---|
85 |
|
---|
86 | userStatusElement.addEventListener("change", (event)=>{
|
---|
87 | setInitialSelectValue(userParameterElement);
|
---|
88 | document.getElementById("search-input-container").style.display = 'none';
|
---|
89 | const selectedValue = event.currentTarget.value;
|
---|
90 | if(selectedValue!=='defaultValue')
|
---|
91 | filterUsers("status",selectedValue).then(r => console.log(r));
|
---|
92 | });
|
---|
93 |
|
---|
94 | userParameterElement.addEventListener("change", (event)=>{
|
---|
95 | setInitialSelectValue(userStatusElement);
|
---|
96 | const dataCheck = event.currentTarget.value;
|
---|
97 | updateSearchInputVisibility(dataCheck);
|
---|
98 |
|
---|
99 | document.getElementById("search-button").addEventListener('click',function (){
|
---|
100 | let filterTemp=document.getElementById("search-input");
|
---|
101 | filterUsers(dataCheck,filterTemp.value).then(r => console.log(r));
|
---|
102 | filterTemp.value='';
|
---|
103 | })
|
---|
104 | });
|
---|
105 |
|
---|
106 |
|
---|