-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsale.php
341 lines (315 loc) · 17.3 KB
/
sale.php
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
include_once './header.php';
if (!isLogin() && !isAdmin() && !isMod()) {
exit("<script>window.location.href='index'</script>");
}
?>
<div class="container">
<!-- Notice for success/fail on insert -->
<?php
if (isset($_GET['error'])):
include_once './src/inc/error.inc.php';
if (array_key_exists($_GET['error'], $errorList)): ?>
<div class="alert w-100 mt-3 text-center alert-danger rounded">
<h3><?=$errorList[$_GET['error']]?></h3>
</div>
<?php endif;
endif;
?>
<?php if (isset($_GET['status']) && isset($_GET['status']) == "success"):?>
<div class="alert w-100 mt-3 text-center alert-success rounded">
<h3>Popust uspešno vnesen!</h3>
</div>
<?php endif;
if (isset($_GET['warning'])): ?>
<div class="alert w-100 mt-3 text-center alert-warning rounded">
<p><?=$_GET['warning']?> Popustov ni bilo možno vnesti!</p>
</div>
<?php endif;
?>
<?php
if (!isset($_GET['option']) || $_GET['option'] != "add" && $_GET['option'] != "modify" || empty($_GET['option'])):
?>
<div class="text-center">
<h2>Izberite, kaj želite storiti:</h2>
<div class="btn-group mb-2" style="width:180px">
<a type="button" class="btn btn-info" href="./sale?option=add"><i class="fas fa-plus"></i> Dodaj popuste</a>
</div>
<div class="btn-group mb-2" style="width:180px">
<a type="button" class="btn btn-info" href="./sale?option=modify"><i class="fas fa-wrench"></i> Spremeni popuste</a>
</div>
</div>
<?php
endif;
if (isset($_GET['option']) && $_GET['option'] == "add"):
?>
<div class="d-flex my-2 text-center">
<!-- update next / previous product -->
<div class="col-4 float-left">
<a class="p-2 btn btn-info text-center" style="width:180px;" href="./sale"><i class="fas fa-list"></i> Nazaj</a>
</div>
<div class="col-4 text-center">
<a class="p-2 btn btn-info text-center" style="width:180px;" href="./sale?option=modify"><i class="fas fa-wrench"></i> Spremeni popuste</a>
</div>
</div> <!-- d-flex my-2 text-center -->
<div class="card">
<div class="card-header">
<h2>Dodaj obdobja razprodaje</h2>
</div>
<div class="card-body">
<form id="sales" method="post" enctype="multipart/form-data" action="./src/inc/sale-insert.inc.php" >
<!-- add more button -->
<div class="d-flex justify-content-end mt-2">
<button class="add_form_field btn btn-success float-right"><i class="fa fa-plus"></i></button>
</div>
<div class="form-row">
<div class="form-group col-md-3">
<label class="col-form-label" for="start_date">Začetek popusta:</label>
<div class="input-group">
<input type="date" class="form-control" name="start_date[]" id=start_date placeholder="Začetek...">
<div class="input-group-append">
<div class="input-group-text"><i class="fas fa-calendar-alt"></i></div>
</div>
</div>
</div>
<div class="form-group col-md-3">
<label class="col-form-label" for="end_date">Konec popusta:</label>
<div class="input-group">
<input type="date" class="form-control" name="end_date[]" id=end_date placeholder="Konec...">
<div class="input-group-append">
<div class="input-group-text"><i class="fas fa-calendar-alt"></i></div>
</div>
</div>
</div>
<div class="form-group col-md-3">
<label class="col-form-label" for="discount">Popust:</label>
<div class="input-group">
<input type="number" class="form-control text-center hide-arrow" name="discount[]" id="discount" min=0 step="1" placeholder="Popust..." required>
<div class="input-group-append">
<div class="input-group-text">%</div>
</div>
</div>
</div>
</div> <!-- form-row -->
</form> <!-- form -->
<div class="d-flex justify-content-end mt-2">
<button form="sales" id="submit-btn" type="submit" name="submit" class="btn btn-primary float-right">Shrani</button>
</div>
</div><!-- card-body -->
</div> <!-- card -->
<script>
$(document).ready(function() {
var wrapper = $("form");
var add_button = $(".add_form_field");
$(add_button).click(function(e) {
e.preventDefault();
$(wrapper).append('<div class="form-row">\
<div class="form-group col-md-3">\
<label class="col-form-label" for="start_date">Začetek popusta:</label>\
<div class="input-group">\
<input type="date" class="form-control" name="start_date[]" id="start_date" placeholder="Začetek...">\
<div class="input-group-append">\
<div class="input-group-text"><i class="fas fa-calendar-alt"></i></div>\
</div>\
</div>\
</div>\
<div class="form-group col-md-3">\
<label class="col-form-label" for="end_date">Konec popusta:</label>\
<div class="input-group">\
<input type="date" class="form-control" name="end_date[]" id="end_date" placeholder="Konec...">\
<div class="input-group-append">\
<div class="input-group-text"><i class="fas fa-calendar-alt"></i></div>\
</div>\
</div>\
</div>\
<div class="form-group col-md-3">\
<label class="col-form-label" for="discount">Popust:</label>\
<div class="input-group">\
<input type="number" class="form-control text-center hide-arrow" name="discount[]" id="discount" min=0 step="1" placeholder="Popust..." required>\
<div class="input-group-append">\
<div class="input-group-text">%</div>\
</div>\
</div>\
</div>\
<div class="form-group col-md-1 align-self-center float-right">\
<a href="#" class="delete btn btn-danger"><i class="fa fa-minus"></i></a>\
</div>\
</div>');
});
$(wrapper).on("click", ".delete", function(e) {
e.preventDefault();
$(this).closest('.form-row').remove();
});
});
</script>
<?php
endif;
if (isset($_GET['option']) && $_GET['option'] == "modify"):
$pdo = pdo_connect_mysql();
// number of list items per page
$num_of_product_per_page = 10;
// URL this will appear as ./sale?p=1, ./sale?p=2
$current_page = isset($_GET['p']) && is_numeric($_GET['p']) ? (int)$_GET['p'] : 1;
// select products by latest
$stmt = $pdo->prepare('SELECT * FROM sale ORDER BY date_start ASC, date_end ASC, discount ASC LIMIT ?,? ');
// bindValue allows us to use integer in SQL, need it for LIMIT
$stmt->bindValue(1, ($current_page - 1) * $num_of_product_per_page, PDO::PARAM_INT);
$stmt->bindValue(2, $num_of_product_per_page, PDO::PARAM_INT);
$stmt->execute();
// fetch and return products as ARRAY
$sales = $stmt->fetchAll(PDO::FETCH_ASSOC);
// total amount of products
$total_sales = $pdo->query('SELECT * FROM sale')->rowCount();
$total_pages = ceil($total_sales/$num_of_product_per_page);
?>
<div class="d-flex my-2 text-center">
<!-- update next / previous product -->
<div class="col-4 float-left">
<a class="p-2 btn btn-info text-center" style="width:180px;" href="./sale"><i class="fas fa-list"></i> Nazaj</a>
</div>
<div class="col-4 text-center">
<a class="p-2 btn btn-info text-center" style="width:180px;" href="./sale?option=add"><i class="fas fa-plus"></i> Dodaj popuste</a>
</div>
</div> <!-- d-flex my-2 text-center -->
<div class="card">
<div class="card-header text-center">
<h2>Posodobi popuste</h2>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-12">
<div class="mt-3">
<ul class="list list-inline">
<?php foreach ($sales as $sale): ?>
<li class="d-flex justify-content-between border rounded mt-2 sale-item">
<div class="d-flex flex-row align-items-center col-4">
<div class="form-group col">
<label class="col-form-label" for="startDate">Začetek popusta:</label>
<div class="input-group">
<input type="date" class="form-control" name="startDate" id="startDate" data-original-value="<?=$sale['date_start']?>" value="<?=$sale['date_start']?>" disabled>
<div class="input-group-append">
<div class="input-group-text"><i class="fas fa-calendar-alt"></i></div>
</div>
</div>
</div>
</div>
<div class="d-flex flex-row align-items-center col-4">
<div class="form-group col">
<label class="col-form-label" for="endDate">Konec popusta:</label>
<div class="input-group">
<input type="date" class="form-control" name="endDate" id="endDate" data-original-value="<?=$sale['date_end']?>" value="<?=$sale['date_end']?>" disabled>
<div class="input-group-append">
<div class="input-group-text"><i class="fas fa-calendar-alt"></i></div>
</div>
</div>
</div>
</div>
<div class="d-flex flex-row align-items-center col-2">
<div class="form-group col">
<label class="col-form-label" for="discount">Popust:</label>
<div class="input-group">
<input type="number" class="form-control hide-arrow" name="discount" id="discount" data-original-value="<?=$sale['discount']?>" value="<?=$sale['discount']?>" disabled required>
<div class="input-group-append">
<div class="input-group-text"><i class="fas fa-percentage"></i></div>
</div>
</div>
</div>
</div>
<div class="d-flex flex-row align-items-center col-2">
<div class="d-flex flex-column mr-2">
<!-- put buttons in line -->
<div class="btn_group edit-button">
<button id="update" class="btn btn-info" value="<?=$sale['id']?>" name="sid">
<i class="fas fa-wrench"></i>
</button>
<button id="save" class="btn btn-success" value="<?=$sale['id']?>" name="sid" hidden disabled>
<i class="fas fa-save"></i>
</button>
<button id="delete" class="btn btn-danger" value="<?=$sale['id']?>" name="sid">
<i class="fa fa-minus"></i>
</button>
</div>
</div>
</div>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
</div>
</div>
</div>
<?=pagination($page, $total_pages, $current_page)?>
<!-- script to delete sales -->
<script>
$(document).on('click', '#delete', function() {
if (confirm("Želite odstraniti izdelek?")) {
var sid = $(this).val();
var ele = $(this).closest("li");
$.ajax({
type:'POST',
data: {'sid':sid},
url: './src/inc/sale-delete.inc.php',
success: function(data) {
if(data == "OK") {
alert("Popust uspešno odstranjen!");
ele.fadeOut().remove();
location.reload(true);
} else {
alert("Zgodila se je napaka pri odstranjevanju!");
}
}
});
} else {
}
});
</script>
<!-- script to update sales -->
<script>
$(document).on('click', '#update', function() {
// enable input fields
$(this).closest('.sale-item').find('#discount').prop( "disabled", false );
$(this).closest('.sale-item').find('#startDate').prop( "disabled", false );
$(this).closest('.sale-item').find('#endDate').prop( "disabled", false );
$(this).closest('.sale-item').find("#save").prop({"disabled": false, "hidden":false});
$(this).prop({"disabled": true, "hidden":true});
});
$(document).on('click', '#save', function() {
if (confirm("Želite shraniti popust?")) {
// get current input fields values
var sid = $(this).val();
var discount = $(this).closest('.sale-item').find('#discount').val();
var startDate = $(this).closest('.sale-item').find('#startDate').val();
var endDate = $(this).closest('.sale-item').find('#endDate').val();
console.log(sid, discount, startDate, endDate);
$.ajax({
type:'POST',
data: {'sid':sid, 'discount':discount, 'startDate':startDate, 'endDate':endDate},
url: './src/inc/sale-update.inc.php',
success: function(data) {
if(data == "OK") {
alert("Izdelek uspešno posodobljen!");
location.reload();
} else {
alert("Zgodila se je napaka pri odstranjevanju!");
}
}
});
} else {
$(this).closest('.sale-item').find('#discount').val($(this).closest('.sale-item').find('#discount').data("original-value"));
$(this).closest('.sale-item').find('#startDate').val($(this).closest('.sale-item').find('#startDate').data("original-value"));
$(this).closest('.sale-item').find('#endDate').val($(this).closest('.sale-item').find('#endDate').data("original-value"));
}
// disable input fields
$(this).closest('.sale-item').find('#discount').prop( "disabled", true );
$(this).closest('.sale-item').find('#startDate').prop( "disabled", true );
$(this).closest('.sale-item').find('#endDate').prop( "disabled", true );
$(this).prop({"disabled":true, "hidden":true});
$(this).closest('.sale-item').find('#update').prop({"disabled":false, "hidden":false});
});
</script>
<?php endif; ?>
</div> <!-- container -->
<?php
include_once './footer.php';
?>