$(document).ready(function () { console.log('jQuery loaded successfully.'); ImgUpload(); }); // ------------------------------------ // multi step form handling start // ------------------------------------ var currentTab = 0; // Current tab is set to be the first tab (0) showTab(currentTab); // Display the current tab function showTab(n) { var x = document.getElementsByClassName('step'); // Check if the index 'n' is within the bounds of the 'x' array if (n >= 0 && n < x.length) { // Hide all steps for (var i = 0; i < x.length; i++) { x[i].style.display = 'none'; } // Display the selected step x[n].style.display = 'block'; // Fix Previous/Next buttons if (n == 0) { document.getElementById('prevBtn').style.display = 'none'; } else { document.getElementById('prevBtn').style.display = 'inline'; } if (n == x.length - 1) { document.getElementById('nextBtn').innerHTML = 'Submit'; } else { document.getElementById('nextBtn').innerHTML = 'Next'; } // Update step indicator fixStepIndicator(n); } else { console.error('Invalid tab index:', n); } } function nextPrev(n) { var x = document.getElementsByClassName('step'); if (n == 1) { if (!validateForm()) return false; // Check if form is valid before proceeding to the next step // Validate the form synchronously // Hide the current tab: x[currentTab].style.display = 'none'; } currentTab = currentTab + n; if (x.length === 6) { generatePreview(); } if (currentTab >= x.length) { document.getElementById('lotForm').submit(); return false; } showTab(currentTab); } function validateForm() { var x = document.getElementsByClassName('step'); var y = x[currentTab].getElementsByTagName('input'); var valid = true; // Reset validation classes for all input fields in the current tab: // for (var i = 0; i < y.length; i++) { // y[i].classList.remove('invalid'); // } // // A loop that checks every input field in the current tab: // for (var i = 0; i < y.length; i++) { // // If a field is empty and not of type file... // if (y[i].value.trim() === '' && y[i].type !== 'file' && y[i] !== 'radio') { // // Add an "invalid" class to the field: // y[i].classList.add('invalid'); // // Set the current valid status to false: // valid = false; // } // } // If the valid status is true, mark the step as finished and valid: if (valid) { document .getElementsByClassName('stepIndicator') [currentTab].classList.add('finish'); } console.log('Form status:', valid); return valid; } function fixStepIndicator(n) { // This function removes the "active" class of all steps... var i, x = document.getElementsByClassName('stepIndicator'); for (i = 0; i < x.length; i++) { x[i].className = x[i].className.replace(' active', ''); } //... and adds the "active" class on the current step: x[n].className += ' active'; } // ------------------------------------ // multi step form handling start // ------------------------------------ //------------------- // image upload //------------------- function ImgUpload() { var imgWrap = $('.upload-img-wrap'); $('.add-more').click(function (e) { console.log('Add More Photos button clicked.'); e.preventDefault(); // Trigger file input inside the upload-btn $('.upload-btn .upload-inputfile').click(); }); var addMoreButton = $('.add-more'); $('.upload-inputfile').on('change', function (e) { $('.upload-btn').addClass('d-none'); imgWrap.addClass('uploaded'); addMoreButton.removeClass('d-none'); imgWrap.removeClass('d-none'); var maxLength = parseInt($(this).attr('data-max_length')); var files = e.target.files; var filesArr = Array.prototype.slice.call(files); filesArr.forEach(function (f, index) { if (!f.type.match('image.*')) { return; } if (imgWrap.find('.upload-img-box').length >= maxLength) { return false; } else { var reader = new FileReader(); reader.onload = function (e) { var img = document.createElement('img'); img.src = e.target.result; img.classList.add('upload-img-box'); imgWrap.append(img); // Append close button to the image container var closeButton = $('
${description}
`; } summaryHTML += `${label}: ${field.value}
${label}: ${field.value}
No categories found
'; } }) .catch((error) => console.error('Error:', error)); } // Event listener for input change document .querySelector('.search-icon input[name="category_search_query"]') .addEventListener('input', function () { console.log('cat'); var searchValue = this.value.toLowerCase().trim(); if (searchValue.length === 0) { // Clear category results if search query is empty document.getElementById('categoryResult').innerHTML = ''; return; } fetchCategories(searchValue); }); function generateCategoryRadios(categories) { var categoryResultDiv = document.getElementById('categoryResult'); categoryResultDiv.innerHTML = ''; categories.forEach(function (category) { var radioDiv = document.createElement('div'); radioDiv.classList.add('form-check', 'flex-column'); var radioInput = document.createElement('input'); radioInput.type = 'radio'; radioInput.name = 'category_type'; radioInput.id = category.id; radioInput.value = category.name; radioInput.classList.add('form-check-input'); var radioLabel = document.createElement('label'); radioLabel.classList.add('form-check-label'); radioLabel.htmlFor = category.id; radioLabel.textContent = category.name; radioDiv.appendChild(radioInput); radioDiv.appendChild(radioLabel); categoryResultDiv.appendChild(radioDiv); }); } //----------------------- // Category search END //----------------------- function storeImages() { const uploadedImages = $('.upload-img-wrap img'); const imageSrcs = []; console.log('store imagages'); // Collect the src attribute of each uploaded image uploadedImages.each(function (index, img) { const src = img.src; imageSrcs.push(src); }); // Send the image data to the server using AJAX $.ajax({ url: '/lot/images/save/', // Replace '/save_images/' with the URL of your Django view method: 'POST', data: { images: imageSrcs }, // Send the image srcs as JSON data success: function (response) { console.log('Images saved successfully:', response); // Optionally, do something with the response from the server }, error: function (xhr, status, error) { console.error('Error saving images:', error); }, }); } $('#lotForm').submit(function (event) { event.preventDefault(); // Prevent the default form submission $(this).unbind('submit').submit(); storeImages(); // Call the function to store the images // Optionally, submit the form after storing the images });