<?php 
defined('BASEPATH') or exit('Ação não permitida');

class Email_model extends CI_Model{

	public function __construct()
	{
		parent::__construct();
	}

    public function send_email_welcome($email = NULL, $email_params = NULL){
        $data = array(
            'email' => $email,
            'name'  => $email_params,
        );
        $template = $this->load->view('restricted/email_template/welcome_template', $data, TRUE);
        
		$this->email->set_header('MIME-Version', 1.0);
		$this->email->set_header('Content-type', 'text/html');
		$this->email->set_header('charset', 'UTF-8');
        $this->email->set_mailtype("html");
        $this->email->set_newline("\r\n");
        
        $this->email->from('docnex@conexaoproweb.com.br', 'DocNex');
        $this->email->to($email);
        $this->email->subject('Sua conta na DocNex foi criada com sucesso!');
        $this->email->message($template);
        $this->email->send();
        echo "Email enviado";
    }
    
    public function send_email($template = NULL, $user_id = NULL, $email_params = NULL) {
        $user = $this->core_model->get_by_id('users', array('id' => $user_id));
        $user_name = $user->first_name . ' ' . $user->last_name;

        switch ($template):
            case 'new_chapter_admin':
                $email_template = $this->core_model->get('email_templates', array('email_type' => 'new_article', 'role_id' => 1));
                // Replace go here
                $replace_from = array('%admin_name%', '%chapter_title%', '%chapter_code%');
                $replace_to = array($user_name, $email_params['chapter_title'], $email_params['chapter_code']);
                $body = str_replace($replace_from, $replace_to, $email_template->body);
                // 
                $data = array(
                    'user_name'     => $user_name,
                    'subject'       => $email_template->subject,
                    'body'          => $body,
                    'unique_code'   => $email_params['chapter_code'],
                );
            break;

            case 'assigned_editor':
                $email_template = $this->core_model->get('email_templates', array('email_type' => 'assigned_editor', 'role_id' => 3));
                $replace_from = array('%editor_name%', '%chapter_title%', '%chapter_code%');
                $replace_to = array($user_name, $email_params['chapter_title'], $email_params['chapter_code']);
                $body = str_replace($replace_from, $replace_to, $email_template->body);
                $data = array(
                    'user_name'     => $user_name,
                    'subject'       => $email_template->subject,
                    'body'          => $body,
                    'unique_code'   => $email_params['chapter_code'],
                );
            break;

            case 'assigned_reviewer':
                $email_template = $this->core_model->get('email_templates', array('email_type' => 'assigned_reviewer', 'role_id' => 4));
                $replace_from = array('%reviewer_name%', '%chapter_title%', '%chapter_code%');
                $replace_to = array($user_name, $email_params['chapter_title'], $email_params['chapter_code']);
                $body = str_replace($replace_from, $replace_to, $email_template->body);
                $data = array(
                    'user_name'     => $user_name,
                    'subject'       => $email_template->subject,
                    'body'          => $body,
                    'unique_code'   => $email_params['chapter_code'],
                );            
            break;

            case 'resubmit_chapter_admin':
                $email_template = $this->core_model->get('email_templates', array('email_type' => 'resubmit_chapter', 'role_id' => 1));
                $replace_from = array('%admin_name%', '%chapter_title%', '%chapter_code%');
                $replace_to = array($user_name, $email_params['chapter_title'], $email_params['chapter_code']);
                $body = str_replace($replace_from, $replace_to, $email_template->body);
                $data = array(
                    'user_name'     => $user_name,
                    'subject'       => $email_template->subject,
                    'body'          => $body,
                    'unique_code'   => $email_params['chapter_code'],
                );  
            break;

            case 'resubmit_chapter_author':
                $email_template = $this->core_model->get('email_templates', array('email_type' => 'resubmit_chapter', 'role_id' => 2));
                $replace_from = array('%author_name%', '%chapter_title%', '%chapter_code%');
                $replace_to = array($user_name, $email_params['chapter_title'], $email_params['chapter_code']);
                $body = str_replace($replace_from, $replace_to, $email_template->body);
                $data = array(
                    'user_name'     => $user_name,
                    'subject'       => $email_template->subject,
                    'body'          => $body,
                    'unique_code'   => $email_params['chapter_code'],
                );  
            break;

            case 'resubmit_chapter_editor':
                $email_template = $this->core_model->get('email_templates', array('email_type' => 'resubmit_chapter', 'role_id' => 3));
                $replace_from = array('%editor_name%', '%chapter_title%', '%chapter_code%');
                $replace_to = array($user_name, $email_params['chapter_title'], $email_params['chapter_code']);
                $body = str_replace($replace_from, $replace_to, $email_template->body);
                $data = array(
                    'user_name'     => $user_name,
                    'subject'       => $email_template->subject,
                    'body'          => $body,
                    'unique_code'   => $email_params['chapter_code'],
                );  
            break;
            case 'resubmit_chapter_reviewer':
                $email_template = $this->core_model->get('email_templates', array('email_type' => 'resubmit_chapter', 'role_id' => 4));
                $replace_from = array('%reviewer_name%', '%chapter_title%', '%chapter_code%');
                $replace_to = array($user_name, $email_params['chapter_title'], $email_params['chapter_code']);
                $body = str_replace($replace_from, $replace_to, $email_template->body);
                $data = array(
                    'user_name'     => $user_name,
                    'subject'       => $email_template->subject,
                    'body'          => $body,
                    'unique_code'   => $email_params['chapter_code'],
                );  
            break;

            case 'approved_chapter_editor':
                $email_template = $this->core_model->get('email_templates', array('email_type' => 'approved_chapter', 'role_id' => 3));
                $replace_from = array('%author_name%', '%chapter_title%', '%chapter_code%');
                $replace_to = array($user_name, $email_params['chapter_title'], $email_params['chapter_code']);
                $body = str_replace($replace_from, $replace_to, $email_template->body);
                $data = array(
                    'user_name'     => $user_name,
                    'subject'       => $email_template->subject,
                    'body'          => $body,
                    'unique_code'   => $email_params['chapter_code'],
                ); 
            break;

            case 'approved_chapter_reviewer':
                $email_template = $this->core_model->get('email_templates', array('email_type' => 'approved_chapter', 'role_id' => 4));
                $replace_from = array('%author_name%', '%chapter_title%', '%chapter_code%');
                $replace_to = array($user_name, $email_params['chapter_title'], $email_params['chapter_code']);
                $body = str_replace($replace_from, $replace_to, $email_template->body);
                $data = array(
                    'user_name'     => $user_name,
                    'subject'       => $email_template->subject,
                    'body'          => $body,
                    'unique_code'   => $email_params['chapter_code'],
                ); 
            break;

            case 'approved_w_warnings_editor':
                $email_template = $this->core_model->get('email_templates', array('email_type' => 'approved_w_warnings', 'role_id' => 3));
                $replace_from = array('%author_name%', '%chapter_title%', '%chapter_code%');
                $replace_to = array($user_name, $email_params['chapter_title'], $email_params['chapter_code']);
                $body = str_replace($replace_from, $replace_to, $email_template->body);
                $data = array(
                    'user_name'     => $user_name,
                    'subject'       => $email_template->subject,
                    'body'          => $body,
                    'unique_code'   => $email_params['chapter_code'],
                ); 
            break;

            case 'approved_w_warnings_reviewer':
                $email_template = $this->core_model->get('email_templates', array('email_type' => 'approved_w_warnings', 'role_id' => 4));
                $replace_from = array('%author_name%', '%chapter_title%', '%chapter_code%');
                $replace_to = array($user_name, $email_params['chapter_title'], $email_params['chapter_code']);
                $body = str_replace($replace_from, $replace_to, $email_template->body);
                $data = array(
                    'user_name'     => $user_name,
                    'subject'       => $email_template->subject,
                    'body'          => $body,
                    'unique_code'   => $email_params['chapter_code'],
                ); 
            break;

            case 'recused_chapter_editor':
                $email_template = $this->core_model->get('email_templates', array('email_type' => 'recused_chapter', 'role_id' => 3));
                $replace_from = array('%author_name%', '%chapter_title%', '%chapter_code%');
                $replace_to = array($user_name, $email_params['chapter_title'], $email_params['chapter_code']);
                $body = str_replace($replace_from, $replace_to, $email_template->body);
                $data = array(
                    'user_name'     => $user_name,
                    'subject'       => $email_template->subject,
                    'body'          => $body,
                    'unique_code'   => $email_params['chapter_code'],
                ); 
            break;

            case 'recused_chapter_reviewer':
                $email_template = $this->core_model->get('email_templates', array('email_type' => 'recused_chapter', 'role_id' => 4));
                $replace_from = array('%author_name%', '%chapter_title%', '%chapter_code%');
                $replace_to = array($user_name, $email_params['chapter_title'], $email_params['chapter_code']);
                $body = str_replace($replace_from, $replace_to, $email_template->body);
                $data = array(
                    'user_name'     => $user_name,
                    'subject'       => $email_template->subject,
                    'body'          => $body,
                    'unique_code'   => $email_params['chapter_code'],
                );
            break;

            case 'in_review':
                $email_template = $this->core_model->get('email_templates', array('email_type' => 'in_review'));
                $replace_from = array('%author_name%', '%chapter_title%', '%chapter_code%');
                $replace_to = array($user_name, $email_params['chapter_title'], $email_params['chapter_code']);
                $body = str_replace($replace_from, $replace_to, $email_template->body);
                $data = array(
                    'user_name'     => $user_name,
                    'subject'       => $email_template->subject,
                    'body'          => $body,
                    'unique_code'   => $email_params['chapter_code'],
                ); 
            break;

            default:
                $template = "";
            break;
        endswitch;

        $template = $this->load->view('restricted/email_template/commom_template', $data, TRUE);
        
		$this->email->set_header('MIME-Version', 1.0);
		$this->email->set_header('Content-type', 'text/html');
		$this->email->set_header('charset', 'UTF-8');
        $this->email->set_mailtype("html");
        $this->email->set_newline("\r\n");
        
        $this->email->from('docnex@conexaoproweb.com.br', 'DocNex');
        $this->email->to($user->email);
        $this->email->subject($data['subject']);
        $this->email->message($template);
        $this->email->send();
        echo "Email enviado";
    }


}