<?php
include(__DIR__.'/vendor/autoload.php');
$context = Timber::get_context();
$post = new TimberPost();
$context['post'] = $post;

//~ $site->CONTACT_FORM = dirname( __FILE__ ).'/templates/solicita-presupuesto.html';

$compiled = Timber::compile( CONTACT_FORM, $context );
$subject_prefix = '[Presupuesto] ';

$form = new Gregwar\Formidable\Form($compiled);
$form->setLanguage(new Gregwar\Formidable\Language\Spanish);
$messages = array();
if ($form->posted()) {
    if (count($form->check()) == 0)  {
        $subject = "";
        $body = "\r\n";
        $messages = ["Gracias por contactar!"];
        $to = 'hola@estudionexos.com';
		$subject = $subject_prefix.$form->subject;
		$body = $body.$form->message."\r\n\r\n";
		$body = $body.'Nombre'.$form->name."\r\n";
		$body = $body.'Email'.$form->email."\r\n";
		$body = $body.'Organizacion'.$form->organization."\r\n";
        $body = $body.'Acepto las condiciones de <a href="/politica-de-privacidad">política de privacidad</a>'.$form->rgpd."\r\n";
        $post = [
            'comment_status'    =>  'closed',
            'ping_status'       =>  'closed',
            'post_content'     =>  $body,
            'post_title'        =>  $subject,
            'post_status'       =>  'private',
            'post_type'     =>  'messages'
        ];
        wp_insert_post( $post );
        wp_mail( $to, $subject, $body );
    } else {
        $messages = $form->check();
    }
}

$context['contactform'] = $form;
$context['messages'] = $messages;
$context['check'] = $form->check();
$context['posted'] = $form->posted();
$templates = array( 'contacto.twig' );
//~ if ( is_home() ) {
	//~ array_unshift( $templates, 'page.twig' );
//~ }
Timber::render( $templates, $context );
