from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField, SubmitField, DateField, BooleanField, DecimalField, fields
from wtforms.validators import DataRequired

class FlexibleDecimalField(fields.DecimalField):

    def process_formdata(self, valuelist):
        if valuelist:
            valuelist[0] = valuelist[0].replace(",", ".")
        return super(FlexibleDecimalField, self).process_formdata(valuelist)
    

class RepostajeForm(FlaskForm):
    fecha = DateField('Fecha', validators=[DataRequired()])
    vehiculo = StringField('Vehículo', validators=[DataRequired()])
    kms = IntegerField('Kilómetros', validators=[DataRequired()])
    litros = FlexibleDecimalField('Litros', validators=[DataRequired()])
    descuento = BooleanField('Descuento')
    importe = FlexibleDecimalField('Importe', validators=[DataRequired()])
    
    submit = SubmitField('Crear')
