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

class Chapters extends CI_Controller {

    // Controller used to load chapters for all users. Avoid repetead codes

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

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

        $data = array(
            'title' => 'Todos os capítulos',
            '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))),
            'chapters'                      => $this->core_model->get_all('chapters', array($role.'_id' => $user_id)),
        );
        
        $this->load->view('restricted/layout/header', $data);
        $this->load->view('restricted/'.$role.'/chapters');
        $this->load->view('restricted/layout/footer');
    }

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

        $data = array(
            'title' => 'Capítulos aprovados',
            '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))),
            'chapters'                      => $this->core_model->get_all('chapters', array($role.'_id' => $user_id, 'status' => 'approved')),
        );
        
        $this->load->view('restricted/layout/header', $data);
        $this->load->view('restricted/'.$role.'/chapters_approved');
        $this->load->view('restricted/layout/footer');
    }

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

        $data = array(
            'title' => 'Capítulos recusados',
            '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))),
            'chapters'                      => $this->core_model->get_all('chapters', array($role.'_id' => $user_id, 'status' => 'recused')),
        );
        
        $this->load->view('restricted/layout/header', $data);
        $this->load->view('restricted/'.$role.'/chapters_recused');
        $this->load->view('restricted/layout/footer');
    }

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

        $data = array(
            'title' => 'Capítulos em revisão',
            '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))),
            'chapters'                      => $this->core_model->get_all('chapters', array($role.'_id' => $user_id, 'status' => 'in_review')),
        );
        
        $this->load->view('restricted/layout/header', $data);
        $this->load->view('restricted/'.$role.'/chapters_in_review');
        $this->load->view('restricted/layout/footer');
    }

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

        $data = array(
            'title' => 'Capítulos aguardando revisão',
            '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))),
            'chapters'                      => $this->core_model->get_all('chapters', array($role.'_id' => $user_id, 'status' => 'waiting_review')),
        );
        
        $this->load->view('restricted/layout/header', $data);
        $this->load->view('restricted/'.$role.'/chapters_waiting_review');
        $this->load->view('restricted/layout/footer');
    }

}