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

class Home extends CI_Controller {
    
    public function __construct() {
        parent::__construct();

        $this->load->model('Reviewer_model', 'reviewer');
        $this->load->model('Email_model', 'email_model');

        
        // Get the user id through session
        $user_id = $this->session->user_id;

        if($user_id==1){
            redirect('restricted/admin/home');
        }

        //Valid session?
        if (!$this->ion_auth->in_group('reviewer', $user_id)) {
            //redirect('restricted/auth');
        }
    }

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

        $data = array(
            'title'         => 'Painel do revisor',
            'styles'                =>  array(
                'css/custom.css',
            ),
            'scripts'               =>  array(
                'js/edit-chapter-status.js',
                'js/chapter_details.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))),
            'assigned_to_me'                => $this->reviewer->count_assigned_to_me('chapters', array('reviewer_id' => $user_id)),
            'chapters'                      => $this->core_model->get_all('chapters', array('reviewer_id' => $user_id)),
        );

        $this->load->view('restricted/layout/header', $data);
        $this->load->view('restricted/reviewer/index');
        $this->load->view('restricted/layout/footer');
    }

    //Method to add comments and update status from a chapter. Editors and reviewer can do it.
    public function chapter_comments($chapter_id = NULL){
        if($chapter_id != '' OR $chapter_id != NULL){
            $chapter = $this->core_model->get_by_id('chapters', array('id' => $chapter_id));

            $data = array(
                'comment_author'  =>  $this->input->post('comment_author'),
                'comment'         =>  $this->input->post('comment'),
                'status'          =>  $this->input->post('chapter_status'),
                'created_at'      =>  date('Y-m-d H:i:s'),
                'chapter_id'      =>  $chapter_id
            );

            switch($data['status']){
                case 'approved':
                    // Send email to assigned reviewer
                    $email_params = array(
                    'chapter_title'        => $chapter->title,
                    'chapter_code'         => $chapter->unique_code,
                    );
                    $this->email_model->send_email('approved_chapter_reviewer', $chapter->author_id, $email_params);
                break;
                case 'recused':
                    // Send email to assigned reviewer
                    $email_params = array(
                    'chapter_title'        => $chapter->title,
                    'chapter_code'         => $chapter->unique_code,
                    );
                    $this->email_model->send_email('recused_chapter_reviewer', $chapter->author_id, $email_params);
                break;
                case 'in_review':
                    // Send email to assigned reviewer
                    $email_params = array(
                    'chapter_title'        => $chapter->title,
                    'chapter_code'         => $chapter->unique_code,
                    );
                    $this->email_model->send_email('in_review', $chapter->author_id, $email_params);
                break;
                case 'approved_with_warnings':
                    // Send email to assigned reviewer
                    $email_params = array(
                    'chapter_title'        => $chapter->title,
                    'chapter_code'         => $chapter->unique_code,
                    );
                    $this->email_model->send_email('approved_w_warnings_reviewer', $chapter->author_id, $email_params);
                break;
                default:
                    echo "Nothing";
                break;
            }

            $this->db->insert('comments', $data);
            $this->core_model->update('chapters', array('status' => $data['status']), array('id' => $chapter_id));
            redirect('restricted/reviewer/home');
        } else {
            $this->session->set_flashdata('error_message', 'Dados inválidos. Tente novamente!');
            redirect('restricted/reviewer/home');
        }
    }

}
