<?php

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

class Professions 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_professions_model', 'company_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() . '/professions/' . $this->router->fetch_method();
    }

    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('Stanowiska', '/professions/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_professions_model->get_list($company_id, null, null, true);
        $page = $this->uri->segment(4, 1);
        $config['uri_segment'] = 4;
        $data['stanowiska'] = $this->company_professions_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('professions/index', $data);
        $this->load->view('Base_footer');
    }

    public function delete_profession($profession_id) {
        if (!is_numeric($profession_id))
            redirect('/');
        $company_id = $this->company_professions_model->get_companyid_byprofession($profession_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_professions_model->delete($profession_id);
        if ($del)
            $this->session->set_flashdata('message', 'Stanowisko zostało usunięte');
        else
            $this->session->set_flashdata('error', 'Stanowisko nie zostało usuniete');
        redirect('professions/index/' . $company_id);
    }

    public function edit_profession($profession_id) {
        if (!is_numeric($profession_id))
            redirect('/');
        $company_id = $this->company_professions_model->get_companyid_byprofession($profession_id);
        if (!is_numeric($company_id))
            redirect('/');
        $this->breadcrumbs->push('Firma', '/firma/index/' . $company_id);
        $this->breadcrumbs->push('Stanowiska', '/professions/index/' . $company_id);
        $this->add_profession($company_id, $profession_id);
    }

    public function add_profession($company_id = null, $profession_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('profession_name', 'Stanowisko', 'trim|required');
            $this->form_validation->set_rules('description', 'Opis', 'trim|required|min_length[3]|max_length[1000]');
            $this->form_validation->set_rules('field_of_study', 'Kierunek studiów', 'trim|required|min_length[3]|max_length[200]');
            $this->form_validation->set_rules('qualifications', 'Kwalifikacje', 'trim|required|min_length[3]|max_length[500]');
            if ($this->form_validation->run() == FALSE) {
                $this->session->set_flashdata('error', 'Popraw formularz.<br/>' . validation_errors());
            } else {
                if (is_numeric($profession_id)) {
                    $update = $this->company_professions_model->update($profession_id, $this->input->post());
                    if ($update)
                        $this->session->set_flashdata('message', 'Stanowisko zostało zmienione');
                    else
                        $this->session->set_flashdata('error', 'Stanowisko nie zostało zmienione');
                }else {
                    $add = $this->company_professions_model->insert($company_id, $this->input->post());
                    if ($add)
                        $this->session->set_flashdata('message', 'Stanowisko zostało dodane');
                    else
                        $this->session->set_flashdata('error', 'Stanowisko nie zostało dodane');
                }
                redirect('professions/index/' . $company_id);
            }
        }
        
        $this->breadcrumbs->push('Firma', '/firma/index/' . $company_id);
        $this->breadcrumbs->push('Stanowiska', '/professions/index/' . $company_id);
        
        if (is_numeric($profession_id)) {
            $this->breadcrumbs->push('Edytuj stanowisko', '/professions/edit_profession/' . $company_id);
            $profession_data = $this->company_professions_model->get_profession($profession_id);
        } else{
            $this->breadcrumbs->push('Dodaj stanowisko', '/professions/add_profession/' . $company_id);
            $profession_data = array();
        }
            

        $data = $this->form_add_profession($profession_data);
        $data['id'] = is_numeric($profession_id) ? $profession_id : null;

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

    private function form_add_profession($data = array()) {
        if(!$data){
            $data['profession_name'] = '';
            $data['description'] = '';
            $data['field_of_study'] = '';
            $data['qualifications'] = '';
        }
        $form_data['profession_name'] = array(
            'name' => 'profession_name',
            'type' => 'text',
            'value' => set_value('profession_name') != "" ? set_value('profession_name') : $data['profession_name'],
            'class' => 'form-control inpZielony',
            'placeholder' => 'Stanowisko'
        );

        $form_data['description'] = array(
            'name' => 'description',
            'type' => 'text',
            'value' => set_value('description') != "" ? set_value('description') : $data['description'],
            'class' => 'form-control inpZielony',
            'placeholder' => 'Opis'
        );

        $form_data['field_of_study'] = array(
            'name' => 'field_of_study',
            'type' => 'text',
            'value' => set_value('field_of_study') != "" ? set_value('field_of_study') : $data['field_of_study'],
            'class' => 'form-control inpZielony',
            'placeholder' => 'Kierunek studiów'
        );

        $form_data['qualifications'] = array(
            'name' => 'qualifications',
            'type' => 'text',
            'value' => set_value('qualifications') != "" ? set_value('qualifications') : $data['qualifications'],
            'class' => 'form-control inpZielony',
            'placeholder' => 'Kwalifikacje'
        );
        return $form_data;
    }

}
