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

class Categoriaproveedor extends CI_Controller{
    function __construct()
    {
        parent::__construct();
        $this->load->model('Categoriaproveedor_model');
    } 

    /*
     * Listing of categoriasproveedor
     */
    function index($page=FALSE)
    {
        if(!($this->session->userdata('id'))) 
        {
            redirect(base_url()."login");
        };

        $breadcrumb = array(
            "Categorias de Proveedor" => ""
        );
        $data['breadcrumb'] = $breadcrumb;

        $params['page'] = $page;
        $params['limit'] = 5; 
        $params['offset'] = $page ? (($page - 1) * $params['limit']) : 0;

        
        $config = $this->config->item('pagination');
        $config['base_url'] = site_url('categoriaproveedor/page');
        $config['total_rows'] = $this->Categoriaproveedor_model->get_all_categoriasproveedor_count();
        $config['per_page'] = $params['limit'];
        $config['use_page_numbers'] = TRUE;
        $config['first_url'] = base_url('categoriaproveedor');
        $config['attributes'] = array('class' => 'page-link');
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['cur_tag_open'] = '<li class="page-item active"><span class="page-link">';
        $config['cur_tag_close'] = '</span></li>';
        $config['num_tag_open'] = '<li class="page-item">';
        $config['num_tag_close'] = '</li>';
        $config['prev_tag_open'] = '<li class="page-item">';
        $config['prev_tag_close'] = '</li>';
        $config['next_tag_open'] = '<li class="page-item">';
        $config['next_tag_close'] = '</li>';
        $config['first_tag_open'] = '<li class="page-item">';
        $config['first_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li class="page-item">';
        $config['last_tag_close'] = '</li>';
        $config['first_link'] = "<<";
        $config['last_link'] = ">>";
        $this->pagination->initialize($config);

        $data['listado'] = TRUE;
        $data['controlador'] = 'categoriaproveedor';
        $data['categoriasproveedor'] = $this->Categoriaproveedor_model->get_all_categoriasproveedor($params);
        
        $data['contenido'] = 'categoriaproveedor/index';
        $data['titulo'] = 'Categorias de Proveedor';
        $data['_view'] = 'categoriaproveedor/index';
        $this->load->view('templates/template_admin', $data);
    }

    /*
     * Adding a new categoriaproveedor
     */
    function add()
    {   

        if(!($this->session->userdata('id'))) 
        {
            redirect(base_url()."login");
        };
        $breadcrumb = array(
            "Categorias de Proveedor" => "./",
            "Agregar" => ""
        );
        $data['breadcrumb'] = $breadcrumb;

        $this->load->library('form_validation');

		$this->form_validation->set_rules('nombre_destinatario','Nombre Destinatario','required');
		
		if($this->form_validation->run())     
        {   
            $params = array(
				'nombre_destinatario' => $this->input->post('nombre_destinatario'),
				'id_autor' =>  $this->session->userdata('id'),
				'fecha_registro' => date("Y-m-d H:i:s"),
				'fecha_modificacion' => date("Y-m-d H:i:s"),
				'estado' => 1,
				'id_condominio' => $this->session->userdata('condominio'),
            );
            
            $categoriaproveedor_id = $this->Categoriaproveedor_model->add_categoriaproveedor($params);
            redirect('categoriaproveedor');
        }
        else
        {            
            $data['controlador'] = 'categoriaproveedor';
            $data['contenido'] = 'categoriaproveedor/add';
            $data['titulo'] = 'Nueva Categoria de Proveedor';
            $data['_view'] = 'categoriaproveedor/add';
            $this->load->view('templates/template_admin', $data);
        }
    }  

    /*
     * Editing a categoriaproveedor
     */
    function edit($id)
    {   
        if(!($this->session->userdata('id'))) 
        {
            redirect(base_url()."login");
        };
        // check if the categoriaproveedor exists before trying to edit it
        $breadcrumb = array(
            "Categorias de Proveedor" => "../",
            "Editar" => ""
        );
        $data['breadcrumb'] = $breadcrumb;

        $data['categoriaproveedor'] = $this->Categoriaproveedor_model->get_categoriaproveedor($id);
        
        if(isset($data['categoriaproveedor']['id']))
        {
            $this->load->library('form_validation');

			$this->form_validation->set_rules('nombre_destinatario','Nombre Destinatario','required');
		
			if($this->form_validation->run())     
            {   
                $params = array(
					'nombre_destinatario' => $this->input->post('nombre_destinatario'),
					'id_autor' => $this->input->post('id_autor'),
					'fecha_registro' => $this->input->post('fecha_registro'),
					'fecha_modificacion' => date("Y-m-d H:i:s"),
					'estado' => $this->input->post('estado'),
					'id_condominio' => $this->input->post('id_condominio'),
                );

                $this->Categoriaproveedor_model->update_categoriaproveedor($id,$params);            
                redirect('categoriaproveedor');
            }
            else
            {
               $data['controlador'] = 'categoriaproveedor';
            $data['contenido'] = 'categoriaproveedor/edit';
            $data['titulo'] = 'Editar Categoria de Proveedor';
            $data['_view'] = 'categoriaproveedor/edit';
            $this->load->view('templates/template_admin', $data);
            }
        }
        else
            show_error('The categoriaproveedor you are trying to edit does not exist.');
    } 

    /*
     * Deleting categoriaproveedor
     */
    function remove($id)
    {
        $categoriaproveedor = $this->Categoriaproveedor_model->get_categoriaproveedor($id);

        // check if the categoriaproveedor exists before trying to delete it
        if(isset($categoriaproveedor['id']))
        {
            $this->Categoriaproveedor_model->delete_categoriaproveedor($id);
            redirect('categoriaproveedor');
        }
        else
            show_error('The categoriaproveedor you are trying to delete does not exist.');
    }
    
}
