Subversion Repositories SmartDukaan

Rev

Rev 37270 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 37270 Rev 37643
Line 7... Line 7...
7
 
7
 
8
    $(document).on('click', ".manage-hot-deals", function () {
8
    $(document).on('click', ".manage-hot-deals", function () {
9
        loadHotDeals("main-content");
9
        loadHotDeals("main-content");
10
    });
10
    });
11
 
11
 
12
    // Brand choice drives the add flow: model picker + slot usage for that brand
-
 
13
    $(document).on('change', "#hot-deal-brand", function () {
-
 
14
        var brandId = $(this).val();
-
 
15
        var input = $('#hot-deal-model-search');
-
 
16
        input.val('').removeData('catalogId');
-
 
17
        if (!brandId) {
-
 
18
            input.prop('disabled', true).attr('placeholder', 'Select a brand first...');
-
 
19
            $('#hot-deal-slots').text('');
-
 
20
            $('.add-hot-deal').prop('disabled', false);
-
 
21
            $('#hot-deal-cap-notice').hide();
-
 
22
            return;
-
 
23
        }
-
 
24
        input.prop('disabled', false).attr('placeholder', 'Type 2+ letters to search models...');
-
 
25
        loadHotDealModels(brandId);
-
 
26
        loadSlots(brandId);
-
 
27
    });
-
 
28
 
-
 
29
    // All five attributes are mandatory with NO defaults - the add form starts empty
12
    // All five attributes are mandatory with NO defaults - the add form starts empty
30
    function readAddAttributes() {
13
    function readAddAttributes() {
31
        var warranty = $('#hot-deal-warranty').val();
14
        var warranty = $('#hot-deal-warranty').val();
32
        var condition = $('#hot-deal-condition').val();
15
        var condition = $('#hot-deal-condition').val();
33
        var fresh = $('#hot-deal-fresh').val();
16
        var fresh = $('#hot-deal-fresh').val();
Line 62... Line 45...
62
            + "&fresh=" + fresh + "&financeMapping=" + finance
45
            + "&fresh=" + fresh + "&financeMapping=" + finance
63
            + "&affordability=" + affordability;
46
            + "&affordability=" + affordability;
64
    }
47
    }
65
 
48
 
66
    $(document).on('click', ".add-hot-deal", function () {
49
    $(document).on('click', ".add-hot-deal", function () {
67
        var brandId = $('#hot-deal-brand').val();
-
 
68
        var catalogId = $('#hot-deal-model-search').data('catalogId');
50
        var catalogId = $('#hot-deal-model-search').data('catalogId');
69
        var startDate = $('#hot-deal-start-date').val();
-
 
70
        var endDate = $('#hot-deal-end-date').val();
-
 
71
        if (!brandId) {
-
 
72
            alert("Please select a brand");
-
 
73
            return false;
-
 
74
        }
-
 
75
        if (!catalogId) {
51
        if (!catalogId) {
76
            alert("Please select a model from the list");
52
            alert("Please select a model from the list");
77
            return false;
53
            return false;
78
        }
54
        }
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
        }
-
 
87
        var attrParams = readAddAttributes();
55
        var attrParams = readAddAttributes();
88
        if (attrParams == null) {
56
        if (attrParams == null) {
89
            return false;
57
            return false;
90
        }
58
        }
-
 
59
        // Upsert: the server creates the row if this model has no attributes yet.
91
        doPostAjaxRequestHandler(context + "/hotDeals/add?brandId=" + brandId
60
        var oemCatalogId = $('#hot-deal-oem-search').data('catalogId');
92
            + "&catalogItemId=" + catalogId + "&startDate=" + startDate + "&endDate=" + endDate
61
        doPostAjaxRequestHandler(context + "/hotDeals/saveAttributes?catalogItemId=" + catalogId
93
            + attrParams,
62
            + attrParams
-
 
63
            + (oemCatalogId ? "&oemCatalogId=" + oemCatalogId : ""),
94
            function (response) {
64
            function (response) {
95
                if (response == 'true') {
65
                if (response == 'true') {
96
                    loadDealsTable();
66
                    loadDealsTable();
97
                    loadHotDealModels(brandId);
67
                    loadHotDealModels();
98
                    loadSlots(brandId);
-
 
99
                    $('#hot-deal-model-search').val('').removeData('catalogId');
68
                    $('#hot-deal-model-search').val('').removeData('catalogId');
-
 
69
                    $('#hot-deal-oem-search').val('').removeData('catalogId')
-
 
70
                        .prop('disabled', true).attr('placeholder', 'Pick a Hot Deal model first...');
100
                    $('#hot-deal-warranty').val('');
71
                    $('#hot-deal-warranty').val('');
101
                    $('#hot-deal-condition, #hot-deal-fresh, #hot-deal-finance, #hot-deal-affordability').val('');
72
                    $('#hot-deal-condition, #hot-deal-fresh, #hot-deal-finance, #hot-deal-affordability').val('');
102
                } else {
73
                } else {
103
                    alert(response);
74
                    alert(response);
104
                }
75
                }
Line 162... Line 133...
162
                alert(response);
133
                alert(response);
163
                refreshPushButton();
134
                refreshPushButton();
164
            });
135
            });
165
    });
136
    });
