{# datatables.jinja2 is used, in conjunction with datatables.js to display a single
   dataTable $('#datatable'), with optional Editor support
   to use an Editor template, extend this template and store the Editor template within editorform block

   parameters:
      pagename:     name of page to be displayed at the top of the page
      tabledata:    data for the table, as expected by DataTables, 
                    with additional label: field per column, used for column headings
      tablebuttons: buttons: option value for options.dtopts
      tablefiles:   if configured, will be used to initialize Editor files database
                    See https://editor.datatables.net/manual/server#File-upload for format
      options:      options with the following keys
        dtopts:     options to be passed to DataTables instance, 
                      except for data: and buttons: options, passed in tabledata, tablebuttons
        editoropts: options to be passed to Editor instance, 
                    if not present, Editor will not be configured
        updateopts: options to configure Editor select fields with, see crudapi.py for more details
        yadcfopts:  yadcf options to be passed to yadcf 
                    if not present, yadcf will not be configured
        childrow:   options for childrow management for table
                    if not present, childrow management will not be configured

      pagejsfiles:  list of js files for page
      pagecssfiles: list of css files for page

      chartloc:     present only if chart is to be drawn
                    'beforeprehtml' - chart is before pretablehtml
                    'beforetable' - chart is between pretablehtml and table
                    'aftertable' - chart is after table
          NOTES: - if chart is to be drawn, pagejsfiles must include script file 
                   which has datatables_chart() entry point
                 - if table should not be visible, use 'beforetable' and have dtopts 'dom': ''

      ## TODO: should this be in this file or should there be a dbdatables.jinja2 which extends this?
      saformjsurls: list of js urls which contain Editor standalone form code (see DbCrudApi)
#}

{% extends "layout.jinja2" %}
{% block pagename %}
    {{ pagename }}
{% endblock pagename %}
{% block css %}
{% endblock css %}

{% block scripts %}
  <script type="text/javascript">
    $( document ).ready( function() {
      var 
        options = {{ options|tojson|safe }},
        tabledata  = {{ tabledata|tojson|safe }},
        tablefiles = {{ tablefiles|tojson|safe }},
        tablebuttons = {{ tablebuttons|tojson|safe }};

      datatables(tabledata, tablebuttons, options, tablefiles);
      
      {% if chartloc %} 
        datatables_chart();
      {% endif %}
    })
  </script>
  
  {% for saformurl in saformjsurls %}
    <script type="text/javascript" src="{{ saformurl }}"></script>
  {% endfor %}

{% endblock scripts %}
{% block body %}
  {% if chartloc == 'beforeprehtml' %}
    <div class='dt-chart'></div>
  {% endif %}
  {{ pretablehtml|safe }}
  {% if chartloc == 'beforetable' %}
    <div class='dt-chart'></div>
  {% endif %}
  <table class="" id="datatable" >
    <thead>
      <tr>
        {% for col in options.dtopts.columns %}
          {% if col.label is defined %}
            <th class="">{{ col.label }}</th>
          {% endif %}
        {% endfor %}
      </tr>
    </thead>
    <tbody>
    </tbody>
  </table>
  {{ posttablehtml|safe }}
  {% if chartloc == 'aftertable' %}
    <div class='dt-chart'></div>
  {% endif %}
  {% block editorform %}
  {% endblock editorform %}
{% endblock body %}
