from django import forms
from .models import country_state,tags, patient,employee,disease,task,treatment,Zone,gender,Master_GST,District_State,Zone_System,Zone_Type,ID_type, User_Group
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit,Layout, ButtonHolder,HTML, Div, Reset,Fieldset
from django.urls import reverse_lazy
from django.utils.safestring import mark_safe
from datetime import datetime
from diagnoses.models import disease_details
from django.db.models import Q, Count
#from .models import patient

from django import forms
# from captcha.fields import CaptchaField

# class form_login_captcha(forms.ModelForm):
#     captcha = CaptchaField()


def get_Zone():
    output_list = []
    for obj_Zone in Zone.objects.all():        output_list.append((obj_Zone.id, str(obj_Zone)))
    return output_list
 
def get_Employ():
    output_list = []
    for obj_Employ in employee.objects.all():        output_list.append((obj_Employ.id, str(obj_Employ)))
    return output_list 
 
 
class form_ID_type(forms.ModelForm):
   def __init__(self, *args, **kwargs): 
      super().__init__(*args, **kwargs)
      self.helper = FormHelper(self)
      self.helper.form_method = "Post" 
      self.helper.add_input(Submit("submit", "Save"))
      self.as_p = True        
   class Meta:
      model = ID_type
      fields = ["name",
                "shrt_txt",
               "field_type",
               "regex_validation","No_of_character"]

class form_ID_type_search(forms.Form):
   def __init__(self, *args, **kwargs): 
      super().__init__(*args, **kwargs)
      self.helper = FormHelper(self)
      self.helper.form_method = "GET" 
      self.helper.add_input(Submit("submit", "Search"))
      self.as_p = True    

   name = forms.CharField(required=False, help_text="Search by proof name",label=mark_safe("<strong> Proof Name </strong>"))
   field_type = forms.ChoiceField(required=False, help_text="Search by state",label=mark_safe("<strong> State </strong>"),choices=ID_type.list_type)     





class form_District_State(forms.ModelForm):
   def __init__(self, *args, **kwargs): 
      super().__init__(*args, **kwargs)
      self.helper = FormHelper(self)
      self.helper.form_method = "Post" 
      self.helper.add_input(Submit("submit", "Save"))
      self.as_p = True        
   class Meta:
      model = District_State
      fields = ["District",
               "State"]

class form_District_State_search(forms.Form):
   def __init__(self, *args, **kwargs): 
      super().__init__(*args, **kwargs)
      self.helper = FormHelper(self)
      self.helper.form_method = "GET" 
      self.helper.add_input(Submit("submit", "Search"))
      self.as_p = True    

   District = forms.CharField(required=False, help_text="Search by district",label=mark_safe("<strong> District </strong>"))
   State = forms.CharField(required=False,max_length=75, help_text="Search by state",label=mark_safe("<strong> State </strong>"))     



class form_Zone_System(forms.ModelForm):
   def __init__(self, *args, **kwargs): 
      super().__init__(*args, **kwargs)
      self.helper = FormHelper(self)
      self.helper.form_method = "Post" 
      self.helper.add_input(Submit("submit", "Save"))
      self.as_p = True        
   class Meta:
      model = Zone_System
      fields = ["System",
      ]

class form_Zone_System_search(forms.Form):
   def __init__(self, *args, **kwargs): 
      super().__init__(*args, **kwargs)
      self.helper = FormHelper(self)
      self.helper.form_method = "GET" 
      self.helper.add_input(Submit("submit", "Search"))
      self.as_p = True    

   System = forms.CharField(required=False, help_text="Search by zone system",label=mark_safe("<strong> Zone System </strong>"))
 

class form_Zone_Type(forms.ModelForm):
   def __init__(self, *args, **kwargs): 
      super().__init__(*args, **kwargs)
      self.helper = FormHelper(self)
      self.helper.form_method = "Post" 
      self.helper.add_input(Submit("submit", "Save"))
      self.as_p = True        
   class Meta:
      model = Zone_Type
      fields = ["Ztype",
               ]

class form_Zone_Type_search(forms.Form):
   def __init__(self, *args, **kwargs): 
      super().__init__(*args, **kwargs)
      self.helper = FormHelper(self)
      self.helper.form_method = "GET" 
      self.helper.add_input(Submit("submit", "Search"))
      self.as_p = True    

   Ztype = forms.CharField(required=False, help_text="Search by zone type",label=mark_safe("<strong> Zone Type </strong>"))

        
