{% load fertilizer_tags %}
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<style>
  @page { size: A4 landscape; margin: 12mm; }
  body { font-family: "Noto Sans CJK JP", "Hiragino Kaku Gothic Pro", sans-serif; font-size: 10pt; }
  h1 { font-size: 14pt; text-align: center; margin-bottom: 4px; }
  .subtitle { text-align: center; font-size: 9pt; color: #555; margin-bottom: 10px; }
  table { width: 100%; border-collapse: collapse; margin-top: 6px; }
  th, td { border: 1px solid #888; padding: 4px 6px; text-align: right; }
  th { background: #e8f5e9; text-align: center; }
  .col-name { text-align: left; }
  .group-row { font-weight: bold; background: #c8e6c9; }
  .group-row td { font-size: 10pt; }
  .group-star { color: #2e7d32; margin-right: 2px; }
  .field-row td { font-size: 8.5pt; color: #444; background: #fafafa; }
  .field-indent { padding-left: 14px; }
  tr.total-row { font-weight: bold; background: #f5f5f5; }
  .zero { color: #bbb; }
  .page-break { page-break-before: always; }
</style>
</head>
<body>
{% for page in trip_pages %}
{% if not forloop.first %}<div class="page-break"></div>{% endif %}

<h1>運搬計画書　{{ page.trip.order|add:1 }}回目</h1>
<p class="subtitle">
  {{ plan.year }}年度　「{{ plan.name }}」
  {% if page.trip.name %}／{{ page.trip.name }}{% endif %}
  {% if page.trip.date %}／{{ page.trip.date }}{% endif %}
</p>

<table>
  <thead>
    <tr>
      <th class="col-name">グループ / 圃場</th>
      {% for fert in page.fertilizers %}
      <th>{{ fert.name }}<br><small>（袋）</small></th>
      {% endfor %}
    </tr>
  </thead>
  <tbody>
    {% for group in page.group_rows %}
    {# グループ合計行 #}
    <tr class="group-row">
      <td class="col-name"><span class="group-star">★</span>{{ group.name }}</td>
      {% for total in group.totals %}
      <td>{% if total %}{{ total|bags_fmt }}{% else %}<span class="zero">-</span>{% endif %}</td>
      {% endfor %}
    </tr>
    {# 圃場サブ行 #}
    {% for row in group.field_rows %}
    <tr class="field-row">
      <td class="col-name field-indent">{{ row.field.name }}（{{ row.field.area_tan }}反）</td>
      {% for cell in row.cells %}
      <td>{% if cell %}{{ cell|bags_fmt }}{% else %}<span class="zero">-</span>{% endif %}</td>
      {% endfor %}
    </tr>
    {% endfor %}
    {% endfor %}
  </tbody>
  <tfoot>
    <tr class="total-row">
      <td class="col-name">合計</td>
      {% for total in page.fert_totals %}
      <td>{{ total|bags_fmt }}</td>
      {% endfor %}
    </tr>
  </tfoot>
</table>
{% endfor %}
</body>
</html>
