<?php
class Messages  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()
       {

        if ($this->session->userdata('SOCAR_PROFIL_ID') == 0) {
          $SOCAR_ID_USER = $this->session->userdata('SOCAR_ID_USER');
          $employe = ' AND receiver.ID_EMPLOYE = '.$SOCAR_ID_USER.'';
        }
        else{
          $SOCAR_ID_USER = $this->session->userdata('SOCAR_ID_USER');  
          $employe = ' ';

        }
        
   
         $data['listemessage']=$this->Model->getRequete(' SELECT ID_MESSAGE_CONGE, ID_DEMANDE_CONGE, conge_type_message.DESCRIPTION AS TYPE_MESSAGE, DATE_SYSTEME_DEMANDE, CORPS_MESSAGE, conge_type_destinataire.DESCRIPTION AS TYPE_DESTINATAIRE, ID_EMPLOYE_EMETTEUR, sender.NOM AS SNOM, sender.PRENOM AS SPRENOM, ID_EMPLOYE_DESTINATAIRE, receiver.NOM AS RNOM, receiver.PRENOM AS RPRENOM, conge_message_conge.ID_STATUS_MESSAGE, conge_status_message.DESCRIPTION AS STATUS_MESSAGE FROM `conge_message_conge` LEFT JOIN conge_type_message ON conge_type_message.ID_TYPE_MESSAGE = conge_message_conge.ID_TYPE_MESSAGE LEFT JOIN conge_type_destinataire ON conge_type_destinataire.ID_TYPE_DESTINATAIRE = conge_message_conge.ID_TYPE_DESTINATAIRE LEFT JOIN rh_employe AS sender ON sender.ID_EMPLOYE = conge_message_conge.ID_EMPLOYE_EMETTEUR LEFT JOIN rh_employe AS receiver ON receiver.ID_EMPLOYE = conge_message_conge.ID_EMPLOYE_DESTINATAIRE LEFT JOIN conge_status_message ON conge_status_message.ID_STATUS_MESSAGE = conge_message_conge.ID_STATUS_MESSAGE WHERE 1  '.$employe.' ORDER BY DATE_SYSTEME_DEMANDE DESC ');
         $data['nbmessage']=$this->Model->getRequeteOne('SELECT COUNT(ID_MESSAGE_CONGE) AS NB_MESSAGE_UNREAD FROM `conge_message_conge` LEFT JOIN rh_employe AS receiver ON receiver.ID_EMPLOYE = conge_message_conge.ID_EMPLOYE_DESTINATAIRE WHERE 1 AND conge_message_conge.ID_STATUS_MESSAGE = 1  '.$employe.' ');

         $tabledata=array();
   
         $data['title']='Messages Reçus';
           $this->load->view('Messages_List_View',$data);
   
       }


       public function sentmail()
       {

        if ($this->session->userdata('SOCAR_PROFIL_ID') == 0) {
          $SOCAR_ID_USER = $this->session->userdata('SOCAR_ID_USER');
          $employe = ' AND sender.ID_EMPLOYE = '.$SOCAR_ID_USER.'';
          $employes = ' AND receiver.ID_EMPLOYE = '.$SOCAR_ID_USER.'';
        }
        else{
          $SOCAR_ID_USER = $this->session->userdata('SOCAR_ID_USER');  
          $employe = ' ';
          $employes = ' ';

        }
        
   
         $data['listemessage']=$this->Model->getRequete(' SELECT ID_MESSAGE_CONGE, ID_DEMANDE_CONGE, conge_type_message.DESCRIPTION AS TYPE_MESSAGE, DATE_SYSTEME_DEMANDE, CORPS_MESSAGE, conge_type_destinataire.DESCRIPTION AS TYPE_DESTINATAIRE, ID_EMPLOYE_EMETTEUR, sender.NOM AS SNOM, sender.PRENOM AS SPRENOM, ID_EMPLOYE_DESTINATAIRE, receiver.NOM AS RNOM, receiver.PRENOM AS RPRENOM, conge_message_conge.ID_STATUS_MESSAGE, conge_status_message.DESCRIPTION AS STATUS_MESSAGE FROM `conge_message_conge` LEFT JOIN conge_type_message ON conge_type_message.ID_TYPE_MESSAGE = conge_message_conge.ID_TYPE_MESSAGE LEFT JOIN conge_type_destinataire ON conge_type_destinataire.ID_TYPE_DESTINATAIRE = conge_message_conge.ID_TYPE_DESTINATAIRE LEFT JOIN rh_employe AS sender ON sender.ID_EMPLOYE = conge_message_conge.ID_EMPLOYE_EMETTEUR LEFT JOIN rh_employe AS receiver ON receiver.ID_EMPLOYE = conge_message_conge.ID_EMPLOYE_DESTINATAIRE LEFT JOIN conge_status_message ON conge_status_message.ID_STATUS_MESSAGE = conge_message_conge.ID_STATUS_MESSAGE WHERE 1  '.$employe.' ORDER BY DATE_SYSTEME_DEMANDE DESC ');
         $data['nbmessage']=$this->Model->getRequeteOne('SELECT COUNT(ID_MESSAGE_CONGE) AS NB_MESSAGE_UNREAD FROM `conge_message_conge` LEFT JOIN rh_employe AS receiver ON receiver.ID_EMPLOYE = conge_message_conge.ID_EMPLOYE_DESTINATAIRE WHERE 1 AND conge_message_conge.ID_STATUS_MESSAGE = 1  '.$employes.' ');

         $tabledata=array();
   
         $data['title']='Messages envoyés';
           $this->load->view('Messages_Sent_List_View',$data);
   
       }

       public function read($id)
       {

        if ($this->session->userdata('SOCAR_PROFIL_ID') == 0) {
          $SOCAR_ID_USER = $this->session->userdata('SOCAR_ID_USER');
          $employe = ' AND receiver.ID_EMPLOYE = '.$SOCAR_ID_USER.'';
        }
        else{
          $SOCAR_ID_USER = $this->session->userdata('SOCAR_ID_USER');  
          $employe = ' ';

        }

        
         $message=$this->Model->getRequeteOne('SELECT ID_MESSAGE_CONGE, ID_DEMANDE_CONGE, conge_type_message.DESCRIPTION AS TYPE_MESSAGE, DATE_SYSTEME_DEMANDE, CORPS_MESSAGE, conge_type_destinataire.DESCRIPTION AS TYPE_DESTINATAIRE, ID_EMPLOYE_EMETTEUR, sender.NOM AS SNOM, sender.PRENOM AS SPRENOM, ID_EMPLOYE_DESTINATAIRE, conge_message_conge.ID_TYPE_DESTINATAIRE, receiver.NOM AS RNOM, receiver.PRENOM AS RPRENOM, conge_message_conge.ID_STATUS_MESSAGE, conge_status_message.DESCRIPTION AS STATUS_MESSAGE FROM `conge_message_conge` LEFT JOIN conge_type_message ON conge_type_message.ID_TYPE_MESSAGE = conge_message_conge.ID_TYPE_MESSAGE LEFT JOIN conge_type_destinataire ON conge_type_destinataire.ID_TYPE_DESTINATAIRE = conge_message_conge.ID_TYPE_DESTINATAIRE LEFT JOIN rh_employe AS sender ON sender.ID_EMPLOYE = conge_message_conge.ID_EMPLOYE_EMETTEUR LEFT JOIN rh_employe AS receiver ON receiver.ID_EMPLOYE = conge_message_conge.ID_EMPLOYE_DESTINATAIRE LEFT JOIN conge_status_message ON conge_status_message.ID_STATUS_MESSAGE = conge_message_conge.ID_STATUS_MESSAGE WHERE 1 AND ID_MESSAGE_CONGE = '.$id.' ');

         // echo $id;
         // echo '<br>';
         // echo $employe;
         // print_r($message);
        // exit();

         $data['message']=$message;
         $data['nbmessage']=$this->Model->getRequeteOne('SELECT COUNT(ID_MESSAGE_CONGE) AS NB_MESSAGE_UNREAD FROM `conge_message_conge` LEFT JOIN rh_employe AS receiver ON receiver.ID_EMPLOYE = conge_message_conge.ID_EMPLOYE_DESTINATAIRE WHERE 1 AND conge_message_conge.ID_STATUS_MESSAGE = 1  '.$employe.' ');

         $data['data']=$this->Model->getRequeteOne('SELECT ID_DEMANDE_CONGE, config_exercice.DESCRIPTION AS EXERCICE, conge_credit.ID_CREDIT, conge_credit.NB_JOURS_RESTANT, rh_employe.NOM, rh_employe.PRENOM, DATE_DEMANDE, conge_status_conge.DESCRIPTION AS STATUS_CONGE, conge_status_conge.STATUS_CONGE AS STATUS_CONGES, organigramme_niveau.ID_NIVEAUX_ORGANIGRAMME, organigramme_niveau.DESCRIPTION AS NIVEAU , NB_JOURS_DEMANDE, NB_JOURS_ACCORDE, config_type_conge.NOM_CONGE AS TYPE_CONGE, organigramme_direction.DESCRIPTION AS DIRECTION, organigramme_service.DESCRIPTION AS SERVICE, organigramme_section.DESCRIPTION AS SECTION, conge_demande_conge.DATE_DERNIER_REPOSE, conge_demande_conge.DATE_DEBUT_CONGE_DEMANDE, conge_demande_conge.DATE_FIN_CONGE_DEMANDE FROM `conge_demande_conge` LEFT JOIN config_exercice ON config_exercice.ID_EXERCICE = conge_demande_conge.ID_EXERCICE LEFT JOIN conge_credit ON conge_credit.ID_CREDIT = conge_demande_conge.ID_CREDIT LEFT JOIN rh_employe ON rh_employe.ID_EMPLOYE = conge_demande_conge.ID_EMPLOYE JOIN conge_status_conge ON conge_status_conge.STATUS_CONGE = conge_demande_conge.STATUS_CONGE LEFT JOIN organigramme_niveau ON organigramme_niveau.ID_NIVEAUX_ORGANIGRAMME = conge_status_conge.ID_NIVEAUX JOIN config_type_conge ON config_type_conge.ID_TYPE_CONGE = conge_demande_conge.ID_TYPE_CONGE LEFT JOIN organigramme_direction ON organigramme_direction.ID_DIRECTION = conge_demande_conge.ID_DIRECTION LEFT JOIN organigramme_service ON organigramme_service.ID_SERVICE = conge_demande_conge.ID_SERVICE LEFT JOIN organigramme_section ON organigramme_section.ID_SECTION = conge_demande_conge.ID_SECTION WHERE conge_demande_conge.ID_DEMANDE_CONGE = '.$message['ID_DEMANDE_CONGE'].' ');

        $data['historique']=$this->Model->getRequete('SELECT DATE_TRAITEMENT, organigramme_direction.DESCRIPTION AS DIRECTION,  organigramme_service.DESCRIPTION AS SERVICE , organigramme_section.DESCRIPTION AS SECTION, rh_employe.NOM, rh_employe.PRENOM, conge_status_conge.DESCRIPTION AS STATUS_CONGE, COMMENTAIRE, DATE_DEBUT_CONGE_ACCORDE, DATE_FIN_CONGE_ACCORDE, organigramme_niveau.DESCRIPTION AS NIVEAU FROM `conge_traitement_conge` JOIN organigramme_direction ON organigramme_direction.ID_DIRECTION = conge_traitement_conge.ID_DIRECTION LEFT JOIN organigramme_service ON organigramme_service.ID_SERVICE=conge_traitement_conge.ID_DEPARTEMENT LEFT JOIN organigramme_section ON organigramme_section.ID_SECTION = conge_traitement_conge.ID_SERVICE JOIN rh_employe ON rh_employe.ID_EMPLOYE = conge_traitement_conge.ID_EMPLOYE_TRAITANT JOIN conge_status_conge ON conge_status_conge.STATUS_CONGE = conge_traitement_conge.STATUS_CONGE JOIN organigramme_niveau ON organigramme_niveau.ID_NIVEAUX_ORGANIGRAMME = conge_status_conge.ID_NIVEAUX WHERE 1 AND ID_DEMANDE_CONGE = '.$message['ID_DEMANDE_CONGE'].'');
   
         $data['title']='Details Messages';
           $this->load->view('Messages_Read_Reply_View',$data);
   
       }