class form_tags(forms.ModelForm):
   def __init__(self, *args, **kwargs): 
      super().__init__(*args, **kwargs)
      self.helper = FormHelper(self)
      self.helper.form_method = "Post" 
      self.helper.add_input(Submit("submit", "Add New/Update Tag"))
      self.as_p = True     
   class Meta:
      model = tags
      fields = ["tag_name",
               "tag_description",
               "tag_color"]
class form_tags_search(forms.Form):
   def __init__(self, *args, **kwargs): 
      super().__init__(*args, **kwargs)
      self.helper = FormHelper(self)
      self.helper.form_method = "GET" 
      self.helper.add_input(Submit("submit", "Search"))
      self.as_p = True        
   tag_name = forms.CharField(required=False,max_length=75, help_text="Enter your tag name",label="Tag Name") 
   tag_description = forms.CharField(required=False,max_length=250,help_text="Enter a tag description",label="Tag Description")
   tag_color = forms.CharField(required=False,max_length=10,help_text="Enter your tag colour",label="Tag Color")


class form_gender(forms.ModelForm):
   def __init__(self, *args, **kwargs): 
      super().__init__(*args, **kwargs)
      self.helper = FormHelper(self)
      self.helper.form_method = "Post" 
      self.helper.add_input(Submit("submit","Save"))
      self.as_p = True     
   class Meta:
      model = gender
      fields = ["gender_name"]

class form_gender_search(forms.Form):
   def __init__(self, *args, **kwargs): 
      super().__init__(*args, **kwargs)
      self.helper = FormHelper(self)
      self.helper.form_method = "GET" 
      self.helper.add_input(Submit("submit", "Search"))
      self.as_p = True        
   gender_name = forms.CharField(required=False,max_length=75, help_text="Search by gender",label=mark_safe("<strong> Gender </strong>") )
  




class form_patient(forms.ModelForm):
      def __init__(self, *args, **kwargs): 
         super().__init__(*args, **kwargs)
         self.helper = FormHelper(self)
         self.helper.form_method = "Post" 
         self.helper.add_input(Submit("submit", "Save"))
         
         self.as_p = True          
      class Meta:
         model = patient
         fields = ["profile_pic",               "Title",                "Patient_name",              "last_name",
                  "fk_gender",                  "DoB",                  "Address1",                  "locality",
                  "city",                       "Pincode",              "fk_dist",                   "proof",
                  'proof_number',               "email",                "phone_number",              "alt_phone_number",]
                      
class form_patient_search(forms.Form):
      def __init__(self, *args, **kwargs): 
         super().__init__(*args, **kwargs)
         self.helper = FormHelper(self)
         self.helper.form_method = "GET" 
         self.helper.add_input(Submit("submit", "Search"))
         self.as_p = True 
      Patient_name =  forms.CharField(required=False,max_length=75, help_text="Search by patient name",label=mark_safe("<strong> Patient Name </strong>"))
      fk_gender =  forms.ChoiceField(required=False,label=mark_safe("<strong> Gender </strong>"),help_text="Search by gender",choices=[(0,"Not Selected")] +list(gender.objects.all().values_list("id","gender_name"))) 
      city = forms.CharField(required=False,max_length=75, help_text="Search by city",label=mark_safe("<strong> City </strong>"))
      Pincode = forms.IntegerField(required=False,widget=forms.NumberInput, help_text="Search by pincode",label=mark_safe("<strong> Pincode </strong>"))
      fk_dist =  forms.ChoiceField(required=False, help_text="Search by district",label=mark_safe("<strong> District </strong>"),choices= [(0,"Not Selected")] + list(District_State.objects.all().values_list("id","District")))  
      email = forms.EmailField(required=False,max_length=75, help_text="Search by email id",label=mark_safe("<strong> Email </strong>"))
      phone_number = forms.IntegerField(required=False,widget=forms.NumberInput, help_text="Search by phone number",label=mark_safe("<strong> Phone Number </strong>"))
      fk_zone =  forms.MultipleChoiceField(required=False,label=mark_safe("<strong> Zone </strong>"),help_text="Search by zone",choices=get_Zone()) #[(0,"Not Selected")] +
            
       


