# sendemail/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

def contactView(request):
    head_title = "Contact"
    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']
            societe = form.cleaned_data['societe']
            sujet = form.cleaned_data['sujet']
            email = form.cleaned_data['email']
            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">Demande d'informations Foire Exposition Barbezieux</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;">Société :&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;">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;">Message :&nbsp;</div><div style="font-size:14px">%s</div>
                    </div>

                </body>
                </html>
                """ % (nom, prenom, societe, sujet, email, message)

            sujet_email = "Demade d'informations Foire Exposition Barbezieux"

            try:
                demand = EmailMessage(sujet_email, email_body, 'no-reply@foire-exposition-barbezieux.fr', to=['contact@marzat-informatique.com'])
                demand.content_subtype = "html" # this is the crucial part
                demand.send()
                demand = EmailMessage(sujet_email, email_body, 'no-reply@foire-exposition-barbezieux.fr', 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})

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