<?php
class Etape_Interne  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['type_document']=$this->Model->getRequete('SELECT * FROM `doc_interne_type` WHERE 1 ORDER BY DESCRIPTION');
      $data['profils']=$this->Model->getRequete('SELECT * FROM `config_profil` WHERE 1 ORDER BY DESCRIPTION');


      $data['title']='Demande interne';      
      $this->load->view('Etape_Interne_Add_View',$data);

    }



      public function add()
    {
           
      $DESCRIPTION=$this->input->post('DESCRIPTION');
      $ID_TYPE_DOC_INTERNE=$this->input->post('ID_TYPE_DOC_INTERNE');
      $ID_PROFIL=$this->input->post('ID_PROFIL');
      $ON_PDF=$this->input->post('ON_PDF');
      $NUMERO=$this->input->post('NUMERO');

// exit();


      $this->form_validation->set_rules('DESCRIPTION', 'Nom du etape', 'required');
      $this->form_validation->set_rules('ID_TYPE_DOC_INTERNE', 'Type document interne', 'required');
      $this->form_validation->set_rules('ID_PROFIL', 'Profils', 'required');
      $this->form_validation->set_rules('ON_PDF', 'Le PDF', 'required');
      $this->form_validation->set_rules('NUMERO', 'Numero', 'required');
  
   if ($this->form_validation->run() == FALSE){
    $message = "<div class='alert alert-danger'>
                              Etape non enregistr&eacute;
                            <button type='button' class='close' data-dismiss='alert'>&times;</button>
                      </div>";
    $this->session->set_flashdata(array('message'=>$message));
    $data['type_document']=$this->Model->getRequete('SELECT * FROM `doc_interne_type` WHERE 1 ORDER BY DESCRIPTION');
    $data['profils']=$this->Model->getRequete('SELECT * FROM `config_profil` WHERE 1 ORDER BY DESCRIPTION');
    $data['title']='Demande interne';
      $this->load->view('Etape_Interne_Add_View',$data); 
   }
   else{

    $data=array(
      'DESCRIPTION'=>$DESCRIPTION,
      'ID_TYPE_DOC_INTERNE'=>$ID_TYPE_DOC_INTERNE,
      'ID_PROFIL'=>$ID_PROFIL,
      'ON_PDF'=>$ON_PDF,
      'NUMERO'=>$NUMERO,

    );    

    $this->Model->insert_last_id('doc_interne_etape',$data);

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

    }


      public function listing()
    { 
    $data['resultat']=$this->Model->getRequete('SELECT ID_ETAPE_DOC_INTERNE, NUMERO, doc_interne_etape.DESCRIPTION, doc_interne_type.DESCRIPTION AS ID_TYPE_DOC_INTERNE, config_profil.DESCRIPTION AS ID_PROFIL, IF(ON_PDF =1, "Oui", "Non") AS ON_PDF,doc_interne_etape.STATUS FROM `doc_interne_etape` JOIN doc_interne_type ON doc_interne_type.ID_TYPE_DOC_INTERNE = doc_interne_etape.ID_TYPE_DOC_INTERNE JOIN config_profil ON config_profil.PROFIL_ID = doc_interne_etape.ID_PROFIL');
    $data['title']='Demande interne';
    $this->load->view('Etape_Interne_List_View',$data);
    }

    public function desactiver($id)
    {
      $this->Model->update('doc_interne_etape',array('ID_ETAPE_DOC_INTERNE'=>$id),array('STATUS'=>0));
      
      $message = "<div class='alert alert-primary' id='message'>
                            Etape 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('document_interne/Etape_Interne/listing'));  
    }

  public function reactiver($id)
    {
      $this->Model->update('doc_interne_etape',array('ID_ETAPE_DOC_INTERNE'=>$id),array('STATUS'=>1));
      $message = "<div class='alert alert-primary' id='message'>
                            Etape 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('document_interne/Etape_Interne/listing'));  
    }


    
    public function index_update($ID_ETAPE_DOC_INTERNE)
    {

      $data['title']='Demande interne';     
      $data['data']=$this->Model->getRequeteOne('SELECT * FROM `doc_interne_etape` WHERE 1 AND ID_ETAPE_DOC_INTERNE = '.$ID_ETAPE_DOC_INTERNE.''); 
      $data['type_document']=$this->Model->getRequete('SELECT * FROM `doc_interne_type` WHERE 1 ORDER BY DESCRIPTION');
      $data['profils']=$this->Model->getRequete('SELECT * FROM `config_profil` WHERE 1 ORDER BY DESCRIPTION');
      $this->load->view('Etape_Interne_Update_View',$data);

    }


    public function update()
    {
      

      $DESCRIPTION=$this->input->post('DESCRIPTION');
      $ID_TYPE_DOC_INTERNE=$this->input->post('ID_TYPE_DOC_INTERNE');
      $ID_PROFIL=$this->input->post('ID_PROFIL');
      $ON_PDF=$this->input->post('ON_PDF');
      $NUMERO=$this->input->post('NUMERO');


      $ID_ETAPE_DOC_INTERNE=$this->input->post('ID_ETAPE_DOC_INTERNE');      
// exit();
      $this->form_validation->set_rules('DESCRIPTION', 'Nom du etape', 'required');
      $this->form_validation->set_rules('ID_TYPE_DOC_INTERNE', 'Type document interne', 'required');
      $this->form_validation->set_rules('ID_PROFIL', 'Profils', 'required');
      // $this->form_validation->set_rules('ON_PDF', 'Le PDF', 'required');
      $this->form_validation->set_rules('NUMERO', 'Numero', 'required');
  
   if ($this->form_validation->run() == FALSE){
    $message = "<div class='alert alert-danger'>
                              Etape non modifi&eacute;
                            <button type='button' class='close' data-dismiss='alert'>&times;</button>
                      </div>";
    $this->session->set_flashdata(array('message'=>$message));
    $data['type_document']=$this->Model->getRequete('SELECT * FROM `doc_interne_type` WHERE 1 ORDER BY DESCRIPTION');
    $data['profils']=$this->Model->getRequete('SELECT * FROM `config_profil` WHERE 1 ORDER BY DESCRIPTION');
    $data['data']=$this->Model->getRequeteOne('SELECT * FROM `doc_interne_etape` WHERE 1 AND ID_ETAPE_DOC_INTERNE = '.$ID_ETAPE_DOC_INTERNE.''); 
    $data['title']='Demande interne';
      $this->load->view('Etape_Interne_Update_View',$data); 
   }
   else{

    $data=array(
      'DESCRIPTION'=>$DESCRIPTION,
      'ID_TYPE_DOC_INTERNE'=>$ID_TYPE_DOC_INTERNE,
      'ID_PROFIL'=>$ID_PROFIL,
      'ON_PDF'=>$ON_PDF,
      'NUMERO'=>$NUMERO,

    );    

    $this->Model->update('doc_interne_etape',array('ID_ETAPE_DOC_INTERNE'=>$ID_ETAPE_DOC_INTERNE),$data);

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

    }






}