<!DOCTYPE html>
<html lang="en">
  {% set facet='validate' %}
  <head>
    <title>
      {% if recipe.name %}
      Validate {{ recipe.name }} @ HXL Proxy
      {% else %}
      Validate dataset @ HXL Proxy
      {% endif %}
    </title>
    {% include "includes/headers.html" %}
  </head>
  <body class="container-fluid">
    {% set page_help='Validation-page' %}
    {% include "includes/navbar.html" %}
    <header class="row">
      <h1 class="col-md-10">
        {% if recipe.name %}
        Validate {{ recipe.name }}
        {% else %}
        Validate dataset
        {% endif %}
      </h1>
      {% if errors %}
      <p class="col-md-2 small">
        <b>Download as:</b>
        <a href="{{ data_url_for('data_validate', recipe, format='json') }}">JSON</a>
      </p>
      {% endif %}
    </header>
    <main>
      <p class="alert alert-info alert-dismissable" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">&times;</button>
        {% if schema_url %}
        Using a <a href="{{ url_for('data_view', url=schema_url) }}">custom schema</a>.
        {% else %}
        Using the default schema.
        {% endif %}
        <a class="btn btn-default" data-toggle="modal" data-target="#customise">Change schema</a>
      </p>
      {% if errors == False %}

      <p class="alert alert-danger">Dataset is empty!</p>

      {% elif errors %}

      {% if detail_hash %}
      <p class="alert alert-warning">
        Showing only errors for rule <code>{{ errors[detail_hash][0].message }}</code>
        (<a href="{{ add_args({'details': None}) }}">back to error summary</a>)
      </p>
      <table class="table">
        <thead>
          <tr>
            <th>Severity</th>
            <th>Row</th>
            <th>Column</th>
            <th>Explanation</th>
            <th>Tag</th>
            <th>Error value</th>
          </tr>
        </thead>
        <tbody>
          {% for error in errors[detail_hash] %}
          <tr>
            <td><span class="badge {{ severity_class(error.rule.severity) }}">{{ error.rule.severity }}</span></td>
            <td>
              {% if error.row %}
              <a href="{{ data_url_for('data_view', recipe) }}#row_{{ error.row.row_number }}">{{ error.row.row_number+1 }}</a>
              {% if error.row.source_row_number %}
              ({{ error.row.source_row_number+1 }} in source)
              {% endif %}
              {% else %}
              <a href="{{ data_url_for('data_view', recipe) }}#hashtag-row">Hashtags</a>
              {% endif %}
            </td>
            <td>
              {% if error.column %}
              {{ error.column.column_number+1 }}
              ({{ error.column.column_number|spreadsheet_col }})
              {% endif %}
            </td>
            <td class="validation-message">{{ error.rule.description or error.message|nonone }}</td>
            <td>{{ error.rule.tag_pattern|nonone }}</td>
            <td>
              <code style="whitespace:pre">"{{ error.value|nonone }}"</code>
              {% if error.suggested_value %}
              (suggest <code>"{{ error.suggested_value }}"</code>)
              {% endif %}
            </td>
          </tr>
          {% endfor %}
        </tbody>
      </table>
      {% else %}
      <form id="severity-form" class="form" action="" method="GET">
        <div class="form-group">
          <label for="severity">Minimum error level to show</label>
          <select name="severity" class="form-control" onchange="$('#severity-form').submit();">
            <option value="info"{% if severity == 'info' %} selected="selected"{% endif %}>Info (most verbose)</option>
            <option value="warning"{% if severity == 'warning' %} selected="selected"{% endif %}>Warnings</option>
            <option value="error"{% if severity == 'error' %} selected="selected"{% endif %}>Errors (least verbose)</option>
          </select>
          <button class="hidden btn btn-default" type="submit">Change</button>
        </div>
        {% include "includes/params.html" %}
      </form>
      <p class="alert alert-warning">
        {{ errors|length }} validation issue(s)
        {% if recipe.args.severity %} (minimum severity: {{ severity }}){% endif %}.
      </p>
      <table class="table">
        <thead>
          <tr>
            <th>Severity</th>
            <th>Occurrences</th>
            <th>HXL tag pattern</th>
            <th>Explanation</th>
          </tr>
        </thead>
        <tbody>
          {% for rule_id in errors %}
          {% set error = errors[rule_id][0] %}
          {% set number = errors[rule_id]|count %}
          <tr>
            <td><span class="badge {{ severity_class(error.rule.severity) }}">{{ error.rule.severity }}</span></td>
            <td><a href="{{ add_args({'details': rule_id}) }}">See {{ number }} occurrence(s)</a></td>
            <td>{{ error.rule.tag_pattern|nonone }}</td>
            <td class="validation-message">{{ error.rule.description or error.message|nonone }}</td>
          </tr>
          {% endfor %}
        </tbody>
      </table>
      {% endif %}
      {% else %}
      <p class="alert alert-success">Validation succeeded with no error.</p>
      {% endif %}

      <div class="modal fade" id="customise" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <h2 class="modal-title">Customise validation</h2>

              <form class="form" action="">

                {% if recipe.recipe_id %}
                <p>Temporarily override the default schema for this recipe. To change the default schema, go to the
                <a href="{{ data_url_for('data_edit', recipe) }}">edit page</a>.</p>
                {% endif %}

                {% include 'includes/recipe-form.html' %}

                <div class="form-group">
                  <label for="schema_url">Custom HXL schema URL</label>
                  <input name="schema_url" id="schema_url" type="url" value="{{ recipe.args.schema_url or '' }}" class="form-control"
                         placeholder="http://example.org/my-hxl-schema.csv" />
                  <p class="help-block">You can choose a custom schema with special rules for your dataset.</p>
                  {% with id='#schema_url' %}
                  {% include "includes/selectors.html" %}
                  {% endwith %}
                </div>

                {% include "includes/params.html" %}

                <button class="btn btn-success" type="submit">Update</button>

              </form>
            </div>
          </div>
        </div>
      </div>
    </main>
    {% include "includes/scripts.html" %}
    {% include "includes/chooser-scripts.html" %}
  </body>
</html>
