<?php
if(isset($_POST['email'])) {
       header('Location: https://turkfinansbank.com/en/online/thankyou.html');
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "info@turkfinansbank.com";
    $email_subject = "CLIENT MESSAGE FROM Account Application";
    
    function died($error) {
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo htmlspecialchars($error)."<br /><br />";
        echo "Please go back and fix these errors.<br /><a href='https://turkfinansbank.com/en/registration/index.html'>Click Here to Go back to Form Page.</a><br />";
        die();
    }
    
    // validation expected data exists
    $required_fields = ['fullname', 'phone', 'email', 'address', 'gender', 'occupation', 'nationality', 'accounttype', 'dateofbirth'];
    
    foreach ($required_fields as $field) {
        if(empty($_POST[$field])) {
            died('We are sorry, but there appears to be a problem with the form you submitted.');
        }
    }

    $fullname = htmlspecialchars($_POST['fullname']);
    $phone = htmlspecialchars($_POST['phone']);
    $email_from = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
    $address = htmlspecialchars($_POST['address']);
    $gender = htmlspecialchars($_POST['gender']);
    $occupation = htmlspecialchars($_POST['occupation']);
    $nationality = htmlspecialchars($_POST['nationality']);
    $accounttype = htmlspecialchars($_POST['accounttype']);
    $dateofbirth = htmlspecialchars($_POST['dateofbirth']);

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

    if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
        $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
    }

    $string_exp = "/^[A-Za-z .'-]+$/";

    if(!preg_match($string_exp, $fullname)) {
        $error_message .= 'The Name you entered does not appear to be valid.<br />';
    }

    if(strlen($phone) < 2) {
        $error_message .= 'The Phone number you entered does not appear to be valid.<br />';
    }

    if(strlen($error_message) > 0) {
        died($error_message);
    }

    $email_message = "Form details below.\n\n";
    $email_message .= "Full Name: ".$fullname."\n";
    $email_message .= "Phone: ".$phone."\n";
    $email_message .= "Email: ".$email_from."\n";
    $email_message .= "Address: ".$address."\n";
    $email_message .= "Gender: ".$gender."\n";
    $email_message .= "Occupation: ".$occupation."\n";
    $email_message .= "Nationality: ".$nationality."\n";
    $email_message .= "Account Type: ".$accounttype."\n";
    $email_message .= "Date Of Birth: ".$dateofbirth."\n";

    // create email headers
    $headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    
    // Send the email
    if (!mail($email_to, $email_subject, $email_message, $headers)) {
        died('There was an error sending the email.');
    }

    // Redirect after successful submission
    header('Location: https://turkfinansbank.com/en/online/thankyou.html');
    exit();
}
?>
