Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

$(function () {

    var hotDealModels = [];

    $(document).on('click', ".manage-hot-deals", function () {
        loadHotDeals("main-content");
    });

    $(document).on('change', "#hot-deal-brand", function () {
        var brandId = $(this).val();
        if (!brandId) {
            $('#hot-deal-add-panel').hide();
            $('#hot-deals-table-container').empty();
            $('#hot-deal-slots').text('');
            return;
        }
        loadHotDealsTable(brandId);
        loadHotDealModels(brandId);
    });

    $(document).on('click', ".add-hot-deal", function () {
        var brandId = $('#hot-deal-brand').val();
        var catalogId = $('#hot-deal-model-search').data('catalogId');
        var startDate = $('#hot-deal-start-date').val();
        var endDate = $('#hot-deal-end-date').val();
        if (!catalogId) {
            alert("Please select a model from the list");
            return false;
        }
        if (!startDate || !endDate) {
            alert("Start and end dates are required");
            return false;
        }
        if (endDate < startDate) {
            alert("End date cannot be before start date");
            return false;
        }
        doPostAjaxRequestHandler(context + "/hotDeals/add?brandId=" + brandId
            + "&catalogItemId=" + catalogId + "&startDate=" + startDate + "&endDate=" + endDate,
            function (response) {
                if (response == 'true') {
                    loadHotDealsTable(brandId);
                    loadHotDealModels(brandId);
                    $('#hot-deal-model-search').val('').removeData('catalogId');
                } else {
                    alert(response);
                }
            });
    });

    $(document).on('change', ".hot-deal-row-start, .hot-deal-row-end", function () {
        $(this).closest('tr').find('.save-hot-deal').show();
    });

    $(document).on('click', ".save-hot-deal", function () {
        var row = $(this).closest('tr');
        var id = row.data('dealId');
        var startDate = row.find('.hot-deal-row-start').val();
        var endDate = row.find('.hot-deal-row-end').val();
        if (!startDate || !endDate) {
            alert("Start and end dates are required");
            return false;
        }
        if (endDate < startDate) {
            alert("End date cannot be before start date");
            return false;
        }
        doPostAjaxRequestHandler(context + "/hotDeals/update?id=" + id
            + "&startDate=" + startDate + "&endDate=" + endDate,
            function (response) {
                if (response == 'true') {
                    loadHotDealsTable($('#hot-deal-brand').val());
                    loadHotDealModels($('#hot-deal-brand').val());
                } else {
                    alert(response);
                }
            });
    });

    $(document).on('click', ".remove-hot-deal", function () {
        var id = $(this).closest('tr').data('dealId');
        if (confirm("Remove this model from hot deals?") == true) {
            doPostAjaxRequestHandler(context + "/hotDeals/remove?id=" + id,
                function (response) {
                    if (response == 'true') {
                        loadHotDealsTable($('#hot-deal-brand').val());
                        loadHotDealModels($('#hot-deal-brand').val());
                    } else {
                        alert(response);
                    }
                });
        }
    });

    function loadHotDeals(domId) {
        doGetAjaxRequestHandler(context + "/manageHotDeals", function (response) {
            $('#' + domId).html(response);
            setDefaultHotDealDates();
        });
    }

    function loadHotDealsTable(brandId) {
        doGetAjaxRequestHandler(context + "/hotDeals?brandId=" + brandId, function (response) {
            $('#hot-deals-table-container').html(response);
            var panel = $('#hot-deals-table-panel');
            var active = panel.data('activeCount');
            var max = panel.data('maxDeals');
            $('#hot-deal-slots').text(active + " / " + max + " slots used")
                .toggleClass('label-danger', active >= max)
                .toggleClass('label-default', active < max);
            $('#hot-deal-add-panel').show();
            var full = active >= max;
            $('.add-hot-deal').prop('disabled', full);
            $('#hot-deal-cap-notice').toggle(full);
            setDefaultHotDealDates();
        });
    }

    function loadHotDealModels(brandId) {
        doGetAjaxRequestHandler(context + "/hotDeals/models?brandId=" + brandId, function (response) {
            hotDealModels = JSON.parse(response);
            var input = $('#hot-deal-model-search');
            input.val('').removeData('catalogId');
            input.typeahead('destroy').typeahead({
                source: hotDealModels,
                items: 15,
                minLength: 2,
                displayText: function (item) {
                    return item.description;
                },
                autoSelect: true,
                afterSelect: function (item) {
                    input.data('catalogId', item.catalogId);
                }
            });
        });
    }

    function setDefaultHotDealDates() {
        if ($('#hot-deal-start-date').length && !$('#hot-deal-start-date').val()) {
            $('#hot-deal-start-date').val(moment().format('YYYY-MM-DD'));
            $('#hot-deal-end-date').val(moment().add(15, 'days').format('YYYY-MM-DD'));
        }
    }

});