Subversion Repositories SmartDukaan

Rev

Details | 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
 
21
    $(document).on('click', ".add-hot-deal", function () {
22
        var brandId = $('#hot-deal-brand').val();
23
        var catalogId = $('#hot-deal-model-search').data('catalogId');
24
        var startDate = $('#hot-deal-start-date').val();
25
        var endDate = $('#hot-deal-end-date').val();
26
        if (!catalogId) {
27
            alert("Please select a model from the list");
28
            return false;
29
        }
30
        if (!startDate || !endDate) {
31
            alert("Start and end dates are required");
32
            return false;
33
        }
34
        if (endDate < startDate) {
35
            alert("End date cannot be before start date");
36
            return false;
37
        }
38
        doPostAjaxRequestHandler(context + "/hotDeals/add?brandId=" + brandId
39
            + "&catalogItemId=" + catalogId + "&startDate=" + startDate + "&endDate=" + endDate,
40
            function (response) {
41
                if (response == 'true') {
42
                    loadHotDealsTable(brandId);
43
                    loadHotDealModels(brandId);
44
                    $('#hot-deal-model-search').val('').removeData('catalogId');
45
                } else {
46
                    alert(response);
47
                }
48
            });
49
    });
50
 
51
    $(document).on('change', ".hot-deal-row-start, .hot-deal-row-end", function () {
52
        $(this).closest('tr').find('.save-hot-deal').show();
53
    });
54
 
55
    $(document).on('click', ".save-hot-deal", function () {
56
        var row = $(this).closest('tr');
57
        var id = row.data('dealId');
58
        var startDate = row.find('.hot-deal-row-start').val();
59
        var endDate = row.find('.hot-deal-row-end').val();
60
        if (!startDate || !endDate) {
61
            alert("Start and end dates are required");
62
            return false;
63
        }
64
        if (endDate < startDate) {
65
            alert("End date cannot be before start date");
66
            return false;
67
        }
68
        doPostAjaxRequestHandler(context + "/hotDeals/update?id=" + id
69
            + "&startDate=" + startDate + "&endDate=" + endDate,
70
            function (response) {
71
                if (response == 'true') {
72
                    loadHotDealsTable($('#hot-deal-brand').val());
73
                    loadHotDealModels($('#hot-deal-brand').val());
74
                } else {
75
                    alert(response);
76
                }
77
            });
78
    });
79
 
80
    $(document).on('click', ".remove-hot-deal", function () {
81
        var id = $(this).closest('tr').data('dealId');
82
        if (confirm("Remove this model from hot deals?") == true) {
83
            doPostAjaxRequestHandler(context + "/hotDeals/remove?id=" + id,
84
                function (response) {
85
                    if (response == 'true') {
86
                        loadHotDealsTable($('#hot-deal-brand').val());
87
                        loadHotDealModels($('#hot-deal-brand').val());
88
                    } else {
89
                        alert(response);
90
                    }
91
                });
92
        }
93
    });
94
 
95
    function loadHotDeals(domId) {
96
        doGetAjaxRequestHandler(context + "/manageHotDeals", function (response) {
97
            $('#' + domId).html(response);
98
            setDefaultHotDealDates();
99
        });
100
    }
101
 
102
    function loadHotDealsTable(brandId) {
103
        doGetAjaxRequestHandler(context + "/hotDeals?brandId=" + brandId, function (response) {
104
            $('#hot-deals-table-container').html(response);
105
            var panel = $('#hot-deals-table-panel');
106
            var active = panel.data('activeCount');
107
            var max = panel.data('maxDeals');
108
            $('#hot-deal-slots').text(active + " / " + max + " slots used")
109
                .toggleClass('label-danger', active >= max)
110
                .toggleClass('label-default', active < max);
111
            $('#hot-deal-add-panel').show();
112
            var full = active >= max;
113
            $('.add-hot-deal').prop('disabled', full);
114
            $('#hot-deal-cap-notice').toggle(full);
115
            setDefaultHotDealDates();
116
        });
117
    }
118
 
119
    function loadHotDealModels(brandId) {
120
        doGetAjaxRequestHandler(context + "/hotDeals/models?brandId=" + brandId, function (response) {
121
            hotDealModels = JSON.parse(response);
122
            var input = $('#hot-deal-model-search');
123
            input.val('').removeData('catalogId');
124
            input.typeahead('destroy').typeahead({
125
                source: hotDealModels,
126
                items: 15,
127
                minLength: 2,
128
                displayText: function (item) {
129
                    return item.description;
130
                },
131
                autoSelect: true,
132
                afterSelect: function (item) {
133
                    input.data('catalogId', item.catalogId);
134
                }
135
            });
136
        });
137
    }
138
 
139
    function setDefaultHotDealDates() {
140
        if ($('#hot-deal-start-date').length && !$('#hot-deal-start-date').val()) {
141
            $('#hot-deal-start-date').val(moment().format('YYYY-MM-DD'));
142
            $('#hot-deal-end-date').val(moment().add(15, 'days').format('YYYY-MM-DD'));
143
        }
144
    }
145
 
146
});