# -*- 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
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.Form):
	startDate = forms.DateField(label="Fecha de Inicio", widget=forms.DateInput(attrs={'type':'date', 'class':"form-control"}), required=False)
	endDate = forms.DateField(label="Fecha de Fin", widget=forms.DateInput(attrs={'type':'date', 'class':"form-control"}), required=False)

#	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 ] 
#	publishers = forms.MultipleChoiceField(label="Fuentes", widget=forms.SelectMultiple(attrs={'class':"form-control"}), choices=choice, required=False)

	publishers = forms.MultipleChoiceField(label="Fuentes", widget=forms.SelectMultiple(attrs={'class':"form-control"}), choices=[], required=False)
	text = forms. CharField(label="Palabras Clave", widget=forms.TextInput(attrs={'class':"form-control", "placeholder":"Palabras clave"}), required=False)

	def __init__(self, *args, **kwargs):
		super(SearchForm, 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['publishers'] = forms.MultipleChoiceField(label="Fuentes", widget=forms.SelectMultiple(attrs={'class':"form-control"}), choices=choice, required=False)

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',)