public function readed($id)
    {
      $this->Model->update('conge_message_conge',array('ID_MESSAGE_CONGE'=>$id),array('ID_STATUS_MESSAGE'=>4));
      // $message = "<div class='alert alert-success' id='message'>
      //                       Direction 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('conge/Messages/read/'.$id.''));  
    }



    public function reply($id)
       {

        if ($this->session->userdata('SOCAR_PROFIL_ID') == 0) {
          $SOCAR_ID_USER = $this->session->userdata('SOCAR_ID_USER');
          $employe = ' AND receiver.ID_EMPLOYE = '.$SOCAR_ID_USER.'';
        }
        else{
          $SOCAR_ID_USER = $this->session->userdata('SOCAR_ID_USER');  
          $employe = ' ';

        }
        
         $message=$this->Model->getRequeteOne('SELECT ID_MESSAGE_CONGE, ID_DEMANDE_CONGE, conge_type_message.DESCRIPTION AS TYPE_MESSAGE, DATE_SYSTEME_DEMANDE, CORPS_MESSAGE, conge_type_destinataire.DESCRIPTION AS TYPE_DESTINATAIRE, ID_EMPLOYE_EMETTEUR, sender.NOM AS SNOM, sender.PRENOM AS SPRENOM, ID_EMPLOYE_DESTINATAIRE, receiver.NOM AS RNOM, receiver.PRENOM AS RPRENOM, conge_message_conge.ID_STATUS_MESSAGE, conge_status_message.DESCRIPTION AS STATUS_MESSAGE FROM `conge_message_conge` LEFT JOIN conge_type_message ON conge_type_message.ID_TYPE_MESSAGE = conge_message_conge.ID_TYPE_MESSAGE LEFT JOIN conge_type_destinataire ON conge_type_destinataire.ID_TYPE_DESTINATAIRE = conge_message_conge.ID_TYPE_DESTINATAIRE LEFT JOIN rh_employe AS sender ON sender.ID_EMPLOYE = conge_message_conge.ID_EMPLOYE_EMETTEUR LEFT JOIN rh_employe AS receiver ON receiver.ID_EMPLOYE = conge_message_conge.ID_EMPLOYE_DESTINATAIRE LEFT JOIN conge_status_message ON conge_status_message.ID_STATUS_MESSAGE = conge_message_conge.ID_STATUS_MESSAGE WHERE 1  '.$employe.' AND ID_MESSAGE_CONGE = '.$id.' ');
         $data['message']=$message;
         $data['nbmessage']=$this->Model->getRequeteOne('SELECT COUNT(ID_MESSAGE_CONGE) AS NB_MESSAGE_UNREAD FROM `conge_message_conge` LEFT JOIN rh_employe AS receiver ON receiver.ID_EMPLOYE = conge_message_conge.ID_EMPLOYE_DESTINATAIRE WHERE 1 AND conge_message_conge.ID_STATUS_MESSAGE = 1  '.$employe.' ');

         $data['data']=$this->Model->getRequeteOne('SELECT ID_DEMANDE_CONGE, config_exercice.DESCRIPTION AS EXERCICE, conge_credit.ID_CREDIT, conge_credit.NB_JOURS_RESTANT, rh_employe.NOM, rh_employe.PRENOM, DATE_DEMANDE, conge_status_conge.DESCRIPTION AS STATUS_CONGE, conge_status_conge.STATUS_CONGE AS STATUS_CONGES, organigramme_niveau.ID_NIVEAUX_ORGANIGRAMME, organigramme_niveau.DESCRIPTION AS NIVEAU , NB_JOURS_DEMANDE, NB_JOURS_ACCORDE, config_type_conge.NOM_CONGE AS TYPE_CONGE, organigramme_direction.DESCRIPTION AS DIRECTION, organigramme_service.DESCRIPTION AS SERVICE, organigramme_section.DESCRIPTION AS SECTION, conge_demande_conge.DATE_DERNIER_REPOSE, conge_demande_conge.DATE_DEBUT_CONGE_DEMANDE, conge_demande_conge.DATE_FIN_CONGE_DEMANDE FROM `conge_demande_conge` LEFT JOIN config_exercice ON config_exercice.ID_EXERCICE = conge_demande_conge.ID_EXERCICE LEFT JOIN conge_credit ON conge_credit.ID_CREDIT = conge_demande_conge.ID_CREDIT LEFT JOIN rh_employe ON rh_employe.ID_EMPLOYE = conge_demande_conge.ID_EMPLOYE JOIN conge_status_conge ON conge_status_conge.STATUS_CONGE = conge_demande_conge.STATUS_CONGE LEFT JOIN organigramme_niveau ON organigramme_niveau.ID_NIVEAUX_ORGANIGRAMME = conge_status_conge.ID_NIVEAUX JOIN config_type_conge ON config_type_conge.ID_TYPE_CONGE = conge_demande_conge.ID_TYPE_CONGE LEFT JOIN organigramme_direction ON organigramme_direction.ID_DIRECTION = conge_demande_conge.ID_DIRECTION LEFT JOIN organigramme_service ON organigramme_service.ID_SERVICE = organigramme_service.ID_SERVICE LEFT JOIN organigramme_section ON organigramme_section.ID_SECTION = conge_demande_conge.ID_SECTION WHERE conge_demande_conge.ID_DEMANDE_CONGE = '.$message['ID_DEMANDE_CONGE'].' ');

        $data['historique']=$this->Model->getRequete('SELECT DATE_TRAITEMENT, organigramme_direction.DESCRIPTION AS DIRECTION,  organigramme_service.DESCRIPTION AS SERVICE , organigramme_section.DESCRIPTION AS SECTION, rh_employe.NOM, rh_employe.PRENOM, conge_status_conge.DESCRIPTION AS STATUS_CONGE, COMMENTAIRE, DATE_DEBUT_CONGE_ACCORDE, DATE_FIN_CONGE_ACCORDE, organigramme_niveau.DESCRIPTION AS NIVEAU FROM `conge_traitement_conge` JOIN organigramme_direction ON organigramme_direction.ID_DIRECTION = conge_traitement_conge.ID_DIRECTION LEFT JOIN organigramme_service ON organigramme_service.ID_SERVICE=conge_traitement_conge.ID_DEPARTEMENT LEFT JOIN organigramme_section ON organigramme_section.ID_SECTION = conge_traitement_conge.ID_SERVICE JOIN rh_employe ON rh_employe.ID_EMPLOYE = conge_traitement_conge.ID_EMPLOYE_TRAITANT JOIN conge_status_conge ON conge_status_conge.STATUS_CONGE = conge_traitement_conge.STATUS_CONGE JOIN organigramme_niveau ON organigramme_niveau.ID_NIVEAUX_ORGANIGRAMME = conge_status_conge.ID_NIVEAUX WHERE 1 AND ID_DEMANDE_CONGE = '.$message['ID_DEMANDE_CONGE'].'');
   
         $data['title']='Reponse au Message de'.$message['SNOM'].' '.$message['SPRENOM'];
           $this->load->view('Messages_Reply_View',$data);
   
       }



       public function save_reply()
       {

      $ID_MESSAGE_CONGE =$this->input->post('ID_MESSAGE_CONGE'); 
      $ID_DEMANDE_CONGE=$this->input->post('ID_DEMANDE_CONGE');
      $DATE_SYSTEME_DEMANDE = date('Y-m-d H:i');
      $CORPS_MESSAGE=$this->input->post('CORPS_MESSAGE');
      $ID_STATUS_MESSAGE=1;
      $ID_TYPE_DESTINATAIRE=0;
      $ID_TYPE_MESSAGE=4;
      // $ID_TYPE_DESTINATAIRE=$this->input->post('ID_TYPE_DESTINATAIRE');

      $message = $this->Model->getRequeteOne('SELECT * FROM `conge_message_conge` WHERE 1 AND `ID_MESSAGE_CONGE` = '.$ID_MESSAGE_CONGE.'');

    

      $data_traitement = array(
      'ID_DEMANDE_CONGE' =>$ID_DEMANDE_CONGE,
      'ID_TYPE_MESSAGE' =>$ID_TYPE_MESSAGE,
      'DATE_SYSTEME_DEMANDE' =>$DATE_SYSTEME_DEMANDE,
      'CORPS_MESSAGE' =>$CORPS_MESSAGE,
      'ID_TYPE_DESTINATAIRE' =>$ID_TYPE_DESTINATAIRE,
      'ID_EMPLOYE_EMETTEUR' =>$message['ID_EMPLOYE_DESTINATAIRE'],
      'ID_STATUS_MESSAGE' => $ID_STATUS_MESSAGE,
      'ID_EMPLOYE_DESTINATAIRE' =>$message['ID_EMPLOYE_EMETTEUR'],      
     );

    $this->Model->insert_last_id('conge_message_conge',$data_traitement);

    $this->Model->update('conge_message_conge',array('ID_MESSAGE_CONGE'=>$ID_MESSAGE_CONGE),array('ID_STATUS_MESSAGE'=>4));

  
    redirect(base_url('conge/Messages'));  
   
       }

}