from django.forms import ModelForm
from django import forms
from .models import SalesOrder, SalesOrderItemLine
from crispy_forms.helper import FormHelper

class SalesOrderForm(ModelForm):
    class Meta:
        model = SalesOrder
        fields = "__all__"
        widgets = {
            'TxnDate' : forms.DateInput(attrs={'type':"date"}),
            'BillAddr1' : forms.Textarea(attrs={'rows':7, 'style':'height:180px'}),
            'ShipAddr1' : forms.Textarea(attrs={'rows':7, 'style':'height:180px'}),
            'TotalAmount' : forms.TextInput(attrs={'class':'text-end hidden', 'onkeypress':'return event.preventDefault()'})
        }
    def __init__(self, *args, **kwargs):
        super(SalesOrderForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper(self)
        # self.helper.form_show_labels = False
        self.fields['TotalAmount'].label = False
        self.fields['TotalAmount'].field_class = ''

class SalesOrderItemLineForm(ModelForm):
    class Meta:
        model = SalesOrderItemLine
        fields = ('ItemRefFullName', 'Desc', 'Quantity', 'UnitOfMeasure', 'Rate', 'Amount', 'Invoiced', 'LineIsManuallyClosed')
        # fields = "__all__"
        widgets = {
            # 'ItemRefFullName' : forms.Select(choices=[('1', '1')]),
            'ItemRefFullName' : forms.NumberInput(attrs={'class':'hidden itemreffullname'}),
            'Desc' : forms.TextInput(attrs={'class':'desc'}),
            'Quantity' : forms.TextInput(attrs={'class':'quantity','onkeypress':'numberOnly(event)'}),  #'onkeypress':'return (event.charCode >= 48 && event.charCode <= 57) || event.charCode == 46'}),
            'UnitOfMeasure' : forms.Select(attrs={'class':'unitofmeasure'}),
            'Rate' : forms.TextInput(attrs={'class':'rate text-end', 'onkeypress':'numberOnly(event)'}),
            'Amount' : forms.TextInput(attrs={'class':'amount text-end', 'onkeypress':'numberOnly(event)', 'onchange':'amountchanged(event)'}),
        }

    def __init__(self, *args, **kwargs):
        super(SalesOrderItemLineForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper(self)
        self.helper.form_show_labels = False
        # self.helper.form_class = "abc"
        # self.helper.field_class = "xyz"