// পেজ লোড হওয়ার সাথে সাথেই কারেন্সি এবং টোটাল হিসাব আপডেট করবে
window.onload = function() {
    updateCurrency();
    calculateTotal();
};

// কারেন্সি চেঞ্জ করার ফাংশন
function updateCurrency() {
    let val = document.getElementById("currencySelect").value;
    let symbol = val.split("|")[1];
    document.getElementById("hiddenCurrency").value = symbol;
    
    let symbols = document.querySelectorAll(".curr-symbol");
    symbols.forEach(el => el.innerText = symbol);
}

// অটোমেটিক টোটাল হিসাব করার ফাংশন
function calculateTotal() {
    let subtotal = 0;
    let rows = document.querySelectorAll("#itemsBody tr");
    
    rows.forEach(row => {
        let qty = parseFloat(row.querySelector(".qty-input").value) || 0;
        let rate = parseFloat(row.querySelector(".rate-input").value) || 0;
        let lineTotal = qty * rate;
        row.querySelector(".line-total").innerText = lineTotal.toFixed(2);
        subtotal += lineTotal;
    });
    
    document.getElementById("subtotal").innerText = subtotal.toFixed(2);
    
    let taxPercent = parseFloat(document.getElementById("taxInput").value) || 0;
    let discountVal = parseFloat(document.getElementById("discountInput").value) || 0;
    
    let taxAmount = (subtotal * taxPercent) / 100;
    let grandTotal = (subtotal + taxAmount) - discountVal;
    
    if(grandTotal < 0) grandTotal = 0;
    document.getElementById("grandTotal").innerText = grandTotal.toFixed(2);
    
    let paid = parseFloat(document.getElementById("paidInput").value) || 0;
    let balance = grandTotal - paid;
    if(balance < 0) balance = 0;
    document.getElementById("balanceDue").innerText = balance.toFixed(2);
}

// নতুন আইটেম (Row) যোগ করার ফাংশন
function addRow() {
    let tbody = document.getElementById("itemsBody");
    let tr = document.createElement("tr");
    let currency = document.getElementById("hiddenCurrency").value;
    
    tr.innerHTML = `
        <td><input type="text" name="item_desc[]" class="item-input" placeholder="Description of service/product" required></td>
        <td><input type="number" name="item_qty[]" class="item-input qty-input" value="1" min="1" oninput="calculateTotal()"></td>
        <td>
            <div style="display:flex; align-items:center;">
                <span class="curr-symbol" style="color:#64748b; margin-right:5px;">${currency}</span>
                <input type="number" name="item_rate[]" class="item-input rate-input" value="0.00" step="0.01" oninput="calculateTotal()">
            </div>
        </td>
        <td class="item-num">
            <span class="curr-symbol">${currency}</span><span class="line-total">0.00</span>
            <i class="fas fa-trash-alt btn-remove-row" title="Remove Item" onclick="removeRow(this)"></i>
        </td>
    `;
    tbody.appendChild(tr);
    calculateTotal(); // নতুন রো অ্যাড হওয়ার পর টোটাল রি-ক্যালকুলেট করবে
}

// আইটেম (Row) ডিলিট করার ফাংশন
function removeRow(btn) {
    let rowCount = document.querySelectorAll("#itemsBody tr").length;
    if(rowCount > 1) {
        btn.closest("tr").remove();
        calculateTotal();
    } else {
        alert("You must have at least one item in the invoice.");
    }
}