-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
218 lines (192 loc) · 8.8 KB
/
index.html
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
209
210
211
212
213
214
215
216
217
218
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Order Form</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100">
<div class="container mx-auto px-4 py-8">
<!-- Header -->
<div class="mb-8">
<h1 class="text-3xl font-bold text-gray-800">Order Form</h1>
<p class="text-gray-600">Select items from our inventory</p>
</div>
<!-- Configuration Section -->
<div class="bg-white p-6 rounded-lg shadow-md mb-8">
<h2 class="text-xl font-semibold mb-4">Configuration</h2>
<div class="space-y-4">
<div>
<label class="block text-gray-700 mb-2">Inventory Sheet CSV URL</label>
<input type="text" id="csvUrl" class="w-full p-2 border rounded"
placeholder="Enter CSV URL or ?sheetId=YOUR_SHEET_ID">
</div>
<div>
<label class="block text-gray-700 mb-2">Order Form URL</label>
<input type="text" id="orderFormUrl" class="w-full p-2 border rounded"
placeholder="Enter Order Form URL">
</div>
<button id="loadInventory"
class="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600">
Load Inventory
</button>
</div>
</div>
<!-- Order Form -->
<div id="orderForm" class="bg-white p-6 rounded-lg shadow-md">
<h2 class="text-xl font-semibold mb-4">Order Items</h2>
<div id="loading" class="hidden">
<div class="flex items-center justify-center p-4">
<div class="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-500"></div>
<span class="ml-2">Loading inventory...</span>
</div>
</div>
<div id="categories" class="space-y-6"></div>
<button id="submitOrder"
class="mt-6 bg-green-500 text-white px-4 py-2 rounded hover:bg-green-600">
Submit Order
</button>
</div>
</div>
<script>
// Add these functions at the beginning of your script
function getSheetIdFromUrl(url) {
// Handle direct sheetId parameter
if (url.startsWith('?sheetId=')) {
return url.replace('?sheetId=', '');
}
// Handle full Google Sheets URL
const matches = url.match(/spreadsheets\/d\/e\/([-\w]+)/);
return matches ? matches[1] : null;
}
function updateUrlWithSheetId(sheetId) {
const newUrl = new URL(window.location.href);
newUrl.searchParams.set('sheetId', sheetId);
window.history.pushState({}, '', newUrl);
}
// Modify the document ready function
$(document).ready(function() {
let inventory = [];
// Check URL parameters first
const urlParams = new URLSearchParams(window.location.search);
const sheetId = urlParams.get('sheetId');
if (sheetId) {
$('#csvUrl').val(`https://docs.google.com/spreadsheets/d/e/${sheetId}/pub?output=csv`);
$('#loadInventory').click();
} else {
// Fall back to localStorage
const lastUsedUrl = localStorage.getItem('lastUsedCsvUrl');
if (lastUsedUrl) {
$('#csvUrl').val(lastUsedUrl);
}
}
// Modify the load inventory click handler
$('#loadInventory').click(function() {
const csvUrl = $('#csvUrl').val();
const sheetId = getSheetIdFromUrl(csvUrl);
if (sheetId) {
updateUrlWithSheetId(sheetId);
localStorage.setItem('lastUsedCsvUrl', csvUrl);
}
$('#loading').removeClass('hidden');
$.get(csvUrl, function(data) {
try {
inventory = parseCSV(data);
displayInventory(inventory);
$('#loading').addClass('hidden');
} catch (e) {
$('#loading').addClass('hidden');
alert('Error parsing CSV data: ' + e.message);
}
}).fail(function(error) {
$('#loading').addClass('hidden');
alert('Error loading inventory: ' + error.statusText);
});
});
// Update the parseCSV function to handle comma-separated values
function parseCSV(csv) {
const lines = csv.split('\n');
const result = [];
const headers = lines[0].split(',').map(h => h.trim());
for(let i = 1; i < lines.length; i++) {
if(!lines[i]) continue;
const obj = {};
const currentline = lines[i].split(',');
for(let j = 0; j < headers.length; j++) {
obj[headers[j]] = currentline[j] ? currentline[j].trim() : '';
}
result.push(obj);
}
return result;
}
// Display inventory grouped by category
function displayInventory(inventory) {
const $categories = $('#categories');
$categories.empty();
const groupedInventory = groupByCategory(inventory);
Object.keys(groupedInventory).forEach(category => {
const $category = $(`
<div class="category-section">
<h3 class="text-lg font-semibold mb-3">${category}</h3>
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4"></div>
</div>
`);
groupedInventory[category].forEach(item => {
const $item = $(`
<div class="border p-4 rounded">
<h4 class="font-medium">${item.Product}</h4>
<p class="text-gray-600">₹${item.Cost}/${item.Unit}</p>
<div class="mt-2">
<input type="number"
class="order-quantity w-20 p-1 border rounded"
min="${item['Min Unit']}"
max="${item['Available Units']}"
step="${item['Min Unit']}"
data-product="${item.Product}">
<span class="ml-2">${item.Unit}</span>
</div>
</div>
`);
$category.find('.grid').append($item);
});
$categories.append($category);
});
}
// Group inventory items by category
function groupByCategory(inventory) {
return inventory.reduce((acc, item) => {
if (!acc[item.Category]) {
acc[item.Category] = [];
}
acc[item.Category].push(item);
return acc;
}, {});
}
// Handle order submission
$('#submitOrder').click(function() {
const orderFormUrl = $('#orderFormUrl').val();
const orders = [];
$('.order-quantity').each(function() {
const quantity = $(this).val();
if (quantity && quantity > 0) {
orders.push({
product: $(this).data('product'),
quantity: quantity
});
}
});
if (orders.length === 0) {
alert('Please select at least one item to order');
return;
}
// Here you would typically format the order data according to your Google Form structure
// and redirect to the form URL with the pre-filled data
console.log('Orders:', orders);
// window.location.href = orderFormUrl + '?' + $.param(formattedData);
});
});
</script>
</body>
</html>