/*

          .then(response => {
            if (!response.ok) {
              throw new Error(`HTTP error! status: ${response.status}`);
            }
            return response.json(); // Or handle other response formats
          })
          .then(responseData => {
            console.log('Response received:', responseData);
            // Handle the server's response
          });

*/

document.addEventListener('DOMContentLoaded', function() {
  var todos = document.getElementsByClassName('todo_item');
  //var todos = document.querySelectorAll('todo_item');

  for (let i = 0; i < todos.length; i++) {
    var item = todos[i];

    var checkbox = item.querySelector('input[type="checkbox"]');
    checkbox.todo_id = item.querySelector('input[name="todo_id"]').value;
    checkbox.is_checked = checkbox.checked;

    checkbox.addEventListener('click', function() {
      if (!this.is_checked) {
        this.is_checked = true;
        fetch('director.php', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
          },
          body: 'ajax_id=todo&action_id=status&todo_id=' + this.todo_id + '&status=completed',
        });
      } else {
        this.is_checked = false;
        fetch('director.php', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
          },
          body: 'ajax_id=todo&action_id=status&todo_id=' + this.todo_id + '&status=open',
        });
      }
    });
  }
});
