<?php
  include VIEWPATH.'includes/new_header.php';
  ?>
  
<body class="hold-transition sidebar-mini">
<!-- Site wrapper -->
<div class="wrapper">
  <?php
  include VIEWPATH.'includes/new_top_menu.php';
  include VIEWPATH.'includes/new_menu_principal.php';
  ?>

  

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <?php 
    include 'includes/menu_demande_conge.php';
    ?>

    <!-- Main content -->
    <section class="content">

      <!-- Default box -->
      <div class="card">
      
      <div class="card card-primary">
              <div class="card-header">
                <h3 class="card-title">Traitement demande d'un cong&eacute;</h3>
              </div>
              <?php 
                          if(!empty($this->session->flashdata('message')))
                             echo $this->session->flashdata('message');
            ?>
              <!-- /.card-header -->
              <!-- form start -->
              <?php 
              $employe= $this->Model->getRequeteOne("SELECT rh_employe_affectation.ID_DIRECTION, organigramme_direction.DESCRIPTION AS DIRECTION, rh_employe_affectation.ID_RESPONSABILITE, rh_employe_affectation.ID_SERVICE, organigramme_service.DESCRIPTION AS DEPARTEMENT, rh_employe_affectation.ID_SECTION, organigramme_section.DESCRIPTION AS SERVICE, rh_employe.NOM, rh_employe.PRENOM , rh_employe.ID_EMPLOYE FROM rh_employe_affectation JOIN organigramme_direction ON organigramme_direction.ID_DIRECTION = rh_employe_affectation.ID_DIRECTION LEFT JOIN organigramme_service ON organigramme_service.ID_SERVICE = rh_employe_affectation.ID_SERVICE LEFT JOIN organigramme_section ON organigramme_section.ID_SECTION = rh_employe_affectation.ID_SECTION JOIN rh_employe ON rh_employe.ID_EMPLOYE = rh_employe_affectation.ID_EMPLOYE WHERE rh_employe_affectation.STATUS_AFFECTATION = 1 AND rh_employe_affectation.ID_EMPLOYE = ".$this->session->userdata('ID_EMPLOYE')." ");
              // print_r($employe);
              // ID_NIVEAUX_ORGANIGRAMME 

              // echo "<br>";
              $different = 'AND STATUS_CONGE != '.$data['STATUS_CONGES']. ' AND STATUS_CONGE NOT IN (1,4,7)';

              if ($employe['SERVICE'] != NULL || $employe['ID_SECTION'] != 0) {
                // echo "1<br>";
                $niveau = ' AND ID_NIVEAUX = 1';
                $niveaunb = 1;
              }
              elseif ($employe['DEPARTEMENT'] != NULL || $employe['ID_SERVICE'] != 0) {
                $niveau = ' AND ID_NIVEAUX = 2';
                $niveaunb = 2;
                // echo "2<br>";
              }
              elseif ($employe['DIRECTION'] != NULL || $employe['ID_DIRECTION'] != 0) {
                $niveau = ' AND ID_NIVEAUX = 3';
                $niveaunb = 3;
                // echo "3<br>";
              }
              else {
                $niveau = ' AND ID_NIVEAUX = 4';
                $niveaunb = 4;

                // echo "3<br>";
              }
              

              $sniveau = '';

              

              // if (in_array('29',$this->session->userdata('SOCAR_DROIT'))){

                // $newniveau = $niveaunb + 1;
                
                // $niveau_actuel=$this->Model->getRequeteOne('SELECT * FROM `conge_status_conge` WHERE 1 AND ID_NIVEAUX = '.$newniveau.' ');
                $status_conge=$this->Model->getRequete('SELECT * FROM `conge_status_conge` WHERE 1 '.$different.' ');
              // }
              // else{
                $status_conge=$this->Model->getRequete('SELECT * FROM `conge_status_conge` WHERE 1 '.$different.' '.$niveau.' '.$sniveau.' ');
              // }
              
              ?>
                <div class="card-body row">
                <div class="form-group col-lg-12 text-center">
                  <h3>Traitement d'un conge</h3>
                </div>
                
                <div class="form-group col-lg-12">
                  <h5>Details du conge</h5>
                  <table class="table table-striped">
                    <tr>
                      <td>Exercice</td>
                      <td>
                        <?php
                        $datacredit=$this->Model->getRequete('SELECT config_exercice.DESCRIPTION AS EXERCICE , conge_credit.NB_JOURS_CREDITE, conge_credit.NB_JOURS_RESTANT FROM conge_credit JOIN config_exercice ON config_exercice.ID_EXERCICE = conge_credit.ID_EXERCICE WHERE `ID_EMPLOYE` = '.$data['ID_EMPLOYE'].' ORDER BY config_exercice.STATUS_ACTIF DESC LIMIT 2');

                        if (!empty($datacredit[0]['EXERCICE'])) {
                          $premier = $datacredit[0]['EXERCICE'];
                          // echo $premier;
                          echo'<button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#myModal">
                          Excercice actuelle '.$premier.'
                        </button>
                        
                        <!-- Modal -->
                        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                          <div class="modal-dialog" role="document">
                            <div class="modal-content">
                             
                              <div class="modal-body">
                                <table class="table">
                                <tr>
                                  <td>Exercice</td>
                                  <td>'.$datacredit[0]['EXERCICE'].'</td>
                                </tr>
                                <tr>
                                  <td>Nb jours crédité</td>
                                  <td>'.$datacredit[0]['NB_JOURS_CREDITE'].'</td>
                                </tr>
                                <tr>
                                  <td>Nb jours restant</td>
                                  <td>'.$datacredit[0]['NB_JOURS_RESTANT'].'</td>
                                </tr>
                                </table>
                              </div>
                              <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">Fermer</button>
                              </div>
                            </div>
                          </div>
                        </div>';
                        }

                        if (!empty($datacredit[1]['EXERCICE'])) {
                          $second = $datacredit[1]['EXERCICE'];
                          // echo $second;
                          echo'<button type="button" class="btn btn-danger btn-xs" data-toggle="modal" data-target="#myModaldeux">
                          Excercice precedent '.$second.'
                        </button>
                        
                        <!-- Modal -->
                        <div class="modal fade" id="myModaldeux" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                          <div class="modal-dialog" role="document">
                            <div class="modal-content">
                             
                              <div class="modal-body">
                                <table class="table">
                                <tr>
                                  <td>Exercice</td>
                                  <td>'.$datacredit[1]['EXERCICE'].'</td>
                                </tr>
                                <tr>
                                  <td>Nb jours crédité</td>
                                  <td>'.$datacredit[1]['NB_JOURS_CREDITE'].'</td>
                                </tr>
                                <tr>
                                  <td>Nb jours restant</td>
                                  <td>'.$datacredit[1]['NB_JOURS_RESTANT'].'</td>
                                </tr>
                                </table>
                              </div>
                              <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">Fermer</button>
                              </div>
                            </div>
                          </div>
                        </div>';
                        }
                        ?>
                      
                    
                    
                    </td> 
                      <td>Employ&eacute;</td>
                      <td><?php echo $data['NOM']?> <?php echo $data['PRENOM']?></td> 
                    </tr>
                    
                    <tr>
                      <td>Conge & Nb Jours Restant</td>
                      <td><?php echo $data['TYPE_CONGE']?> restant avec <?php echo $data['NB_JOURS_RESTANT']?> Jours</td> 
                      <td>Periode conge</td>
                      <td><?php echo $data['DATE_DEBUT_CONGE_DEMANDE']?> au <?php echo $data['DATE_FIN_CONGE_DEMANDE']?> (<?php echo $data['NB_JOURS_DEMANDE']?> Jours)</td> 
                    </tr>
                    
                    <tr>
                      <td>Niveau de demande</td>
                      <td><?php echo $data['NIVEAU']?></td> 
                      <td>Affectation</td>
                      <td>
                      <button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#modal-default">
                  Affectation
                </button>
                  <div class="modal fade" id="modal-default">
                  <div class="modal-dialog">
                    <div class="modal-content">
                      <div class="modal-header">
                        <h4 class="modal-title">Affectation</h4>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                          <span aria-hidden="true">&times;</span>
                        </button>
                      </div>
                      <div class="modal-body">
                        <table class="table">
                          <tr>
                            <td>Direction</td>
                            <td><?php echo $data['DIRECTION']?></td>
                          </tr>
                          <tr>
                            <td>Service</td>
                            <td><?php echo $data['DEPARTEMENT']?></td>
                          </tr>
                          <tr>
                            <td>Section</td>
                            <td><?php echo $data['SERVICE']?></td>
                          </tr>
                          <tr>
                            <td>Status</td>
                            <td><?php echo $data['STATUS_CONGE']?></td>
                          </tr>
                          
                        </table>
                      </div>
                      <div class="modal-footer justify-content-between">
                        <button type="button" class="btn btn-primary" data-dismiss="modal">Fermer</button>
                      </div>
                    </div>
                    <!-- /.modal-content -->
                  </div>
                  <!-- /.modal-dialog -->
                </div>
                      </td> 
                    </tr>
                    
                  </table>

                  
                </div>
                <div class="form-group col-lg-12">
                  <h5>Historique de traitement</h5>

                  <table class="table">
                    <tr>
                      <td>Date Traitement</td>
                      <td>Direction</td>
                      <td>Departement</td>
                      <td>Service</td>
                      <td>Employe</td>
                      <td>Status</td>
                      <!-- <td>Niveau</td> -->
                      <td>Commentaire</td>
                    </tr>
                    
                    <?php
                    foreach ($historique as $value) {                    
                    ?>

                    <tr>
                      <td><?php echo $value['DATE_TRAITEMENT']; ?></td>
                      <td><?php echo $value['DIRECTION']; ?></td>
                      <td><?php echo $value['DEPARTEMENT']; ?></td>
                      <td><?php echo $value['SERVICE']; ?></td>
                      <td><?php echo $value['NOM']; ?> <?php echo $value['PRENOM']; ?></td>
                      <td><?php echo $value['STATUS_CONGE']; ?></td>
                      <!-- <td><?php echo $value['NIVEAU']; ?></td> -->
                      <td><?php echo $value['COMMENTAIRE']; ?></td>
                    </tr>

                    <?php
                    }
                    ?>

                  </table>
                </div>
                <div class="form-group col-lg-12 row">
                  <div class="alert alert-danger col-lg-4" role="alert">
                    <!-- <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span> -->
                    <!-- <span class="sr-only">Error:</span> -->
                    
                    <?php 
                    $location_dema= $this->Model->getRequeteOne("SELECT ID_DIRECTION, ID_SERVICE, ID_SECTION, DATE_FIN_CONGE_ACCORDE FROM `conge_demande_conge` WHERE ID_DEMANDE_CONGE = ".$data['ID_DEMANDE_CONGE']." ");
                    $congedonne= $this->Model->getRequeteOne("SELECT COUNT(ID_DEMANDE_CONGE) AS NB_CONGE_DONNE FROM `conge_demande_conge` WHERE DATE_FIN_CONGE_ACCORDE >= CURDATE() AND STATUS_CONGE = 11 AND ID_DIRECTION = ".$location_dema['ID_DIRECTION']." AND ID_SERVICE = ".$location_dema['ID_SERVICE']." AND ID_SECTION = ".$location_dema['ID_SECTION']."  AND DATE_DEBUT_CONGE_ACCORDE <= CURDATE() ");
                    ?>
                    Conge dans le service<br>
                    <b><?php echo $congedonne['NB_CONGE_DONNE']?></b>
                  </div>
                  <div class="alert alert-primary col-lg-4" role="alert">
                    <?php
                    $totafect= $this->Model->getRequeteOne("SELECT COUNT(ID_AFFECTATION) AS ID_AFFECTATION FROM `rh_employe_affectation` WHERE 1 AND ID_DIRECTION = ".$location_dema['ID_DIRECTION']." AND ID_SERVICE = ".$location_dema['ID_SERVICE']." AND ID_SECTION = ".$location_dema['ID_SECTION']." AND STATUS_AFFECTATION = 1");
                    $personne_au_boulot = $totafect['ID_AFFECTATION'] - $congedonne['NB_CONGE_DONNE'];
                    ?>
                    Personne actuel au service<br>
                    <?php echo $personne_au_boulot?>

                  </div>
                  <div class="alert alert-danger col-lg-4" role="alert">
                    
                    <?php
                    $total_jrs= $this->Model->getRequeteOne("SELECT SUM(NB_JOURS_ACCORDE) AS NB_JOURS FROM `conge_demande_conge` WHERE `ID_EMPLOYE` = ".$data['ID_EMPLOYE']." AND `STATUS_CONGE` = 11");
                    ?>
                    Total jrs deja pris par l'employe cet exercice<br>
                    <?php echo $total_jrs['NB_JOURS']?>
                  </div>
                </div>
                <div class="form-group col-lg-12">
                  <h5>Traitement du conge</h5>
                  <form id="FormData" action="<?php echo base_url()?>conge/Demande_Conge/traiter" method="POST" enctype="multipart/form-data">

                  <div class="row">

                  <div class="form-group col-lg-4">
                    <label for="exampleInputEmail1">Status</label>
                    <select class="custom-select" name="STATUS_CONGE" id="STATUS_CONGE" onchange="check_refus(this)">
                          <option value="">-- Select --</option>
                          <?php
                          
                          if ($data['STATUS_CONGES'] == 8 &&  $this->session->userdata('SOCAR_PROFIL_ID') == 3) {
                            echo"<option value='11'>Accord Final</option>";
                            echo"<option value='12'>Refus Final</option>";
                          }
                          else if ($data['STATUS_CONGES'] == 1 && $this->session->userdata('SOCAR_PROFIL_ID') == 8) {
                            echo"<option value='13'>Accord Administration & Logistique</option>";
                            echo"<option value='14'>Refus Administration & Logistique</option>";
                          }
                          else if ($data['STATUS_CONGES'] == 4 && $this->session->userdata('SOCAR_PROFIL_ID') == 8) {
                            echo"<option value='13'>Accord Administration & Logistique</option>";
                            echo"<option value='14'>Refus Administration & Logistique</option>";
                          }
                          // else if ($data['STATUS_CONGES'] == 8 && $this->session->userdata('SOCAR_PROFIL_ID') == 8) {
                          //   echo"<option value='13'>Accord Administration & Logistique</option>";
                          //   echo"<option value='14'>Refus Administration & Logistique</option>";
                          // }
                          elseif ($data['STATUS_CONGES'] == 13) {
                            echo"<option value='5'>Accordé Niveaux Service</option>";
                            echo"<option value='6'>Refusé Niveaux Service</option>";
                          }
                          else{
                            foreach ($status_conge as $type_conges) {
                           
                           echo"<option value='".$type_conges['STATUS_CONGE']."'>".$type_conges['DESCRIPTION']." </option>";
                          }
                          }

                          
                          ?>
                        </select>
                        <input type="hidden" id="REFUS_STATUS" name="REFUS_STATUS">
                    <?php echo form_error('STATUS_CONGE', '<div class="text-danger">', '</div>'); ?>
                  </div>

                  <div class="form-group col-lg-4">
                    <label for="exampleInputEmail1">Nb de jours accord&eacute;</label>
                    <?php
                    if ($data['NB_JOURS_ACCORDE'] != NULL) {
                      $NB_JOURS = $data['NB_JOURS_ACCORDE'];
                    }
                    else{
                      $NB_JOURS = $data['NB_JOURS_DEMANDE'];
                    }
                     ?>
                    <input type="number" class="form-control" id="NB_JOURS_ACCORDE" name="NB_JOURS_ACCORDE" value="<?php echo $NB_JOURS; ?>">
                    <input type="hidden" id="ID_DEMANDE_CONGE" step=".01" name="ID_DEMANDE_CONGE" placeholder="" value="<?php echo $data['ID_DEMANDE_CONGE'];?>">
                    <?php echo form_error('NB_JOURS_ACCORDE', '<div class="text-danger">', '</div>'); ?>
                  </div>


                  <div class="form-group col-lg-4">
                    <label for="exampleInputPassword1">Date de debut</label>
                    <input type="date" class="form-control" data-target="#DATE_DEBUT_CONGE_ACCORDE" name="DATE_DEBUT_CONGE_ACCORDE" required="required" value="<?php echo $data['DATE_DEBUT_CONGE_DEMANDE']?>" readonly/>
                    <!-- <div class="input-group date" id="DATE_DEBUT_CONGE_ACCORDE" data-target-input="nearest">
                        
                        <div class="input-group-append" data-target="#DATE_DEBUT_CONGE_ACCORDE" data-toggle="datetimepicker">
                            <div class="input-group-text"><i class="fa fa-calendar"></i></div>
                        </div>
                    </div> -->
                    <?php echo form_error('DATE_DEBUT_CONGE_ACCORDE', '<div class="text-danger">', '</div>'); ?>
                  </div>

                  <div class="form-group col-lg-12">
                    <label for="exampleInputPassword1">Commentaire</label>
                    <textarea class="form-control" name="COMMENTAIRE" id="COMMENTAIRE" rows="3"><?=set_value('COMMENTAIRE')?></textarea>
                    
                    <?php echo form_error('COMMENTAIRE', '<div class="text-danger">', '</div>'); ?>
                  </div>

                  <div class="form-group col-lg-12">
                    <label for="exampleInputEmail1">Employe connect&eacute;</label>
                    
              

                  <input type="text" class="form-control" value="<?php echo $employe['NOM']?> <?php echo $employe['PRENOM']?>" readonly="readonly">
                  <input type="hidden" id="ID_EMPLOYE" name="ID_EMPLOYE" value="<?php echo $employe['ID_EMPLOYE']?>">
                  
                

                    <?php echo form_error('ID_EMPLOYE', '<div class="text-danger">', '</div>'); ?>
                  </div>

                  
                  
                  
                  <div class="form-group col-lg-12">
                  <button type="submit" class="btn btn-primary btn-block">Enregistrer</button>
                  </div>
                  </div>
                  </form>
                </div>
                
                  <input type="hidden" id="DATE_DEBUT_CONGE_DEMANDES" name="DATE_DEBUT_CONGE_DEMANDES" value="<?php echo $data['DATE_DEBUT_CONGE_DEMANDE']?>">
                   
                  
                  
                       
                 
                  
                </div>
                <!-- /.card-body -->

                
            </div>
        
        
        <!-- /.card-body -->
        <!-- <div class="card-footer">
          Footer
        </div> -->
        <!-- /.card-footer-->
      </div>
      <!-- /.card -->

    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->

  <?php
 include VIEWPATH.'includes/new_copy_footer.php';  
  ?>
  <!-- /.control-sidebar -->
