| 37212 |
amit |
1 |
$(function () {
|
|
|
2 |
|
|
|
3 |
var hotDealModels = [];
|
| 37256 |
amit |
4 |
var dealsPage = 1;
|
|
|
5 |
var dealsQuery = '';
|
|
|
6 |
var searchDebounce = null;
|
| 37212 |
amit |
7 |
|
|
|
8 |
$(document).on('click', ".manage-hot-deals", function () {
|
|
|
9 |
loadHotDeals("main-content");
|
|
|
10 |
});
|
|
|
11 |
|
| 37256 |
amit |
12 |
// Brand choice drives the add flow: model picker + slot usage for that brand
|
| 37212 |
amit |
13 |
$(document).on('change', "#hot-deal-brand", function () {
|
|
|
14 |
var brandId = $(this).val();
|
| 37256 |
amit |
15 |
var input = $('#hot-deal-model-search');
|
|
|
16 |
input.val('').removeData('catalogId');
|
| 37212 |
amit |
17 |
if (!brandId) {
|
| 37256 |
amit |
18 |
input.prop('disabled', true).attr('placeholder', 'Select a brand first...');
|
| 37212 |
amit |
19 |
$('#hot-deal-slots').text('');
|
| 37256 |
amit |
20 |
$('.add-hot-deal').prop('disabled', false);
|
|
|
21 |
$('#hot-deal-cap-notice').hide();
|
| 37212 |
amit |
22 |
return;
|
|
|
23 |
}
|
| 37256 |
amit |
24 |
input.prop('disabled', false).attr('placeholder', 'Type 2+ letters to search models...');
|
| 37212 |
amit |
25 |
loadHotDealModels(brandId);
|
| 37256 |
amit |
26 |
loadSlots(brandId);
|
| 37212 |
amit |
27 |
});
|
|
|
28 |
|
| 37246 |
amit |
29 |
// All five attributes are mandatory with NO defaults - the add form starts empty
|
|
|
30 |
function readAddAttributes() {
|
|
|
31 |
var warranty = $('#hot-deal-warranty').val();
|
|
|
32 |
var condition = $('#hot-deal-condition').val();
|
|
|
33 |
var activated = $('#hot-deal-activated').val();
|
|
|
34 |
var finance = $('#hot-deal-finance').val();
|
|
|
35 |
var affordability = $('#hot-deal-affordability').val();
|
|
|
36 |
if (warranty === '' || warranty === null) {
|
|
|
37 |
alert("Warranty months is required (0 = no warranty, up to 12)");
|
|
|
38 |
return null;
|
|
|
39 |
}
|
|
|
40 |
var warrantyNum = parseInt(warranty, 10);
|
|
|
41 |
if (isNaN(warrantyNum) || warrantyNum < 0 || warrantyNum > 12) {
|
|
|
42 |
alert("Warranty months must be between 0 and 12");
|
|
|
43 |
return null;
|
|
|
44 |
}
|
|
|
45 |
if (!condition) {
|
|
|
46 |
alert("Condition is required");
|
|
|
47 |
return null;
|
|
|
48 |
}
|
|
|
49 |
if (!activated) {
|
|
|
50 |
alert("Activated (Yes/No) is required");
|
|
|
51 |
return null;
|
|
|
52 |
}
|
|
|
53 |
if (!finance) {
|
|
|
54 |
alert("Finance mapping (Yes/No) is required");
|
|
|
55 |
return null;
|
|
|
56 |
}
|
|
|
57 |
if (!affordability) {
|
|
|
58 |
alert("Affordability (Yes/No) is required");
|
|
|
59 |
return null;
|
|
|
60 |
}
|
|
|
61 |
return "&warrantyMonths=" + warrantyNum + "&condition=" + condition
|
|
|
62 |
+ "&activated=" + activated + "&financeMapping=" + finance
|
|
|
63 |
+ "&affordability=" + affordability;
|
|
|
64 |
}
|
|
|
65 |
|
| 37212 |
amit |
66 |
$(document).on('click', ".add-hot-deal", function () {
|
|
|
67 |
var brandId = $('#hot-deal-brand').val();
|
|
|
68 |
var catalogId = $('#hot-deal-model-search').data('catalogId');
|
|
|
69 |
var startDate = $('#hot-deal-start-date').val();
|
|
|
70 |
var endDate = $('#hot-deal-end-date').val();
|
| 37256 |
amit |
71 |
if (!brandId) {
|
|
|
72 |
alert("Please select a brand");
|
|
|
73 |
return false;
|
|
|
74 |
}
|
| 37212 |
amit |
75 |
if (!catalogId) {
|
|
|
76 |
alert("Please select a model from the list");
|
|
|
77 |
return false;
|
|
|
78 |
}
|
|
|
79 |
if (!startDate || !endDate) {
|
|
|
80 |
alert("Start and end dates are required");
|
|
|
81 |
return false;
|
|
|
82 |
}
|
|
|
83 |
if (endDate < startDate) {
|
|
|
84 |
alert("End date cannot be before start date");
|
|
|
85 |
return false;
|
|
|
86 |
}
|
| 37246 |
amit |
87 |
var attrParams = readAddAttributes();
|
|
|
88 |
if (attrParams == null) {
|
|
|
89 |
return false;
|
|
|
90 |
}
|
| 37212 |
amit |
91 |
doPostAjaxRequestHandler(context + "/hotDeals/add?brandId=" + brandId
|
| 37246 |
amit |
92 |
+ "&catalogItemId=" + catalogId + "&startDate=" + startDate + "&endDate=" + endDate
|
|
|
93 |
+ attrParams,
|
| 37212 |
amit |
94 |
function (response) {
|
|
|
95 |
if (response == 'true') {
|
| 37256 |
amit |
96 |
loadDealsTable();
|
| 37212 |
amit |
97 |
loadHotDealModels(brandId);
|
| 37256 |
amit |
98 |
loadSlots(brandId);
|
| 37212 |
amit |
99 |
$('#hot-deal-model-search').val('').removeData('catalogId');
|
| 37246 |
amit |
100 |
$('#hot-deal-warranty').val('');
|
|
|
101 |
$('#hot-deal-condition, #hot-deal-activated, #hot-deal-finance, #hot-deal-affordability').val('');
|
| 37212 |
amit |
102 |
} else {
|
|
|
103 |
alert(response);
|
|
|
104 |
}
|
|
|
105 |
});
|
|
|
106 |
});
|
|
|
107 |
|
| 37256 |
amit |
108 |
// Server-side search over the all-deals table (model or brand), debounced
|
|
|
109 |
$(document).on('input', "#hot-deal-table-search", function () {
|
|
|
110 |
var q = $(this).val();
|
|
|
111 |
clearTimeout(searchDebounce);
|
|
|
112 |
searchDebounce = setTimeout(function () {
|
|
|
113 |
dealsQuery = q;
|
|
|
114 |
dealsPage = 1;
|
|
|
115 |
loadDealsTable();
|
|
|
116 |
}, 400);
|
|
|
117 |
});
|
|
|
118 |
|
|
|
119 |
$(document).on('click', ".hot-deal-page-prev", function () {
|
|
|
120 |
if (dealsPage > 1) {
|
|
|
121 |
dealsPage--;
|
|
|
122 |
loadDealsTable();
|
|
|
123 |
}
|
|
|
124 |
});
|
|
|
125 |
|
|
|
126 |
$(document).on('click', ".hot-deal-page-next", function () {
|
|
|
127 |
var pages = $('#hot-deals-table-panel').data('pages') || 1;
|
|
|
128 |
if (dealsPage < pages) {
|
|
|
129 |
dealsPage++;
|
|
|
130 |
loadDealsTable();
|
|
|
131 |
}
|
|
|
132 |
});
|
|
|
133 |
|
| 37246 |
amit |
134 |
$(document).on('change', ".hot-deal-row-start, .hot-deal-row-end, .hot-deal-row-field", function () {
|
| 37212 |
amit |
135 |
$(this).closest('tr').find('.save-hot-deal').show();
|
|
|
136 |
});
|
|
|
137 |
|
|
|
138 |
$(document).on('click', ".save-hot-deal", function () {
|
|
|
139 |
var row = $(this).closest('tr');
|
|
|
140 |
var id = row.data('dealId');
|
|
|
141 |
var startDate = row.find('.hot-deal-row-start').val();
|
|
|
142 |
var endDate = row.find('.hot-deal-row-end').val();
|
| 37246 |
amit |
143 |
var warranty = row.find('.hot-deal-row-warranty').val();
|
| 37212 |
amit |
144 |
if (!startDate || !endDate) {
|
|
|
145 |
alert("Start and end dates are required");
|
|
|
146 |
return false;
|
|
|
147 |
}
|
|
|
148 |
if (endDate < startDate) {
|
|
|
149 |
alert("End date cannot be before start date");
|
|
|
150 |
return false;
|
|
|
151 |
}
|
| 37246 |
amit |
152 |
var warrantyNum = parseInt(warranty, 10);
|
|
|
153 |
if (warranty === '' || isNaN(warrantyNum) || warrantyNum < 0 || warrantyNum > 12) {
|
|
|
154 |
alert("Warranty months must be between 0 and 12");
|
|
|
155 |
return false;
|
|
|
156 |
}
|
| 37212 |
amit |
157 |
doPostAjaxRequestHandler(context + "/hotDeals/update?id=" + id
|
| 37246 |
amit |
158 |
+ "&startDate=" + startDate + "&endDate=" + endDate
|
|
|
159 |
+ "&warrantyMonths=" + warrantyNum
|
|
|
160 |
+ "&condition=" + row.find('.hot-deal-row-condition').val()
|
|
|
161 |
+ "&activated=" + row.find('.hot-deal-row-activated').val()
|
|
|
162 |
+ "&financeMapping=" + row.find('.hot-deal-row-finance').val()
|
|
|
163 |
+ "&affordability=" + row.find('.hot-deal-row-affordability').val(),
|
| 37212 |
amit |
164 |
function (response) {
|
|
|
165 |
if (response == 'true') {
|
| 37256 |
amit |
166 |
loadDealsTable();
|
|
|
167 |
refreshBrandPanel();
|
| 37212 |
amit |
168 |
} else {
|
|
|
169 |
alert(response);
|
|
|
170 |
}
|
|
|
171 |
});
|
|
|
172 |
});
|
|
|
173 |
|
|
|
174 |
$(document).on('click', ".remove-hot-deal", function () {
|
|
|
175 |
var id = $(this).closest('tr').data('dealId');
|
|
|
176 |
if (confirm("Remove this model from hot deals?") == true) {
|
|
|
177 |
doPostAjaxRequestHandler(context + "/hotDeals/remove?id=" + id,
|
|
|
178 |
function (response) {
|
|
|
179 |
if (response == 'true') {
|
| 37256 |
amit |
180 |
loadDealsTable();
|
|
|
181 |
refreshBrandPanel();
|
| 37212 |
amit |
182 |
} else {
|
|
|
183 |
alert(response);
|
|
|
184 |
}
|
|
|
185 |
});
|
|
|
186 |
}
|
|
|
187 |
});
|
|
|
188 |
|
|
|
189 |
function loadHotDeals(domId) {
|
|
|
190 |
doGetAjaxRequestHandler(context + "/manageHotDeals", function (response) {
|
|
|
191 |
$('#' + domId).html(response);
|
| 37256 |
amit |
192 |
dealsPage = 1;
|
|
|
193 |
dealsQuery = '';
|
| 37212 |
amit |
194 |
setDefaultHotDealDates();
|
| 37256 |
amit |
195 |
loadDealsTable();
|
| 37212 |
amit |
196 |
});
|
|
|
197 |
}
|
|
|
198 |
|
| 37256 |
amit |
199 |
function loadDealsTable() {
|
|
|
200 |
doGetAjaxRequestHandler(context + "/hotDeals/all?page=" + dealsPage
|
|
|
201 |
+ "&q=" + encodeURIComponent(dealsQuery), function (response) {
|
| 37212 |
amit |
202 |
$('#hot-deals-table-container').html(response);
|
| 37256 |
amit |
203 |
dealsPage = $('#hot-deals-table-panel').data('page') || 1;
|
|
|
204 |
});
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
function refreshBrandPanel() {
|
|
|
208 |
var brandId = $('#hot-deal-brand').val();
|
|
|
209 |
if (brandId) {
|
|
|
210 |
loadHotDealModels(brandId);
|
|
|
211 |
loadSlots(brandId);
|
|
|
212 |
}
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
function loadSlots(brandId) {
|
|
|
216 |
doGetAjaxRequestHandler(context + "/hotDeals/slots?brandId=" + brandId, function (response) {
|
|
|
217 |
$('#hot-deal-slots').text(response + " slots used");
|
|
|
218 |
var parts = response.split('/');
|
|
|
219 |
var full = parts.length == 2 && parseInt(parts[0], 10) >= parseInt(parts[1], 10);
|
|
|
220 |
$('#hot-deal-slots').toggleClass('label-danger', full).toggleClass('label-default', !full);
|
| 37212 |
amit |
221 |
$('.add-hot-deal').prop('disabled', full);
|
|
|
222 |
$('#hot-deal-cap-notice').toggle(full);
|
|
|
223 |
});
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
function loadHotDealModels(brandId) {
|
|
|
227 |
doGetAjaxRequestHandler(context + "/hotDeals/models?brandId=" + brandId, function (response) {
|
|
|
228 |
hotDealModels = JSON.parse(response);
|
|
|
229 |
var input = $('#hot-deal-model-search');
|
|
|
230 |
input.typeahead('destroy').typeahead({
|
|
|
231 |
source: hotDealModels,
|
|
|
232 |
items: 15,
|
|
|
233 |
minLength: 2,
|
|
|
234 |
displayText: function (item) {
|
|
|
235 |
return item.description;
|
|
|
236 |
},
|
|
|
237 |
autoSelect: true,
|
|
|
238 |
afterSelect: function (item) {
|
|
|
239 |
input.data('catalogId', item.catalogId);
|
|
|
240 |
}
|
|
|
241 |
});
|
|
|
242 |
});
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
function setDefaultHotDealDates() {
|
|
|
246 |
if ($('#hot-deal-start-date').length && !$('#hot-deal-start-date').val()) {
|
|
|
247 |
$('#hot-deal-start-date').val(moment().format('YYYY-MM-DD'));
|
|
|
248 |
$('#hot-deal-end-date').val(moment().add(15, 'days').format('YYYY-MM-DD'));
|
|
|
249 |
}
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
});
|