<?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_traitement_courier.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 du courier recu</h3>
              </div>
              <!-- /.card-header -->
              <!-- form start -->
                <?php 
                          if(!empty($this->session->flashdata('message')))
                             echo $this->session->flashdata('message');
            ?>
            
            
                <div class="card-body row">
                  <div class="form-group col-lg-2"> <label for="exampleInputEmail1">Type de courier : </label> </div>
                  <div class="form-group col-lg-4"> <?php echo $resultat['TYPE_COURIER']?> </div>
                  <div class="form-group col-lg-2"> <label for="exampleInputEmail1">Nature du courier : </label> </div>
                  <div class="form-group col-lg-4"><?php echo $resultat['NOM_CORRESPONDANT']?></div>
                  <div class="form-group col-lg-2">
                    <label for="exampleInputEmail1">Référence du courier : </label>  </div>
                  <div class="form-group col-lg-4"><?php echo $resultat['NUM_COURIER']?> </div>
                  <div class="form-group col-lg-2"><label for="exampleInputEmail1">Personne en copie : </label></div>
                 
                  <div class="form-group col-lg-4">
                  <?php foreach ($resultatcopie as $key => $value) {
                      echo $value['DESTINATAIRE_COPIE'].'<br>';
                    } ?>
                  </div>
                    
                   
                  
                  <div class="form-group col-lg-2">
                    <label for="exampleInputEmail1">Objet du courier : </label> </div>
                  <div class="form-group col-lg-4"><?php echo $resultat['OBJET_COURIER']?> </div>
                  <div class="form-group col-lg-2">
                    <label for="exampleInputEmail1">Date reception du courier : </label> </div>
                  <div class="form-group col-lg-4"><?php echo $resultat['DATE_TIME_ENREGISTREMENT']?> </div>
                  <div class="form-group col-lg-2">
                    <label for="exampleInputEmail1">Commentaire : </label> </div>
                  <div class="form-group col-lg-4"><?php echo $resultat['COMMENTAIRE']?> </div>
                  <div class="form-group col-lg-2">
                    <label for="exampleInputEmail1">Affectation actuel : </label> </div>
                  <div class="form-group col-lg-4"><?php echo $resultat['AFFECTATION_ACTUEL']?> </div>

                  <div class="form-group col-lg-2">
                    <label for="exampleInputEmail1">Voir documents : </label> 
                    
                    
                  </div>

                  <div class="form-group col-lg-4">
                  <button type="button" style="" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#myModalalldoc">
                        Documents
                      </button>
                      <div class="modal fade" id="myModalalldoc" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                        <div class="modal-dialog modal-lg" role="document" style="max-width: 80%;">
                          <div class="modal-content">
                         
                            <div class="modal-body">
                              <?php
                        $urlpass = base_url();
                        $elementspas = explode("/", $urlpass);
                        $nouveau_urlpass = null ;
                          $indicepass = sizeof($elementspas);
                          for ($i = 0; $i < ($indicepass - 2); $i++) {
                              $nouveau_urlpass .=$elementspas[$i] . '/';
                            }

                          ?>
                          <div class="text-center"><h6>Scan du document</h6></div>

                        <embed src="<?=$nouveau_urlpass?>/uploads/scandocument/<?=$resultat['SCAN_ORIGINAL']?>" type="application/pdf" scrolling="auto" height="500px" width="100%">

                            </div>
                            <div class="modal-footer">
                              <button type="button" class="btn btn-default" data-dismiss="modal">Fermer</button>
                            </div>
                          </div>
                        </div>
                      </div>
                
                </div>
                <div class="form-group col-lg-12 text-center">
                    <label for="exampleInputEmail1">Historique de traitement</label> 
                  </div> 
                  <div class="form-group col-lg-12">
                  <table id="example1" class="table table-bordered table-striped table-hover">
                  <thead>
                  <tr>
                    <th>Date Traitement</th>
                    <th>Avis</th>
                    <th>Traitant</th>
                    <!-- <th>Attachement</th> -->
                    <th>Commentaire</th>
                    <th>Personnel Designe</th>
                    <th>En copie</th>
                    <th>Deadline</th>
                    <th>Fichier</th>
                    <!-- <th></th> -->
                    
                  </tr>
                  </thead>
                  <tbody>

                  <?php
                    foreach ($historique as $key) 
                    {
                       
                      $urlpass = base_url();
                      $elementspas = explode("/", $urlpass);
                      $nouveau_urlpass = null ;
                        $indicepass = sizeof($elementspas);
                        for ($i = 0; $i < ($indicepass - 2); $i++) {
                            $nouveau_urlpass .=$elementspas[$i] . '/';
                          }
                          if ($key['ATTACHEMENT'] != NULL) {
                            $buttons = "<button type='button' class='btn btn-primary btn-xs' data-toggle='modal' data-target='#myModalDoc".$key['ID_COURIER_HISTORIQUE']."'>
                        Attachement
                      </button>";
                          }
                          else{
                            $buttons = '';
                          }
                          
                          $list = $this->Model->getRequete('SELECT rh_employe.NOM, rh_employe.PRENOM FROM `cour_courier_en_copie_traitement` JOIN rh_employe ON rh_employe.ID_EMPLOYE = cour_courier_en_copie_traitement.ID_RH_COPIE WHERE ID_COURIER_HISTORIQUE = '.$key['ID_COURIER_HISTORIQUE'].'');
                          $lcopie ='';
                          foreach ($list as $encopie) {
                            $lcopie .=$encopie['NOM'].' '.$encopie['PRENOM'].'<br>';
                          }


                          $encopie = "<button type='button' class='btn btn-primary btn-xs' data-toggle='modal' data-target='#myModalCopie".$key['ID_COURIER_HISTORIQUE']."'>
                        En copie
                      </button>
                      <div class='modal fade' id='myModalCopie".$key['ID_COURIER_HISTORIQUE']."' tabindex='-1' role='dialog' aria-labelledby='myModalLabel'>
                        <div class='modal-dialog' role='document'>
                          <div class='modal-content'>
                         
                            <div class='modal-body'>
                          <div class='text-center'><h6>Personne en copie</h6></div>

                        ".$lcopie."

                            </div>
                            <div class='modal-footer'>
                              <button type='button' class='btn btn-default' data-dismiss='modal'>Fermer</button>
                            </div>
                          </div>
                        </div>
                      </div>";
                    $date_enregistrement = $key['DATE_TRAITEMENT'];
                    $date_formattee = date('h:i d/m/Y', strtotime($date_enregistrement));

                    $date_traitement= $key['DEADLINE'];
                    $date_fo_traitement = date('h:i d/m/Y', strtotime($date_traitement));
                     echo "<tr>
                     <td>".$date_formattee."</td>
                     <td>".$key['ID_STATUS_COURIER']."</td>
                     <td>".$key['ID_RH_TRAITANT']."</td>
                     <td>".$key['COMMENTAIRE']."</td>
                     <td>".$key['PERSONNEL_DESIGNE']."</td>
                     <td>".$encopie."</td>
                     <td>".$date_fo_traitement."</td>
                     <td>
                     ".$buttons."
                      <div class='modal fade' id='myModalDoc".$key['ID_COURIER_HISTORIQUE']."' tabindex='-1' role='dialog' aria-labelledby='myModalLabel'>
                        <div class='modal-dialog modal-lg' role='document' style='max-width: 80%;'>
                          <div class='modal-content'>
                         
                            <div class='modal-body'>
                          <div class='text-center'><h6>Scan du document</h6></div>

                        <embed src='".$nouveau_urlpass."/uploads/scandocument/".$key['ATTACHEMENT']."' type='application/pdf' scrolling='auto' height='500px' width='100%'>

                            </div>
                            <div class='modal-footer'>
                              <button type='button' class='btn btn-default' data-dismiss='modal'>Fermer</button>
                            </div>
                          </div>
                        </div>
                      </div>
                     
                     </td>
                   </tr>";
           
                }
                  ?>
                  
                  </tfoot>
                </table>
                  </div> 
                
                <form id="FormData" action="<?php echo base_url()?>courier/Traitement_Courier/add" method="POST" accept-charset="utf-8" enctype="multipart/form-data">
              <input type="hidden" name="ID_COURIER" id="ID_COURIER" value="<?php echo $resultat['ID_COURIER']?>">

                

                <div class="card-body row">
                <div class="form-group col-lg-12 text-center">
                    <label for="exampleInputEmail1">Traitement du dossier proprement dite</label>
                    
                  </div> 
                <div class="form-group col-lg-4">
                    <label for="exampleInputEmail1">Action à faire <spam class="text-danger">*</spam></label>
                    <select class="custom-select" name="ID_STATUS_COURIER" id="ID_STATUS_COURIER">
                          <option>-- Select --</option>
                          <?php
                          foreach ($status_courier as $status) {
                           echo"<option value='".$status['ID_STATUS_COURIER']."'>".$status['DESCRIPTION']."</option>";
                          }
                          ?>
                        </select>
                    <?php echo form_error('ID_STATUS_COURIER', '<div class="text-danger">', '</div>'); ?>
                  </div> 

                  <div class="form-group col-lg-4">
                  <label for="exampleInputEmail1">Deadline</label>
                  <!-- <input type="number" name="" class="form-control" > -->
                  <!-- <input type="text" class="form-control" id="datemask" data-inputmask-alias="datetime" data-inputmask-inputformat="dd/mm/yyyy" data-mask> -->
                  <!-- <input type="text" class="form-control" id="datemask" name="DEADLINE" data-inputmask-alias="datetime" data-inputmask-inputformat="dd/mm/yyyy hh:ss" data-mask> -->
                  <input type="text" class="form-control" id="datemask" name="DEADLINE" data-inputmask-alias="datetime" data-inputmask-inputformat="dd/mm/yyyy HH:MM" data-mask >

                    
                  </div>
                  
                  
                  <div class="form-group col-lg-4">
                  <label for="exampleInputEmail1">Fichier scan (.pdf)</label>
                  <input type="file" name="ATTACHEMENT" class="form-control" accept=".pdf">
                    
                    <?php echo form_error('ATTACHEMENT', '<div class="text-danger">', '</div>'); ?>
                  </div>
                  
                  <div class="form-group col-lg-4">
                    <label for="exampleInputEmail1">Direction traitant </label>
                    <select class="custom-select" name="ID_DIRECTION" id="ID_DIRECTION" onchange="get_departement(this)">
                          <option>-- Select --</option>
                          <?php
                          foreach ($direction as $direct) {
                           echo"<option value='".$direct['ID_DIRECTION']."'>".$direct['DESCRIPTION']."</option>";
                          }
                          ?>
                        </select>
                    <?php echo form_error('ID_DIRECTION', '<div class="text-danger">', '</div>'); ?>
                  </div> 
                  <div class="form-group col-lg-4">
                    <label for="exampleInputEmail1">Service traitant </label>
                    <select class="custom-select" name="ID_SERVICE" id="ID_SERVICE" onchange="get_service(this)">
                          <option>-- Select --</option>
                          
                        </select>
                  </div> 
                  <div class="form-group col-lg-4 ">
                    <label for="exampleInputEmail1">Personnel traitant </label>
                    <select class="custom-select" name="DESTINATAIRE_PRINCIPAL" id="DESTINATAIRE_PRINCIPAL">
                          
                        </select>
                    <?php echo form_error('DESTINATAIRE_PRINCIPAL', '<div class="text-danger">', '</div>'); ?>
                  </div> 
                  <div class="form-group col-lg-12">
                    <label for="exampleInputEmail1">Traitant secondaire</label>
                  </div>
                  <div class="form-group col-lg-4">
                    <label for="exampleInputEmail1">Direction traitant secondaire <spam class="text-danger">*</spam></label>
                    <select class="custom-select" name="ID_DIRECTIONC" id="ID_DIRECTIONC" onchange="get_departementc(this)">
                          <option>-- Select --</option>
                          <?php
                          foreach ($direction as $direct) {
                           echo"<option value='".$direct['ID_DIRECTION']."'>".$direct['DESCRIPTION']."</option>";
                          }
                          ?>
                        </select>
                  </div> 
                  <div class="form-group col-lg-3">
                    <label for="exampleInputEmail1">Service traitant secondaire </label>
                    <select class="custom-select" name="ID_SERVICEC" id="ID_SERVICEC" onchange="get_servicec(this)">
                          <option>-- Select --</option>
                          
                        </select>
                  </div> 
                  <div class="form-group col-lg-4">
                    <label for="exampleInputEmail1">Traitant secondaire <spam class="text-danger">*</spam></label>
                    <select class="custom-select" name="ID_RH_COPIE" id="ID_RH_COPIE">
                          
                        </select>
                  </div> 
                  <div class="form-group col-md-1">
                        <label for=""> _ </label>
                        <a  class="btn btn-primary btn-block" onclick="addElement()">+</a>

                      </div>

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

                <div class="card-footer">
                  <button type="submit" class="btn btn-primary btn-block">Enregistrer</button>
                </div>
              </form>
                  
                 
                  
               
                  
                </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('courier/Enregistrement_Courier/get_departement')?>',
     {ID_DIRECTION:ID_DIRECTION},
     function(data){
       $('#ID_SERVICE').html(data);
     });
