const { ui, $ } = Admin;

Admin.onStatus((status) => {
  const maintenance = status.maintenance || {};
  $('maintenance-state').innerHTML = maintenance.enabled
    ? ui.tag('offline', 'bad') : ui.tag('online', 'ok');
  Admin.fill($('maintenance-message'), maintenance.message || '');
});

const set = (enabled) => Admin.api('/admin/api/maintenance', {
  method: 'POST',
  body: JSON.stringify({ enabled, message: $('maintenance-message').value }),
});

Admin.ready(() => {
  $('maintenance-on').addEventListener('click', () => {
    if (!confirm('Take Memby offline for every television?')) return;
    Admin.act(() => set(true));
  });
  $('maintenance-off').addEventListener('click', () => Admin.act(() => set(false)));
});
