<?php
class Organigramme  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']='Organigramme';
      $direction=$this->Model->getRequete('SELECT * FROM `organigramme_direction`');



      $donnes = "";
      $daffichage ='';
      
      foreach ($direction as $direction_value) {
        // $donnes .= "['0', 'SD_".$direction_value['ID_DIRECTION']."'],";

        $daffichage .= "{
            id: 'SD_".$direction_value['ID_DIRECTION']."',
            name: '".$direction_value['DESCRIPTION']."'
        },";
        
      }
      // $daffichage.="{
      //       id: '0',
      //       name: 'C A'
      //   },";

      $departement=$this->Model->getRequete('SELECT * FROM `organigramme_service`');
      foreach ($departement as $departement_value) {

          $donnes .= "['SD_".$departement_value['ID_DIRECTION']."', 'SSD_".$departement_value['ID_SERVICE']."'],";
          $daffichage .= "{
            id: 'SSD_".$departement_value['ID_SERVICE']."',
            title: '".$departement_value['DESCRIPTION']."',
            name: ' '
        },";
        
      }

      $services=$this->Model->getRequete('SELECT * FROM `organigramme_section`');
      foreach ($services as $service_value) {
        $donnes .= "['SSD_".$service_value['ID_SERVICE']."', 'SSS_".$service_value['ID_SECTION']."'],";
          $daffichage .= "{
            id: 'SSS_".$service_value['ID_SECTION']."',
            title: '".$service_value['DESCRIPTION']."',
            name: ' '
        },";
      }

      
      

      $data['donnes'] = $donnes;
      $data['daffichage'] = $daffichage;
      // $data['daffichageduex'] = $daffichageduex;
      $data['departement']=$this->Model->getRequete('SELECT * FROM `organigramme_service` order by DESCRIPTION');
      $this->load->view('Organigramme_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()
    {

  $ACCORDE_CONGE=$this->input->post('ACCORDE_CONGE');
  $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'>
                            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('ACCORDE_CONGE'=>$ACCORDE_CONGE,'DESCRIPTION'=>$DESCRIPTION,'ID_DIRECTION'=>$ID_DIRECTION,'ID_SERVICE'=>$ID_SERVICE);
    $this->Model->insert_last_id('organigramme_section',$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 ID_SECTION, organigramme_section.DESCRIPTION, organigramme_service.DESCRIPTION AS DEPARTEMENT, organigramme_direction.DESCRIPTION AS DIRECTION, IF(organigramme_section.ACCORDE_CONGE = 1, "Oui", "Non") AS ACCORDE_CONGE, 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_section.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_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('Service_Update_View',$data);

    }



    public function update()
    {

      $ACCORDE_CONGE=$this->input->post('ACCORDE_CONGE');
      $DESCRIPTION=$this->input->post('DESCRIPTION');
      $ID_DIRECTION=$this->input->post('ID_DIRECTION');
      $ID_SERVICE=$this->input->post('ID_SERVICE');
      $ID_SECTION=$this->input->post('ID_SECTION');
        
      $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'>
                                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('ACCORDE_CONGE'=>$ACCORDE_CONGE,'DESCRIPTION'=>$DESCRIPTION, 'ID_DIRECTION'=>$ID_DIRECTION, 'ID_SERVICE'=>$ID_SERVICE);
        $this->Model->update('organigramme_section',array('ID_SECTION'=>$ID_SECTION),$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_section',array('ID_SECTION'=>$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_section',array('ID_SECTION'=>$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'));  
    }

    
    


}