<?php
class Action_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['etape_interne']=$this->Model->getRequete('SELECT * FROM `doc_interne_action` WHERE 1 ORDER BY DESCRIPTION');


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

    }


    
    public function get_etapes()
  {
  $etapes= $this->Model->getList("doc_interne_etape",array('ID_TYPE_DOC_INTERNE'=>$this->input->post('ID_TYPE_DOC_INTERNE')));
  $datas= '<option value="">-- Sélectionner --</option>';
  foreach($etapes as $etapes_one){
  $datas.= '<option value="'.$etapes_one["ID_ETAPE_DOC_INTERNE"].'">'.$etapes_one["DESCRIPTION"].'</option>';
  }
  $datas.= '';
  echo $datas;
  }

  public function get_next_etapes()
  {
    
    
  $etapes= $this->Model->getRequete("SELECT * FROM doc_interne_etape WHERE ID_TYPE_DOC_INTERNE = ".$this->input->post('ID_TYPE_DOC_INTERNE')." AND ID_ETAPE_DOC_INTERNE != ".$this->input->post('ID_ETAPE_DOC_INTERNE')."");
  $datas= '<option value="">-- Sélectionner --</option>';
  foreach($etapes as $etapes_one){
  $datas.= '<option value="'.$etapes_one["ID_ETAPE_DOC_INTERNE"].'">'.$etapes_one["DESCRIPTION"].'</option>';
  }
  $datas.= '';
  echo $datas;
  }



      public function add()
    {
           
      $DESCRIPTION=$this->input->post('DESCRIPTION');
      $ID_TYPE_DOC_INTERNE=$this->input->post('ID_TYPE_DOC_INTERNE');
      $ID_ETAPE_DOC_INTERNE=$this->input->post('ID_ETAPE_DOC_INTERNE');
      $NEXT=$this->input->post('NEXT');
      $IS_LAST=$this->input->post('IS_LAST');
// exit();


      $this->form_validation->set_rules('DESCRIPTION', 'Nom du Action', 'required');
      $this->form_validation->set_rules('ID_ETAPE_DOC_INTERNE', 'Etape', 'required');
      $this->form_validation->set_rules('NEXT', 'Etape Suivant', 'required');
      $this->form_validation->set_rules('IS_LAST', 'Dernier', 'required');
  
   if ($this->form_validation->run() == FALSE){
    $message = "<div class='alert alert-danger'>
                              Action 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_ACTION_INTENE'=>$ID_ACTION_INTENE,
      'ID_ETAPE_DOC_INTERNE'=>$ID_ETAPE_DOC_INTERNE,
      'NEXT'=>$NEXT,
      'IS_LAST'=>$IS_LAST,
    );    

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

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

    }


      public function listing()
    { 
    $data['resultat']=$this->Model->getRequete('SELECT ID_ACTION_INTENE, doc_interne_etape.DESCRIPTION AS ID_ETAPE_DOC_INTERNE, doc_interne_type.DESCRIPTION AS TYPE_DOC_INTERNE, nex.DESCRIPTION AS NEXT, doc_interne_action.DESCRIPTION, IF(IS_LAST=1, "Refus", IF(IS_LAST=2, "Fin", "Normal")) AS IS_LAST, doc_interne_action.STATUS FROM `doc_interne_action` LEFT JOIN doc_interne_etape ON doc_interne_etape.ID_ETAPE_DOC_INTERNE = doc_interne_action.ID_ETAPE_DOC_INTERNE LEFT JOIN doc_interne_etape as nex ON nex.ID_ETAPE_DOC_INTERNE = doc_interne_action.NEXT JOIN doc_interne_type ON doc_interne_type.ID_TYPE_DOC_INTERNE = doc_interne_etape.ID_TYPE_DOC_INTERNE');
    $data['title']='Demande interne';
    $this->load->view('Action_Interne_List_View',$data);
    }

    public function desactiver($id)
    {
      $this->Model->update('doc_interne_action',array('ID_ACTION_INTENE'=>$id),array('STATUS'=>0));
      
      $message = "<div class='alert alert-primary' id='message'>
                            Action 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/Action_Interne/listing'));  
    }

  public function reactiver($id)
    {
      $this->Model->update('doc_interne_action',array('ID_ACTION_INTENE'=>$id),array('STATUS'=>1));
      $message = "<div class='alert alert-primary' id='message'>
                            Action 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/Action_Interne/listing'));  
    }


    
    public function index_update($ID_ACTION_INTENE)
    {

      $data['title']='Demande interne';     
      $data['data']=$this->Model->getRequeteOne('SELECT * FROM `doc_interne_action` WHERE 1 AND ID_ACTION_INTENE = '.$ID_ACTION_INTENE.''); 
      $data['type_document']=$this->Model->getRequete('SELECT * FROM `doc_interne_type` WHERE 1 ORDER BY DESCRIPTION');
      $data['etape_interne']=$this->Model->getRequete('SELECT * FROM `doc_interne_action` WHERE 1 ORDER BY DESCRIPTION');
      $this->load->view('Action_Interne_Update_View',$data);

    }


    public function update()
    {
          
      $DESCRIPTION=$this->input->post('DESCRIPTION');
      $ID_TYPE_DOC_INTERNE=$this->input->post('ID_TYPE_DOC_INTERNE');
      $ID_ETAPE_DOC_INTERNE=$this->input->post('ID_ETAPE_DOC_INTERNE');
      $NEXT=$this->input->post('NEXT');
      $IS_LAST=$this->input->post('IS_LAST');
      $ID_ACTION_INTENE=$this->input->post('ID_ACTION_INTENE');

// exit();


      $this->form_validation->set_rules('DESCRIPTION', 'Nom du Action', 'required');
      $this->form_validation->set_rules('ID_ETAPE_DOC_INTERNE', 'Etape', 'required');
      $this->form_validation->set_rules('NEXT', 'Etape Suivant', 'required');
      $this->form_validation->set_rules('IS_LAST', 'Dernier', 'required');
  
   if ($this->form_validation->run() == FALSE){
    $message = "<div class='alert alert-danger'>
                              Action non enregistr&eacute;
                            <button type='button' class='close' data-dismiss='alert'>&times;</button>
                      </div>";
    $this->session->set_flashdata(array('message'=>$message));
      $data['title']='Demande interne';     
      $data['data']=$this->Model->getRequeteOne('SELECT * FROM `doc_interne_action` WHERE 1 AND ID_ACTION_INTENE = '.$ID_ACTION_INTENE.''); 
      $data['type_document']=$this->Model->getRequete('SELECT * FROM `doc_interne_type` WHERE 1 ORDER BY DESCRIPTION');
      $data['etape_interne']=$this->Model->getRequete('SELECT * FROM `doc_interne_action` WHERE 1 ORDER BY DESCRIPTION');
      $this->load->view('Etape_Interne_Update_View',$data); 
   }
   else{

    $data=array(
      'DESCRIPTION'=>$DESCRIPTION,
      'ID_ACTION_INTENE'=>$ID_ACTION_INTENE,
      'ID_ETAPE_DOC_INTERNE'=>$ID_ETAPE_DOC_INTERNE,
      'NEXT'=>$NEXT,
      'IS_LAST'=>$IS_LAST,
    );    

    $this->Model->update('doc_interne_action',array('ID_ACTION_INTENE'=>$ID_ACTION_INTENE),$data);

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

    }






}