<?php

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

class Projects extends CI_Controller {

    public $user_data = array();

    public function __construct() {
        parent::__construct();
        $this->user_data = $this->ion_auth->check_user_privilege(array('Firma', 'Admin'));
        $this->load->model(array('company_model', 'company_projects_model'));
        $this->breadcrumbs->push('Start', '/');
        $this->company_id = $this->ion_auth->user()->row()->id;
        $this->company = $this->ion_auth->logged_in() ? $this->ion_auth->user()->row()->company : false;
        $this->base_url = base_url() . '/projects/' . $this->router->fetch_method();
    }
    
    public function pre($tab){
        echo '<pre>';
        print_r($tab);
        echo '</pre>';
    }
    

    public function index($company_id = null) {
        if (!is_numeric($company_id))
            redirect('/');
        if (!($this->user_data['is_admin'] or ( $company_id == $this->company_id) and $this->user_data['is_firma'])) {
            $this->session->set_flashdata('error', 'Nie masz uprawnień');
            redirect('/');
        }
        $this->breadcrumbs->push('Firma', '/firma/index/' . $company_id);
        $this->breadcrumbs->push('Staże', '/projects/index' . $company_id);
        $this->load->config('pagination', true);
        $config = $this->config->config['pagination'];
        $config['base_url'] = $this->base_url . '/' . $company_id;
        $config['total_rows'] = $this->company_projects_model->get_list($company_id, null, null, true);
        $page = $this->uri->segment(4, 1);
        $config['uri_segment'] = 4;
        $data['projects'] = $this->company_projects_model->get_list($company_id, $config['per_page'], $page, false);
        $data['company'] = $this->company_model->get_name($company_id);
        $data['company_id'] = $company_id;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();

        $this->load->view('Base_header');
        $this->load->view('projects/index', $data);
        $this->load->view('Base_footer');
    }

    public function edit_project($project_id) {
        if (!is_numeric($project_id))
            redirect('/');
        $company_id = $this->company_projects_model->get_companyid_byproject($project_id);
        if (!is_numeric($company_id))
            redirect('/');

        $this->add_project($company_id, $project_id);
    }

    public function add_project($company_id = null, $project_id = null) {
        if (!is_numeric($company_id))
            redirect('/');
        if (!($this->user_data['is_admin'] or ( $company_id == $this->company_id) and $this->user_data['is_firma'])) {
            $this->session->set_flashdata('error', 'Nie masz uprawnień');
            redirect('/');
        }
        if (!$this->company_model->check_isset($company_id))
            redirect('/');

        if (!empty($this->input->post())) {
            $this->form_validation->set_rules('project_name', 'Nazwa stażu', 'trim|required');
            $this->form_validation->set_rules('description', 'Opis', 'trim|required|min_length[3]|max_length[1000]');
            if ($this->form_validation->run() == FALSE) {
                $this->session->set_flashdata('error', 'Popraw formularz.<br/>' . validation_errors());
            } else {
                if (is_numeric($project_id)) {
                    $update = $this->company_projects_model->update($project_id, $this->input->post());
                    if ($update)
                        $this->session->set_flashdata('message', 'Staż został zmieniony');
                    else
                        $this->session->set_flashdata('error', 'Staż nie został zmieniony');
                    redirect('projects/index/' . $company_id);
                }else {
                    $add = $this->company_projects_model->insert($company_id, $this->input->post());
                    if ($add)
                        $this->session->set_flashdata('message', 'Staż został dodany');
                    else
                        $this->session->set_flashdata('error', 'Staż nie został dodany');
                    redirect('projects/index/' . $company_id);
                }
            }
        }
        
        $this->breadcrumbs->push('Staże', '/projects/index/'.$company_id);
        
        if (is_numeric($project_id)) {
            $project_data = $this->company_projects_model->get_project($project_id);
            
            $this->breadcrumbs->push('Edytuj staż', '/projects/edit_project');
        } 
        else{
            $project_data = array();
            $this->breadcrumbs->push('Dodaj staż', '/projects/add_project');            
        }


        $data = $this->form_add_project($project_data);
        $data['id'] = is_numeric($project_id) ? $project_id : null;

        $this->load->view('Base_header');
        $this->load->view('projects/form_add_project', $data);
        $this->load->view('Base_footer');
    }

