<?php
class Section  extends CI_Controller{
    function __construct() {
        parent::__construct();
        $this->Is_Connected();

      }

      public function Is_Connected()
       {
       if (empty($this->session->userdata('SOCAR_ID_USER')))
        {
         redirect(base_url('Login/'));
        }
       }

        


  
    public function index()
    {

      $data['title']='Section';
      $data['direction']=$this->Model->getRequete('SELECT * FROM `organigramme_direction` order by DESCRIPTION');
      $data['departement']=$this->Model->getRequete('SELECT * FROM `organigramme_service` order by DESCRIPTION');
      $this->load->view('Section_Add_View',$data);

    }

    
    public function get_departement()
  {
  $departement= $this->Model->getList("organigramme_service",array('ID_DIRECTION'=>$this->input->post('ID_DIRECTION')));
  $datas= '<option value="">-- Sélectionner --</option>';
  foreach($departement as $sele_departement){
  $datas.= '<option value="'.$sele_departement["ID_SERVICE"].'">'.$sele_departement["DESCRIPTION"].'</option>';
  }
  $datas.= '';
  echo $datas;
  }

    public function add()
    {

  $DESCRIPTION=$this->input->post('DESCRIPTION');
  $ID_DIRECTION=$this->input->post('ID_DIRECTION');
  $ID_SERVICE=$this->input->post('ID_SERVICE');
    
  $this->form_validation->set_rules('DESCRIPTION', 'Description', 'required');
  $this->form_validation->set_rules('ID_DIRECTION', 'Direction', 'required');
  $this->form_validation->set_rules('ID_SERVICE', 'Departement', 'required');

   if ($this->form_validation->run() == FALSE){
    $message = "<div class='alert alert-danger'>
                            Section non enregistr&eacute;
                            <button type='button' class='close' data-dismiss='alert'>&times;</button>
                      </div>";
    $this->session->set_flashdata(array('message'=>$message));
    $data['title']='Section';
    $data['direction']=$this->Model->getRequete('SELECT * FROM `organigramme_direction` order by DESCRIPTION');
  $this->load->view('Section_Add_View',$data);
   }
   else{

    
    $datas=array('DESCRIPTION'=>$DESCRIPTION,'ID_DIRECTION'=>$ID_DIRECTION,'ID_SERVICE'=>$ID_SERVICE);
    $this->Model->insert_last_id('organigramme_section',$datas);

    $message = "<div class='alert alert-primary' id='message'>
                            Section enregistr&eacute; avec succés
                            <button type='button' class='close' data-dismiss='alert'>&times;</button>
                      </div>";
    $this->session->set_flashdata(array('message'=>$message));
      redirect(base_url('organigramme/Section/listing'));  
   }

    }

    

    public function listing()
    {

      $data['resultat']=$this->Model->getRequete('SELECT ID_SECTION, organigramme_section.DESCRIPTION, organigramme_service.DESCRIPTION AS SERVICE, organigramme_direction.DESCRIPTION AS DIRECTION, organigramme_section.STATUS FROM `organigramme_section` JOIN organigramme_service ON organigramme_service.ID_SERVICE = organigramme_section.ID_SERVICE JOIN organigramme_direction ON organigramme_direction.ID_DIRECTION = organigramme_service.ID_DIRECTION WHERE 1;');
      $tabledata=array();

      $data['title']='Section';
        $this->load->view('Section_List_View',$data);

    }


    public function index_update($id)
    {

      $data['title']='Section';
      $data['data']=$this->Model->getRequeteOne('SELECT * FROM organigramme_section WHERE ID_SECTION = '.$id.'');
      $data['direction']=$this->Model->getRequete('SELECT * FROM `organigramme_direction` order by DESCRIPTION');
      $data['departement']=$this->Model->getRequete('SELECT * FROM `organigramme_service` order by DESCRIPTION');
      $this->load->view('Section_Update_View',$data);

    }



    public function update()
    {

      $DESCRIPTION=$this->input->post('DESCRIPTION');
      $ID_DIRECTION=$this->input->post('ID_DIRECTION');
      $ID_SECTION=$this->input->post('ID_SECTION');
      $ID_SERVICE=$this->input->post('ID_SERVICE');
        
      $this->form_validation->set_rules('DESCRIPTION', 'Description', 'required');
      $this->form_validation->set_rules('ID_DIRECTION', 'Direction', 'required');
      $this->form_validation->set_rules('ID_SECTION', 'Section', 'required');
    
       if ($this->form_validation->run() == FALSE){
        $message = "<div class='alert alert-danger'>
                                Section non modifi&eacute;
                                <button type='button' class='close' data-dismiss='alert'>&times;</button>
                          </div>";
        $this->session->set_flashdata(array('message'=>$message));
        $data['title']='Section';
      $this->load->view('Section_Update_View',$data);
       }
       else{
    
        
        $datas=array('DESCRIPTION'=>$DESCRIPTION, 'ID_DIRECTION'=>$ID_DIRECTION, 'ID_SECTION'=>$ID_SECTION);
        $this->Model->update('organigramme_section',array('ID_SECTION'=>$ID_SECTION),$datas);
    
        $message = "<div class='alert alert-success' id='message'>
                                Section modifi&eacute; avec succés
                                <button type='button' class='close' data-dismiss='alert'>&times;</button>
                          </div>";
        $this->session->set_flashdata(array('message'=>$message));
          redirect(base_url('organigramme/Section/listing'));  
       }
     

    }


    public function desactiver($id)
    {
      $this->Model->update('organigramme_section',array('ID_SECTION'=>$id),array('STATUS'=>0));
      $message = "<div class='alert alert-success' id='message'>
                            Section désactivé avec succés
                            <button type='button' class='close' data-dismiss='alert'>&times;</button>
                      </div>";
      $this->session->set_flashdata(array('message'=>$message));
      redirect(base_url('organigramme/Section/listing'));  
    }

  public function reactiver($id)
    {
      $this->Model->update('organigramme_section',array('ID_SECTION'=>$id),array('STATUS'=>1));
      $message = "<div class='alert alert-success' id='message'>
                              Section Réactivé avec succés
                            <button type='button' class='close' data-dismiss='alert'>&times;</button>
                      </div>";
      $this->session->set_flashdata(array('message'=>$message));
      redirect(base_url('organigramme/Section/listing'));  
    }

    
    


}