# -*- coding: utf-8 -*-
from django import forms
#from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from catalog.models import Publisher, News, Profile, Search
from django.db.models import Q
from django.contrib.admin.widgets import FilteredSelectMultiple
import os
from django.conf import settings
from django.urls import reverse
#-------------------------------------------------------------------------------
class SearchForm(forms.ModelForm):
	class Meta:
		model = Search
		exclude = ["user"]

		widgets = {'startDate':  forms.DateInput(attrs={'type':'date', 'class':"form-control"}),
			'endDate':  forms.DateInput(attrs={'type':'date', 'class':"form-control"}),
			'text':forms.TextInput(attrs={'class':"form-control", "placeholder":"Palabras clave"}),
			'publishers':forms.SelectMultiple(attrs={'class':"form-control"}),
			'user': forms.HiddenInput(),
			}
		labels = {'startDate':"Fecha de Inicio", 'endDate':"Fecha de Fin", 'text': "Palabras Clave", 'publishers':"Fuentes",}

	def save(self, user):
		form = super(SearchForm, self).save(commit=False)
		form.user = user
		form.save()

#-------------------------------------------------------------------------------
class ProfileForm(forms.Form):
	subscriptions = forms.ModelMultipleChoiceField(queryset=Publisher.objects.all(), widget=FilteredSelectMultiple("Publishers", is_stacked=False), required=False)
	class Media:
		# Django also includes a few javascript files necessary
		# for the operation of this form element. You need to
		# include <script src="/admin/jsi18n"></script>
		# in the temple
		css = {'all': (os.path.join(settings.BASE_DIR, '/static/admin/css/widgets.css'),),}
		js = ('/catalog/js/jsi18n',)
#-------------------------------------------------------------------------------
class SubscriptionsForm(forms.ModelForm):

#	def __init__(self, *args, **kwargs):
#		super(SubscriptionsForm, self).__init__(*args, **kwargs)
#		news = News.objects.all()
#		publishersList = Publisher.objects.all().filter( Q(id__in = news.values('publisher').distinct()))
#		choice =  [ (r.id,r.name) for r in publishersList ]
#		self.fields['subscriptions'].choices = []

	class Meta:
		model = Profile
		fields = ('subscriptions',)
#		field_classes = {'subscriptions': forms.ModelMultipleChoiceField,}
		widgets = {'subscriptions':  FilteredSelectMultiple("Publishers", is_stacked=False),}
	class Media:
                # Django also includes a few javascript files necessary
                # for the operation of this form element. You need to
                # include <script src="/admin/jsi18n"></script>
                # in the temple
                css = {'all': (os.path.join(settings.BASE_DIR, '/static/admin/css/widgets.css'),),}
                js = ('/catalog/js/jsi18n')



class UserEditForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'email')
        widgets = {
            'first_name': forms.TextInput(attrs={'class': 'form-control'}),
            'last_name': forms.TextInput(attrs={'class': 'form-control'}),
            'email': forms.EmailInput(attrs={'class': 'form-control'}),
        }
