from django.shortcuts import render, redirect
from datetime import date
from django.core.mail import BadHeaderError, EmailMessage
from django.http import HttpResponse

from .forms import AvisLocataireForm
from .models import AvisLocataire


# Create your views here.
def form_avis_locataire_view(request):
    head_title = "Contact"
    month = date.today().month
    year = date.today().year

    form = AvisLocataireForm()
    if request.method == "POST":
        form = AvisLocataireForm(request.POST or None)
        if form.is_valid():
            form.save()
            nom = form.cleaned_data['nom']
            prenom = form.cleaned_data['prenom']
            email = form.cleaned_data['email']
            note = form.cleaned_data['note']
            message = form.cleaned_data['message']

            email_body = """
                <html>
                <head></head>
                <body>
                    <div style="font-size:20px; font-weight:bold; text-decoration:underline; margin-bottom:20px">Formulaire : votre avis nous intéresse</div>
                    <div style="display:flex; flex-wrap:nowrap; margin-bottom:10px">
                        <div style="font-size:14px; font-weight:bold;">Nom :&nbsp;</div><div style="font-size:14px">%s</div>
                    </div>
                    <div style="display:flex; flex-wrap:nowrap; margin-bottom:10px">
                        <div style="font-size:14px; font-weight:bold;">Prénom :&nbsp;</div><div style="font-size:14px">%s</div>
                    </div>
                    <div style="display:flex; flex-wrap:nowrap; margin-bottom:10px">
                        <div style="font-size:14px; font-weight:bold;">Email :&nbsp;</div><div style="font-size:14px">%s</div>
                    </div>
                    <div style="display:flex; flex-wrap:nowrap; margin-bottom:10px">
                        <div style="font-size:14px; font-weight:bold;">note :&nbsp;</div><div style="font-size:14px">%s</div>
                    </div>
                    <div style="display:flex; flex-wrap:nowrap; margin-bottom:10px">
                        <div style="font-size:14px; font-weight:bold;">Message :&nbsp;</div><div style="font-size:14px">%s</div>
                    </div>
                    </div>
                </body>
                </html>
                """ % (nom, prenom, email, note, message)

            sujet_message = "Votre avis sur votre location"

            try:
                demand = EmailMessage(sujet_message, email_body, 'no-reply@ile-maurice-location.com', to=['christianbab1@orange.fr'], bcc=['contact@marzat-informatique.com'])
                demand.content_subtype = "html" # this is the crucial part
                demand.send()
                demand = EmailMessage(sujet_message, email_body, 'no-reply@ile-maurice-location.com', to=[email])
                demand.content_subtype = "html" # this is the crucial part
                demand.send()
            except BadHeaderError:
                return HttpResponse('Invalid header found.')
            return redirect('avis_success')
        
    return render(request, "avis_locataire_form.html", {
        'form': form, 
        'head_title': head_title,
        'year': year,
        'month': month,
    })


def avis_success_view(request):
    head_title = "Avis locataire : email envoyé"
    month = date.today().month
    year = date.today().year
    
    return render(request, "avis_success.html", {
        'head_title': head_title,
        'year': year,
        'month': month,
    })


def all_avis_view(request):
    head_title = "Tous les avis des locatairesé"
    month = date.today().month
    year = date.today().year

    list_avis = AvisLocataire.objects.filter(afficher='Oui').order_by('-date_avis')

    return render(request, "all_avis.html", {
        'head_title': head_title,
        'year': year,
        'month': month,
        'list_avis': list_avis,
    })