from django.shortcuts import render
from django.contrib import messages

from .models import Carousel, Teacher, Student, Notice, Routine, Syllabus, President, Founder
from .models import SHIFTS, SECTIONS, CLASSES

# Create your views here.
def index(request):
    carousel = Carousel.objects.first()
    headmaster = Teacher.objects.filter(is_headmaster=True).first()
    notices = Notice.objects.all()[:3]
    for notice in notices:
        print(notice.updated_at, 'notice')
        print(notice.updated_at.day, notice.updated_at.month)
    context = {
        "carousel": carousel,
        "headmaster": headmaster,
        "notices": notices
    }
    return render(request, "school_management_main/index.html", context)


def about(request):
    headmaster = Teacher.objects.filter(is_headmaster=True).first()
    founder = Founder.objects.first()
    president = President.objects.first()
    context = {
        "headmaster": headmaster,
        "founder": founder,
        "president": president
    }
    return render(request, "school_management_main/about.html", context)


def teachers(request):
    teachers = Teacher.objects.filter(is_headmaster=False).all()
    context = {
        'headmaster': Teacher.objects.filter(is_headmaster=True).first(),
        "teachers": teachers
    }
    return render(request, "school_management_main/teachers.html", context)


def students(request):
    context = {
        "classes": CLASSES,
        "shifts": SHIFTS,
        "sections": SECTIONS
    }
    if request.method == "POST":
        shift = request.POST.get("shift")
        enrolled_class = request.POST.get('class')
        section = request.POST.get('section')
        if shift != 'Select Shift' and enrolled_class != ' Select Class' and section != 'Select Section':
            students = Student.objects.filter(shift=shift, enrolled_class=enrolled_class, section=section)
            if not students:
                messages.warning(request, "No student can be found for your query. If you don't have any shift then select morning and if you don't have any section in your class, please select A")
            context = {
                "students": students,
                "classes": CLASSES,
                "shifts": SHIFTS,
                "sections": SECTIONS
            }
            return render(request, "school_management_main/students.html", context)
        else:
            messages.error(request, 'Please select all fields')

    return render(request, "school_management_main/students.html", context)


def teacher(request, id):
    teacher = Teacher.objects.get(id=id)
    context = {
        "teacher": teacher
    }
    return render(request, "school_management_main/teacher.html", context)


def notices(request):
    notices = Notice.objects.all()
    context = {
        "header": "Notices",
        "bn_header": "বিজ্ঞপ্তি",
        "notices": notices
    }
    return render(request, "school_management_main/notices.html", context)


def routines(request):
    routines = Routine.objects.all()
    context = {
        "header": "Routines",
        "bn_header": "রুটিন",
        "notices": routines
    }
    return render(request, "school_management_main/notices.html", context)


def syllabus(request):
    syllabus = Syllabus.objects.all()
    context = {
        "header": "Syllabus",
        "bn_header": "পাঠ্যক্রম",
        "notices": syllabus
    }
    return render(request, "school_management_main/notices.html", context)