<?php
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST['BTEnvia'])) {
    // Configurações
    $emailRemetente = "contato@agenciasense.com.br";
    $emailDestinatario = "mayconrizzardo.sense@gmail.com";
    $nomeEmpresa = "$nomeEmpresa";
    $url_envio = "agradecimento.php";

    // Sanitização dos campos
    $nome = trim($_POST['nome'] ?? '');
    $email = filter_var(trim($_POST['email'] ?? ''), FILTER_SANITIZE_EMAIL);
    $telefone = trim($_POST['telefone'] ?? '');
    $servico = trim($_POST['servico'] ?? '');
    $assunto = trim($_POST['assunto'] ?? '');

    // Validação simples
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        exit('<b>E-mail inválido.</b>');
    }

    // reCAPTCHA
    $recaptchaSecret = 'Key Chace SECRETAA';
    $recaptchaResponse = $_POST['g-recaptcha-response'] ?? '';

    $recaptchaData = [
        'secret'   => $recaptchaSecret,
        'response' => $recaptchaResponse,
        'remoteip' => $_SERVER['REMOTE_ADDR'] ?? ''
    ];

    $context = stream_context_create([
        'http' => [
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query($recaptchaData),
        ]
    ]);

    $result = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context);
    $recaptcha = json_decode($result);

    if ($recaptcha && $recaptcha->success) {
        $email_conteudo = "Nome: {$nome}\n";
        $email_conteudo .= "Telefone: {$telefone}\n";
        $email_conteudo .= "Email: {$email}\n";
        $email_conteudo .= "Serviço: {$servico}\n";
        $email_conteudo .= "Assunto: {$assunto}\n";

        $headers = "From: {$emailRemetente}\r\n";
        $headers .= "Reply-To: {$email}\r\n";
        $headers .= "Return-Path: {$emailRemetente}\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "X-Priority: 3\r\n";
        $headers .= "Content-Type: text/html; charset=UTF-8\r\n";

        if (mail($emailDestinatario, "Contato via Site - {$nomeEmpresa}", nl2br($email_conteudo), $headers)) {
            echo "<b>E-Mail enviado com sucesso!</b>";
        } else {
            echo "<b>Falha no envio do E-Mail.</b>";
        }

        echo "<script>window.location.href='$url_envio';</script>";
        exit;
    } else {
        echo "<b>reCAPTCHA inválido. Por favor, tente novamente.</b>";
    }
}
?>



<form class="book-taxi-form" method="POST"> 
    <span>
        <input placeholder="Seu nome *" type="text" size="30" name="nome" required>
    </span>

    <span>
         <input placeholder="Celular/WhatsApp *" size="35" name="telefone" type="tel" maxlength="15" onkeyup="handlePhone(event)" required>
    </span>
    
    <span>
        <input placeholder="Seu e-mail *" type="text" size="30" name="email" required>
    </span>
    
    <span>
        <input placeholder="Assunto *" type="text" size="30" name="assunto" required>
    </span>

    <span>
        <select name="servico" required>
            <option value="" disabled selected>Selecione o Serviço...</option>
            <option value="Serviço 1">Serviço 1</option>
            <option value="Serviço 2">Serviço 2</option>
            <option value="Serviço 3">Serviço 3</option>
        </select>
    </span>



    <div class="g-recaptcha" data-sitekey="Key Chace SITE reCapcha"></div>

    <button class="metrica-envio" type="submit" name="BTEnvia" value="Enviar">Enviar Mensagem</button>
</form>

<script src="https://www.google.com/recaptcha/api.js" async defer></script>