166
 
137
 
167
    $(document).on('change', ".hot-deal-row-start, .hot-deal-row-end, .hot-deal-row-field", function () {
138
    $(document).on('change', ".hot-deal-row-field", function () {
168
        $(this).closest('tr').find('.save-hot-deal').show();
139
        $(this).closest('tr').find('.save-hot-deal').show();
169
    });
140
    });
170
 
141
 
171
    $(document).on('click', ".save-hot-deal", function () {
142
    $(document).on('click', ".save-hot-deal", function () {
172
        var row = $(this).closest('tr');
143
        var row = $(this).closest('tr');
173
        var id = row.data('dealId');
144
        var catalogId = row.data('catalogId');
174
        var startDate = row.find('.hot-deal-row-start').val();
-
 
175
        var endDate = row.find('.hot-deal-row-end').val();
-
 
176
        var warranty = row.find('.hot-deal-row-warranty').val();
145
        var warranty = row.find('.hot-deal-row-warranty').val();
177
        if (!startDate || !endDate) {
-
 
178
            alert("Start and end dates are required");
-
 
179
            return false;
-
 
180
        }
-
 
181
        if (endDate < startDate) {
-
 
182
            alert("End date cannot be before start date");
-
 
183
            return false;
-
 
184
        }
-
 
185
        var warrantyNum = parseInt(warranty, 10);
146
        var warrantyNum = parseInt(warranty, 10);
186
        if (warranty === '' || isNaN(warrantyNum) || warrantyNum < 0 || warrantyNum > 12) {
147
        if (warranty === '' || isNaN(warrantyNum) || warrantyNum < 0 || warrantyNum > 12) {
187
            alert("Warranty months must be between 0 and 12");
148
            alert("Warranty months must be between 0 and 12");
188
            return false;
149
            return false;
189
        }
150
        }
190
        doPostAjaxRequestHandler(context + "/hotDeals/update?id=" + id
151
        doPostAjaxRequestHandler(context + "/hotDeals/saveAttributes?catalogItemId=" + catalogId
191
            + "&startDate=" + startDate + "&endDate=" + endDate
-
 
192
            + "&warrantyMonths=" + warrantyNum
152
            + "&warrantyMonths=" + warrantyNum
193
            + "&condition=" + row.find('.hot-deal-row-condition').val()
153
            + "&condition=" + row.find('.hot-deal-row-condition').val()
194
            + "&fresh=" + row.find('.hot-deal-row-fresh').val()
154
            + "&fresh=" + row.find('.hot-deal-row-fresh').val()
195
            + "&financeMapping=" + row.find('.hot-deal-row-finance').val()
155
            + "&financeMapping=" + row.find('.hot-deal-row-finance').val()
196
            + "&affordability=" + row.find('.hot-deal-row-affordability').val(),
156
            + "&affordability=" + row.find('.hot-deal-row-affordability').val(),
197
            function (response) {
157
            function (response) {
198
                if (response == 'true') {
158
                if (response == 'true') {
199
                    loadDealsTable();
159
                    loadDealsTable();
200
                    refreshBrandPanel();
-
 
201
                } else {
160
                } else {
202
                    alert(response);
161
                    alert(response);
203
                }
162
                }
204
            });
163
            });
205
    });
164
    });
Line 209... Line 168...
209
        if (confirm("Remove this model from hot deals?") == true) {
168
        if (confirm("Remove this model from hot deals?") == true) {
210
            doPostAjaxRequestHandler(context + "/hotDeals/remove?id=" + id,
169
            doPostAjaxRequestHandler(context + "/hotDeals/remove?id=" + id,
211
                function (response) {
170
                function (response) {
212
                    if (response == 'true') {
171
                    if (response == 'true') {
213
                        loadDealsTable();
172
                        loadDealsTable();
214
                        refreshBrandPanel();
173
                        loadHotDealModels();
215
                    } else {
174
                    } else {
216
                        alert(response);
175
                        alert(response);
217
                    }
176
                    }
218
                });
177
                });
219
        }
178
        }
Line 222... Line 181...
222
    function loadHotDeals(domId) {
181
    function loadHotDeals(domId) {
223
        doGetAjaxRequestHandler(context + "/manageHotDeals", function (response) {
182
        doGetAjaxRequestHandler(context + "/manageHotDeals", function (response) {
224
            $('#' + domId).html(response);
183
            $('#' + domId).html(response);
225
            dealsPage = 1;
184
            dealsPage = 1;
226
            dealsQuery = '';
185
            dealsQuery = '';
227
            setDefaultHotDealDates();
-
 
228
            loadDealsTable();
186
            loadDealsTable();
-
 
187
            loadHotDealModels();
229
        });
188
        });
230
    }
189
    }
231
 
190
 
-
 
