$(function(){

    $('.calc').keyup(function(){

        calculate();

    });

    $('.calc').change(function(){

        calculate();

    });

});


function calculate(){

    let weight=parseFloat($('#weight').val())||0;

    let rate=parseFloat($('#rate').val())||0;


    makeRow(weight,rate,.88,
        '#hall_gold',
        '#hall_amount');

    makeRow(weight,rate,.85,
        '#machine_gold',
        '#machine_amount');

    makeRow(weight,rate,.80,
        '#manual_gold',
        '#manual_amount');

    makeRow(weight,rate,.75,
        '#kdm_gold',
        '#kdm_amount');

}



function makeRow(weight,rate,purity,goldId,amountId){

    let gold=(weight*purity);

    let amount=(gold*rate);

    $(goldId).html(gold.toFixed(2));

    $(amountId).html(numberFormat(amount));

}


function numberFormat(num){

    return "₹ "+num.toLocaleString('en-IN',{
        minimumFractionDigits:2,
        maximumFractionDigits:2
    });

}

$('#saveBtn').click(function(){

    $.ajax({

        url:BASE_URL+"GoldCalculator/save",

        type:"POST",

        data:$("#goldForm").serialize(),

        dataType:"JSON",

        success:function(res){

            if(res.status)
            {

                Swal.fire({

                    icon:'success',

                    title:'Success',

                    text:res.message

                }).then(()=>{

                    window.open(BASE_URL+"GoldCalculator/receipt/"+res.id);

                });

            }
            else
            {

                Swal.fire({

                    icon:'error',

                    title:'Error',

                    text:res.message

                });

            }

        }

    });

});

$(function(){

    console.log("Application Loaded");

});


function successMsg(msg)
{

    Swal.fire({

        icon:"success",

        title:"Success",

        text:msg

    });

}


function errorMsg(msg)
{

    Swal.fire({

        icon:"error",

        title:"Error",

        text:msg

    });

}