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

class Email extends CI_Controller {

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

        $this->load->config('email');
        $this->load->library('email');

        //Valid session?
        if (!$this->ion_auth->is_admin()) {
            redirect('restricted/auth');
        }
    }
    
    public function send_email_view(){
        // Get the user id through session
        $user_id = $this->session->user_id;

        $data = array(
            'title'                         => 'Enviar email',
            'messages'                      => $this->core_model->get_all('messages', array('comment_destination' => $user_id)),
            'unread_messages'               => count($this->core_model->get_all('messages', array('comment_destination' => $user_id, 'was_read' => 0))),
            'alerts'                        => $this->core_model->get_all('alerts', array('user_id' => $user_id)),
            'unread_alerts'                 => count($this->core_model->get_all('alerts', array('user_id' => $user_id, 'was_read' => 0))),
            'chapters'                      => $this->core_model->get_all('chapters'),
        );

        $this->load->view('restricted/layout/header', $data);
        $this->load->view('restricted/admin/email/send_email');
        $this->load->view('restricted/layout/footer');
    }

    public function send_email(){
        if($_POST){
            $email_to = $this->input->post('email_to');
            $subject = $this->input->post('subject');
            $message = $this->input->post('message');

            $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_to);
            $this->email->subject($subject);
            $this->email->message($message);
            if($this->email->send()){
                $this->session->set_flashdata('success_message', 'Email enviado com sucesso!');
                redirect('restricted/admin/email/send_email_view');
            } else {
                $this->session->set_flashdata('error_message', 'Ops! Algo deu errado. Tente novamente!');
                redirect('restricted/admin/email/send_email_view');
            }
        }

    }

    public function email_configuration() {
        // Get the user id through session
        $user_id = $this->session->user_id;

        $data = array(
            'title'                         => 'Configurações de email',
            'styles'    => array(
                'css/custom.css',
                'vendor/datatables/dataTables.bootstrap4.min.css',
            ),
            'scripts'   => array(
                'vendor/datatables/jquery.dataTables.min.js',
                'vendor/datatables/dataTables.bootstrap4.min.js',
                'js/initialize-ckeditor.js',
            ),
            'messages'                      => $this->core_model->get_all('messages', array('comment_destination' => $user_id)),
            'unread_messages'               => count($this->core_model->get_all('messages', array('comment_destination' => $user_id, 'was_read' => 0))),
            'alerts'                        => $this->core_model->get_all('alerts', array('user_id' => $user_id)),
            'unread_alerts'                 => count($this->core_model->get_all('alerts', array('user_id' => $user_id, 'was_read' => 0))),
            'chapters'                      => $this->core_model->get_all('chapters'),
            'email_templates'               => $this->core_model->get_all('email_templates'),
        );

        $this->load->view('restricted/layout/header', $data);
        $this->load->view('restricted/admin/email/email_configuration');
        $this->load->view('restricted/layout/footer');
    }

    public function save_email_configuration($id = NULL){
        $data['body'] = $this->input->post('email_body');
        if($id != 0 OR $id != NULL){
            if($this->core_model->update('email_templates', $data, array('id' => $id)) == TRUE){
                redirect('restricted/admin/email/email_configuration');
            } else {
                redirect('restricted/admin/email/email_configuration');
            }
        }
    }
}