{% load i18n %}
{% getMenu as menu %}
<form role="form" method="post" action="{{request.prefix}}/execute/launch/empty/">{% csrf_token %}
  <table>
    <tr>
      <td style="padding: 1rem; vertical-align:top"><button  class="btn btn-primary" type="submit" id="erase" value="{% trans "launch"|capfirst %}">{% trans "launch"|capfirst %}</button></td>
      <td style="padding: 1rem; line-height: 1.5rem">{% trans "Erase selected tables." %}<br><br>
        <label class="form-check-label">
          <input class="form-check-input empty_all" type="checkbox" name="alldata" checked value="1">&nbsp;<strong>{%trans 'data tables'|upper%}</strong>
        </label class="form-check-label">
        <br>
        {% for group in menu %}{% for item in group.1 %}{% if item.1.model and not item.1.excludeFromBulkOperations and not item.1.admin %}
        <label class="form-check-label" for="empty_{{ item.1.model | model_name }}">
          <input class="form-check-input empty_entity" type="checkbox" name="models" data-tables="data" value="{{ item.1.model | model_name }}" checked id="empty_{{ item.1.model | model_name }}">&nbsp;{{ group.0 }} - {{ item.0 }}
        </label>
        <br>
        {% endif %}{% endfor %}{% endfor %}
        <label class="form-check-label">
          <input class="form-check-input empty_all" type="checkbox" name="alladmin" value="1">&nbsp;<strong>{%trans 'admin tables'|upper%}</strong>
        </label>
        <br>
        {% for group in menu %}{% for item in group.1 %}{% if item.1.model and not item.1.excludeFromBulkOperations and item.1.admin %}
        <label class="form-check-label" for="empty_{{ item.1.model | model_name }}">
          <input class="form-check-input empty_entity" type="checkbox" name="models" data-tables="admin" value="{{ item.1.model | model_name }}" id="empty_{{ item.1.model | model_name }}">&nbsp;{{ group.0 }} - {{ item.0 }}
        </label>
        <br>
        {% endif %}{% endfor %}{% endfor %}
        {% if with_archive_app %}
        <label class="form-check-label">
          <input class="form-check-input empty_all" type="checkbox" name="allhistory" value="1">&nbsp;<strong>{%trans 'plan history'|upper%}</strong>
          <input class="form-check-input empty_entity d-none" type="checkbox" name="models" data-tables="archive" value="archive.archivemanager">
        </label>
        {% endif %}
        <br>
      </td>
    </tr>
  </table>
</form>
<script>
function checkChildren(id) {
  var m = id.substring(6,100);
  var children = models[m];
  var entities = $(".empty_entity[data-tables='data']");
  entities = entities.filter(function(e){return children.indexOf(entities[e].defaultValue)>=0;});
  entities.prop("checked",true);
  for (var i = 0 ; i < entities.length ; i++)
    checkChildren(entities[i].id);
}

$(function(){
$(".empty_all").click( function() {
  if ($(this).prop("name") === "alldata") {
    $(".empty_entity[data-tables='data']").prop("checked", $(this).prop("checked"));
  } else if ($(this).prop("name") === "alladmin") {
    $(".empty_entity[data-tables='admin']").prop("checked", $(this).prop("checked"));
  }
  else if ($(this).prop("name") === "allhistory") {
    $(".empty_entity[data-tables='archive']").prop("checked", $(this).prop("checked"));
  }
  updateButtonState();
  });
$(".empty_entity").click( function() {
  if ($(this).attr("data-tables") === "data") {
    $(".empty_all[name='alldata']").prop("checked",$(".empty_entity[data-tables='data']:not(:checked)").length === 0);
  } else if ($(this).attr("data-tables") === "admin") {
    $(".empty_all[name='alladmin']").prop("checked",$(".empty_entity[data-tables='admin']:not(:checked)").length === 0);
  }
  if ($(this).prop("checked")) checkChildren($(this).attr('id'));
  updateButtonState();
  });
});

function updateButtonState() {
	// count how many checkboxes are enabled to enable/disable erase button
	if (($(".empty_entity[data-tables='data']:checked").length > 0)
			|| ($(".empty_entity[data-tables='admin']:checked").length > 0)
			|| ($(".empty_entity[data-tables='archive']:checked").length > 0))
	  $("#erase").attr("disabled", false);
	else
	  $("#erase").attr("disabled", true);

}
</script>