191
    /**
-
 
192
     * OEM mapping autosuggest, scoped to the brand the selected Hot Deal model came from.
-
 
193
     * Optional: a model with no channel twin is simply left unmapped.
-
 
194
     */
-
 
195
    function loadOemModels(oemBrand) {
-
 
196
        var input = $('#hot-deal-oem-search');
-
 
197
        input.val('').removeData('catalogId');
-
 
198
        if (!oemBrand) {
-
 
199
            input.prop('disabled', true).attr('placeholder', 'No OEM brand recorded');
-
 
200
            return;
-
 
201
        }
-
 
202
        doGetAjaxRequestHandler(context + "/hotDeals/oemModels?brand=" + encodeURIComponent(oemBrand),
-
 
203
            function (response) {
-
 
204
                var oemModels = JSON.parse(response);
-
 
205
                input.prop('disabled', false)
-
 
206
                    .attr('placeholder', 'Optional - search ' + oemBrand + ' models...');
-
 
207
                input.typeahead('destroy').typeahead({
-
 
208
                    source: oemModels,
-
 
209
                    items: 15,
-
 
210
                    minLength: 2,
-
 
211
                    displayText: function (item) {
-
 
212
                        return item.description;
-
 
213
                    },
-
 
214
                    autoSelect: true,
-
 
215
                    afterSelect: function (item) {
-
 
216
                        input.data('catalogId', item.catalogId);
-
 
217
                    }
-
 
218
                });
-
 
219
            });
-
 
220
    }
-
 
221
 
232
    function loadDealsTable() {
222
    function loadDealsTable() {
233
        doGetAjaxRequestHandler(context + "/hotDeals/all?page=" + dealsPage
223
        doGetAjaxRequestHandler(context + "/hotDeals/all?page=" + dealsPage
234
            + "&q=" + encodeURIComponent(dealsQuery), function (response) {
224
            + "&q=" + encodeURIComponent(dealsQuery), function (response) {
235
            $('#hot-deals-table-container').html(response);
225
            $('#hot-deals-table-container').html(response);
236
            dealsPage = $('#hot-deals-table-panel').data('page') || 1;
226
            dealsPage = $('#hot-deals-table-panel').data('page') || 1;
237
        });
227
        });
238
    }
228
    }
239
 
229
 
240
    function refreshBrandPanel() {
-
 
241
        var brandId = $('#hot-deal-brand').val();
-
 
242
        if (brandId) {
-
 
243
            loadHotDealModels(brandId);
-
 
244
            loadSlots(brandId);
-
 
245
        }
-
 
246
    }
-
 
247
 
-
 
248
    function loadSlots(brandId) {
-
 
249
        doGetAjaxRequestHandler(context + "/hotDeals/slots?brandId=" + brandId, function (response) {
-
 
250
            $('#hot-deal-slots').text(response + " slots used");
230
    // Every Hot Deal-brand model. One brand, so nothing to scope by.
251
            var parts = response.split('/');
-
 
252
            var full = parts.length == 2 && parseInt(parts[0], 10) >= parseInt(parts[1], 10);
-
 
253
            $('#hot-deal-slots').toggleClass('label-danger', full).toggleClass('label-default', !full);
-
 
254
            $('.add-hot-deal').prop('disabled', full);
-
 
255
            $('#hot-deal-cap-notice').toggle(full);
-
 
256
        });
-
 
257
    }
-
 
258
 
-
 
259
    function loadHotDealModels(brandId) {
231
    function loadHotDealModels() {
260
        doGetAjaxRequestHandler(context + "/hotDeals/models?brandId=" + brandId, function (response) {
232
        doGetAjaxRequestHandler(context + "/hotDeals/models", function (response) {
261
            hotDealModels = JSON.parse(response);
233
            hotDealModels = JSON.parse(response);
262
            var input = $('#hot-deal-model-search');
234
            var input = $('#hot-deal-model-search');
263
            input.typeahead('destroy').typeahead({
235
            input.typeahead('destroy').typeahead({
264
                source: hotDealModels,
236
                source: hotDealModels,
265
                items: 15,
237
                items: 15,
Line 268... Line 240...
268
                    return item.description;
240
                    return item.description;
269
                },
241
                },
270
                autoSelect: true,
242
                autoSelect: true,
271
                afterSelect: function (item) {
243
                afterSelect: function (item) {
272
                    input.data('catalogId', item.catalogId);
244
                    input.data('catalogId', item.catalogId);
-
 
245
                    loadOemModels(item.oemBrand);
273
                }
246
                }
274
            });
247
            });
275
        });
248
        });
276
    }
249
    }
277
 
250
 
278
    function setDefaultHotDealDates() {
-
 
279
        if ($('#hot-deal-start-date').length && !$('#hot-deal-start-date').val()) {
-
 
280
            $('#hot-deal-start-date').val(moment().format('YYYY-MM-DD'));
-
 
281
            $('#hot-deal-end-date').val(moment().add(15, 'days').format('YYYY-MM-DD'));
-
 
282
        }
-
 
283
    }
-
 
284
 
-
 
285
});
251
});