from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from BasicData.models import *
from .forms import *
from BasicData.custom_fonctions import *
from django.contrib import messages
from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.forms import PasswordChangeForm
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger


@login_required(login_url='BasicData:login')
def president_home(request):
    # ROLE
    user = Enseignant.objects.get(email=request.user.email)

    ############ new Enseignant Ufr ############
    # On recupere l'UFR
    UFR = None
    if user:
        UFR = user.ufr
    ############################################
    # ON RECUPERE LES UFR
    ufrs = Ufr.objects.all()

    # ON RECUPERE LES ENSEIGNANTS
    annees = Annee.objects.all().order_by('-id')
    niveaux = Niveau.objects.all()
    anUniv = Annee.objects.last()
    niveau = Niveau.objects.first().id

    # FILTRE FORM
    niveau = 1
    formFil = PvFilterForm()
    if request.method == 'POST':
        if 'AnneeFilter' in request.POST:
            formFil = PvFilterForm(request.POST)
            if formFil.is_valid():
                cd = formFil.cleaned_data
                anUniv = Annee.objects.get(id=cd.get('annee'))
                niveau = cd.get('niveau')

    # ETUDIANT PAR UFR/ ENSEIGNANTS
    EnseignantList = [len(Enseignant.objects.filter(ufr=ufr)) for ufr in ufrs]
    TotalEnseignant= sum(EnseignantList)
    EtudiantsList = [len(NiveauEtudiant.objects.filter(etudiant__ufr=ufr, annee=anUniv)) for ufr in ufrs]
    TotalEtudiant = sum(EtudiantsList)
    zipdataET = list(zip(ufrs,EtudiantsList))
    zipdataEN = list(zip(ufrs,EnseignantList))

    context = {
        'user':user,
        'ufrs':ufrs,
        'TotalEnseignant':TotalEnseignant,
        'zipdata':zipdataET,
        'zipdataEN':zipdataEN,
        #'stat_zip_data':stat_zip_data,
        'anUniv': anUniv,
        'annees': annees,
        'niveaux': niveaux,
        'niveau': Niveau.objects.get(id=niveau),
        'formFil': formFil,
        'TotalEtudiant': TotalEtudiant,
    }
    return render(request,'president/pr_home.html',context)


@login_required(login_url='BasicData:login')
def pr_manage_password(request):
    show_form = 'show'
    if request.method == 'POST':
        form = PasswordChangeForm(request.user, request.POST)
        if form.is_valid():
            user = form.save()
            cd = form.cleaned_data
            an = cd.get('old_password')
            update_session_auth_hash(request, user)  # Important!
            messages.success(request, 'Votre mot de passe a bien été modifié!')
            show_form ='hide'
        else:
            messages.error(request, 'Verifiez les champs')
    else:
        form = PasswordChangeForm(request.user)
    context = {'form':form,'show_form':show_form,'AnUniv': Annee.objects.last()}
    return render(request,'president/pr_change_password.html',context)


@login_required(login_url='BasicData:login')
def pr_enseignant_list(request,ufr,an):

    # ON RECUPERE LES ENSEIGNANTS DE L'UFR
    enseignants = Enseignant.objects.filter(ufr__id=ufr).order_by('nom')

    context={
        'enseignants':enseignants,
        'anUniv': Annee.objects.get(id=an),
        'UFR': Ufr.objects.get(id=ufr)
    }
    return render(request,'president/pr_enseignant_list.html',context)


@login_required(login_url='BasicData:login')
def pr_etudiant_list(request,ufr,an):
    # ROLE
    user = Enseignant.objects.get(email=request.user.email)

    # ON RECUPERE L'UFR ET L'ANNEE
    UFR = Ufr.objects.get(id=ufr)

    # ON RECUPERE LES ANNEES ET LES NIVEAUX
    annees = Annee.objects.all().order_by('-id')
    niveaux = Niveau.objects.all()

    niveau = 1
    formFil = PvFilterForm()
    if request.method == 'POST':
        if 'AnneeFilter' in request.POST:
            formFil = PvFilterForm(request.POST)
            if formFil.is_valid():
                cd = formFil.cleaned_data
                #anUniv = Annee.objects.get(id=cd.get('annee'))
                niveau = cd.get('niveau')

    # ON RECUPERE LES ETUDIANTS DE L'UFR
    etudiants = [ etudNIV.etudiant for etudNIV in NiveauEtudiant.objects.filter(annee__id=an, etudiant__ufr=UFR,niveau__id=niveau) ]

    context={
        'user': user,
        'UFR':UFR,
        'etudiant_list':etudiants,
        'annees':annees,
        'niveaux':niveaux,
        'niveau':Niveau.objects.get(id=niveau),
        'anUniv': Annee.objects.get(id=an),
    }
    return render(request,'president/pr_etudiant_list.html',context)
