-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackend.js
250 lines (236 loc) · 7.67 KB
/
backend.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
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
//* Load Content
function loadContent(pathUrl) {
window.scrollTo(0, 0);
$.ajax({
url: pathUrl,
success: function (data) {
$('#content').html(data);
AOS.init();
}
});
}
//* Popup Notification
function notify(type, icon, position, msg) {
Lobibox.notify(type, {
pauseDelayOnHover: true,
size: 'mini',
rounded: true,
icon: icon,
continueDelayOnInactiveTab: false,
position: position,
msg: msg
});
}
//* Load Header
function reLoadHeader() {
$.ajax({
url: './frontend/header.php',
success: function (data) {
$('#header').html(data);
}
});
}
// *Sign in
var callLogin = function () {
let user = $('#loginForm input[name=user]').val();
let pass = $('#loginForm input[name=pass]').val();
if ($('.is-invalid').length > 0) return;
if (user == '' || pass == '') {
notify('warning', 'fa-duotone fa-pen-field', 'center', 'Bạn chưa nhập tài khoản hoặc mật khẩu');
return;
}
$.ajax({
type: 'POST',
url: './backend/login.php',
async: false,
data: {
user: user,
pass: pass,
submit: true,
},
success: function (data) {
if (data == 'true') {
window.history.pushState('home', 'HOME', './');
setTimeout(function () {
loadContent('./home.php');
notify('success', 'fa-duotone fa-user-check', 'bottom', 'Đăng Nhập Thành Công');
}, 300);
reLoadHeader();
} else {
notify('error', 'fa-duotone fa-user-xmark', 'center', 'Sai tài khoản & mật khẩu');
}
}
});
};
// *Register
var callRegister = function () {
let fullname = $('#registerForm input[name=fullname]').val();
let username = $('#registerForm input[name=username]').val();
let email = $('#registerForm input[name=email]').val();
let password = $('#registerForm input[name=password]').val();
if ($('.is-invalid').length > 0) return;
if (fullname == '' || username == '' || email == '' || password == '') {
notify('warning', 'fa-duotone fa-pen-field', 'center', 'Bạn chưa nhập đầy đủ thông tin');
return;
}
$.ajax({
type: 'POST',
url: './backend/register.php',
async: false,
data: {
fullname: fullname,
username: username,
email: email,
password: password,
submit: true,
},
success: function (data) {
if (data == 'true') {
window.history.pushState('login', 'LOGIN', './login');
setTimeout(function () {
loadContent('./login.php');
notify('success', 'fa-duotone fa-user-plus', 'bottom', 'Đăng Ký Thành Công');
}, 300);
reLoadHeader();
} else {
notify('error', 'fa-duotone fa-person-walking-arrow-right', 'center', 'Tài khoản đã tồn tại');
}
}
});
}
// *Change Info
var callChangeInfo = function () {
let fullname = $('#changeInfoForm input[name=fullname]').val();
let phone = $('#changeInfoForm input[name=phone]').val();
let email = $('#changeInfoForm input[name=email]').val();
if ($('.is-invalid').length > 0) return;
if (fullname == '' || phone == '' || email == '') {
notify('warning', 'fa-duotone fa-pen-field', 'center', 'Bạn chưa nhập đầy đủ thông tin');
return;
}
$.ajax({
type: 'POST',
url: './backend/change_info.php',
data: {
fullname: fullname,
phone: phone,
email: email,
submit: true,
},
async: false,
success: function (data) {
if (data == 'true') {
notify('success', 'fa-duotone fa-user-check', 'center', 'Thay Đổi Thành Công');
} else {
notify('error', 'fa-duotone fa-user-xmark', 'center', 'Thay Đổi Thất Bại');
}
}
});
}
// *Change Password
var callChangePass = function () {
let current_pwd = $('#changePassForm input[name=current_pwd]').val();
let new_pwd = $('#changePassForm input[name=new_pwd]').val();
let confirm_pwd = $('#changePassForm input[name=confirm_pwd]').val();
if (current_pwd == '' || new_pwd == '' || confirm_pwd == '') {
notify('warning', 'fa-duotone fa-pen-field', 'center', 'Bạn chưa nhập đầy đủ thông tin');
return;
}
if ($('.is-invalid').length > 0) return;
$.ajax({
type: 'POST',
url: './backend/change_password.php',
data: {
current_pwd: current_pwd,
new_pwd: new_pwd,
confirm_pwd: confirm_pwd,
submit: true,
},
async: false,
success: function (data) {
if (data == 'true') {
notify('success', 'fa-duotone fa-user-check', 'center', 'Thay Đổi Thành Công');
} else if (data == 'wrong_comfirm') {
notify('error', 'fa-duotone fa-badge-check', 'center', 'Mật Khẩu Xác Nhận Không Chính Xác');
} else if (data = 'wrong_pwd') {
notify('error', 'fa-duotone fa-user-lock', 'center', 'Mật Khẩu Cũ Không Chính Xác');
} else {
notify('error', 'fa-duotone fa-user-xmark', 'center', 'Thay Đổi Thất Bại');
}
}
});
}
// *Cancel Order
var callCancelOrder = function (id) {
$.ajax({
type: 'POST',
url: './backend/cancel_order.php',
data: { id: id },
async: false,
success: function (data) {
window.history.pushState('account', 'ACCOUNT', './account');
if(data == 'true'){
let msg = 'Hủy Đơn ' + id + ' Thành Công';
notify('success', 'fa-duotone fa-box-archive', 'center', msg);
loadContent('./account.php');
} else {
notify('error', 'fa-duotone fa-user-xmark', 'center', 'Hủy Đơn Hàng Thất Bại');
}
}
});
}
// Sign-in
$(document).on('click', '#loginForm button[name=submit]', function () {
callLogin();
});
$(document).on('keypress', '#loginForm', function (e) {
if (e.which == 13) {
callLogin()
}
});
// Register
$(document).on('click', '#registerForm button[name=submit]', function () {
callRegister();
});
$(document).on('keypress', '#registerForm', function () {
if (e.which == 13) {
callRegister()
}
})
// Logout
$(document).on('click', '#logout', function () {
$.ajax({
url: './backend/logout.php',
success: function () {
reLoadHeader();
}
});
setTimeout(function () {
window.history.pushState('home', 'HOME', './');
loadContent('./home.php');
notify('success', 'fa-duotone fa-right-from-bracket', 'bottom left', 'Đăng Xuất Thành Công');
}, 300);
})
// Change Info Form
$(document).on('click', '#changeInfoForm button[name=submit]', function () {
callChangeInfo();
})
$(document).on('keypress', '#changeInfoForm', function (e) {
if (e.which == 13) {
callChangeInfo()
}
})
// Change Password Form
$(document).on('click', '#changePassForm button[name=submit]', function () {
callChangePass();
})
$(document).on('keypress', '#changePassForm', function (e) {
if (e.which == 13) {
callChangePass()
}
})
// Cancel Order
$(document).on('click', '#cancelOrder', function () {
let id = $(this).attr('id_order');
callCancelOrder(id);
});