{% extends "base.html" %}

{% block title %}
銘柄リスト - My Flask App
{% endblock %}

{% block content %}

<h1 class="h3 mb-3">銘柄リスト</h3>

  <!-- フラッシュメッセージの表示部分 -->
  {% with messages = get_flashed_messages(with_categories=true) %}
  {% if messages %}
  <div class="mb-3">
    {% for category, message in messages %}
    <div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
      {{ message }}
      <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
    {% endfor %}
  </div>
  {% endif %}
  {% endwith %}

  <div class="row mb-2 mb-xl-3">
    <div class="col-auto ms-auto text-end mt-n1">
      <a href="{{ url_for('create') }}" class="btn btn-primary">New</a>
    </div>
  </div>
  <div class="row">
    <div class="col-xl-12 col-xxl-5 d-flex">
      <div class="w-100">
        <div class="card p-3">
          <div class="card-header">
          </div>

          <table class="table">
            <thead>
              <tr>
                <th style="width:20%;">code</th>
                <th style="width:20%">name</th>
                <th class="d-none d-md-table-cell">memo</th>
                <th style="width:15%" class="text-end">Actions</th>
              </tr>
            </thead>
            <tbody>

              {% for stock in stocks %}
              <tr>
                <td>{{ stock.code }}</td>
                <td>{{ stock.name }}</td>
                <td class="d-none d-md-table-cell">{{ stock.memo }}</td>
                <td class="table-action text-end">
                  <!-- 詳細アイコン -->
                  <a href="{{ url_for('stock_detail', code=stock.code) }}" class=" text-decoration-none">
                    <i class="align-middle" data-feather="alert-circle"></i>
                  </a>
                  <!-- 更新アイコン -->
                  <a href="{{ url_for('update', id=stock.id) }}" class="text-decoration-none">
                    <i class="align-middle" data-feather="edit-2"></i>
                  </a>
                  <!-- 削除アイコン -->
                  <form id="delete-form-{{ stock.id }}" method="POST" style="display:inline;"
                    action="{{ url_for('delete', id=stock.id) }}" method="POST"
                    onsubmit="return confirm('削除してよろしいですか？')">
                    <button type="submit" style="background: none; border: none; padding: 0; cursor: pointer;">
                      <i class="align-middle text-primary" data-feather="trash" style="cursor: pointer;"></i>
                    </button>
                  </form>
                </td>
              </tr>

              {% endfor %}

            </tbody>
          </table>

          <div class="row">

            <nav>
              <ul class="pagination custom-pagination">
                <!-- Previous ボタン -->
                <li class="page-item {% if page == 1 %}disabled{% endif %}">
                  <a class="page-link" href="?page={{ page - 1 }}" tabindex="-1">Previous</a>
                </li>

                <!-- ページ番号 -->
                {% for p in range(1, total_pages + 1) %}
                <li class="page-item {% if p == page %}active{% endif %}">
                  <a class="page-link" href="?page={{ p }}">{{ p }}</a>
                </li>
                {% endfor %}

                <!-- Next ボタン -->
                <li class="page-item {% if page == total_pages %}disabled{% endif %}">
                  <a class="page-link" href="?page={{ page + 1 }}">Next</a>
                </li>
              </ul>
            </nav>


          </div>

        </div>

      </div>
    </div>
  </div>


  {% endblock %}