"use strict"; // product img upload code start ----------------> $(document).ready(function () { // Choices.js initializer function initChoices($root) { $root.find(".tagSelect").each(function () { new Choices(this, { removeItemButton: true, placeholder: true, placeholderValue: "Select variations", }); }); } const $container = $("#productTypeContainer"); // When product type changes $("#product-type").on("change", function () { loadComponent($(this).val()); }); loadComponent($("#product-type").val()); // Main function function loadComponent(type) { // Remove existing component from DOM $container.empty(); // Choose template let templateHtml; if (type === "combo") { $(".serial-code-checkbox").addClass("d-none"); $(".low-stock-input").addClass("d-none"); templateHtml = $("#comboTemplate").html(); loadAllProducts(); } else if (type === "single") { $(".serial-code-checkbox").removeClass("d-none"); $(".low-stock-input").removeClass("d-none"); templateHtml = $("#singleTemplate").html(); } else { $(".serial-code-checkbox").removeClass("d-none"); $(".low-stock-input").removeClass("d-none"); templateHtml = $("#variationTemplate").html(); setTimeout(() => { resetVariantRows(false); }, 100); } // Append to container $container.append($(templateHtml)); if (!$("#previous-stocks").length || $("#previous-stocks").val() === "[]") { $('.batch_no').each(function (index) { if (!$(this).val()) { $(this).val($('#productCode').val() + '-' + (index + 1)); } }); } // Re-initialize Choices.js for newly inserted selects initChoices($container); return; } $(document).on("change", ".tagSelect[name='variation_ids[]']", function () { resetVariantRows(true); }); function resetVariantRows(regenerateRows) { const selectedVariationIds = $(".variation_ids").val() || []; const variationsData = JSON.parse($("#variations-data").val() || "[]"); const selectedValuesMap = JSON.parse( $("#selected-variation-values").val() || "{}" ); const $variationContainer = $(".variation-values-container"); $variationContainer.empty(); // clear old variation selects selectedVariationIds.forEach(function (variationId) { const variation = variationsData.find((v) => v.id == variationId); if (!variation) return; const selectedValues = selectedValuesMap[variation.name] || []; const selectHtml = `
Code: ${product.productCode}
Code: ${product_code}, Batch No: ${batch_no}