    private function form_add_project($data = array()) {
            $data['profession_name'] = '';
            $data['description'] = '';
            $data['field_of_study'] = '';
            $data['qualifications'] = '';
        
        if(!isset($data['project_name']) ){
            $data['project_name'] = '';
        }
        if(!isset($data['description']) ){
            $data['description'] = '';
        }
        if(!isset($data['number_of_students']) ){
            $data['number_of_students'] = '';
        }
        
        $form_data['project_name'] = array(
            'name' => 'project_name',
            'type' => 'text',
            'value' => set_value('project_name') != "" ? set_value('project_name') : $data['project_name'],
            'class' => 'form-control',
            'placeholder' => 'Nazwa stażu'
        );

        $form_data['description'] = array(
            'name' => 'description',
            'type' => 'text',
            'value' => set_value('description') != "" ? set_value('description') : $data['description'],
            'class' => 'form-control',
            'placeholder' => 'Opis'
        );
        
        $form_data['number_of_students'] = array(
            'name' => 'number_of_students',
            'type' => 'text',
            'value' => set_value('number_of_students') != "" ? set_value('number_of_students') : $data['number_of_students'],
            'class' => 'form-control',
            'placeholder' => 'Liczba studentów'
        );
        
        return $form_data;
    }
    
    
    private function form_rate_project($data = array()) {

        if(!isset($data['description']) ){
            $data['description'] = '';
        }
    
        $form_data['description'] = array(
            'name' => 'description',
            'type' => 'text',
            'value' => set_value('description') != "" ? set_value('description') : $data['description'],
            'class' => 'form-control',
            'placeholder' => 'Opis'
        );
        
        if(!isset($data['rate'])){
            $data['rate'] = null;
        }
        
        $form_data['rate_update'] = $data['rate'];
        
        
        
        return $form_data;
    }
    

    public function delete_project($project_id) {
        if (!is_numeric($project_id))
            redirect('/');
        $company_id = $this->company_projects_model->get_companyid_byproject($project_id);
        if (!is_numeric($company_id))
            redirect('/');
        if (!($this->user_data['is_admin'] or ( $company_id == $this->company_id) and $this->user_data['is_firma'])) {
            $this->session->set_flashdata('error', 'Nie masz uprawnień');
            redirect('/');
        }
        $del = $this->company_projects_model->delete($project_id);
        if ($del)
            $this->session->set_flashdata('message', 'Staż został usunięty');
        else
            $this->session->set_flashdata('error', 'Staż nie został usuniety');
        redirect('projects/index/' . $company_id);
    }
    
        public function rate_project($project_id) {
        if (!is_numeric($project_id))
            redirect('/');
        $company_id = $this->company_projects_model->get_companyid_byproject($project_id);
        if (!is_numeric($company_id))
            redirect('/');
        if (!($this->user_data['is_admin'] or ( $company_id == $this->company_id) and $this->user_data['is_firma'])) {
            $this->session->set_flashdata('error', 'Nie masz uprawnień');
            redirect('/');
        }
            
        if (!empty($this->input->post())) {
            //$this->form_validation->set_rules('description', 'Opis', 'trim|required|min_length[3]|max_length[1000]');
            $this->form_validation->set_rules('rate', 'Uzasadnienie oceny', 'numeric');
            if ($this->form_validation->run() == FALSE) {
                $this->session->set_flashdata('error', 'Popraw formularz.<br/>' . validation_errors());
            } else {
                $check_exists = $this->company_projects_model->check_rate_row($company_id, $project_id);
                
                //$this->pre($check_exists);
                //exit;
                
                if (is_numeric($project_id) and $check_exists['id'] > 0 ) {
                    $project_rate_id = $check_exists['id'];
                    $update = $this->company_projects_model->update_rate($project_id, $this->input->post(), $project_rate_id );
                    if ($update)
                        $this->session->set_flashdata('message', 'Ocena stażu została zmieniona');
                    else
                        $this->session->set_flashdata('error', 'Ocena stażu nie został zmieniona');
                    redirect('projects/index/' . $company_id);
                }else {
                    $add = $this->company_projects_model->insert_rate($company_id, $this->input->post(), $project_id);
                    if ($add)
                        $this->session->set_flashdata('message', 'Ocena stażu została dodana');
                    else
                        $this->session->set_flashdata('error', 'Ocena stażu nie została dodana');
                    redirect('projects/index/' . $company_id);
                }
            }
        }            
            
        if (is_numeric($project_id)) {
            $rate_data = $this->company_projects_model->get_project_rates($project_id);
            
        } else{
            $rate_data = array();
        }
            

        $data = $this->form_rate_project($rate_data);
        $data['id'] = is_numeric($project_id) ? $project_id : null;

        $this->load->view('Base_header');
        $this->load->view('projects/form_rate_project', $data);
        $this->load->view('Base_footer'); 
            
            

    }
    

}
