Subversion Repositories SmartDukaan

Rev

Rev 26694 | Blame | Last modification | View Log | RSS feed

$(function() {
        $(".scheme_offer").live('click', function() {
                loadOffer("main-content");
        });

        $(".marginDetail").live(
                        'click',
                        function() {

                                var offerid = $(this).data('offerid');
                                console.log(offerid);
                                doGetAjaxRequestHandler(context + "/getOfferMargin?offerId="
                                                + offerid, function(response) {
                                        $('.offer-container .modal-content').html(response);
                                });
                        });

        $(".offer_history").live('click', function() {
                loadOfferHistory("main-content");
        });

        $("#offer-history-paginated .next").live(
                        'click',
                        function() {

                                loadPaginatedNextItems('/getPaginatedOffers', null,
                                                'offer-history-paginated', 'offer-listings-table',
                                                'offer-details-container');

                                $(this).blur();
                        });

        $("#offer-history-paginated .previous").live(
                        'click',
                        function() {

                                loadPaginatedPreviousItems('/getPaginatedOffers', null,
                                                'offer-history-paginated', 'offer-listings-table',
                                                'offer-details-container');

                                $(this).blur();
                        });

        $(".new-offer").live("click", function(e) {
                createOffer();
        });

        $("#offer-search-button").live('click', function() {
                var searchText = $("#offer-search-text").val();
                if (typeof (searchText) == "undefined" || !searchText) {
                        searchText = "";
                }

                console.log(searchText)
                loadOfferSearchInfo(searchText);
        });

        $(".offer-listing-details").live('click', function() {
                var offerId = $(this).attr('data');
                console.log("offerId = " + offerId
                                );
                loadOfferDetails(offerId, "offer-details-container");
        });

        $("#extendOffer-button")
                        .live(
                                        'click',
                                        function() {
                                                console.log("extend button click");
                                                var extendDatetime = $('input[name="extendOffer"]')
                                                                .val();
                                                var offerId = $('#offerId').val()
                                                console.log("extendDatetime : " + extendDatetime);
                                                console.log(offerId);
                                                if (extendDatetime) {
                                                        if (confirm("Are you sure you want to Extend expireDateTime for Scheme!") == true) {
                                                                extendOfferById(offerId)
                                                        }
                                                } else {
                                                        alert("feild can't be blank");
                                                }
                                        });

        $(".change-status")
                        .live(
                                        'click',
                                        function() {
                                                var id = $(this).data('offid');
                                                if (confirm("Are you sure you want to update the offer!") == true) {
                                                        doPostAjaxRequestHandler(context
                                                                        + "/updateOfferStatus?id=" + id, function(
                                                                        response) {
                                                                if (response == 'true') {
                                                                        alert("successfully update");
                                                                        loadOfferHistory("main-content");

                                                                }
                                                        });

                                                        return false;
                                                }
                                        });
});

function loadOffer(domId) {
        console.log(domId);
        doGetAjaxRequestHandler(context + "/getCreateOffer", function(response) {
                $('#' + domId).html(response);
                $('#qtyMargin').hide();
                $('#qtyPercentageMargin').hide();
                $('#valueMargin').hide();
                $('#oneBrandMarginOffer').hide();
                $('#itemModelMarginOffer').hide();
                $('#itemModelOffer').hide();

        });

}

function loadOfferHistory(domId) {
        doGetAjaxRequestHandler(context + "/getOffers", function(response) {
                $('#' + domId).html(response);

        });
}

function selecteditemCriteria() {
        var itemCriteria = $('#itemCriteria').val();
        console.log(itemCriteria);
        if (itemCriteria == "All_Brands") {
                $('#oneBrandMarginOffer').hide();
                $('#itemModelMarginOffer').hide();
                $('#itemModelOffer').hide();
        } else if (itemCriteria == "Multiple_Brand") {
                $('#oneBrandMarginOffer').show();
                $('#itemModelMarginOffer').hide();
                $('#itemModelOffer').hide();
        } else if (itemCriteria == "Item_Model") {
                $('#oneBrandMarginOffer').hide();
                $('#itemModelMarginOffer').show();
                $('#itemModelOffer').show();
        }

}

function selectedTargetType() {

        var targetType = $('#targetType').val();
        selectedAmountType()

}

function selectedAmountType() {
        var targetType = $('#targetType').val();
        var amountType = $('#amountType').val();
        console.log(amountType);
        if (targetType == "value" && amountType == "percentage") {
                $('#valueMargin').show();
                $('#qtyMargin').hide();
                $('#qtyPercentageMargin').hide();

        } else if (targetType == "quantity" && amountType == "percentage") {
                $('#qtyPercentageMargin').show();
                $('#qtyMargin').hide();
                $('#valueMargin').hide();

        } else if (targetType == "quantity" && amountType == "amount") {
                $('#qtyMargin').show();
                $('#qtyPercentageMargin').hide();
                $('#valueMargin').hide();
        }

}