$.post('<?php echo base_url('courier/Enregistrement_Courier/get_destinataire')?>',
     {ID_DIRECTION:ID_DIRECTION},
     function(data){
       $('#DESTINATAIRE_PRINCIPAL').html(data);
     });
}
</script>

<script>

function get_service(va){
 var ID_SERVICE= $(va).val();
 $.post('<?php echo base_url('courier/Enregistrement_Courier/get_destinataire_ser')?>',
     {ID_SERVICE:ID_SERVICE},
     function(data){
       $('#DESTINATAIRE_PRINCIPAL').html(data);
     });
}
</script>
<!-- <script>
$(document).ready(function() {
  $('#datemask').inputmask();
});
</script> -->
<!-- <script>
$(document).ready(function() {
  $('#datemask').inputmask({
    alias: 'datetime',
    inputFormat: 'dd/mm/yyyy HH:MM',
    placeholder: 'dd/mm/yyyy HH:MM',
    regex: "([0-3][0-9])/([0-1][0-9])/(20[2-9][0-9]) ([0-2][0-9]):([0-5][0-9])",
    showMaskOnHover: false
  });

  $('#datemask').on('change', function() {
    var inputDate = $(this).val();
    var currentDate = new Date();
    var selectedDate = new Date(inputDate.split('/').reverse().join('/'));

    if (selectedDate < currentDate) {
      alert('Veuillez sélectionner une date ultérieure à la date actuelle.');
      $(this).val(''); // Efface la date saisie
    }
  });
});
</script> -->

