# contact/views.py

from django.core.mail import send_mail, BadHeaderError, EmailMessage
from django.http import HttpResponse
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']
            email = form.cleaned_data['email']
            club = form.cleaned_data['club']
            phone_number = form.cleaned_data['phone_number']
            sujet = form.cleaned_data['sujet']
            message = form.cleaned_data['message']
            message = message.replace('\n', '<br />')

            email_body = """
                <html>
                <head></head>
                <body>
                    <div style="font-size:20px; font-weight:bold; text-decoration:underline; margin-bottom:20px">Formulaire de contact du site www.inscription-district_pc.fr</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;">Club :&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;">Message :&nbsp;</div>
                    </div>
                    <div style="font-size:14px">%s</div>
                </body>
                </html>
                """ % (nom, prenom, email, club, phone_number, message)

            try:
                demand = EmailMessage(sujet, email_body, 'no-reply@inscription-district-pc.fr', to=['contact@marzat-informatique.com', 'frederic.marzat@orange.fr'], cc=[email])
                demand.content_subtype = "html" # this is the crucial part
                demand.send()
                demand = EmailMessage(sujet, email_body, 'no-reply@inscription-district-pc.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})
