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

class Chapters extends CI_Controller{

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

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

    public function index(){
        // 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',
                'js/delete-confirm.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'),
        );
        
        $this->load->view('restricted/layout/header', $data);
        $this->load->view('restricted/admin/chapters/index');
        $this->load->view('restricted/layout/footer');
    }

    public function chapters_approved(){
        // 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('status' => 'approved')),
        );
        
        $this->load->view('restricted/layout/header', $data);
        $this->load->view('restricted/admin/chapters/chapters_approved');
        $this->load->view('restricted/layout/footer');
    }

    public function chapters_recused(){
        // 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('status' => 'recused')),
        );
        
        $this->load->view('restricted/layout/header', $data);
        $this->load->view('restricted/admin/chapters/chapters_recused');
        $this->load->view('restricted/layout/footer');
    }

    public function chapters_in_review(){
        // 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('status' => 'in_review')),
        );
        
        $this->load->view('restricted/layout/header', $data);
        $this->load->view('restricted/admin/chapters/chapters_in_review');
        $this->load->view('restricted/layout/footer');
    }

    public function chapters_waiting_review(){
        // 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('status' => 'waiting_review')),
        );
        
        $this->load->view('restricted/layout/header', $data);
        $this->load->view('restricted/admin/chapters/chapters_waiting_review');
        $this->load->view('restricted/layout/footer');
    }

}