<script>
$(document).ready(function() {
  $('#datemask').inputmask({
    alias: 'datetime',
    inputFormat: 'dd/mm/yyyy HH:MM',
    placeholder: 'dd/mm/yyyy HH:MM',
    regex: "([0-3][0-9])/([0-1][0-9])/(20[2-9][0-9]) ([0-2][0-9]):([0-5][0-9])",
    showMaskOnHover: false
  });

  

  

});
</script>

<script>

// // Fonction de vérification de la date
// function checkDate(dateStr) {
//   alert(dateStr);
//   // function checkDate(dateStr) {
//     var currentDate = new Date();
//     var selectedDate = new Date(dateStr);

//     if (isNaN(selectedDate.getTime())) {
//       alert('La date saisie est invalide.');
//       $(this).val('');
//       return false;
//     }

//     if (selectedDate > currentDate) {
//       alert('Veuillez sélectionner une date ultérieure à la date actuelle.');
//       return false;
//     }

//     return true;
//   }

  // // Appel de la fonction de vérification de date
  // $('#datemask').on('change', function() {
  //   if (!checkDate($(this).val())) {
  //     $(this).val(''); // Efface la date saisie
  //   }
  // });

  // // Exemple d'appel de la fonction avec une date spécifique
  // var dateToCheck = '2024-11-15T14:30:00';
  // if (!checkDate(dateToCheck)) {
  //   alert('La date est antérieure à la date actuelle ou invalide.');
  // }
  // }
</script>

</body>
</html>

<script>

function get_departementc(va){
 var ID_DIRECTION= $(va).val();
 $.post('<?php echo base_url('courier/Expedier_Courier/get_departement')?>',
     {ID_DIRECTION:ID_DIRECTION},
     function(data){
       $('#ID_SERVICEC').html(data);
     });
$.post('<?php echo base_url('courier/Expedier_Courier/get_destinataire')?>',
     {ID_DIRECTION:ID_DIRECTION},
     function(data){
       $('#ID_RH_COPIE').html(data);
     });
}
</script>
<script type="text/javascript">
  function addElement() {
   
    var ID_RH_COPIE= $('#ID_RH_COPIE').val();
    var ID_COURIER= $('#ID_COURIER').val();

    $.post('<?php echo base_url('courier/Traitement_Courier/save_element')?>',
          {
            ID_RH_COPIE:ID_RH_COPIE,
            ID_COURIER:ID_COURIER
          },
          function(data){
            // alert(data);
            $('#resultattablea').html(data); 
            
          });
    // body...
  }
</script>