Subversion Repositories SmartDukaan

Rev

Rev 37212 | 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 = [];
4
 
5
    $(document).on('click', ".manage-hot-deals", function () {
6
        loadHotDeals("main-content");
7
    });
8
 
9
    $(document).on('change', "#hot-deal-brand", function () {
10
        var brandId = $(this).val();
11
        if (!brandId) {
12
            $('#hot-deal-add-panel').hide();
13
            $('#hot-deals-table-container').empty();
14
            $('#hot-deal-slots').text('');
15
            return;
16
        }
17
        loadHotDealsTable(brandId);
18
        loadHotDealModels(brandId);
19
    });
20
 
37246 amit 21
    // All five attributes are mandatory with NO defaults - the add form starts empty
22
    function readAddAttributes() {
23
        var warranty = $('#hot-deal-warranty').val();
24
        var condition = $('#hot-deal-condition').val();
25
        var activated = $('#hot-deal-activated').val();
26
        var finance = $('#hot-deal-finance').val();
27
        var affordability = $('#hot-deal-affordability').val();
28
        if (warranty === '' || warranty === null) {
29
            alert("Warranty months is required (0 = no warranty, up to 12)");
30
            return null;
31
        }
32
        var warrantyNum = parseInt(warranty, 10);
33
        if (isNaN(warrantyNum) || warrantyNum < 0 || warrantyNum > 12) {
34
            alert("Warranty months must be between 0 and 12");
35
            return null;
36
        }
37
        if (!condition) {
38
            alert("Condition is required");
39
            return null;
40
        }
41
        if (!activated) {
42
            alert("Activated (Yes/No) is required");
43
            return null;
44
        }
45
        if (!finance) {
46
            alert("Finance mapping (Yes/No) is required");
47
            return null;
48
        }
49
        if (!affordability) {
50
            alert("Affordability (Yes/No) is required");
51
            return null;
52
        }
53
        return "&warrantyMonths=" + warrantyNum + "&condition=" + condition
54
            + "&activated=" + activated + "&financeMapping=" + finance
55
            + "&affordability=" + affordability;
56
    }
57
 
37212 amit 58
    $(document).on('click', ".add-hot-deal", function () {
59
        var brandId = $('#hot-deal-brand').val();
60
        var catalogId = $('#hot-deal-model-search').data('catalogId');
61
        var startDate = $('#hot-deal-start-date').val();
62
        var endDate = $('#hot-deal-end-date').val();
63
        if (!catalogId) {
64
            alert("Please select a model from the list");
65
            return false;
66
        }
67
        if (!startDate || !endDate) {
68
            alert("Start and end dates are required");
69
            return false;
70
        }
71
        if (endDate < startDate) {
72
            alert("End date cannot be before start date");
73
            return false;
74
        }
37246 amit 75
        var attrParams = readAddAttributes();
76
        if (attrParams == null) {
77
            return false;
78
        }
37212 amit 79
        doPostAjaxRequestHandler(context + "/hotDeals/add?brandId=" + brandId
37246 amit 80
            + "&catalogItemId=" + catalogId + "&startDate=" + startDate + "&endDate=" + endDate
81
            + attrParams,
37212 amit 82
            function (response) {
83
                if (response == 'true') {
84
                    loadHotDealsTable(brandId);
85
                    loadHotDealModels(brandId);
86
                    $('#hot-deal-model-search').val('').removeData('catalogId');
37246 amit 87
                    $('#hot-deal-warranty').val('');
88
                    $('#hot-deal-condition, #hot-deal-activated, #hot-deal-finance, #hot-deal-affordability').val('');
37212 amit 89
                } else {
90
                    alert(response);
91
                }
92
            });
93
    });
94
 
37246 amit 95
    $(document).on('change', ".hot-deal-row-start, .hot-deal-row-end, .hot-deal-row-field", function () {
37212 amit 96
        $(this).closest('tr').find('.save-hot-deal').show();
97
    });
98
 
