$(document).ready(function(){
    /**
     * 
     * @returns 
     */
    $.fn.validations = function(){
        return [
           {
            "name":"noEmpty",
            "message":window.translations["create-patient"].fieldRequired,
            "run":function (field){
                if(field.key === "user_reference") return true;
                return ((field.value != undefined) && (field.value.trim() !== "")); 
            }
           },
           {
            "name":"noEmptySelect",
            "message":window.translations["create-appointment"].fieldRequired,
            "run":function (field){
                return (field.value !== "SELECT"); 
            },
        },
           {
            "name":"noNumber",
            "message":window.translations["create-patient"].fieldMustNumber,
            "run":function (field){
                if(field.key !== "amount") return true;
                let amount = field.value;
                amount = amount.replaceAll(".","");
                amount = amount.replace(",",".");
                return (!isNaN(Number(amount)));
            }
           },
           {
            "name":"greaterThanZero",
            "message":window.translations["create-patient"].fieldMustGreaterThanZero,
            "run":function (field){
                if(field.key !== "amount") return true;
                let amount = field.value;
                amount = amount.replaceAll(".","");
                amount = amount.replace(",",".");
                return (Number(amount) > 0);
            }
           },
        ];
    }
    $("#Payment").on("submit",function(e){
        let form    = document.querySelector("#Payment");
        let data    = $.fn.getDataForm.call(this,form);
        let valid   = $.fn.runValidations(data,$.fn.validations());
        if(!valid.valid)
            e.preventDefault();
        else{
            $(this).find(':submit').prop('disabled', true);
            $('#amount').val( $('#amount').val().replaceAll(".",""));
            $('#amount').val( $('#amount').val().replaceAll(",","."));
        }
           
    });
    $('#amount').on("keydown",(function(e) {
        // Obtener el valor de la tecla presionada
        var key = e.keyCode || e.charCode;
       
        // Permitir las teclas numéricas, de navegación y del teclado numérico derecho
        if (key >= 48 && key <= 57 || key >= 96 && key <= 105 || key == 8 || key == 9 || key == 37 || key == 39) {         
            return true;
        }
    
        // Prevenir la entrada de valores no numéricos
        e.preventDefault();
        return false;
    }));
    $("#amount").on("blur",(function(e){
        if(this.value !== "" && this.value != undefined){
            const value = this.value.replace(/,/g, '');
            this.value = parseFloat(value).toLocaleString('es-VE', {
                style: 'decimal',
                maximumFractionDigits: 2,
                minimumFractionDigits: 2
              });
        }
    }))
})