var id_societe = "";
var id_annonce = "";
var id_ligne = "";
var date_debut = "";
var date_fin = "";


  $("#societe").change(function () {
    var option = $(this).find('option:selected');

    if(option.val() != ''){
        Annonce(option.val());
        id_societe = option.val();
    }
    
  });

  function Annonce(idSociete){

    $('.annonce').css('display','none');
    $.post( pathReservation, {idSociete:idSociete}, function( data ) {
        if(data != ''){
            $('#annonce').empty().html('<option value=""></option>'+data);
            $('.annonce').css('display','block'); 
        }
          
    }).fail(function(response) {
        console.log('Error: ' + response.responseText);
    });
  }



  $("#annonce").change(function () {

    var option = $(this).find('option:selected');
    
    if(option.val() != ''){
      
      $('.largeur').css('display','block'); 
      
      let dimention = option.attr("largeur");

      if(dimention == "1"){
        $('#largeur').empty().append("1/4 (438 * 240) pixels");
      }else if(dimention == "2"){
        $('#largeur').empty().append("2/4 (876 * 240) pixels");
      }else if(dimention == "3"){
        $('#largeur').empty().append("3/4 (1314 * 240) pixels");
      }else if(dimention == "4"){
        $('#largeur').empty().append("4/4 (1752 * 240) pixels");
      }

      $('.ligne').css('display','block'); 
      
      id_annonce = option.val();
      
      $.post( pathLigneDetail, { dimension:dimention}, function( data ) {

        $("#ligne").empty().html(data);

      }).fail(function(response) {
        console.log('Error: ' + response.responseText);
      });

    }
    
  });


  
  $("#ligne").change(function () {

    var option = $(this).find('option:selected');
    
    if(option.val() != ''){

      $('.intervalle-date').css('display','block'); 

      id_ligne = option.val();
    }

  });


  
  $("#dateDebut").change(function () {

    let sysDate = new Date();
    sysDate.setHours(0,0,0,0);
    let selDate = new Date($("#dateDebut").val());
    
    if(selDate >= sysDate ){
      $("#dateDebut").removeClass("is-invalid");
      $(".dateFin").css('display','block'); 
      
    }else{
      $("#dateDebut").addClass("is-invalid");
      $(".dateFin").css('display','none'); 
    }

  }); 


  $("#dateFin").change(function () {

    let selDatedebut = new Date($("#dateDebut").val());
    let selDatefin = new Date($("#dateFin").val());

    if(selDatedebut > selDatefin){
      $("#dateFin").addClass("is-invalid");
      $(".valOperation").css('display','none'); 
    }else{
      $("#dateFin").removeClass("is-invalid");
      date_fin = $("#dateFin").val();
      date_debut = $("#dateDebut").val();
      $(".valOperation").css('display','block'); 
    }

    
  });  


  $( "#valOperation" ).click(function() {

    $("#loading-data").empty().html('<div style="height:600px"><div style="position: relative; top: 44%; left: 45%;"><div class="spinner-border" style="width: 4rem; height: 4rem;" role="status"></div></div></div>');
    console.log(date_debut);
    console.log(date_fin);
    $.post( pathTestReservation, {id_societe:id_societe, 
                                  id_annonce:id_annonce, 
                                  id_ligne:id_ligne, 
                                  date_debut:date_debut, 
                                  date_fin:date_fin }, function( data ) {
      
      $("#loading-data").empty();

     
      $("#annonce-ligne").empty().html(data);

      $(".save-operation").css('display','block');
      
    }).fail(function(response) {
        console.log('Error: ' + response.responseText);
    });

  });


  $( "#save-operation" ).click(function() {
      $( "#save-operation" ).empty().append('<i class="fas fa-spinner fa-spin" style="color: #ffffff;"></i> enregister');
      $( "#save-operation" ).prop('disabled', 'disabled');

    $.post( pathSave, { id_annonce:id_annonce,  id_ligne:id_ligne,  date_debut:date_debut,  date_fin:date_fin }, function( data ) {

      if(data != ""){
        window.open(pathPositionDetail+'?idPosition='+data+'&msg=add', "_self"); 
      }

    }).fail(function(response) {
        console.log('Error: ' + response.responseText);
    });

  });