Subversion Repositories SmartDukaan

Rev

Rev 37256 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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();
37270 amit 33
        var fresh = $('#hot-deal-fresh').val();
37246 amit 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
        }
37270 amit 49
        if (!fresh) {
50
            alert("Fresh (Yes/No) is required");
37246 amit 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
37270 amit 62
            + "&fresh=" + fresh + "&financeMapping=" + finance
37246 amit 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('');
37270 amit 101
                    $('#hot-deal-condition, #hot-deal-fresh, #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
 
37270 amit 134
    // Model selection for the manual Solr push
135
    function refreshPushButton() {
136
        $('.push-solr').prop('disabled', $('.hot-deal-select:checked').length === 0);
137
    }
138
 
139
    $(document).on('change', "#hot-deal-select-all", function () {
140
        $('.hot-deal-select').prop('checked', $(this).prop('checked'));
141
        refreshPushButton();
142
    });
143
 
144
    $(document).on('change', ".hot-deal-select", refreshPushButton);
145
 
146
    $(document).on('click', ".push-solr", function () {
147
        var catalogIds = $('.hot-deal-select:checked').map(function () {
148
            return $(this).closest('tr').data('catalogId');
149
        }).get();
150
        if (catalogIds.length === 0) {
151
            alert("Select at least one model to push");
152
            return false;
153
        }
154
        if (!confirm("Push " + catalogIds.length + " model(s) to Solr now?")) {
155
            return false;
156
        }
157
        var btn = $(this);
158
        btn.prop('disabled', true).text('Pushing...');
159
        doPostAjaxRequestHandler(context + "/hotDeals/pushSolr?catalogIds=" + catalogIds.join(','),
160
            function (response) {
161
                btn.prop('disabled', false).html('<i class="fa fa-cloud-upload"></i> Push to Solr');
162
                alert(response);
163
                refreshPushButton();
164
            });
165
    });
166
 
37246 amit 167
    $(document).on('change', ".hot-deal-row-start, .hot-deal-row-end, .hot-deal-row-field", function () {
37212 amit 168
        $(this).closest('tr').find('.save-hot-deal').show();
169
    });
170
 
171
    $(document).on('click', ".save-hot-deal", function () {
172
        var row = $(this).closest('tr');
173
        var id = row.data('dealId');
174
        var startDate = row.find('.hot-deal-row-start').val();
175
        var endDate = row.find('.hot-deal-row-end').val();
37246 amit 176
        var warranty = row.find('.hot-deal-row-warranty').val();
37212 amit 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
        }
37246 amit 185
        var warrantyNum = parseInt(warranty, 10);
186
        if (warranty === '' || isNaN(warrantyNum) || warrantyNum < 0 || warrantyNum > 12) {
187
            alert("Warranty months must be between 0 and 12");
188
            return false;
189
        }
37212 amit 190
        doPostAjaxRequestHandler(context + "/hotDeals/update?id=" + id
37246 amit 191
            + "&startDate=" + startDate + "&endDate=" + endDate
192
            + "&warrantyMonths=" + warrantyNum
193
            + "&condition=" + row.find('.hot-deal-row-condition').val()
37270 amit 194
            + "&fresh=" + row.find('.hot-deal-row-fresh').val()
37246 amit 195
            + "&financeMapping=" + row.find('.hot-deal-row-finance').val()
196
            + "&affordability=" + row.find('.hot-deal-row-affordability').val(),
37212 amit 197
            function (response) {
198
                if (response == 'true') {
37256 amit 199
                    loadDealsTable();
200
                    refreshBrandPanel();
37212 amit 201
                } else {
202
                    alert(response);
203
                }
204
            });
205
    });
206
 
207
    $(document).on('click', ".remove-hot-deal", function () {
208
        var id = $(this).closest('tr').data('dealId');
209
        if (confirm("Remove this model from hot deals?") == true) {
210
            doPostAjaxRequestHandler(context + "/hotDeals/remove?id=" + id,
211
                function (response) {
212
                    if (response == 'true') {
37256 amit 213
                        loadDealsTable();
214
                        refreshBrandPanel();
37212 amit 215
                    } else {
216
                        alert(response);
217
                    }
218
                });
219
        }
220
    });
221
 
222
    function loadHotDeals(domId) {
223
        doGetAjaxRequestHandler(context + "/manageHotDeals", function (response) {
224
            $('#' + domId).html(response);
37256 amit 225
            dealsPage = 1;
226
            dealsQuery = '';
37212 amit 227
            setDefaultHotDealDates();
37256 amit 228
            loadDealsTable();
37212 amit 229
        });
230
    }
231
 
37256 amit 232
    function loadDealsTable() {
233
        doGetAjaxRequestHandler(context + "/hotDeals/all?page=" + dealsPage
234
            + "&q=" + encodeURIComponent(dealsQuery), function (response) {
37212 amit 235
            $('#hot-deals-table-container').html(response);
37256 amit 236
            dealsPage = $('#hot-deals-table-panel').data('page') || 1;
237
        });
238
    }
239
 
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");
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);
37212 amit 254
            $('.add-hot-deal').prop('disabled', full);
255
            $('#hot-deal-cap-notice').toggle(full);
256
        });
257
    }
258
 
259
    function loadHotDealModels(brandId) {
260
        doGetAjaxRequestHandler(context + "/hotDeals/models?brandId=" + brandId, function (response) {
261
            hotDealModels = JSON.parse(response);
262
            var input = $('#hot-deal-model-search');
263
            input.typeahead('destroy').typeahead({
264
                source: hotDealModels,
265
                items: 15,
266
                minLength: 2,
267
                displayText: function (item) {
268
                    return item.description;
269
                },
270
                autoSelect: true,
271
                afterSelect: function (item) {
272
                    input.data('catalogId', item.catalogId);
273
                }
274
            });
275
        });
276
    }
277
 
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
});