<?php
if(!empty($_SESSION['admin_connect_id']))
{


 ?>
 <h4 class="title"><i class="fa fa-calendar-alt"></i> Ajouter planning</h4>
 	<div class="row">
          		<div class="col-lg-12">
                  <div class="form-panel">
                   	<form id="add-form" method="" class="form-horizontal" action=""  enctype="multipart/form-data">
              	
                      <div class="form-group">
                      <div class="col-sm-6 col-xs-12">
                      <label>Heur </label>
                      <input type="time"required="required"  class="form-control"  id="heure" name="heure">
                      </div>
                      </div> 
                    

                      
                       <div class="form-group">
                      <div class="col-sm-6 col-xs-12">
                      <label>Unité</label>
        
                      <select name="unite" id="unite" class="form-control" >
                         
                        <option value="0">sélectionnez l'unité</option>
                        <?php
                       $reqq= "SELECT * FROM unite";
                        $q_unite  = mysqli_query($con,$reqq);
                         while ($unite = mysqli_fetch_array($q_unite)) {
                        ?>
                         <option value="<?php echo $unite['id'] ?>" ><?php echo $unite['uninom'] ?></option>
                      <?php  }  ?>
                                          
                        
                      </select>
                      </div>
                      </div> 
 
                       <div class="form-group">
                      <div class="col-sm-6 col-xs-12">
                      <label>Destination  </label>
        
                      <select name="destinations" class="form-control" id="destinations" >
                         
                        <option value="0">sélectionnez la Destination </option>
                        <?php
                       $reqq= "SELECT * FROM destinations";
                        $q_destinations  = mysqli_query($con,$reqq);
                         while ($destinations = mysqli_fetch_array($q_destinations)) {
                        ?>
                         <option value="<?php echo $destinations['id'] ?>" ><?php echo $destinations['dstnom'] ?></option>
                      <?php  }  ?>
                                          
                        
                      </select>
                      </div>
                      </div> 

                         <div class="form-group">
                      <div class="col-sm-6 col-xs-12">
                      <label>Parcours</label>
        
                      <select name="parcours" id="parcours" class="form-control" disabled="" >
                         <option value="0">sélectionnez le parcours</option>
                      </select>
                      </div>
                      </div> 



   <div class="form-group">
                      <div class="col-sm-6 col-xs-12">
                      <label>Client</label>
        
                      <select name="client" class="form-control" >
                         
                        <option value="0">sélectionnez le client</option>
                        <?php
                       $reqclient= "SELECT * FROM clients";
                        $q_client  = mysqli_query($con,$reqclient);
                         while ($client = mysqli_fetch_array($q_client)) {
                        ?>
                         <option value="<?php echo $client['id'] ?>" ><?php echo $client['clinom'] ?></option>
                      <?php  }  ?>
                                          
                        
                      </select>
                      </div>
                      </div> 
              <div class="form-group">
                         <div class="col-sm-6 col-xs-12">
              <input type="submit" id="Ajouter" class="btn btn-primary btn-block" value="Ajouter">
                     </div>
                     </div>
                <div class="input-group message col-sm-6" id="mess">

              </div>
              	</form>

                  </div>
        </div>
 	</div>


<script type="text/javascript" src="assets/js/jquery.validate.js"></script>
<script type="text/javascript">
$(document).ready( function () {
 /*unite*/
      $('#destinations').on('change', function() {
          /* var destinations=$('#destinations  option:selected').attr('class');*/

         var destinations= $('#destinations option:selected').val();
         var unite= $('#unite option:selected').val();
         if(destinations != '0' && unite != '0' ) {
                               $("#parcours").prop('disabled', false);
              }else {
                 $('#destinations').val('0');
               $("#parcours").prop('disabled', true);
              

              }
   $.post("actions/l_parcours.php?dest="+destinations+"&unite="+unite, function(data){
        $('#parcours').html(data);

            });
         });

$("#g_planning").addClass("active");
document.title = 'Ajouter un planning';

   jQuery.validator.addMethod('selectcheck', function (value) {
    return (value != '0');

    }, "type required");
$("#add-form").validate({
submitHandler: function(form) {
$.ajax({
 url: 'actions/planning/ajouter.php',
 type: "POST",
 data: new FormData(form),
 contentType: false,
 cache: false,
 processData: false,
 success: function(data){

 if(data == 1){
          $("#mess").html('<div class="alert alert-success">  <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>   <strong>Insertion avec succès </strong> </div>')  ;
              $("#add-form")[0].reset();
               setTimeout(function() {
                location.reload();
               }, 2000);
    } else  {
 $("#mess").html('<div class="alert alert-danger">  <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>   <strong>Erreur.</strong> </div>')  ;

     }
},
 error: function(){}
 });
  } ,
                rules: {
               heure: "required",
               unite: "selectcheck",
               parcours: "selectcheck",
               destinations: "selectcheck",
               client: "selectcheck"


                               },
                messages: {
                        heure: "Entrez l'heur ",
                        unite: "sélectionnez l'unité",  
                        destinations: "sélectionnez la destination",  
                        parcours: "sélectionnez la destination",  
                        client: "sélectionnez le client",  

                                                },
                errorElement: "em",
                errorPlacement: function ( error, element ) {

                    error.addClass( "help-block" );

                    element.parents( ".col-sm-6" ).addClass( "has-feedback" );

                    if ( element.prop( "type" ) === "checkbox" ) {
                        error.insertAfter( element.parent( "label" ) );
                    } else {
                        error.insertAfter( element );
                    }

                },

                highlight: function ( element, errorClass, validClass ) {
                    $( element ).parents( ".col-sm-6" ).addClass( "has-error" ).removeClass( "has-success" );
                    $( element ).next( "span" ).addClass( "glyphicon-remove" ).removeClass( "glyphicon-ok" );
                },
                unhighlight: function ( element, errorClass, validClass ) {
                    $( element ).parents( ".col-sm-6" ).addClass( "has-success" ).removeClass( "has-error" );
                    $( element ).next( "span" ).addClass( "glyphicon-ok" ).removeClass( "glyphicon-remove" );
                }
            } );
        } );


</script>
<?php }else{   include("404.php");  } ?>