$(document).ready(function(){

  //hides waitlist form
  //toggle form on Add Party button
  $("#add-party-button").click(function(event){
    $(".form-container").toggle();
    $("#add-party-form").css("z-index", "2")
  })

  //add to wailist grays out unless all fields are filled
  $('#name, #party_size, #phone_number').bind('keyup', function() {
    if(allFilled()) $('#add-to-waitlist').removeAttr('disabled');
  })

  //checks that all fields are filled in
  function allFilled() {
    if (!($('#name').val() === '') && !($('#party_size').val() === '') && !($('#phone_number').val() === '')) {
      return true;
    }
    else {
      return false;
    }
  }

  $("body").on("submit", "form#add-party-form", function(event) {
    event.preventDefault();
    $.ajax({
      method: "post",
      url: $(this).attr("action"),
      data: $(this).serialize()
    }).done(function(response){
      $("#add-party-form").trigger("reset");
      if (Array.isArray(response)) {
        $(".errors").html(listErrors(response))
      }
      else {
        $("#add-party-form").css("z-index", "-1");
        $(".waitlist").append(response);
      }
      $(".form-container").toggle();
      $(".waitlist").append(response);
    }).fail(function(response){
      console.log(response)
      $(".errors").html(response)
    })
  })
  $("#cancel-party-button").on("click", function(event){
    event.preventDefault()
    $(".form-container").toggle()
  })

  function listErrors(errors) {
    html = "<ul>"
    errors.forEach(function(error) {
      html += ("<li>" + error + "</li>")
    })
    return html += "</ul>"
  }

  var updateWaittimes = function(est_waittimes){
    Object.keys(est_waittimes).forEach(function(property){
      id = "#waittime-" + property
      $(id).find(".time").html("").html(est_waittimes[property])
    })
  }

  $(".waitlist").on("submit", ".waittime-seat-form", function(event){
    event.preventDefault()
    var $form = $(this);
    var $chosenWaittimeItem = $(this).closest(".waittime-item");
    var waittimeId = $chosenWaittimeItem.attr("id").replace("waittime-","");
    console.log(waittimeId)
    var data = {'waittime': {'id': waittimeId}};
    // UPDATE Waittime object in the database (set seated to true)
    $.ajax({
      method: "PATCH",
      url: $form.attr("action"),
      dataType: "json",
      data: data
    })
    .done(function(response){
      updateWaittimes(response)
      // When done, remove the <li> from the list
      $chosenWaittimeItem.remove();
    });
  });



  $(".waitlist").on("click", ".almost-ready", function(event){
    var waittimeId = $(this).parent().prop("id");
    $.ajax({
      method: "get",
      url: '/waittimes/send_notice',
      data: {
        id: waittimeId
      }
    }).done(function(response){
      $("#" + response).find('.almost-ready').css('background-color', 'red');
    })
  })

  // =============================================
  // Screen can be unlocked (for Cabin Boys/Girls)
  // =============================================
  var $privilegedButtons = $(".table-ready-btn, .seat-party-btn");
  var $pinInput = $(".pin-input, .pin-btn");
  var lockScreen = function(){
    $privilegedButtons.hide();
    $pinInput.hide();
    $(".unlock-screen-btn").show();
    $(".lock-screen-btn").hide();
    $(".lock-status").html("")
  };
  // Default screen to locked
  // lockScreen();

  // Cabin Boy/Girl can show pin form
  $(".unlock-screen-btn").on("click", function(event){
    $(this).hide();
    $pinInput.show();
  });

  // Cabin Boy/Girl can enter pin to unlock screen
  $(".pin-btn").on("click", function(event){
    // If pin correct, unlock screen
    if ( $(".pin-input").val() === '1234' ) {
      $pinInput.hide();
      $privilegedButtons.show();
      $(".lock-screen-btn").show();
      // (Lock screen automatically after 15 seconds)
      setTimeout(lockScreen, 15000);
      $(".lock-status").html("<p style='color:seagreen;'>Screen unlocked!</p><p style='color:orange;'>Will lock automatically in 5 seconds.</p>")
    }
    // If pin incorrect, give alert
    else {
      $(".lock-status").html("<p style='color:red;'>Incorrect pin!</p>")
    }
  });

  // Cabin Boy/Girl can re-lock screen when they are done
  $(".lock-screen-btn").on("click", function(event){
    lockScreen();
  });

  //working progress with errors
  // $(document).ajaxComplete(function(event, request) {
  //   console.log("it's here!!")
  //   console.log(request)
  // })


});

@@ -881,32 +881,60 @@
 tion(response)%7B%0A
+      console.log(response)%0A
       $(%22#add-pa
@@ -1072,108 +1072,8 @@
 e %7B%0A
-        $(%22#add-party-form%22).css(%22z-index%22, %22-1%22);%0A        $(%22.waitlist%22).append(response);%0A      %7D%0A
     
@@ -1148,68 +1148,8 @@
 e);%0A
-    %7D).fail(function(response)%7B%0A      console.log(response)%0A
     
@@ -1168,25 +1168,28 @@
 %22).html(
-response)
+%22%22);%0A      %7D
 %0A    %7D)%0A
@@ -1193,16 +1193,17 @@
 %7D)%0A  %7D)%0A
+%0A
   $(%22#ca
@@ -4182,170 +4182,8 @@
 );%0A%0A
-  //working progress with errors%0A  // $(document).ajaxComplete(function(event, request) %7B%0A  //   console.log(%22it's here!!%22)%0A  //   console.log(request)%0A  // %7D)%0A%0A%0A
 %7D);%0A

Fix double ajax call