Subversion Repositories SmartDukaan

Rev

Rev 34881 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

$(document).on('click', '.affiliate-product', function () {
    doGetAjaxRequestHandler(`${context}/affiliate/getAffiliateProductList`, function (response) {
        $('#main-content').html(response);
    });
});
$(document).on('click', '.saveAffiliateProduct', function () {
    if (!$('#catalog_id').val()) {
        alert('Please select a catalog');
        return;
    }
    const formData = {
        catalogId: $('#catalog_id').val(),
        status: $('#status').val(),
        expiryDate: $('#expiry_date').val(),
        giftId: $('#gift_ids').val()?.toString(),
    };
    doAjaxRequestWithJsonHandler(
        `${context}/affiliate/create-affiliate-product`,
        "POST",
        JSON.stringify(formData),
        function (response) {
            if (response.responseStatus === "SUCCESS") {
                alert('Affiliate product created successfully!');
                $('#addAffiliateProductModal').modal('hide');
                $('.affiliate-product').click();
            } else {
                alert('Error: ' + response.message);
            }

        }
    );
});

$(document).on('click', '.map-catalog', function () {
    $('#addAffiliateProductModal').modal('show');
});

$(document).on('click', '.edit-affiliate-product', function () {
    const productId = $(this).data('productid');

    doGetAjaxRequestHandler(`${context}/affiliate/updateAffiliateProductList/${productId}`, function (response) {
        console.log('Response received:', response);

        try {
            if (!response) {
                console.error('Empty response received');
                return;
            }

            // Set catalog ID
            if (response.catalogId) {
                $('#edit_catalog_id').val(response.catalogId).multiselect('refresh');
            }
            const giftIds = response.giftId ? response.giftId.split(',') : [];
            $('#edit_gift_ids').val(giftIds).multiselect('refresh');
            if (response.status) {
                $('#edit_status').val(response.status);
            }
            if (response.expiryDate && Array.isArray(response.expiryDate)) {
                const [year, month, day, hour, minute] = response.expiryDate;
                const dateString = `${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}T${String(hour).padStart(2, '0')}:${String(minute).padStart(2, '0')}`;
                $('#edit_expiry_date').val(dateString);
            }

            console.log('About to show modal'); // Debug log

            // Show modal
            $('#editAffiliateProductModal').data('product-id', productId);
            $('#editAffiliateProductModal').modal('show');

            console.log('Modal show command executed'); // Debug log

        } catch (error) {
            console.error('Error processing response:', error);
        }
    }, function (error) {
        // Error callback
        console.error('AJAX request failed:', error);
    });
});


$(document).on('click', '.updateAffiliateProduct', function () {
    const productId = $('#editAffiliateProductModal').data('product-id');

    const formData = {
        id: productId,
        catalogId: $('#edit_catalog_id').val(),
        giftId: $('#edit_gift_ids').val().join(','), // Convert array to CSV
        status: $('#edit_status').val(),
        expiryDate: $('#edit_expiry_date').val()
    };

    doAjaxRequestWithJsonHandler(
        `${context}/affiliate/edit-affiliate-product`,
        "POST",
        JSON.stringify(formData),
        function (response) {
            if (response) {
                alert('Updated successfully!');
                $('#editAffiliateProductModal').modal('hide');
                $('.affiliate-product').click();
            } else {
                alert('Error: ' + response.message);
            }
        }
    );
});
// Reset CREATE modal when closed
$('#addAffiliateProductModal').on('hidden.bs.modal', function () {
    $(this).find('form')[0].reset();
    $('#catalog_id, #gift_ids').val('').multiselect('refresh');
});

$('#editAffiliateProductModal').on('hidden.bs.modal', function () {
    $(this).find('form')[0].reset();
    $(this).removeData('product-id');
    $('#edit_catalog_id, #edit_gift_ids').val('').multiselect('refresh');
});

$(document).on('click', '.delete-affiliate-product', function () {
    const id = $(this).data('productid');
    console.log("this is the id" + id);
    if (confirm('Are you sure you want to delete this product?')) {
        doDeleteAjaxRequestHandler(
            `${context}/affiliate/delete-affiliate-product/${id}`,
            function () {
                alert('Product deleted successfully!');

            }
        );
    }
});


$(document).on('click', '.affiliate-gift', function () {
    doGetAjaxRequestHandler(`${context}/affiliate/getAffiliateGiftList`, function (response) {
        $('#main-content').html(response);
    });
});

$(document).on('click', '.saveAffiliateGift', function () {
    const formData = {
        name: $('#gift_name').val(),
        status: $('#gift_Status').val() || "ACTIVE",
        qty: $('#gift_qty').val(),
    };
    console.log("we are getting the request body", formData);
    doAjaxRequestWithJsonHandler(
        `${context}/affiliate/create-affiliate-gift`,
        "POST",
        JSON.stringify(formData),
        function (response) {
            if (response.responseStatus === "SUCCESS") {
                alert('Affiliate product created successfully!');
                $('#addAffiliateGiftModal').modal('hide');
                $('.affiliate-gift').click();
            } else {
                alert('Error: ' + response.message);
            }

        }
    );
});


$(document).on('click', '.edit-affiliate-gift', function () {
    const giftId = $(this).data('giftid');

    doGetAjaxRequestHandler(`${context}/affiliate/updateAffiliateGift/${giftId}`, function (response) {
        console.log('Response received:', response);

        try {
            if (!response) {
                console.error('Empty response received');
                return;
            }
            if (response.name) {
                $('#edit_gift_name').val(response.name);
            }
            $('#edit_gift_qty').val(response.qty);
            if (response.status) {
                $('#edit_gift_Status').val(response.status);
            }
            console.log('About to show modal'); // Debug log
            $('#editAffiliateGiftModal').data('gift-id', giftId);
            $('#editAffiliateGiftModal').modal('show');
            console.log('Modal show command executed'); // Debug log
        } catch (error) {
            console.error('Error processing response:', error);
        }
    }, function (error) {
        console.error('AJAX request failed:', error);
    });
});

$(document).on('click', '.updateAffiliateGift', function () {
    const giftId = $('#editAffiliateGiftModal').data('gift-id');

    const formData = {
        id: giftId,
        name: $('#edit_gift_name').val(),
        qty: $('#edit_gift_qty').val(),
        status: $('#edit_gift_Status').val(),

    };

    doAjaxRequestWithJsonHandler(
        `${context}/affiliate/edit-affiliate-gift`,
        "POST",
        JSON.stringify(formData),
        function (response) {
            if (response) {
                alert('Updated successfully!');
                $('#editAffiliateGiftModal').modal('hide');
                $('.affiliate-gift').click();
            } else {
                alert('Error: ' + response.message);
            }
        }
    );
});


$(document).on('click', '.delete-affiliate-gift', function () {
    const id = $(this).data('giftid');
    console.log("this is the id" + id);
    if (confirm('Are you sure you want to delete this gift?')) {
        doDeleteAjaxRequestHandler(
            `${context}/affiliate/delete-affiliate-gift/${id}`,
            function () {
                alert('Gift deleted successfully!');

            }
        );
    }
});