getId=(idname)=>{
    return document.getElementById(idname);
}
getQuery=(name)=>{
    return document.querySelectorAll(name);
}
let emptyWeight = getId("emptyWeightAdd");
    let loadWeight = getId("loadWeightAdd");
    let netWeight = getId("netWeightAdd");
    let netQty = getId("netQtyAdd");
    let netQtyOri = getId("netQtyOri");
    let unitConversion = getId("unitConversion");
    let totalAmtAdd = getId("totalAmtAdd");
    let priceAdd = getId("priceadd");
    let customer =getId("customerAdd");
    let product = getId("productAdd");
    let vehicle = getId("vehicleAdd");
    let chakka = getId("chakkaType");
    let billType = getId("billType");
    let requiredFeild = getQuery(".required");
let requiredInput = getQuery("input");
let validateButton = getId("validationButton");
let einvoiceSection = getId('einvoiceSection');
einvoiceSection.style.display = 'block';
billType.addEventListener("change",function(e){
e.target.value == 'gst' ?(getId("form1").action = "updatesecond_trans.php",einvoiceSection.style.display = 'block') 
                          : (getId("form1").action = "updatesecond_trans_est.php",einvoiceSection.style.display = 'none');
                          handleProductRate();
})
handleVehicle=(vehicle)=>{
    $.ajax({
        type:'POST',
        url:'ajaxpath1/fetch_vehicle_second.php',
        data:{vehicle:vehicle},
        success:function(data){
        $("#vehicleAdd").html(data);
        console.log(data);
        }
       });  
};
handleCustomer=(customer,customername)=>{
    $.ajax({
        type:'POST',
        url:'ajaxpath1/fetch_customer_second.php',
        data:{customer:customer,customername:customername},
        success:function(data){
        $("#customerAdd").html(data);
        console.log(data);
        }
       });  
};
handleProduct=(product,productname)=>{
    $.ajax({
        type:'POST',
        url:'ajaxpath1/fetch_product_second.php',
        data:{product:product,productname:productname},
        success:function(data){
        $("#productAdd").html(data);
        console.log(data);
        }
       });  
};
handleChakka=(vehicle,chakka)=>{
    $.ajax({
        type:'POST',
        url:'ajaxpath1/fetch_chakka_type_second.php',
        data:{vehicleno:vehicle,chakka:chakka},
        success:function(data){
        $("#chakkaType").html(data);
        console.log(data);
        }
       });  
};
handleLoadType=(data)=>{
    let load_type = "";
    if(data=="Load")
    {
         load_type=`<option value='Load'>In</option>
         <option value='Empty'>Out</option>
         `;
    }
    if(data=="Empty")
    {
         load_type=` <option value='Empty'>Out</option>
         <option value='Load'>In</option>
         `;
    }
        getId("loadType").innerHTML=load_type;
};
handleCalculation=()=>{
    console.log('yes claculation');
    if(loadWeight.value!=''){
    if(Number(emptyWeight.value)>=Number(loadWeight.value))
    {
        netWeight.value=emptyWeight.value-loadWeight.value;
    }
    if(Number(emptyWeight.value)<= Number(loadWeight.value))
    {
        netWeight.value=loadWeight.value-emptyWeight.value;
    }
    netQty.value=(netWeight.value/unitConversion.value).toFixed(2);
    netQtyOri.value=(netWeight.value/unitConversion.value).toFixed(2);

    handleTotalAmt();
}
}
handleLimitValidation=()=>{
    if(getId("loadType").value=="Empty"){
        if(Number(totalAmtAdd.value)>Number(getId("limitValue").value))
        {
            console.log("yes");
            getId("creditButton").click();
        }
        }
        if(getId("loadType").value=="Load"){
            if(getId("advanceLimitValue").value!=0){
            if(Number(totalAmtAdd.value)>Number(getId("limitValue").value))
            {
                
                getId("alertAdvance").click();
            }
        }
            }
}
handleTotalAmt=()=>{
    totalAmtAdd.value = (netQty.value * priceAdd.value).toFixed(2);
    console.log(getId("loadType").value);
    console.log(getId("limitValue").value); 
  
}
emptyWeight.addEventListener("keyup",function(){
 handleCalculation();
});
loadWeight.addEventListener("keyup",function(){
 handleCalculation();
});
netWeight.addEventListener("keyup",function(){
handleCalculation();
 });
 priceAdd.addEventListener("keyup",function(){
 handleCalculation();
 });
 netQty.addEventListener("keyup",function(){
handleTotalAmt();
});

emptyWeight.addEventListener("blur",function(){
    handleLimitValidation();
    });
    loadWeight.addEventListener("blur",function(){
     handleLimitValidation();
    });
    netWeight.addEventListener("blur",function(){
     handleLimitValidation();
     });
     priceAdd.addEventListener("blur",function(){
         handleLimitValidation();
     });
     netQty.addEventListener("blur",function(){
         handleLimitValidation();
    });
