-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
executable file
·208 lines (169 loc) · 6.54 KB
/
script.js
File metadata and controls
executable file
·208 lines (169 loc) · 6.54 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const root_path = "https://kcksejyyjfgpcdmgtzrc.supabase.co/storage/v1/object/public/product_images/";
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/serviceworker.js');
}
async function loadProducts() {
loader.style.display = "flex";
const res = await fetch("https://digitalrdn.netlify.app/.netlify/functions/get-data")
loader.style.display = "none";
if (!res.ok) {
console.log(`HTTP error! status: ${res.status}`);
}
const result = await res.json();
if (!result.success) {
console.log(result.error || "Unknown error occurred");
}
const data = result.data;
if (!data) {
alert("Data not found");
return;
}
console.log(data);
let buyList = JSON.parse(localStorage.getItem("buyList")) || [];
for (let i in data) {
const item = data[i];
let isAdded = false;
buyList.forEach(p => {
if (p.name === item.name) {
isAdded = true;
} else {
isAdded - false;
}
});
const pcont = document.createElement("div");
pcont.className = "product";
pcont.id = item.id;
pcont.innerHTML = `
<img src="${root_path + item.file_name}" alt="${item.name}">
<div class="product-details">
<h3>${item.name}</h3>
<p>Price: ₹${item.price}/${item.unit}</p>
<p>${item.stock_quantity} ${item.unit}s are available.</p>
<input type="${item.type}" min="1" value="1" class="quantity-input"/>
</div>
<button class="book-button ${isAdded ? 'added' : ''}" onclick="toggleProduct(this, '${item.name}', ${item.price}, '${item.type}')">${isAdded ? 'Remove' : 'Add'}</button>
`
document.getElementById("products").appendChild(pcont);
}
}
function openPopup() {
let total = document.getElementById("amountDisplay").textContent;
if (total >= 10) {
document.getElementById("upiModal").style.display = "block";
} else {
document.getElementById("purchasemodel").style.display = "block";
}
}
function closePopup() {
document.getElementById("upiModal").style.display = "none";
document.getElementById("purchasemodel").style.display = "none";
}
function scrollToProducts() {
const target = document.querySelector('#products');
if (target) {
target.scrollIntoView({ behavior: 'smooth' });
}
}
function sendTID() {
const utr = document.getElementById('utrInput').value.trim();
if (utr === "") {
alert("Please enter Your Transaction ID.");
} else {
const phoneNumber = "916003375755";
const message = `My Transection ID is: ${utr}`;
const url = `https://wa.me/${phoneNumber}?text=${encodeURIComponent(message)}`;
window.open(url, '_blank');
}
}
function filterProducts() {
const query = document.getElementById("searchInput").value.toLowerCase();
const products = document.getElementsByClassName("product");
for (let i = 0; i < products.length; i++) {
const productName = products[i].getElementsByTagName("h3")[0].textContent.toLowerCase();
products[i].style.display = productName.includes(query) ? "flex" : "none";
}
}
function toggleProduct(button, name, price, type) {
const quantityInput = button.parentNode.querySelector("input");
let quantity = parseFloat(quantityInput.value) || 1;
if (type === "packaged") {
quantity = parseInt(quantity);
} else if (type === "loose") {
quantity = parseFloat(quantity).toFixed(3);
}
let buyList = JSON.parse(localStorage.getItem("buyList")) || [];
const index = buyList.findIndex(p => p.name === name);
if (index !== -1) {
buyList.splice(index, 1);
button.classList.remove("added");
button.textContent = "Add";
} else {
buyList.push({ name, price, quantity });
button.classList.add("added");
button.textContent = "Remove";
}
localStorage.setItem("buyList", JSON.stringify(buyList));
updateBuyListDisplay();
}
function payNow() {
const total = document.getElementById("amountDisplay").textContent
const upiID = "Q060474773@ybl"; // Replace with your PhonePe UPI ID
const name = "Rongpur Daily Needs";
const upiURL = `upi://pay?pa=${upiID}&pn=${encodeURIComponent(name)}&am=${total}&cu=INR`;
if (total >= 10) {
window.location.href = upiURL;
} else {
}
}
function downloadQR() {
const image = document.getElementById("qrImage");
const link = document.createElement("a");
link.href = image.src;
link.download = "Images/Q060474773.jpg";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
function updateBuyListDisplay() {
const list = JSON.parse(localStorage.getItem("buyList")) || [];
console.log(list)
const container = document.getElementById("buyListDisplay");
container.innerHTML = "<h2>🛒 Your Buy List</h2>";
if (list.length === 0) {
container.innerHTML += "<p>No items added yet.</p>";
return;
}
let total = 0;
container.innerHTML += "<ul>" + list.map(item => {
const subtotal = item.price * item.quantity;
total += subtotal;
document.getElementById("amountDisplay").textContent = total;
return `<li>${item.name} : ₹${item.price} × ${item.quantity} = ₹${subtotal}</li>`;
}).join("") + `</ul><strong>Total: ₹${total}</strong>`;
}
function clearBuyList() {
localStorage.removeItem("buyList");
updateBuyListDisplay();
document.getElementById("amountDisplay").textContent = 0
const buttons = document.getElementsByClassName("book-button");
for (let btn of buttons) {
btn.classList.remove("added");
btn.textContent = "Buy Now";
}
}
function shareWhatsAppList() {
const list = JSON.parse(localStorage.getItem("buyList")) || [];
if (list.length === 0) return alert("Your Buy List is empty!");
let total = 0;
let message = "🛒 *Buy List*:\n";
list.forEach(item => {
const subtotal = item.price * item.quantity;
total += subtotal;
message += `• ${item.name} : ₹${item.price} × ${item.quantity} = ₹${subtotal}\n`;
});
message += `\n*Total = ₹${total}*`;
const whatsappUrl = `https://wa.me/916003375755?text=${encodeURIComponent(message)}`;
window.open(whatsappUrl, "_blank");
}
window.onload = updateBuyListDisplay;
loadProducts();