Subversion Repositories SmartDukaan

Rev

Rev 37212 | Rev 37256 | Go to most recent revision | Blame | Compare with Previous | 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);
    });

    // All five attributes are mandatory with NO defaults - the add form starts empty
    function readAddAttributes() {
        var warranty = $('#hot-deal-warranty').val();
        var condition = $('#hot-deal-condition').val();
        var activated = $('#hot-deal-activated').val();
        var finance = $('#hot-deal-finance').val();
        var affordability = $('#hot-deal-affordability').val();
        if (warranty === '' || warranty === null) {
            alert("Warranty months is required (0 = no warranty, up to 12)");
            return null;
        }
        var warrantyNum = parseInt(warranty, 10);
        if (isNaN(warrantyNum) || warrantyNum < 0 || warrantyNum > 12) {
            alert("Warranty months must be between 0 and 12");
            return null;
        }
        if (!condition) {
            alert("Condition is required");
            return null;
        }
        if (!activated) {
            alert("Activated (Yes/No) is required");
            return null;
        }
        if (!finance) {
            alert("Finance mapping (Yes/No) is required");
            return null;
        }
        if (!affordability) {
            alert("Affordability (Yes/No) is required");
            return null;
        }
        return "&warrantyMonths=" + warrantyNum + "&condition=" + condition
            + "&activated=" + activated + "&financeMapping=" + finance
            + "&affordability=" + affordability;
    }

    $(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;
        }
        var attrParams = readAddAttributes();
        if (attrParams == null) {
            return false;
        }
        doPostAjaxRequestHandler(context + "/hotDeals/add?brandId=" + brandId
            + "&catalogItemId=" + catalogId + "&startDate=" + startDate + "&endDate=" + endDate
            + attrParams,
            function (response) {
                if (response == 'true') {
                    loadHotDealsTable(brandId);
                    loadHotDealModels(brandId);
                    $('#hot-deal-model-search').val('').removeData('catalogId');
                    $('#hot-deal-warranty').val('');
                    $('#hot-deal-condition, #hot-deal-activated, #hot-deal-finance, #hot-deal-affordability').val('');
                } else {
                    alert(response);
                }
            });
    });

    $(document).on('change', ".hot-deal-row-start, .hot-deal-row-end, .hot-deal-row-field", 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();
        var warranty = row.find('.hot-deal-row-warranty').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;
        }
        var warrantyNum = parseInt(warranty, 10);
        if (warranty === '' || isNaN(warrantyNum) || warrantyNum < 0 || warrantyNum > 12) {
            alert("Warranty months must be between 0 and 12");
            return false;
        }
        doPostAjaxRequestHandler(context + "/hotDeals/update?id=" + id
            + "&startDate=" + startDate + "&endDate=" + endDate
            + "&warrantyMonths=" + warrantyNum
            + "&condition=" + row.find('.hot-deal-row-condition').val()
            + "&activated=" + row.find('.hot-deal-row-activated').val()
            + "&financeMapping=" + row.find('.hot-deal-row-finance').val()
            + "&affordability=" + row.find('.hot-deal-row-affordability').val(),
            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'));
        }
    }

});