handleProductRate=()=>{
    let customerAdd = getId("customerAdd").value;
    let productAdd = getId("productAdd").value;
    $.ajax({
        type:'POST',
        url:'ajaxpath1/fetch_rate_second.php',
        data:{customer:customerAdd,product:productAdd},
        success:function(data){
        let output = data.split("||");
        console.log(data);
        $("#productnameadd").val(output[1]);
        $(".productdisplay").html(output[1]);
        $(".pricedisplay").html(billType.value == "gst"? output[0] : output[3]);
        $("#unitConversion").val(output[2]);
        $("#priceshowbutton").click();
        $("#priceadd").val(billType.value == "gst"? output[0] : output[3]);
        $("#priceaddori").val(billType.value == "gst"? output[0] : output[3]);
        handleCalculation();
        }
       });  
}
handleBlockDate=()=>{
    let customerAdd = getId("customerAdd").value;
  let loadType = getId("loadType").value;
  let dateOri = getId("dateOri").value;
  let totalAmtAdd=getId("totalAmtAdd").value;
  let oriLoadType = getId("oriLoadType").value;
  console.log(dateOri);
  console.log(customerAdd);
  if(customerAdd!=''){
    $.ajax({
        type:'POST',
        url:'ajaxpath1/fetch_block_date.php',
        data:{customer:customerAdd,loadtype:loadType},
        success:function(data){
            console.log(data);
            var output= data.split("||");
            console.log(output);
        $("#blockDate").val(output[0]);
    //blockDateValidation(output[0]);
        if(loadType == oriLoadType){
        $("#limitValue").val(Number(output[1])+Number(totalAmtAdd));
    }
    if(loadType != oriLoadType){
        $("#limitValue").val(Number(output[1]));
    }
        $("#advanceLimitValue").val(output[2]);
        if(loadType=="Empty")
        {
            if(output[0]!='-'){
        if(dateOri>=output[0])
        {
            console.log("hai");
            getId("blockButton").click();
            return;
        }
    }
    }
        handleProductRate();
        }
       });  
    }  
}
 getId("loadType").addEventListener("change",function(e){
     console.log(e.target.value);
    if(e.target.value=="Load"){
        getId("limitName").innerHTML="Advance Limit";
        handleBlockDate();
    }
    if(e.target.value=="Empty"){
        getId("limitName").innerHTML="Credit Limit";
        handleBlockDate();
    }
 });
 
feildValidate=()=>{
    let validateFunction = true;
    console.log('query',);
    let requiredValueForm =  getQuery(".requiredvalue");
       if(getQuery(".requiredvalue").length > 0){
        requiredValueForm.forEach((data)=>{
            if(data.value==""){
                data.classList.remove("requiredvalue");
                data.classList.add("required");
                validateFunction=false;
                console.log(data);
            }
            else{
                data.classList.remove("requiredvalue");
                data.classList.add("required");
            }
        });
       }
    let formRequired = getQuery(".required") ;
    formRequired.forEach((data)=>{
            if(data.value==""){
                data.classList.remove("required");
                data.classList.add("requiredvalue");
                validateFunction=false;
                console.log(data);
            }
            else{
                data.classList.remove("requiredvalue");
                data.classList.add("required");
            }
        });
       
console.log(validateFunction);
return validateFunction;
}
// validateButton.addEventListener("mouseover",function(){
//     handleLimitValidation();
// });
validateButton.addEventListener("click",function(){
    if(!feildValidate())
    {
        return;
    }
    console.log("yes");
    getId("submitButton").click();
});
requiredInput.forEach((data)=>{
    data.addEventListener("focus",function(){
        data.classList.remove("requiredvalue");
        data.classList.add("required");
    })
    data.addEventListener("keyup",function(){
        data.classList.remove("requiredvalue");
        data.classList.add("required");
    })
    data.addEventListener("change",function(){
        data.classList.remove("requiredvalue");
        data.classList.add("required");
    })
});
$(document).ready(function(){
    let ticket = $("#ticketselect").val();
  let totalAmtAdd=$("#totalAmtAdd").val();
    $.ajax({
        type:'POST',
        url:'ajaxpath1/fetch_second_trans_details.php',
        data:{ticket:ticket},
        success:function(data){
            let output = data.split("||");
            console.log(output);
            handleChakka(output[0],output[1]);          
            $("#blockDate").val(output[6]);
            if(output[8]=="Empty"){
                $("#limitName").html("Credit-Limit");
            }
            if(output[8]=="Load"){
                $("#limitName").html("Advance-Limit");
            }
            $("#unitConversion").val(output[13]);
            $("#limitValue").val(Number(output[7])+Number(totalAmtAdd));
            $("#advanceLimitValue").val(output[16]);
            //handleLoadType(output[8]);
            $(".cft").val(output[10]);
            $("#dateOri").val(output[15]);
        }
  
});
$("#vehicleAdd").on('change',function(){
    let vehicleNo = $(this).val();
    $.ajax({
        type:'POST',
        url:'ajaxpath1/fetch_chakka_type.php',
        data:{vehicleno:vehicleNo},
        success:function(data){
        $("#chakkaType").html(data);
        }
       });  
});
$("#customerAdd").on('change',function(){
handleBlockDate();

});
$("#productAdd").on('change',function(){
    handleBlockDate();
    
    });
    $(".reloadPage").click(function(){
        location.reload();
        });

        
});
$(document).ready(function(){
    $("#getweightbutton").click(function(){
        $.ajax({
         type:'POST',
         url:'ajaxpath/search1.php',
         //data:{chakka:chakkaType,vehicleno:vehicleNo},
         success:function(data){
         $("#loadWeightAdd").val(data);
       //  $("#cftValueOri").val(data);
       handleCalculation();
       handleLimitValidation();
         }
        });  
    });
})