class form_employee(forms.ModelForm):
      def __init__(self, *args, **kwargs): 
         super().__init__(*args, **kwargs)
         self.helper = FormHelper(self)
         self.helper.form_method = "Post" 
         self.helper.add_input(Submit("submit", "Save"))
         self.as_p = True          
      class Meta:
         model = employee
         fields = ["profile_pic",
                  "Title",
                  "employee_name",
                  "last_name",
                  "fk_gender",
                  "DoB",
                  "education_qualification",
                  "Address1",
                  "locality",
                  "city",
                  "Pincode",
                  "fk_dist",
                  #"State",
                  "email",
                  "phone_number",
                  "alt_phone_number",
                  'fk_group',
                  "str_username",
                  "fk_zone"]
                      
class form_employee_search(forms.Form):
      def __init__(self, *args, **kwargs): 
         super().__init__(*args, **kwargs)
         self.helper = FormHelper(self)
         self.helper.form_method = "GET" 
         self.helper.add_input(Submit("submit", "Search"))
         self.as_p = True 
                      
      employee_name =  forms.CharField(required=False,max_length=75, help_text="Search by employee name",label=mark_safe("<strong> Employee Name </strong>"))
      city = forms.CharField(required=False,max_length=75, help_text="Search by city",label=mark_safe("<strong> City </strong>"))

      fk_gender =  forms.ChoiceField(required=False,label=mark_safe("<strong> Gender </strong>"),help_text="Search by gender",choices=[(0,"Not Selected")] +list(gender.objects.all().values_list("id","gender_name"))) 

      fk_dist =  forms.ChoiceField(required=False, help_text="Search by district",label=mark_safe("<strong> District </strong>"),choices= [(0,"Not Selected")] + list(District_State.objects.all().values_list("id","District")))     
      #State = forms.CharField(required=False,max_length=75, help_text="Search by state",label=mark_safe("<strong> State </strong>"))
      email = forms.EmailField(required=False,max_length=75, help_text="Search by email id",label=mark_safe("<strong >Email </strong>"))
      phone_number = forms.IntegerField(required=False,widget=forms.NumberInput, help_text="Search by phone number",label=mark_safe("<strong> Phone Number </strong>"))
      alt_phone_number = forms.IntegerField(required=False,widget=forms.NumberInput, help_text="Search by alternative phone number",label=mark_safe("<strong> Alternative Phone Number </strong>"))
      #fk_zone =  forms.ChoiceField(required=False,label=mark_safe("<strong> Zone </strong>"),help_text="Search by zone",choices=[(0,"Not Selected")] + list(Zone.objects.all().values_list("id","center"))) #insert(0,"not selected")
      fk_zone =  forms.MultipleChoiceField(required=False,label=mark_safe("<strong> Zone </strong>"),help_text="Search by zone",choices=get_Zone()) #[(0,"Not Selected")] +

            


class form_disease(forms.ModelForm):
      def __init__(self, *args, **kwargs): 
         super().__init__(*args, **kwargs)
         self.helper = FormHelper(self)
         self.helper.form_method = "Post" 
         self.helper.add_input(Submit("submit", "Save"))
         self.as_p = True          
      class Meta:
         model = disease
         fields = ["disease_name",
                  "disease_name_tamil",
                  "fk_system"]


class form_disease_search(forms.ModelForm):
      def __init__(self, *args, **kwargs): 
         super().__init__(*args, **kwargs)
         self.helper = FormHelper(self)
         self.helper.form_method = "GET" 
         self.helper.add_input(Submit("submit", "Search"))
         self.as_p = True 
      class Meta:
         model = disease
         fields = ["disease_name" ]                 
      disease_name =  forms.CharField(required=False,max_length=200, help_text="Search by disease name",label=mark_safe("<strong> Disease Name </strong>"))
      fk_System =  forms.ChoiceField(required=False, help_text="Search by system",label=mark_safe("<strong> System </strong>"),choices= [(0,"Not Selected")] + list(Zone_System.objects.all().values_list("id","System")))     


