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