-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditProduct.html
187 lines (175 loc) · 7.75 KB
/
EditProduct.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
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body style="padding-top:20px">
<div class="col-md-10 col-md-offset-1">
<div class="well">
<img width="50" height="50" id="productImageFrame" src="">
Ảnh: <input type="text" id="Anhtxt" value="" hidden>
<input id="fileUpload" type="file" name="files" /><input id="btnUploadFile" type="button" value="Upload File" class="btn-primary" /><br>
<br>
Tên sản phẩm: <br><input type="text" id="Tentxt" value=""><br>
Loại sản phẩm: <br><select id="loaisanphamSelectList"></select><br>
Hãng: <br><select id="hangSelectList"></select><br>
Chi tiết: <br><textarea id="Detailtxt" value=""></textarea><br>
Số lượng còn lại: <br><input type="text" id="Quantitytxt" value=""><br>
Giá cũ: <br><input type="text" id="OldPricetxt" value=""><br>
Giá mới: <br><input type="text" id="Pricetxt" value=""><br>
<input type="button" id="editButton" value="Sửa">
<input type="button" id="deleteButton" value="Xóa">
</div>
</div>
<script type="text/javascript">
function loadProducType() {
$.ajax({
url: 'http://localhost:25225/api/ProductTypes',
method: 'GET',
contentType: 'application/json',
headers: {
'Authorization': 'Bearer '
+ localStorage.accessToken
},
success: function (response) {
$.each(response, function (index, val) {
$('#loaisanphamSelectList').append(new Option(val.ProductTypeName, val.ProductTypeID));
});
},
error: function (jqXHR) {
alert(jqXHR.responseText);
}
});
$.ajax({
url: 'http://localhost:25225/api/Brands',
method: 'GET',
contentType: 'application/json',
headers: {
'Authorization': 'Bearer '
+ localStorage.accessToken
},
success: function (response) {
$.each(response, function (index, val) {
$('#hangSelectList').append(new Option(val.BrandName, val.BrandID));
});
},
error: function (jqXHR) {
alert(jqXHR.responseText);
}
});
}
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,
function (m, key, value) {
vars[key] = value;
});
return vars;
}
var pid = getUrlVars()["pid"];
$(document).ready(function () {
loadProducType();
$.ajax({
url: 'http://localhost:25225/api/Products/LoadProductByIDforEdit?id=' + pid,
method: 'GET',
contentType: 'application/json',
headers: {
'Authorization': 'Bearer '
+ localStorage.accessToken
},
success: function (response) {
$('#loaisanphamSelectList').val(response.ProductTypeID),
$('#hangSelectList').val(response.BrandID),
$('#Tentxt').val(response.ProductName),
$('#Detailtxt').val(response.Detail),
$('#Anhtxt').val(response.ProductImage),
$('#productImageFrame').attr("src", 'http://localhost:25225/' + response.ProductImage);
$('#OldPricetxt').val(response.OldPrice),
$('#Pricetxt').val(response.Price),
$('#Quantitytxt').val(response.Stock)
},
// Display errors if any in the Bootstrap alert <div>
error: function (jqXHR) {
alert(jqXHR.responseText);
}
});
$('#editButton').click(function () {
var dataJSON = {
ProductTypeID: $('#loaisanphamSelectList').val(),
BrandID: $('#hangSelectList').val(),
ProductName: $('#Tentxt').val(),
Detail: $('#Detailtxt').val(),
ProductImage: $('#Anhtxt').val(),
OldPrice: $('#OldPricetxt').val(),
Price: $('#Pricetxt').val(),
Stock: $('#Quantitytxt').val()
};
$.ajax({
url: 'http://localhost:25225/api/Product/'+pid,
method: 'PUT',
contentType: 'application/json',
data: JSON.stringify(dataJSON),
headers: {
'Authorization': 'Bearer '
+ localStorage.accessToken
},
success: function (response) {
alert(response);
window.location.href = "ProductManagerment.html";
},
// Display errors if any in the Bootstrap alert <div>
error: function (jqXHR) {
$('#divErrorText').text(jqXHR.responseText);
$('#divError').show('fade');
}
});
});
$('#deleteButton').click(function () {
$.ajax({
url: 'http://localhost:25225/api/Product/' + pid,
method: 'DELETE',
contentType: 'application/json',
headers: {
'Authorization': 'Bearer '
+ localStorage.accessToken
},
success: function (response) {
alert(response);
window.location.href = "ProductManagerment.html";
},
// Display errors if any in the Bootstrap alert <div>
error: function (jqXHR) {
$('#divErrorText').text(jqXHR.responseText);
$('#divError').show('fade');
}
});
});
$('#btnUploadFile').on('click', function () {
var data = new FormData()
var files = $("#fileUpload").get(0).files;
// Add the uploaded image content to the form data collection
if (files.length > 0) {
data.append("UploadedImage", files[0]);
}
// Make Ajax request with the contentType = false, and procesDate = false
var ajaxRequest = $.ajax({
type: "POST",
url: 'http://localhost:25225/api/Upload/user/PostUserImage',
contentType: false,
processData: false,
data: data
});
ajaxRequest.done(function (xhr, textStatus) {
$('#Anhtxt').val(xhr.Message);
alert("Đã up ảnh thành công");
$('#productImageFrame').attr("src", 'http://localhost:25225/'+xhr.Message);
});
});
});
</script>
</body>
</html>