Subversion Repositories SmartDukaan

Rev

Rev 35775 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

$(function () {
        $(document).on('click', ".price-circular", function () {
                loadPriceCircular("main-content");
        });

        $(document).on('click', ".partner-price-circular", function () {

                bootbox.confirm({
                        message: "<div class = 'disclosurehindata'> <img src='resources/images/disclosure.png' style='width:100%'> </div>"
                        ,
                        buttons: {
                                confirm: {
                                        label: 'I agree',
                                        className: 'btn-success'
                                },
                                cancel: {
                                        label: 'I disagree',
                                        className: 'btn-danger'
                                }
                        },
                        callback: function (result) {
                                if (result == true) {
                                        loadPartnerPriceCircular("main-content");
                                }
                        }
                });

        });

        $(document).on('click', ".submit-circular-request", function () {
                let brand = $("#brands").val();
                let priceCircularUrl = `${context}/priceCircularByBrand?brand=${brand}`;
                if (typeof partnerId == "number") {
                        priceCircularUrl = `${priceCircularUrl}&fofoId=${partnerId}`;
                }
                doGetAjaxRequestHandler(priceCircularUrl,
                        function (response) {
                                console.log(response)
                                $('.price-circular-container').html(response);
                        });
        });

        $(document).on('click', ".download-nlc", function () {
                let brand = $("#brands").val();
                let downloadNlcUrl = `${context}/downloadNlcByBrand?brand=${brand}`;
                if (typeof partnerId == "number") {
                        downloadNlcUrl = `${downloadNlcUrl}&fofoId=${partnerId}`;
                }
                doAjaxGetDownload(downloadNlcUrl, `${brand}-nlc.csv`);
        });


});

function loadPriceCircular(domId) {
        doGetAjaxRequestHandler(context + "/priceCircular",
                function (response) {
                        $('#' + domId).html(response);
                });
}

function loadPartnerPriceCircular(domId) {
        doGetAjaxRequestHandler(context + "/partnerPriceCircular",
                function (response) {
                        $('#' + domId).html(response);
                });
}

$(document).on('click', ".today-offer", function () {
        loadTodayOfferVM("main-content");
});

// Main function called to load the shell
function loadTodayOfferVM(domId) {
        doGetAjaxRequestHandler(context + `/todayOffer`, function (response) {
        // Load the brand tabs
        $('#' + domId).html(response);

        // AUTO-LOAD Samsung immediately after tabs are ready
                loadBrandOffers("Motorola", 0);
    });
}


// Click listener for tabs
$(document).on('click', ".brand-tab", function () {
    let fofoId = $(this).data('fofoid');
    let brand = $(this).data("brand");
    loadBrandOffers(brand, fofoId);
});


// Function to handle the actual data loading
function loadBrandOffers(brandName, fofoId) {


    // 1. Show the loader immediately
    const loaderHtml = `
        <center><div class="loader-wrapper">
            <i class="fa fa-spinner fa-spin fa-4x"></i>
            <span class="loader-text">Loading offers for ${brandName}...</span>
        </div></center>`;

    $('.today-offer-container').html(loaderHtml);

    // 2. Prepare the URL
    let todayOfferUrl = `${context}/todayOfferList?brand=${brandName}&fofoId=${fofoId}`;
    if (typeof partnerId == "number") {
        todayOfferUrl = `${todayOfferUrl}&fofoId=${partnerId}`;
    }

    // 3. Hit the API
    doGetAjaxRequestHandler(todayOfferUrl, function (response) {
        // Success: The loader is replaced by the actual table response
        $('.today-offer-container').html(response);

        // Update Active Tab UI
        $(".brand-tabs li").removeClass("active");
        $(`.brand-tab[data-brand='${brandName}']`).closest('li').addClass("active");
    });
}


$(document).on('click', '.today-offer-view', function () {
    let offerId = $(this).data("offerid");
    doGetAjaxRequestHandler(`${context}/getOfferMargins?offerId=${offerId}`, function (data) {
        $('#todayOfferDescription .modal-content').html(data);
        $("#todayOfferDescription").modal('show');
    });
});


$(document).on('click', '.today-fofo-offer', function () {
    let fofoId = $(this).data("fofoid");
    doGetAjaxRequestHandler(`${context}/todayFofoOffer?fofoId=${fofoId}`, function (data) {
        $(".today-fofo-offer-view").show();
        $(".fofo-summry-view").hide();
        $(".scheme-imei-history-view").hide();
        $(".partner-wallet-view").hide();
        $('.today-fofo-offer-view').html(data);
        // AUTO-LOAD Samsung immediately after tabs are ready
        loadFofoBrandOffers("Motorola", fofoId);
    });
});


$(document).on('click', '.fofo-stock-info', function () {

        $(".today-fofo-offer-view").hide();
    $(".scheme-imei-history-view").hide();
    $(".partner-wallet-view").hide();
        $(".fofo-summry-view").show();
});


// Variables for modal search
var modalCurrentItem = null;
var modalFofoId = null;

