<!-- sistema_create_form.html -->


{% block content %}
  <h1>Create a new Sistema</h1>
  <form id="sistema-form" method="post" action="{% url 'create_sistemas_form' %}">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Create</button>
  </form>
{% endblock %}

{% block scripts %}
  

  <script>
    document.addEventListener('DOMContentLoaded', function() {
      document.querySelector('#sistema-form').addEventListener('submit', function(event) {
        event.preventDefault();
        var form = event.target;
        var url = form.getAttribute('action');
        var data = new FormData(form);
        fetch(url, {
          method: 'POST',
          body: data
        })
        .then(response => {
          if(response.ok){
            return response.json();
          }else{
            
            throw new Error('Network response was not ok');
          }
            
        })
        .then(response => {     
          if(response.errors) {
            alert(response.errors)
            id_nombre_sistema.value=''
          }else{
            window.opener.postMessage({data: response, windowName: 'sistemaWindow'}, window.location.origin);
            window.close();          
          }
        })
        .catch(error => {
          if(error.response){
            alert('Error System AS_000')
          }else{
            alert('Error System AS_001')
          }
        });
      });
    });
  </script>
{% endblock scripts %}
