getId=(idName)=>{
    return document.getElementById(idName);
}
let paymentMethod = getId("paymentMethod");
let cashPay = getId("cashEntry");
let bankPay = getId("bankEntry");
let chequeNo = getId("chequeNo");
handlePaymentMethod=()=>{
    let pay = paymentMethod.value;
if(pay == "cash")
{
bankPay.readOnly=true;
cashPay.readOnly =false;
chequeNo.readOnly=true;
bankPay.value = 0;
chequeNo.value = "-";
}
if(pay == "bank")
{
bankPay.readOnly=false;
cashPay.readOnly =true;
chequeNo.readOnly=false;
cashPay.value=0;
}
if(pay == "all")
{
bankPay.readOnly=false;
cashPay.readOnly =false;
chequeNo.readOnly=false;
}
}
paymentMethod.addEventListener("change",function(e){
    handlePaymentMethod();
});

$(document).ready(function(){
    handlePaymentMethod();
    let customer = $("#customerAdd").val();
    $.ajax({
        type:'POST',
        url:'ajaxpath1/fetch_amount_details.php',
        data:{customer:customer},
        success:function(data){
        var output = data.split("||");
        $("#totalAmount").val(output[0]);
        $("#pendingAmount").val(Number(output[1])+Number(cashPay.value)+Number(bankPay.value));
                    }
       });  
    $("#customerAdd").on("change",function(){
        $.ajax({
            type:'POST',
            url:'ajaxpath1/fetch_amount_details.php',
            data:{customer:customer},
            success:function(data){
            var output = data.split("||");
            $("#totalAmount").val(output[0]);
            $("#pendingAmount").val(output[1]);
                        }
           });  
    }); 
    $("#invoiceAdd").on("change",function(){
        fetchInvoiceAmountDetails(invoice)
    });
    fetchSalesBill(customer)
});
function fetchSalesBill(customer){
    let optionData = '';
    let sales_code = getId('prior_sales_value').value;
     $.ajax({
            type:'POST',
            url:'ajaxpath1/fetch_customer_bill_details.php',
            data:{customer:customer},
            success:function(dataRec){
                let data = JSON.parse(dataRec);
           console.log(typeof(data));
           if(data.length > 0) {
            invoiceDetails = data;
            optionData+=`<option value="">-- Select Bill ---</option>`;
             data.map((list)=>{
                optionData+=`<option value="${list.code}">${list.code}</option>`;
             });
             getId("invoiceAdd").innerHTML = optionData;
                         $("#totalAmount").val(0);
            $("#pendingAmount").val(0);
            getId("invoiceAdd").value = sales_code;
            fetchInvoiceAmountDetails(sales_code);
           }
         }
           });  
}
function fetchInvoiceAmountDetails(invoiceCode) {
    let customer = getId("customerAdd").value;
    let cashEntry = getId("cashEntry").value;
    let bankEntry = getId("bankEntry").value;
    invoice = invoiceCode ? invoiceCode : getId("invoiceAdd").value;

 $.ajax({
            type:'POST',
            url:`ajaxpath1/fetch_invoice_amount_details.php`,
            data:{invoice:invoice,customer:customer},
            success:function(data){
            let dataVal = JSON.parse(data);
            console.log(dataVal);
            $("#totalAmount").val(dataVal.value);
            $("#pendingAmount").val(Number(dataVal.pending) + Number(cashEntry) + Number(bankEntry));
                        }
           });   
}