class form_zone(forms.ModelForm):
      def __init__(self, *args, **kwargs): 
         super().__init__(*args, **kwargs)
         self.helper = FormHelper(self)
         self.helper.form_method = "Post" 
         self.helper.add_input(Submit("submit", "Save"))
         self.as_p = True          
      class Meta:
         model = Zone
         fields = ["center",
                  'fk_System', 'fk_Type',
                  'fk_District']


class form_zone_search(forms.Form):
      def __init__(self, *args, **kwargs): 
         super().__init__(*args, **kwargs)
         self.helper = FormHelper(self)
         self.helper.form_method = "GET" 
         self.helper.add_input(Submit("submit", "Search"))
         self.as_p = True 

      center =  forms.CharField(required=False,max_length=200, help_text="Search by center",label=mark_safe("<strong> Center  </strong>"))
      fk_System =  forms.ChoiceField(required=False, help_text="Search by system",label=mark_safe("<strong> System </strong>"),choices= [(0,"Not Selected")] + list(Zone_System.objects.all().values_list("id","System")))     
      fk_Type =  forms.ChoiceField(required=False,help_text="Search by type",label=mark_safe("<strong> Type </strong>"),choices= [(0,"Not Selected")] + list(Zone_Type.objects.all().values_list("id","Ztype")))
      fk_District =  forms.ChoiceField(required=False, help_text="Search by district",label=mark_safe("<strong> District </strong>"),choices= [(0,"Not Selected")] + list(District_State.objects.all().values_list("id","District")))       
    


class form_task(forms.ModelForm):
      def __init__(self, *args, **kwargs): 
         super().__init__(*args, **kwargs)
         self.helper = FormHelper(self)
         self.helper.form_method = "Post" 
         self.helper.add_input(Submit("submit", "Save"))
         self.as_p = True          
      class Meta:
         model = task
         fields = ["task_type",
                  "task_details" ,
                    "applyto_employee"]


class form_task_search(forms.Form):
      def __init__(self, *args, **kwargs): 
         super().__init__(*args, **kwargs)
         self.helper = FormHelper(self)
         self.helper.form_method = "GET" 
         self.helper.add_layout(Div())         
         self.helper.add_input(Submit("submit", "Search"))
         self.as_p = True 
         

            
      task_type = forms.ChoiceField(required=False,label=mark_safe("<strong> Task Type </strong>"),help_text="Search by task type", choices=[(0,"Not Selected")] + list(task.objects.all().values_list("task_type","task_type"))) 
      task_details = forms.CharField(required=False,max_length=250,help_text="Search by task details",label=mark_safe("<strong> Task Details </strong>"))
      applyto_employee = forms.ChoiceField(required=False,label=mark_safe("<strong> Employee </strong>"),help_text="Search by employee", choices= [(0,"Not Selected")] + list(employee.objects.all().values_list("id","str_username")))            
    
class form_treatment(forms.ModelForm):
      def __init__(self, *args, **kwargs): 
         super().__init__(*args, **kwargs)
         self.helper = FormHelper(self)
         self.helper.form_method = "Post" 
         self.helper.add_input(Submit("submit", "Save"))
         self.as_p = True          
      class Meta:
         model = treatment
         fields = ["treatment_name",
                  "details"]


class form_treatment_search(forms.ModelForm):
      def __init__(self, *args, **kwargs): 
         super().__init__(*args, **kwargs)
         self.helper = FormHelper(self)
         self.helper.form_method = "GET" 
         self.helper.add_input(Submit("submit", "Search"))
         self.as_p = True    

      class Meta:
         model = treatment
         fields = ["treatment_name",
                  "details"]

class form_Master_GST(forms.ModelForm):
   def __init__(self, *args, **kwargs): 
      super().__init__(*args, **kwargs)
      self.helper = FormHelper(self)
      self.helper.form_method = "Post" 
      self.helper.add_input(Submit("submit","Save"))
      self.as_p = True     

   class Meta:
      model = Master_GST
      fields = ["gst_shorttext","gst_longtext"]#,"gst_amount","igst_amount","sgst_amount"]