99
    $(document).on('click', ".save-hot-deal", function () {
100
        var row = $(this).closest('tr');
101
        var id = row.data('dealId');
102
        var startDate = row.find('.hot-deal-row-start').val();
103
        var endDate = row.find('.hot-deal-row-end').val();
37246 amit 104
        var warranty = row.find('.hot-deal-row-warranty').val();
37212 amit 105
        if (!startDate || !endDate) {
106
            alert("Start and end dates are required");
107
            return false;
108
        }
109
        if (endDate < startDate) {
110
            alert("End date cannot be before start date");
111
            return false;
112
        }
37246 amit 113
        var warrantyNum = parseInt(warranty, 10);
114
        if (warranty === '' || isNaN(warrantyNum) || warrantyNum < 0 || warrantyNum > 12) {
115
            alert("Warranty months must be between 0 and 12");
116
            return false;
117
        }
37212 amit 118
        doPostAjaxRequestHandler(context + "/hotDeals/update?id=" + id
37246 amit 119
            + "&startDate=" + startDate + "&endDate=" + endDate
120
            + "&warrantyMonths=" + warrantyNum
121
            + "&condition=" + row.find('.hot-deal-row-condition').val()
122
            + "&activated=" + row.find('.hot-deal-row-activated').val()
123
            + "&financeMapping=" + row.find('.hot-deal-row-finance').val()
124
            + "&affordability=" + row.find('.hot-deal-row-affordability').val(),
37212 amit 125
            function (response) {
126
                if (response == 'true') {
127
                    loadHotDealsTable($('#hot-deal-brand').val());
128
                    loadHotDealModels($('#hot-deal-brand').val());
129
                } else {
130
                    alert(response);
131
                }
132
            });
133
    });
134
 
135
    $(document).on('click', ".remove-hot-deal", function () {
136
        var id = $(this).closest('tr').data('dealId');
137
        if (confirm("Remove this model from hot deals?") == true) {
138
            doPostAjaxRequestHandler(context + "/hotDeals/remove?id=" + id,
139
                function (response) {
140
                    if (response == 'true') {
141
                        loadHotDealsTable($('#hot-deal-brand').val());
142
                        loadHotDealModels($('#hot-deal-brand').val());
143
                    } else {
144
                        alert(response);
145
                    }
146
                });
147
        }
148
    });
149
 
150
    function loadHotDeals(domId) {
151
        doGetAjaxRequestHandler(context + "/manageHotDeals", function (response) {
152
            $('#' + domId).html(response);
153
            setDefaultHotDealDates();
154
        });
155
    }
156
 
157
    function loadHotDealsTable(brandId) {
158
        doGetAjaxRequestHandler(context + "/hotDeals?brandId=" + brandId, function (response) {
159
            $('#hot-deals-table-container').html(response);
160
            var panel = $('#hot-deals-table-panel');
161
            var active = panel.data('activeCount');
162
            var max = panel.data('maxDeals');
163
            $('#hot-deal-slots').text(active + " / " + max + " slots used")
164
                .toggleClass('label-danger', active >= max)
165
                .toggleClass('label-default', active < max);
166
            $('#hot-deal-add-panel').show();
167
            var full = active >= max;
168
            $('.add-hot-deal').prop('disabled', full);
169
            $('#hot-deal-cap-notice').toggle(full);
170
            setDefaultHotDealDates();
171
        });
172
    }
173
 
174
    function loadHotDealModels(brandId) {
175
        doGetAjaxRequestHandler(context + "/hotDeals/models?brandId=" + brandId, function (response) {
176
            hotDealModels = JSON.parse(response);
177
            var input = $('#hot-deal-model-search');
178
            input.val('').removeData('catalogId');
179
            input.typeahead('destroy').typeahead({
180
                source: hotDealModels,
181
                items: 15,
182
                minLength: 2,
183
                displayText: function (item) {
184
                    return item.description;
185
                },
186
                autoSelect: true,
187
                afterSelect: function (item) {
188
                    input.data('catalogId', item.catalogId);
189
                }
190
            });
191
        });
192
    }
193
 
194
    function setDefaultHotDealDates() {
195
        if ($('#hot-deal-start-date').length && !$('#hot-deal-start-date').val()) {
196
            $('#hot-deal-start-date').val(moment().format('YYYY-MM-DD'));
197
            $('#hot-deal-end-date').val(moment().add(15, 'days').format('YYYY-MM-DD'));
198
        }
199
    }
200
 
201
});