function showDiv(select) {
  let hiddenDiv = document.getElementById('hidden_div');
  if (select.value != 1) {
    hiddenDiv.classList.remove('is-hidden');
  } else {
    hiddenDiv.classList.add('is-hidden');
  }
}

function play() {
  const audio = document.getElementById("audio");
  const img = document.getElementById("img");

  function play() {
    audio.play();
  }

  function stop() {
    audio.pause();
  }

  img.addEventListener('click', play);
  img.addEventListener('mouseover', play);
  img.addEventListener('mouseout', stop);
}

// document.addEventListener('DOMContentLoaded', () => {
//     (document.querySelectorAll('.toggle') || []).forEach(($toggle) => {
//         $toggle.addEventListener('click', () => {
//
//         });
//     });
// });

document.addEventListener('DOMContentLoaded', () => {
  // Get all "navbar-burger" elements
  const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0);

  // Add a click event on each of them
  $navbarBurgers.forEach(el => {
    el.addEventListener('click', () => {

      // Get the target from the "data-target" attribute
      const target = el.dataset.target;
      const $target = document.getElementById(target);

      // Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu"
      el.classList.toggle('is-active');
      $target.classList.toggle('is-active');
    });
  });
});

document.addEventListener('DOMContentLoaded', () => {
  (document.querySelectorAll('.notification .delete') || []).forEach(($delete) => {
    const $notification = $delete.parentNode;
    $delete.addEventListener('click', () => {
      $notification.parentNode.removeChild($notification);
    });
  });
});

document.addEventListener('DOMContentLoaded', () => {
  (document.querySelectorAll('#endpointURL') || []).forEach(($urlInput) => {
    $urlInput.addEventListener('input', (e) => {
      window.localStorage.setItem("uri", e.target.value);
      console.log('Storing uri input ' + e.target.value);
    });
  });
});

document.addEventListener('DOMContentLoaded', () => {
  let data = window.localStorage.getItem("uri");
  if (data) {
    (document.querySelectorAll('#endpointURL') || []).forEach(($urlInput) => {
      $urlInput.setAttribute('value', data);
      console.log('Setting uri input to ' + data);
    });
  }
});

function sendFormData(uri, pm_iri, pt_iri) {
  console.log(pm_iri);
  console.log(pt_iri);
  // Get value of dropDownList in pm.html
  let formData = new FormData();
  formData.append('action', 'continue');
  formData.append('pmiri', pm_iri);
  formData.append('contTrace', pt_iri);
  fetch(uri, {method: 'POST', body: formData, redirect: "follow"}).then(r => console.log(r));
}

function sendHiddenForm(uri, pm_iri, pt_iri) {
  const hidden_form = document.createElement('form');
  hidden_form.method = 'post';
  hidden_form.action = uri;

  const hidden_pm_input = document.createElement('input');
  hidden_pm_input.type = 'hidden';
  hidden_pm_input.name = 'pmiri';
  hidden_pm_input.value = pm_iri;

  const hidden_pt_input = document.createElement('input');
  hidden_pt_input.type = 'hidden';
  hidden_pt_input.name = 'contTrace';
  hidden_pt_input.value = pt_iri;

  const action_input = document.createElement('input');
  action_input.type = 'hidden';
  action_input.name = 'action';
  action_input.value = 'continue';

  hidden_form.appendChild(hidden_pm_input);
  hidden_form.appendChild(hidden_pt_input);
  hidden_form.appendChild(action_input);

  document.body.appendChild(hidden_form);
  hidden_form.submit();
}

document.addEventListener('DOMContentLoaded', () => {
  (document.querySelectorAll('.trace-continuation-row') || []).forEach(($row) => {
    console.log($row);
    const pm_dropdown_value = document.getElementById('dropDownList').value;
    const button = $row.getElementsByTagName('button')[0];
    button.addEventListener('click', (e) => {
      // sendFormData($row.dataset.action, pm_dropdown_value, $row.dataset.iri)
      sendHiddenForm($row.dataset.action, pm_dropdown_value, $row.dataset.iri)
    });
  });
});
