# -*- coding: utf-8 -*-

from django import forms
from django.forms import ModelForm
from parameters.models import ParLine
from parameters import system_tables as ptables
#from django.forms.widgets import SelectDateWidget

#from datetimewidget.widgets import DateWidget, DateInput
from . import messages as msg
from .models import Exchange_Rate


class CreateExchangeRateForm(ModelForm):
    """
    Form for Exchange Rate
    """

    currency_date = forms.DateField(widget=forms.TextInput(attrs={'class':'form_input', 'required':'true'}),required=True, error_messages={'required': msg.required_date})
    value         = forms.DecimalField(widget=forms.NumberInput(attrs={'class':'form_input', 'required':'true'}), required=True, error_messages={'required': msg.required_price})
    currency      = forms.ModelChoiceField(queryset=ParLine.objects.filter(table_no=ptables.PT_6, line_num4=1).order_by('line_num2'),to_field_name="line_no",label="Currency",required=True, error_messages={'required': msg.required_currency}, empty_label=None)
    currency_ref  = forms.ModelChoiceField(queryset=ParLine.objects.filter(table_no=ptables.PT_6, line_num4=1).order_by('line_num1'),to_field_name="line_no",label="Currency",required=True, error_messages={'required': msg.required_currency_ref}, empty_label=None)
    
    last_user     = forms.IntegerField(required=False)
    user_created  = forms.IntegerField(required=False)
    
    class Meta:
        model = Exchange_Rate
        fields = ['currency_date', 'value', 'currency', 'currency_ref', 'last_user', 'user_created']
        
    def __init__(self, *args, **kwargs):
        self.last_user = kwargs.pop("last_user")
        self.user_created = kwargs.pop("user_created")
        #self.currency = kwargs.pop("currency")
        #self.currency_ref = kwargs.pop("currency_ref")
        #self.date = kwargs.pop("date")
        super(self.__class__, self).__init__(*args, **kwargs)
        self.fields['last_user'].initial = self.last_user
        self.fields['user_created'].initial = self.user_created
        #self.fields['currency'].initial = self.currency
        #self.fields['currency_ref'].initial = self.currency_ref
        #self.fields['date'].initial = self.date
        
    def clean_last_user(self):
        last_user = self.cleaned_data['last_user']
        if last_user is None:
            last_user = self.last_user
        return last_user
    
    def clean_user_created(self):
        user_created = self.cleaned_data['user_created']
        if user_created is None:
            user_created = self.last_user
        return user_created
    
    def clean_currency(self):
        currency = self.cleaned_data['currency']
        if currency == None:
            return None
        currency_line_no = ParLine.objects.filter(table_no=ptables.PT_6, line_desc = currency)[0].line_no
        return currency_line_no
    
    def clean_currency_ref(self):
        currency_ref = self.cleaned_data['currency_ref']
        if currency_ref == None:
            return None
        currency_ref_line_no = ParLine.objects.filter(table_no=ptables.PT_6, line_desc = currency_ref)[0].line_no
        return currency_ref_line_no
    
    def clean_currency_date(self):
        currency_date = self.cleaned_data['currency_date']
        if currency_date is None:
            currency_date = self.currency_date
        return currency_date


class ExchangesAdvanceSearchParameters(forms.Form):
    #currency      = forms.ModelChoiceField(queryset=ParLine.objects.filter(table_no=ptables.PT_6).order_by('line_num2'), to_field_name="line_no", label="Currency",required=True, error_messages={'required': msg.required_currency}, empty_label=None)
    currency       = forms.ModelChoiceField(queryset=ParLine.objects.filter(table_no=ptables.PT_6, line_num4=1).order_by('line_num2'), to_field_name="line_no", label="Currency", widget=forms.Select(attrs={'class':'form-control choice_field'}),required=False)
    
    currency_date = forms.DateField(
        widget=forms.TextInput(attrs={'class': 'form-control choice_field', 'id': 'id_currency_date'}), required=False)
