# contact/views.py

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


def contactView(request):
    head_title = "Contact"
    month = date.today().month
    year = date.today().year

    form = ContactForm()
    if request.method == "POST":
        form = ContactForm(request.POST or None)
        if form.is_valid():
            form.save()
            nom = form.cleaned_data['nom']
            prenom = form.cleaned_data['prenom']
            telephone = form.cleaned_data['telephone']
            email = form.cleaned_data['email']
            sujet = form.cleaned_data['sujet']
            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 de demande d'informations</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;">Téléphone :&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;">Sujet :&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, telephone, email, sujet, message)

            sujet_message = "Demande d'informations Site Internet www.ile-maurice-location.com"

            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('success')
        
    return render(request, "contact.html", {
        'form': form, 
        'head_title': head_title,
        'year': year,
        'month': month,
    })

def successView(request):
    head_title = "Contact : email envoyé"
    month = date.today().month
    year = date.today().year
    
    return render(request, "success.html", {
        'head_title': head_title,
        'year': year,
        'month': month,
    })