Rev 37246 | Rev 37270 | Go to most recent revision | 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");});// Brand choice drives the add flow: model picker + slot usage for that brand$(document).on('change', "#hot-deal-brand", function () {var brandId = $(this).val();var input = $('#hot-deal-model-search');input.val('').removeData('catalogId');if (!brandId) {input.prop('disabled', true).attr('placeholder', 'Select a brand first...');$('#hot-deal-slots').text('');$('.add-hot-deal').prop('disabled', false);$('#hot-deal-cap-notice').hide();return;}input.prop('disabled', false).attr('placeholder', 'Type 2+ letters to search models...');loadHotDealModels(brandId);loadSlots(brandId);});// All five attributes are mandatory with NO defaults - the add form starts emptyfunction 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 (!brandId) {alert("Please select a brand");return false;}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') {loadDealsTable();loadHotDealModels(brandId);loadSlots(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);}});});// 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();}});$(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') {loadDealsTable();refreshBrandPanel();} 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();refreshBrandPanel();} else {alert(response);}});}});function loadHotDeals(domId) {doGetAjaxRequestHandler(context + "/manageHotDeals", function (response) {$('#' + domId).html(response);dealsPage = 1;dealsQuery = '';setDefaultHotDealDates();loadDealsTable();});}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;});}function refreshBrandPanel() {var brandId = $('#hot-deal-brand').val();if (brandId) {loadHotDealModels(brandId);loadSlots(brandId);}}function loadSlots(brandId) {doGetAjaxRequestHandler(context + "/hotDeals/slots?brandId=" + brandId, function (response) {$('#hot-deal-slots').text(response + " slots used");var parts = response.split('/');var full = parts.length == 2 && parseInt(parts[0], 10) >= parseInt(parts[1], 10);$('#hot-deal-slots').toggleClass('label-danger', full).toggleClass('label-default', !full);$('.add-hot-deal').prop('disabled', full);$('#hot-deal-cap-notice').toggle(full);});}function loadHotDealModels(brandId) {doGetAjaxRequestHandler(context + "/hotDeals/models?brandId=" + brandId, 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);}});});}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'));}}});