Subversion Repositories SmartDukaan

Rev

Rev 37270 | Blame | Compare with Previous | Last modification | View Log | RSS feed

$(function () {

    var hotDealModels = [];
    var dealsPage = 1;
    var dealsQuery = '';
    var searchDebounce = null;

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

    // 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 fresh = $('#hot-deal-fresh').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 (!fresh) {
            alert("Fresh (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
            + "&fresh=" + fresh + "&financeMapping=" + finance
            + "&affordability=" + affordability;
    }

    $(document).on('click', ".add-hot-deal", function () {
        var catalogId = $('#hot-deal-model-search').data('catalogId');
        if (!catalogId) {
            alert("Please select a model from the list");
            return false;
        }
        var attrParams = readAddAttributes();
        if (attrParams == null) {
            return false;
        }
        // Upsert: the server creates the row if this model has no attributes yet.
        var oemCatalogId = $('#hot-deal-oem-search').data('catalogId');
        doPostAjaxRequestHandler(context + "/hotDeals/saveAttributes?catalogItemId=" + catalogId
            + attrParams
            + (oemCatalogId ? "&oemCatalogId=" + oemCatalogId : ""),
            function (response) {
                if (response == 'true') {
                    loadDealsTable();
                    loadHotDealModels();
                    $('#hot-deal-model-search').val('').removeData('catalogId');
                    $('#hot-deal-oem-search').val('').removeData('catalogId')
                        .prop('disabled', true).attr('placeholder', 'Pick a Hot Deal model first...');
                    $('#hot-deal-warranty').val('');
                    $('#hot-deal-condition, #hot-deal-fresh, #hot-deal-finance, #hot-deal-affordability').val('');
                } else {
                    alert(response);
                }
            });
    });

    // Server-side search over the all-deals table (model or brand), debounced
    $(document).on('input', "#hot-deal-table-search", function () {
        var q = $(this).val();
        clearTimeout(searchDebounce);
        searchDebounce = setTimeout(function () {
            dealsQuery = q;
            dealsPage = 1;
            loadDealsTable();
        }, 400);
    });

    $(document).on('click', ".hot-deal-page-prev", function () {
        if (dealsPage > 1) {
            dealsPage--;
            loadDealsTable();
        }
    });

    $(document).on('click', ".hot-deal-page-next", function () {
        var pages = $('#hot-deals-table-panel').data('pages') || 1;
        if (dealsPage < pages) {
            dealsPage++;
            loadDealsTable();
        }
    });

    // Model selection for the manual Solr push
    function refreshPushButton() {
        $('.push-solr').prop('disabled', $('.hot-deal-select:checked').length === 0);
    }

    $(document).on('change', "#hot-deal-select-all", function () {
        $('.hot-deal-select').prop('checked', $(this).prop('checked'));
        refreshPushButton();
    });

    $(document).on('change', ".hot-deal-select", refreshPushButton);

    $(document).on('click', ".push-solr", function () {
        var catalogIds = $('.hot-deal-select:checked').map(function () {
            return $(this).closest('tr').data('catalogId');
        }).get();
        if (catalogIds.length === 0) {
            alert("Select at least one model to push");
            return false;
        }
        if (!confirm("Push " + catalogIds.length + " model(s) to Solr now?")) {
            return false;
        }
        var btn = $(this);
        btn.prop('disabled', true).text('Pushing...');
        doPostAjaxRequestHandler(context + "/hotDeals/pushSolr?catalogIds=" + catalogIds.join(','),
            function (response) {
                btn.prop('disabled', false).html('<i class="fa fa-cloud-upload"></i> Push to Solr');
                alert(response);
                refreshPushButton();
            });
    });

    $(document).on('change', ".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 catalogId = row.data('catalogId');
        var warranty = row.find('.hot-deal-row-warranty').val();
        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/saveAttributes?catalogItemId=" + catalogId
            + "&warrantyMonths=" + warrantyNum
            + "&condition=" + row.find('.hot-deal-row-condition').val()
            + "&fresh=" + row.find('.hot-deal-row-fresh').val()
            + "&financeMapping=" + row.find('.hot-deal-row-finance').val()
            + "&affordability=" + row.find('.hot-deal-row-affordability').val(),
            function (response) {
                if (response == 'true') {
                    loadDealsTable();
                } 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') {
                        loadDealsTable();
                        loadHotDealModels();
                    } else {
                        alert(response);
                    }
                });
        }
    });

    function loadHotDeals(domId) {
        doGetAjaxRequestHandler(context + "/manageHotDeals", function (response) {
            $('#' + domId).html(response);
            dealsPage = 1;
            dealsQuery = '';
            loadDealsTable();
            loadHotDealModels();
        });
    }

    /**
     * OEM mapping autosuggest, scoped to the brand the selected Hot Deal model came from.
     * Optional: a model with no channel twin is simply left unmapped.
     */
    function loadOemModels(oemBrand) {
        var input = $('#hot-deal-oem-search');
        input.val('').removeData('catalogId');
        if (!oemBrand) {
            input.prop('disabled', true).attr('placeholder', 'No OEM brand recorded');
            return;
        }
        doGetAjaxRequestHandler(context + "/hotDeals/oemModels?brand=" + encodeURIComponent(oemBrand),
            function (response) {
                var oemModels = JSON.parse(response);
                input.prop('disabled', false)
                    .attr('placeholder', 'Optional - search ' + oemBrand + ' models...');
                input.typeahead('destroy').typeahead({
                    source: oemModels,
                    items: 15,
                    minLength: 2,
                    displayText: function (item) {
                        return item.description;
                    },
                    autoSelect: true,
                    afterSelect: function (item) {
                        input.data('catalogId', item.catalogId);
                    }
                });
            });
    }

    function loadDealsTable() {
        doGetAjaxRequestHandler(context + "/hotDeals/all?page=" + dealsPage
            + "&q=" + encodeURIComponent(dealsQuery), function (response) {
            $('#hot-deals-table-container').html(response);
            dealsPage = $('#hot-deals-table-panel').data('page') || 1;
        });
    }

    // Every Hot Deal-brand model. One brand, so nothing to scope by.
    function loadHotDealModels() {
        doGetAjaxRequestHandler(context + "/hotDeals/models", function (response) {
            hotDealModels = JSON.parse(response);
            var input = $('#hot-deal-model-search');
            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);
                    loadOemModels(item.oemBrand);
                }
            });
        });
    }

});