function toggleSize() {

    let layout = document.getElementById("layout").value;

    // 🔹 Show/Hide size input row
    document.querySelectorAll(".sizeRow").forEach(el => {
        el.style.display = (layout == "16") ? "" : "none";
    });

    // 🔹 Handle table rows
    document.querySelectorAll("#selectedTable tbody tr").forEach(row => {

        let sizeCell = row.querySelector(".size");

        if (layout == "16") {
            // SHOW size column
            if (sizeCell) sizeCell.style.display = "";
        } else {
            // HIDE + CLEAR size column
            if (sizeCell) {
                sizeCell.innerText = "";
                sizeCell.style.display = "none";
            }
        }
    });

    // 🔹 Clear input
    if (layout != "16") {
        let sizeInput = document.getElementById("size");
        if (sizeInput) sizeInput.value = "";
    }
}

function fillMRP() {
    let select = document.getElementById("product");
    let selected = select.options[select.selectedIndex];

    let mrp = selected.getAttribute("data-mrp");
    document.getElementById("mrp").value = mrp ? mrp : "";
}

function addItem() {

    let select = document.getElementById("product");
    let selected = select.options[select.selectedIndex];

    let name = selected.getAttribute("data-name");
    let mrp = document.getElementById("mrp").value;
    let qty = document.getElementById("qty").value;
    let layout = document.getElementById("layout").value;

    let size = "";
    if (layout == "16") {
        size = document.getElementById("size").value;
    }


    if (!name) {
        alert("Select product");
        return;
    }

    if (qty <= 0) {
        alert("Enter valid qty");
        return;
    }

    let table = document.querySelector("#selectedTable tbody");

    // 🔥 check duplicate → increase qty
    let rows = table.querySelectorAll("tr");

    for (let i = 0; i < rows.length; i++) {
        let rowName = rows[i].querySelector(".name").innerText.trim();

        let sizeCell = rows[i].querySelector(".size");
        let rowSize = sizeCell && sizeCell.style.display !== "none"
            ? sizeCell.innerText.trim()
            : "";

        let currentSize = (layout == "16") ? size.trim() : "";

        if (rowName === name.trim() && rowSize === currentSize) {

            let qtyCell = rows[i].querySelector(".qty");
            qtyCell.innerText = parseInt(qtyCell.innerText) + parseInt(qty);
            return;
        }
    }


    let newRow = table.insertRow();

    let sizeTd = "";

    if (layout == "16") {
        sizeTd = `<td class="size">${size}</td>`;
    } else {
        sizeTd = `<td class="size" style="display:none;"></td>`;
    }

    newRow.innerHTML = `
    <td class="name">${name}</td>
    <td class="mrp">${mrp}</td>
    ${sizeTd}
    <td class="qty">${qty}</td>
    <td><button type="button" class="btn btn-remove" onclick="removeRow(this)">✕</button></td>
`;
}

function removeRow(btn) {
    btn.closest("tr").remove();
}

/* BEFORE SUBMIT → PACK DATA */
function prepareData() {

    let rows = document.querySelectorAll("#selectedTable tbody tr");
    let items = [];

    rows.forEach(row => {

        let name = row.querySelector(".name").innerText;
        let mrp = row.querySelector(".mrp").innerText;
        let qty = row.querySelector(".qty").innerText;
        let sizeCell = row.querySelector(".size");
        let size = sizeCell ? sizeCell.innerText : "";

        items.push({
            name: name,
            mrp: mrp,
            qty: qty,
            size: size
        });
    });

    if (items.length === 0) {
        alert("Please add at least one item");
        return false;
    }

    // 🔥 SET VALUE HERE
    document.getElementById("items_data").value = JSON.stringify(items);

    // 🔥 SWITCH FILE BASED ON LAYOUT
    let layout = document.getElementById("layout").value;

    let form = document.getElementById("mainForm");

    if (layout == "16") {
        form.action = "labels16pdf.php";
    } else {
        form.action = "print_labels_fr.php";
    }

    document.getElementById("qty").value = 1;
    document.getElementById("size").value = "";
    return true;
}