</div>
<!-- ./wrapper -->
<?php
  include VIEWPATH.'includes/new_script.php';
  ?>
<script>

function get_departement(va){
 var ID_DIRECTION= $(va).val();
 $.post('<?php echo base_url('rh/Employe/get_departement')?>',
     {ID_DIRECTION:ID_DIRECTION},
     function(data){
       $('#ID_SERVICE').html(data);
     });
}
</script>


<script>

function check_refus(va){
 var STATUS_CONGE= $(va).val();
 $.post('<?php echo base_url('conge/Demande_Conge/check_refus')?>',
     {STATUS_CONGE:STATUS_CONGE},
     function(data){
       $('#REFUS_STATUS').val(data);
       if (data==1) {
        alert('Vous devez mettre un commentaire si c\'est un refus');
        document.getElementById('COMMENTAIRE').required = true;
       }
       else{
        document.getElementById('COMMENTAIRE').required = false;
       }
     });
}
</script>

<script>

function get_service(va){
 var ID_SERVICE= $(va).val();
 $.post('<?php echo base_url('rh/Employe/get_service')?>',
     {ID_SERVICE:ID_SERVICE},
     function(data){
       $('#ID_SECTION').html(data);
     });
}
</script>
<script>
  $(function () {
   
    //Date picker
    $('#DATE_ENTRE_SERVICE').datetimepicker({
        format: 'L'
    });   

  })
 


</script>
<script>
  $(function () {
    //Initialize Select2 Elements
    $('.select2').select2()

    //Initialize Select2 Elements
    $('.select2bs4').select2({
      theme: 'bootstrap4'
    })
  })
</script>

<!-- <script>
  $(function () {
    //Date picker
    var DATE_DEBUT_CONGE_DEMANDES= $('#DATE_DEBUT_CONGE_DEMANDES').val();
    $('#DATE_DEBUT_CONGE_ACCORDE').datetimepicker({
        format: 'L',
        defaultDate: new Date(DATE_DEBUT_CONGE_DEMANDES),
        format: 'DD/MM/YYYY'
    });   
  })
</script> -->

<!-- <script>
  $(function () {
      var DATE_DEBUT_CONGE_DEMANDES= $('#DATE_DEBUT_CONGE_DEMANDES').val();
      alert(DATE_DEBUT_CONGE_DEMANDES);
    $('#DATE_DEBUTCONGE_ACCORDE').datetimepicker({
        format: 'L',
        defaultDate: new Date(DATE_DEBUT_CONGE_DEMANDES),
        format: 'DD/MM/YYYY'
    });   
  })
</script> -->
</body>
</html>
