<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>{{collection.title}}</title>
  <link rel="stylesheet" type="text/css" href="{{static_files}}/style.css">
</head>

<body>

<div id="bodyContent">
<div class="title">
{{collection.title}}
</div>

<div>
{% for region_group in records|groupby('region') %}
  <header>{{ region_group.grouper }}</header>
  {% for country_group in region_group.list|groupby('country') %}
    {% for period_group in country_group.list|groupby('period') %}
      <header>{{ country_group.grouper }} • {{ period_group.grouper }}</header>

      {%- set max_mint_count = namespace(value=0) %}
      {%- for year_group in period_group.list|groupby('year') %}
        {%- set mint_count = year_group.list|groupby('mintmark')|length %}
        {%- if mint_count > max_mint_count.value %}
          {%- set max_mint_count.value = mint_count %}
        {%- endif %}
      {%- endfor %}
      
      <table class="coins">
        <thead>
          <th>{{titles.year}}</th>
          {%- if max_mint_count.value > 1 %}
            <th>{{titles.mintmark}}</th>
          {%- endif %}
          {%- for unit_group in period_group.list|groupby('unit') %}
            {%- for value_group in unit_group.list|rejectattr('value_raw','string')|groupby('value_raw') %}
              <th>{{ value_group.grouper }}{{ unit_group.grouper[0] }}</th>
            {%- endfor %}
          {%- endfor %}
        </thead>
        <tbody>
          {%- for years_group in period_group.list|groupby('year')|reverse %}

            {%- set total_variety_count = namespace(value=0) %}
            {%- for mints_group in years_group.list|groupby('mintmark') %}
              {%- set max_variety_count = namespace(value=0) %}
              {%- for unit_group in mints_group.list|groupby('unit') %}
                {%- for value_group in unit_group.list|groupby('value') %}
                  {%- set variety_count = value_group.list|groupby('variety')|length %}
                  {%- if variety_count > max_variety_count.value %}
                    {%- set max_variety_count.value = variety_count %}
                  {%- endif %}
                {%- endfor %}
              {%- endfor %}
              {%- set total_variety_count.value = total_variety_count.value + max_variety_count.value %}
            {%- endfor %}

            {%- for mints_group in years_group.list|groupby('mintmark') %}
              {%- set mints_loop = loop %}

              {%- set max_variety_count = namespace(value=0) %}
              {%- for unit_group in mints_group.list|groupby('unit') %}
                {%- for value_group in unit_group.list|groupby('value') %}
                  {%- set variety_count = value_group.list|groupby('variety')|length %}
                  {%- if variety_count > max_variety_count.value %}
                    {%- set max_variety_count.value = variety_count %}
                  {%- endif %}
                {%- endfor %}
              {%- endfor %}

              {%- for n in range(max_variety_count.value) %}
                <tr>
                  {%- if loop.first %}
                    {%- if mints_loop.first %}
                      <th rowspan="{{ total_variety_count.value }}">{{ years_group.grouper }}</th>
                    {%- endif %}
                    {%- if max_mint_count.value > 1 %}
                      <th rowspan="{{ loop.length }}">{{ mints_group.grouper }}</th>
                    {%- endif %}
                  {%- endif %}

                  {%- for unit_group in period_group.list|groupby('unit') %}
                    {%- for value_group in unit_group.list|rejectattr('value_raw','string')|groupby('value_raw') %}

                      {%- set coins_group = value_group.list | selectattr("year", "equalto", years_group.grouper) | list | selectattr("mintmark", "equalto", mints_group.grouper) | list %}
                      {%- for variety_group in coins_group|groupby('variety') %}
                        {%- if loop.index0 == n %}
                          {%- set coins_group_owned = variety_group.list | selectattr("status_raw", "in", ('owned', 'ordered', 'sale', 'duplicate', 'replacement')) | list %}
                          {%- if coins_group_owned | length %}
                            <td class="owned" {% if loop.last %}rowspan="{{ max_variety_count.value - loop.length + 1 }}"{% endif %}>
                              {{ coins_group_owned | length }} {% if loop.length > 1 %}({{ variety_group.grouper }}){% endif %}
                            </td>
                          {%- else %}
                            <td class="need" {% if loop.last %}rowspan="{{ max_variety_count.value - loop.length + 1 }}"{% endif %}>
                              {%- if loop.length > 1 %}({{ variety_group.grouper }}){% endif %}
                            </td>
                          {%- endif %}
                        {%- endif %}
                      {%- else %}
                        {%- if n == 0 %}
                          <td class="empty" rowspan="{{ max_variety_count.value }}">-</td>
                        {%- endif %}
                      {%- endfor %}

                    {%- endfor %}
                  {%- endfor %}
                </tr>
              {%- endfor %}
            {%- endfor %}
          {%- endfor %}
        </tbody>
      </table>
      
      <br>

      {% if not loop.last %}
        <p class="breakhere"></p>
      {% endif %}

    {% endfor %}
  {% endfor %}
{% endfor %}
</div>

<div class="footer">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
  <td>Generated by OpenNumismat</td>
  <td align="right">{{date}}</td>
</tr>
</tbody>
</table>
</div>

</body>
</html>
