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

class Books 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 livros',
            'styles'    => array(
                'vendor/datatables/dataTables.bootstrap4.min.css'
            ),
            'scripts' => array(
                'vendor/chart.js/Chart.min.js',
                'js/demo/chart-area-demo.js',
                'js/delete-confirm.js',
                'vendor/datatables/jquery.dataTables.min.js',
                'vendor/datatables/dataTables.bootstrap4.min.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))),
            'books'                         =>  $this->core_model->get_all('books'),
        );
        $this->load->view('restricted/layout/header', $data);
        $this->load->view('restricted/admin/books/index');
        $this->load->view('restricted/layout/footer');
    }

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

        if(!$id){
            // If not exist add new chapter
            $id = (int) $id;

            $this->form_validation->set_rules('title', 'Título', 'trim|required');


            if($this->form_validation->run()){

                $data = array(
                    'title'             =>  $this->input->post('title'),
                    'submission_rule'   =>  $this->input->post('submission_rule'),
                    'created_at'        =>  date('Y-m-d H:i:s'),
                );

                if($this->db->insert('books', $data)){
                    $this->session->set_flashdata('success_message', 'Livro cadastrado com sucesso!');
                    redirect('restricted/admin/books/index');
                } else {
                    $this->session->set_flashdata('error_message', 'Ops! Algo deu errado. Tente novamente!');
                    redirect('restricted/admin/books/core');
                }


            } else {
                //Validation error
                $data = array(
                    'title' => "Criar novo livro",
                    'groups' => $this->ion_auth->groups()->result(),
                    'styles' => array(
                        'css/step-by-step.css',
                        'css/tag-input.css',
                        'css/custom.css',
                    ),
                    'scripts' => array(
                        'js/step-by-step.js',
                        'js/tag-input.js',
                        'js/mask/jquery.mask.min.js',
                        'js/mask/custom-mask.js',
                        'js/co-authors.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))),
                );
                $this->load->view('restricted/layout/header', $data);
                $this->load->view('restricted/admin/books/core');
                $this->load->view('restricted/layout/footer');
            }

        // If exist edit chapter
        } else {

            $this->form_validation->set_rules('title', 'Título', 'trim|required');

            if($this->form_validation->run()){
                
                $data = array(
                    'title'         =>  $this->input->post('title'),
                    'submission_rule'   =>  $this->input->post('submission_rule'),
                    'modified_at'    =>  date('Y-m-d H:i:s'),
                );

                if($this->db->update('books', $data, array('id' => $id))){
                    $this->session->set_flashdata('success_message', 'Livro editado com sucesso!');
                    redirect('restricted/admin/books/index');
                } else {
                    $this->session->set_flashdata('error_message', 'Ops! Algo deu errado. Tente novamente!');
                    redirect('restricted/admin/books/core');
                }

            } else {

                $data = array(
                    'title'         =>  "Editar Livro",
                    'styles'        =>  array(
                        'css/custom.css',
                    ),
                    'scripts'       =>  array(
                        'js/initialize-ckeditor.js'
                    ),
                    'chapter'                       => $this->core_model->get_by_id('chapters', array('author_id' => $user_id)),
                    '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))),
                    'co_authors'                    => $this->core_model->get_all('co_authors', array('chapters_id' => $id)),
                    'book'                          => $this->core_model->get_by_id('books', array('id' => $id)),
                );
                $this->load->view('restricted/layout/header', $data);
                $this->load->view('restricted/admin/books/core');
                $this->load->view('restricted/layout/footer');
            }
        }
    }

    public function delete($id = NULL){
        if($id){
            if($this->db->delete('books', array('id' => $id))){
                $this->session->set_flashdata('success_message', 'Deletado com sucesso!');
                redirect('restricted/admin/books');
            } else {
                $this->session->set_flashdata('error_message', 'Ops! Algo deu errado. Tente novamente!');
                redirect('restricted/admin/books');
            }
        } else {
            $this->session->set_flashdata('error_message', 'Ops! Algo deu errado. Tente novamente!');
            redirect('restricted/admin/books');
        }
    }

    public function books_progress(){
        // Get the user id through session
        $user_id = $this->session->user_id;
        
        $data = array(
            'title' => 'Todos os livros',
            'styles'    => array(
                'vendor/datatables/dataTables.bootstrap4.min.css'
            ),
            'scripts' => array(
                'vendor/datatables/jquery.dataTables.min.js',
                'vendor/datatables/dataTables.bootstrap4.min.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))),
            'books'                         =>  $this->core_model->get_all('books'),
        );
        $this->load->view('restricted/layout/header', $data);
        $this->load->view('restricted/admin/books/books_progress');
        $this->load->view('restricted/layout/footer');
    }

}
