<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Contacto extends CI_Controller
{

	protected $data;


	public function index()
	{
		$this->data['main_content'] = 'contacto';
		$this->vista();
	}


	public function enviarNO()
	{
		//$this->check_request();

		//CI email library
		$this->load->library('email');

		//smtp config params
		$this->config->load('email_smtp');

		//Email Sender Library
		$this->load->library('email_sender');

		$user_data = [
			'dest_email' => $this->config->item('email_admin'),
			'cc_email' => $this->config->item('email_admin'),
			'subject' => $this->config->item('email_subject'),
			'reply_to' => $this->input->post('email'),
			'sender_email' => $this->config->item('email_sender'),
			'sender_name' => $this->config->item('email_sender')
		];
		$email_data = [
			'nombre' => $this->input->post('nombre'),
			'email' => $this->input->post('email'),
			//'asunto' => $this->input->post('asunto'),
			'telefono' => $this->input->post('telefono'),
			'mensaje' => $this->input->post('mensaje'),
		];

		$template = 'admin/contacto';

		if ($this->email_sender->custom_email($user_data, $email_data, $template, false)) {
			$this->data['json'] = [
				"message" => 'Gracias por contactarnos. Atenderemos su consulta en la brevedad posible.',
				"status" => true,
				'redirect' => base_url(),
			];
		} else {
			//Error
			$this->data['json'] = [
				"message" => 'No se ha podido enviar su mensaje. Favor Intente de nuevo.',
				"status" => false
			];
		}

		$this->load->view('json_view', $this->data);
	}

	public function enviar()
	{

		//$this->check_request();

		//CI email library
		//$this->load->library('email');

		//smtp config params
		$this->config->load('email_smtp');

		// Load PHPMailer library
		$this->load->library('PHPMailer_Lib');

		/* 	$user_data = [
			'dest_email' => $this->config->item('email_contacto'),
			'cc_email' => $this->config->item('email_admin'),
			'subject' => $this->config->item('email_subject'),
			'reply_to' => $this->input->post('email'),
			'sender_email' => $this->config->item('email_sender'),
			'sender_name' => $this->config->item('email_sender')
		];
 */

		$email_data = [
			'nombre' => $this->input->post('nombre'),
			'email' => $this->input->post('email'),
			'telefono' => $this->input->post('telefono'),
			'asunto' => $this->input->post('asunto'),
			'mensaje' => $this->input->post('mensaje')
		];

		// PHPMailer object
		$mail = $this->phpmailer_lib->load();

		// SMTP configuration
		$mail->isSMTP();
		$mail->Host     = $this->config->item('smtp_host');
		$mail->SMTPAuth = true;
		$mail->Username = $this->config->item('smtp_user');
		$mail->Password = $this->config->item('smtp_pass');
		$mail->SMTPSecure = $this->config->item('smtp_crypto');
		$mail->Port     = $this->config->item('smtp_port');
		$mail->CharSet = $this->config->item('charset');

		$mail->setFrom($this->config->item('smtp_user'), $this->config->item('smtp_user'));
		$mail->addReplyTo($this->input->post('email'), $this->input->post('nombre'));

		// Add a recipient
		$mail->addAddress($this->config->item('email_contacto'));
		$mail->addCC($this->config->item('admin_email'));

		// Add cc or bcc 
		//$mail->addCC('cc@example.com');
		//$mail->addBCC('bcc@example.com');

		// Email subject
		$mail->Subject = $this->config->item('email_subject');

		// Set email format to HTML
		$mail->isHTML(true);

		// Email body content
		$layout = 'emails/Layout';
		$template = 'emails/admin/contacto';
		$c['content'] = $this->load->view($template, $email_data, true);
        $mailContent = $this->load->view($layout, $c, true);
		$mail->Body = $mailContent;

		if (!$mail->send()) {
			//Error SMTP
			$response = ['json' => [
				'status' => false,
				'message' => 'Su mensaje no ha sido enviado!, Favor intente nuevamente.' . $mail->ErrorInfo
			]];
		} else {
			$response = ['json' => [
				'status' => true,
				'message' => 'Gracias por contactarnos. Hemos recibido su mensaje y nos pondremos en contacto con UD en la brevedad.',
				'redirect' => base_url(),
			]];
			//$this->comunicacion();
		}
		$this->load->view('json_view', $response);
	}

	public function vista()
	{
		$this->load->view('inicio', $this->data);
	}
}