class form_Master_GST_search(forms.Form):
   def __init__(self, *args, **kwargs): 
      super().__init__(*args, **kwargs)
      self.helper = FormHelper(self)
      self.helper.form_method = "GET" 
      self.helper.add_input(Submit("submit", "Search"))
      self.as_p = True  

   gst_shorttext = forms.CharField(required=False,max_length=75, help_text="Search by short Description",label=mark_safe("<strong> Short Description </strong>") )

   gst_longtext = forms.CharField(required=False,max_length=75, help_text="Search by long Description",label=mark_safe("<strong> Long Description </strong>") )

   gst_amount = forms.FloatField(widget=forms.NumberInput,required=False, help_text="Search by amount ",label=mark_safe('<strong> NET GST% </strong>')) 

  




def get_month_year():
    list_data = list(disease_details.objects.all().values("date_modified__year","date_modified__month").annotate( total= Count("pk")) )
    list_output = []
    for dic_data in reversed(list_data) :
       dic_output = (str(dic_data["date_modified__month"]) + "_" + str(dic_data["date_modified__year"]) , str(dic_data["date_modified__month"]) + " - " + str(dic_data["date_modified__year"]) )#+ " -  Total : " + str(dic_data["total"]) 
       #list_output.append(`str(dic_data["date_modified__month"]) + " - " + str(dic_data["date_modified__year"]) + " -  Total : " + str(dic_data["total"]) )  
       list_output.append(dic_output)
    #list_output.append(('ALL','ALL'))
    return tuple(list_output)
    

class form_report_base(forms.Form):
      def __init__(self, *args, **kwargs): 
         super().__init__(*args, **kwargs)
         self.helper = FormHelper(self)
         self.helper.form_method = "GET" 
         self.helper.add_layout(Div())     
         self.helper.add_input(Submit("submit", "Search"))    
         self.as_p = True  
      district =  forms.MultipleChoiceField(required=False,label=mark_safe("<strong> District </strong>"),help_text="Search by District",choices=list(District_State.objects.all().values_list("id","District"))) #[(0,"Not Selected")] +
      zone =  forms.MultipleChoiceField(required=False,label=mark_safe("<strong> Center/PHC </strong>"),help_text="Search by zone",choices=get_Zone()) #[(0,"Not Selected")] +
      try:         report_period = forms.ChoiceField(required=False,label=mark_safe("<strong> Report Period </strong>"),help_text="Select a Report Period", choices=get_month_year(), initial=get_month_year()[0][0])
      except: pass 
      
class form_report_stock_check(forms.Form):
      def __init__(self, *args, **kwargs): 
         super().__init__(*args, **kwargs)
         self.helper = FormHelper(self)
         self.helper.form_method = "GET" 
         self.helper.add_layout(Div())     
         self.helper.add_input(Submit("submit", "Search"))    
         self.as_p = True  
      district =  forms.MultipleChoiceField(required=False,label=mark_safe("<strong> District </strong>"),help_text="Search by District",choices=list(District_State.objects.all().values_list("id","District"))) #[(0,"Not Selected")] +
      zone =  forms.MultipleChoiceField(required=False,label=mark_safe("<strong> Center/PHC </strong>"),help_text="Search by zone",choices=get_Zone()) #[(0,"Not Selected")] +
    
      
      # Level zone or 

class form_report_employ(form_report_base):
      employee = forms.MultipleChoiceField(required=False,label=mark_safe("<strong> Employee (User Name) </strong>"),help_text="Search by Employee UserName",choices=get_Employ())
      
class form_User_Group(forms.ModelForm):
   def __init__(self, *args, **kwargs): 
      super().__init__(*args, **kwargs)
      self.helper = FormHelper(self)
      self.helper.form_method = "Post" 
      self.helper.add_input(Submit("submit","Save"))
      self.as_p = True     

   class Meta:
      model = User_Group
      fields = ["name","short_notes","description"]

class form_User_Group_search(forms.Form):
   def __init__(self, *args, **kwargs): 
      super().__init__(*args, **kwargs)
      self.helper = FormHelper(self)
      self.helper.form_method = "GET" 
      self.helper.add_input(Submit("submit", "Search"))
      self.as_p = True  

   name = forms.CharField(required=False,max_length=75, help_text="Search by short User Group",label=mark_safe("<strong> User Group Name </strong>") )
   short_notes = forms.CharField(required=False,max_length=75, help_text="Search by User Group Description",label=mark_safe("<strong> Short Notes </strong>") )
   #description = forms.CharField(widget=forms.Textarea,required=False, help_text="Search by amount ",label=mark_safe('<strong> NET GST% </strong>')) 

  
