-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgate_nApp.js
185 lines (151 loc) · 7.11 KB
/
gate_nApp.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
///////// dromi ///////////////////////////////////////
const currentDate = displayCurrentDate();
function displayCurrentDate() {
const today = new Date();
const options = { year: 'numeric', month: 'numeric', day: 'numeric' };
const formattedDate = today.toLocaleDateString('en-IL', options);
document.getElementById('date').innerHTML = `תאריך: ${formattedDate}`;
console.log(formattedDate)
const stringWithDots = formattedDate.replace(/\//g, ".");
console.log(stringWithDots);
firestore.collection("412A").doc(stringWithDots).set({});
return formattedDate;
}
function getGateData() {
let Data = "";
console.log("now im here");
// Get the current date
const currentDate = new Date();
const formattedDate = currentDate.toLocaleDateString('he-IL');
const shiftSelect = document.getElementById('shiftSelect');
const selectedShift = shiftSelect.value;
Data += `**מעבירים דרומי**\n`;
Data += `משמרת: ${selectedShift}\n\n`;
Data += `תאריך: ${formattedDate}\n\n`;
// Select all rows with gate data
let rows = document.querySelectorAll(".row");
rows.forEach(row => {
// Find the gate name element
let gateNameElement = row.querySelector(".col:nth-child(1)");
if (!gateNameElement) {
console.warn("Missing gate name in row, skipping...");
return; // Skip this row if no gate name found
}
let gateName = gateNameElement.textContent.trim();
// Determine the status from the dropdown or radio buttons
let dropdown = row.querySelector('select[id*="select"]'); // Find dropdown
let goodRadio = row.querySelector('input[type="radio"][id*="good"]');
let badRadio = row.querySelector('input[type="radio"][id*="bad"]');
let status = "לא ידוע"; // Default status
if (dropdown) {
// Handle dropdown if it exists
let selectedValue = dropdown.value;
let highestOption = Array.from(dropdown.options).reduce((max, option) => {
let num = parseInt(option.value, 10);
return num > max ? num : max;
}, 0);
status = `${selectedValue}/${highestOption} מנעולים`; // Format as selected/highest מנעולים
// Add "תקין" if selectedValue equals highestOption
if (parseInt(selectedValue, 10) === highestOption) {
status += " ,מעביר תקין, ";
}
} else if (goodRadio && goodRadio.checked) {
status = "תקין";
} else if (badRadio && badRadio.checked) {
status = "לא תקין";
}
// Extract notes, ensuring no "undefined" or empty placeholder text
let notesInput = row.querySelector(".notes-gates");
let notes = notesInput ? notesInput.value.trim() : "";
// Construct row data
let rowData = `${gateName} , ${status}`;
if (notes) rowData += ` , ${notes}`; // Only add notes if they exist
rowData += "\n";
// Append to Data
Data += rowData;
});
return Data;
}
function saveGateData() {
// Get the current date in the desired format
const currentDate = new Date();
const dateWithDots = currentDate.toLocaleDateString('he-IL').replace(/\//g, '.'); // Format date as "DD.MM.YYYY"
// Get the selected shift value from the dropdown
let shiftSelect = document.getElementById('shiftSelect');
let selectedShift = shiftSelect.value;
// Validate the shift selection
if (!selectedShift || selectedShift === "בחר משמרת") {
selectedShift = "לא ידוע";
}
// Reference Firebase Firestore
const db = firebase.firestore();
// Select all rows with gate data
let rows = document.querySelectorAll(".row");
rows.forEach(row => {
// Find the gate name element
let gateNameElement = row.querySelector(".col:nth-child(1)");
if (!gateNameElement) {
console.warn("Missing gate name in row, skipping...");
return; // Skip this row if no gate name found
}
let gateName = gateNameElement.textContent.trim();
// Determine the status from the dropdown or radio buttons
let dropdown = row.querySelector('select[id*="select"]'); // Find dropdown
let goodRadio = row.querySelector('input[type="radio"][id*="good"]'); // Radio for "good"
let badRadio = row.querySelector('input[type="radio"][id*="bad"]'); // Radio for "bad"
let status = "לא ידוע"; // Default status
if (dropdown) {
// Handle dropdown if it exists
let selectedValue = dropdown.value;
let highestOption = Array.from(dropdown.options).reduce((max, option) => {
let num = parseInt(option.value, 10);
return num > max ? num : max;
}, 0);
status = `${selectedValue}/${highestOption} מנעולים`; // Format as selected/highest מנעולים
} else if (goodRadio && goodRadio.checked) {
status = "תקין"; // Good is checked
} else if (badRadio && badRadio.checked) {
status = "לא תקין"; // Bad is checked
}
// Extract notes from the left column (leave empty if no notes)
let notesInput = row.querySelector(".notes-gates");
let notes = notesInput ? notesInput.value.trim() : ""; // Add notes only if they exist
// Prepare data object for Firebase
const data = {
gateName: gateName,
status: status,
notes: notes,
date: dateWithDots,
shift: selectedShift // Use the selected shift value
};
// Save the data to Firestore with the additional "gateDromi" collection
db.collection("412A").doc(dateWithDots) // Date document
.collection("gateDromi") // Add "gateDromi" collection
.doc(selectedShift) // Shift collection
.collection("gates") // Collection for all gates within the shift
.doc(gateName) // Unique document for each gate
.set(data, { merge: true })
.then(() => console.log(`Data for gate ${gateName} saved successfully`))
.catch(error => console.error(`Error saving data for gate ${gateName}:`, error));
});
}
function copyGateData() {
console.log("im here ");
saveGateData()
const gateData = getGateData();
navigator.clipboard.writeText(gateData).then(() => {
alert("הנתונים הועתקו בהצלחה");
}).catch(err => {
alert("שגיאה בהעתקת הנתונים: " + err);
});
}
function sendGatWhatsApp(){
const gateData = getGateData();
saveGateData()
const phoneNumber = "972522121836";
const whatsappURL = `https://wa.me/${phoneNumber}?text=${encodeURIComponent(gateData)}`;
window.open(whatsappURL, "_blank");
}
function goHome() {
window.location.href = 'index.html';
}