/**
 * On click of "delete", delete the order
 * @param button_id:Integer Order id to remove
 * @param table_id: Id of the table to reload
 */
function delete_button (button_id, table_id) {
  $.ajax({
    url: '/api/food/order/' + button_id + '/',
    method: 'DELETE',
    headers: { 'X-CSRFTOKEN': CSRF_TOKEN }
  }).done(function () {
    $('#' + table_id).load(location.pathname + ' #' + table_id + ' > *')
  }).fail(function (xhr, _textStatus, _error) {
    errMsg(xhr.responseJSON, 10000)
  })
}

/**
 * On click of "Serve", mark the order as served
 * @param button_id: Order id
 * @param table_id:  Id of the table to reload
 */
function serve_button(button_id, table_id, current_state) {
  const new_state = !current_state;
  $.ajax({
    url: '/api/food/order/' + button_id + '/',
    method: 'PATCH',
    headers: { 'X-CSRFTOKEN': CSRF_TOKEN },
    contentType: 'application/json',
    data: JSON.stringify({
      served: new_state
    })
  })
  .done(function () {
    if (current_state) {
    $('table').load(location.pathname + ' table')
    }
    else {
    $('#' + table_id).load(location.pathname + ' #' + table_id + ' > *');
    }
  })
  .fail(function (xhr) {
    errMsg(xhr.responseJSON, 10000);
  });
}