// IMEI Margin (Scheme IMEI History) handlers
$(document).on('click', '.scheme-imei-history-btn', function () {
    modalFofoId = $(this).data("fofoid");

    // Load the scheme-imei-history view (empty initially, user will search)
    doGetAjaxRequestHandler(`${context}/getImeiMarginModal?searchImei=&fofoId=${modalFofoId}`, function (response) {
        $(".today-fofo-offer-view").hide();
        $(".fofo-summry-view").hide();
        $(".partner-wallet-view").hide();
        $(".scheme-imei-history-view").show();
        $('.scheme-imei-history-view').html(response);

        // Initialize IMEI autocomplete if available
        if (typeof getAllImeiAheadOptions === 'function') {
            getImeiAheadOptions($("#modal-imei-search-text"), modalFofoId, function (inventoryItem) {
                // Trigger search when item selected from autocomplete
            });
        }

        // Initialize model autocomplete for modal
        initModalModelAutocomplete();
    });
});

// Partner Wallet handlers
$(document).on('click', '.partner-wallet-btn', function () {
    let fofoId = $(this).data("fofoid");

    // Load the partner wallet history view
    doGetAjaxRequestHandler(`${context}/getWalletHistoryByPartner?fofoId=${fofoId}`, function (response) {
        $(".today-fofo-offer-view").hide();
        $(".fofo-summry-view").hide();
        $(".scheme-imei-history-view").hide();
        $(".partner-wallet-view").show();
        $('.partner-wallet-view').html(response);
    });
});

// Initialize model autocomplete in modal
function initModalModelAutocomplete() {
    modalCurrentItem = null;
    if (typeof getEntityAheadOptions === 'function') {
        getEntityAheadOptions($("#modal-model-search-text"), function (selectedItem) {
            modalCurrentItem = selectedItem.catalogId_i;
        });
    }
}

// Search IMEI margin on button click within modal
$(document).on('click', '#modal-imei-search-button', function () {
    let imei = $("#modal-imei-search-text").val().trim();
    if (imei) {
        searchImeiMarginInModal(imei);
    } else {
        alert("Please enter IMEI number");
    }
});

// Search Model margin on button click within modal
$(document).on('click', '#modal-model-search-button', function () {
    let searchText = $("#modal-model-search-text").val().trim();
    let date = $(".modal-schemes-date").val();

    if (!searchText) {
        alert("Please select a model");
        return;
    }

    if (modalCurrentItem) {
        searchModelMarginInModal(modalCurrentItem, date);
    } else {
        alert("Please select a valid model from suggestions");
    }
});

// Function to search and load model-wise margin details in modal
function searchModelMarginInModal(catalogId, date) {
    const loaderHtml = `
        <center><div class="loader-wrapper" style="padding: 50px;">
            <i class="fa fa-spinner fa-spin fa-3x"></i>
            <p style="margin-top: 15px;">Loading model margin details...</p>
        </div></center>`;

    // Hide IMEI results, show Model results container
    $('.modal-imei-margin-container').hide();
    $('.modal-model-margin-container').show().html(loaderHtml);

    // Use new endpoint with fofoId
    doGetAjaxRequestHandler(`${context}/getModelMarginModal?searchModel=${catalogId}&date=${date}&fofoId=${modalFofoId}`, function (response) {
        $('.modal-model-margin-container').html(response);
    });
}

// Function to search and load IMEI margin details in modal
function searchImeiMarginInModal(imei) {
    const loaderHtml = `
        <center><div class="loader-wrapper" style="padding: 50px;">
            <i class="fa fa-spinner fa-spin fa-3x"></i>
            <p style="margin-top: 15px;">Loading margin details for ${imei}...</p>
        </div></center>`;

    // Hide Model results, show IMEI results container
    $('.modal-model-margin-container').hide();
    $('.modal-imei-margin-container').show().html(loaderHtml);

    // Use new endpoint with fofoId
    doGetAjaxRequestHandler(`${context}/getImeiMarginModal?searchImei=${imei}&fofoId=${modalFofoId}`, function (response) {
        $('.modal-imei-margin-container').html(response);
    });
}


$(document).on('click', ".fofo-brand-tab", function () {
    let fofoId = $(this).data('fofoid');
    let brand = $(this).data("brand");
    loadFofoBrandOffers(brand, fofoId);
});


// Function to handle the actual data loading
function loadFofoBrandOffers(brandName, fofoId) {


    // 1. Show the loader immediately
    const loaderHtml = `
        <center><div class="loader-wrapper">
            <i class="fa fa-spinner fa-spin fa-4x"></i>
            <span class="loader-text">Loading offers for ${brandName}...</span>
        </div></center>`;

    $('.today-fofo-offer-container').html(loaderHtml);

    // 2. Prepare the URL
    let todayOfferUrl = `${context}/todayOfferList?brand=${brandName}&fofoId=${fofoId}`;
    if (typeof partnerId == "number") {
        todayOfferUrl = `${todayOfferUrl}&fofoId=${partnerId}`;
    }

    // 3. Hit the API
    doGetAjaxRequestHandler(todayOfferUrl, function (response) {
        // Success: The loader is replaced by the actual table response
        $('.today-fofo-offer-container').html(response);

        // Update Active Tab UI
        $(".brand-tabs li").removeClass("active");
        $(`.brand-tab[data-brand='${brandName}']`).closest('li').addClass("active");
    });
}