from django.forms import ModelForm, TextInput, CharField, Form

from containers.models import Container


class ContainerBaseForm(ModelForm):
    class Meta:
        model = Container
        fields = '__all__'
        widgets = {
            'number': TextInput(attrs={
                'oninput': 'validateContainerNumber(this)',
                'class': 'form-control'
            })
        }
class ContainerReceiveForm(ContainerBaseForm):
    """
    Form for creating a new Container instance.
    Inherits from ContainerBaseForm.
    """
    class Meta(ContainerBaseForm.Meta):
        fields = ['number', 'receive_vehicle', 'damages', 'heavy_damaged', 'position',]

    def __init__(self, *args, preinfo=None, **kwargs):
        super().__init__(*args, **kwargs)

        # Add form-control class to all fields
        for field in self.fields.values():
            field.widget.attrs.update({'class': 'form-control'})

        # Checkbox specific styling
        self.fields['heavy_damaged'].widget.attrs.update({'class': 'form-check-input'})

        # If preinfo is provided, set initial values and make fields readonly
        if preinfo:
            ...
            # self.fields['preinfo'].initial = preinfo.container_number
            # self.fields['number'].initial = preinfo.number
            # self.fields['container_number'].widget.attrs['readonly'] = True
            # self.fields['number'].widget.attrs['readonly'] = True
            # self.fields['booking'].disabled = True
            # self.fields['number'].disabled = True


class ContainerExpeditionForm(ContainerBaseForm):
    class Meta(ContainerBaseForm.Meta):
        fields = ['number', 'expedition_vehicle', 'position', 'line', 'container_type', 'damages', 'heavy_damaged']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        readonly_fields = ['number', 'position', 'line', 'container_type', 'damages', 'heavy_damaged']
        for field in readonly_fields:
            self.fields[field].widget.attrs['readonly'] = True
            # self.fields[field].disabled = True


class ContainerSearchForm(Form):
    container_number = CharField(
        max_length=11,
        required=True,
        widget=TextInput(attrs={
            'class': 'form-control',
            'placeholder': 'Enter container number'
        })
    )