<?php
class Service  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']='Service';
      $data['direction']=$this->Model->getRequete('SELECT * FROM `organigramme_direction` order by DESCRIPTION');
      $this->load->view('Service_Add_View',$data);

    }



    public function add()
    {

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

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

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

    $message = "<div class='alert alert-success' id='message'>
                              Service 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/Service/listing'));  
   }

    }

    

    public function listing()
    {

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

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

    }


    public function index_update($id)
    {

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

    }



    public function update()
    {

      $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');
    
       if ($this->form_validation->run() == FALSE){
        $message = "<div class='alert alert-danger'>
                                Service non modifi&eacute;
                                <button type='button' class='close' data-dismiss='alert'>&times;</button>
                          </div>";
        $this->session->set_flashdata(array('message'=>$message));
        $data['title']='Service';
      $this->load->view('Service_Update_View',$data);
       }
       else{
    
        
        $datas=array('DESCRIPTION'=>$DESCRIPTION, 'ID_DIRECTION'=>$ID_DIRECTION);
        $this->Model->update('organigramme_service',array('ID_SERVICE'=>$ID_SERVICE),$datas);
    
        $message = "<div class='alert alert-success' id='message'>
                                Service 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/Service/listing'));  
       }
     

    }


    public function desactiver($id)
    {
      $this->Model->update('organigramme_service',array('ID_SERVICE'=>$id),array('STATUS'=>0));
      $message = "<div class='alert alert-success' id='message'>
                            Service 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/Service/listing'));  
    }

  public function reactiver($id)
    {
      $this->Model->update('organigramme_service',array('ID_SERVICE'=>$id),array('STATUS'=>1));
      $message = "<div class='alert alert-success' id='message'>
                              Service 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/Service/listing'));  
    }

    
    


}