function savePlaceNumber() {
    const selected_id = $('.selected')[0].innerText;
    if(selected_id) {
        const seat_number_input = $('.seat_number')[0];
        seat_number_input.value = selected_id;
        console.log(seat_number_input.value)
    }
}

function toggleButtonAvailability() {
    const buttons = $('.checkout-button');
    const selected_place = $('.selected')[0];
    buttons.each(function(e) {
      if(!$(this).hasClass('end-button')) {
          if(!selected_place){
              $(this).css('pointer-events', 'none');
              $(this).css('cursor', 'unset');

          } else {
              $(this).css('pointer-events', 'unset');
              $(this).css('cursor', 'pointer');
          }
      }
    })

}

function checkForUnavailablePlaces() {
    const unavailablePlaces = Array();
    $('.unavailable_place').each(function(){
        unavailablePlaces.push($(this).val());
    });

    const allPlaces = $('.seatCharts-seat.seatCharts-cell.available');
     if(unavailablePlaces){
         allPlaces.each(function(){
             if(unavailablePlaces.includes($(this).text()) && !$(this).parent().hasClass('seatCharts-legendItem')) {
                 $(this).addClass('unavailable');
                 $(this).css('pointer-events', 'none');
             }
         });
     }
}
$(document).ready(function(){
    checkForUnavailablePlaces();
    toggleButtonAvailability();
    $('.seatCharts-seat').on('click', function(){
        toggleButtonAvailability();
        savePlaceNumber();
    });
});