function createOffer() {
        var offerDetailData = {}
        var amountOffer;
        var amountOfferJson = [];
        var offerName = $('#offerName').val();
        var description = $('#description').val();
        var itemCriteria = $('#itemCriteria').val();
        var targetType = $('#targetType').val();
        var amountType = $('#amountType').val();
        var fofoIds = $('#fofo-users').val();
        var criteria = $('#criteria').val();
        var partnerCategory = $('#partner-category').val();
        var schemeType = $('#schemeType').val();

        if (offerName === "" && description === "" && itemCriteria === ""
                        && targetType === "" && amountType === "" && fofoIds === "") {
                alert("Field can't be empty");
                return;
        }

        if (offerName === "") {
                alert("Offer Name is required");
                return;
        }

        if (itemCriteria === "") {
                alert("Item Criteria is required");
                return;
        }
        if (targetType === "") {
                alert("Target Type Number is required");
                return;
        }
        if (amountType === "") {
                alert("Amount Type is required");
                return;
        }

        if (partnerCategory === "") {
                alert("category is required");
                return;
        } else if (fofoIds === "") {
                alert("Minimum One Partner is required");

                return;
        }
        if (description === "") {
                alert("Description is required");
                return;
        }

        if (schemeType === "") {
                alert("Scheme Type is required");
                return;
        }

        console.log(itemCriteria);
        if (itemCriteria == "Multiple_Brand") {
                var brand = $('#brand').val();

                if (brand === "") {
                        alert("brand is required");
                        return;
                }
        }
        if (targetType == "value" && amountType == "percentage") {
                for (i = 0; i < 6; i++) {
                        var value = $('#value' + i).val();
                        var margin = $('#margin' + i).val();
                        if (value != "") {
                                amountOffer = {
                                        "value" : value,
                                        "margin" : margin
                                }

                                amountOfferJson.push(amountOffer)

                        }
                }

        } else if (targetType == "quantity" && amountType == "percentage") {
                for (i = 0; i < 6; i++) {
                        var value = $('#qtyPer' + i).val();
                        var margin = $('#qtyPermargin' + i).val();
                        if (value != "") {
                                amountOffer = {
                                        "value" : value,
                                        "margin" : margin
                                }
                                amountOfferJson.push(amountOffer)
                        }
                }

        } else if (targetType == "quantity" && amountType == "amount") {
                for (i = 0; i < 6; i++) {
                        var value = $('#qty' + i).val();
                        var margin = $('#qtymargin' + i).val();
                        if (value != "") {
                                amountOffer = {
                                        "value" : value,
                                        "margin" : margin
                                }
                                amountOfferJson.push(amountOffer)
                        }
                }
        }
        if (amountOfferJson.length === 0) {
                alert("value and margin is required");
                return;
        }
        offerDetailData['name'] = $('#offerName').val();
        offerDetailData['description'] = $('#description').val();
        offerDetailData['fofoIds'] = $('#fofo-users').val();
        offerDetailData['price'] = $('#price').val();
        offerDetailData['amountOffer'] = amountOfferJson;
        if (itemCriteria == "Item_Model") {
                offerDetailData['catalogIds'] = $('#item_catalog_id').val();
        }
        if (itemCriteria == "Multiple_Brand") {
                offerDetailData['brands'] = $('#brand').val();
        }
        offerDetailData['amountType'] = $('#amountType').val();
        offerDetailData['itemCriteria'] = $('#itemCriteria').val();
        offerDetailData['targetType'] = $('#targetType').val();
        offerDetailData['criteria'] = $('#criteria').val();
        offerDetailData['partnerType'] = $('#partner-category').val();

        offerDetailData['schemeType'] = $('#schemeType').val();
        offerDetailData['startDate'] = startDate;
        offerDetailData['endDate'] = endDate;
        console.log(offerDetailData);

        if (confirm("Are you sure you want to add offer!") == true) {
                doPostAjaxRequestWithJsonHandler(context + "/createOffer", JSON
                                .stringify(offerDetailData), function(response) {
                        if (response == 'true') {
                                alert("successfully create");
                                loadOffer("main-content");
                        }
                });

                return false;
        }
}

function loadOfferSearchInfo(searchText) {
        loadSearchOffer("main-content", searchText);
}

function extendOfferById(offerId) {
        doPostAjaxRequestWithJsonHandler(context + "/extendByOfferId?offerId="
                        + offerId, JSON.stringify(startDate), function(response) {
                if (response == 'true') {
                        alert("Offer extended successfully");
                        loadOfferHistory("main-content");
                } else {extendOffer-button
                        alert("Offer already Expired");
                        loadOfferHistory("main-content");
                }
        });

}

function loadSearchOffer(domId, search_text) {
        doGetAjaxRequestHandler(
                        context + "/searchOffer/?searchTerm=" + search_text, function(
                                        response) {
                                $('#' + domId).html(response);
                        });

}
function loadOfferDetails(offerId, domId) {
        doGetAjaxRequestHandler(context + "/offerById?offerId=" + offerId,
                        function(response) {
                                $('#' + domId).html(response);
                                window.dispatchEvent(new Event('resize'));
                        });
}