{% extends "framework_modern.html" %}

{% block content %}

  {% block action_modals %}
    {% if job.is_result_downloadable %}
      {% include "modals/objects/download_job_result_modal.html" %}
    {% endif %}
  {% endblock action_modals %}

  <div class="space-y-2 p-4">
    <!-- Header Section -->
    <div class="card bg-base-100">
      <div class="card-body">
        <div class="flex items-center justify-between">
          <h2 class="card-title text-2xl">Job Status for {{ job.job_name }}</h2>
          <div id="actionsButton" class="dropdown dropdown-end hidden">
            <div tabindex="0" role="button" class="btn btn-ghost btn-sm">
              <svg
                xmlns="http://www.w3.org/2000/svg"
                width="16"
                height="16"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
                class="lucide lucide-wrench"
              >
                <path
                  d="M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76z"
                />
              </svg>
              Actions
            </div>
            <ul
              tabindex="-1"
              class="dropdown-content menu bg-base-100 rounded-box z-50 w-52 p-2"
            >
              {% block actions %}
                {% include "actions/objects/joblog.html" %}
              {% endblock %}
            </ul>
          </div>
        </div>
      </div>
    </div>

    <!-- Description -->
    <div class="collapse-arrow bg-base-200 collapse">
      <input type="checkbox" checked />
      <div class="collapse-title text-xl font-medium">Description</div>
      <div class="collapse-content">Status page for Job {{ job.job_name }}</div>
    </div>

    <!-- Job Status -->
    <div class="collapse-arrow bg-base-200 collapse">
      <input type="checkbox" checked />
      <div class="collapse-title text-xl font-medium">Job Status</div>
      <div class="collapse-content">{{ job.status }}</div>
    </div>

    <!-- Job ID -->
    <div class="collapse-arrow bg-base-200 collapse">
      <input type="checkbox" checked />
      <div class="collapse-title text-xl font-medium">Job ID</div>
      <div class="collapse-content">{{ job.task_id }}</div>
    </div>

    <!-- Job Result -->
    {% if job.is_in_terminal_state %}
      <div class="collapse-arrow bg-base-200 collapse">
        <input type="checkbox" checked />
        <div class="collapse-title text-xl font-medium">Job Result</div>
        <div class="collapse-content">
          {% if job.job_name == 'engineer_pathways' %}
            <div class="card bg-base-100">
              <div class="card-body">
                <p>Engineered Pathways:</p>
                <ul class="menu bg-base-200 rounded-box w-full">
                  {% for engineered_url in job.parsed_result.0 %}
                    <li>
                      <a href="{{ engineered_url }}" class="hover:bg-base-300"
                        >{{ engineered_url }}</a
                      >
                    </li>
                  {% endfor %}
                </ul>
              </div>
            </div>
            <div class="card bg-base-100">
              <div class="card-body">
                <p>Predicted Pathways:</p>
                <ul class="menu bg-base-200 rounded-box w-full">
                  {% for engineered_url in job.parsed_result.1 %}
                    <li>
                      <a href="{{ engineered_url }}" class="hover:bg-base-300"
                        >{{ engineered_url }}</a
                      >
                    </li>
                  {% endfor %}
                </ul>
              </div>
            </div>
          {% elif job.job_name == 'batch_predict' or job.job_name == 'identify_missing_rules' %}
            <div
              id="table-container"
              class="overflow-x-auto overflow-y-auto max-h-96 border rounded-lg"
            ></div>

            <script>
              const input = `{{ job.task_result }}`;

              function renderCsvTable(str) {
                const lines = str
                  .split("\n")
                  .map((l) => l.trim())
                  .filter(Boolean);
                const [headerLine, ...rows] = lines;

                const headers = headerLine.split(",").map((h) => h.trim());

                const table = document.createElement("table");
                table.className = "table table-zebra w-full";

                const thead = document.createElement("thead");
                const headerRow = document.createElement("tr");

                headers.forEach((h) => {
                  const th = document.createElement("th");
                  th.textContent = h;
                  headerRow.appendChild(th);
                });

                thead.appendChild(headerRow);

                const tbody = document.createElement("tbody");

                rows.forEach((rowStr) => {
                  console.log(rowStr.split(","));
                  console.log(headers);
                  const row = document.createElement("tr");
                  const cells = rowStr.split(",").map((c) => c.trim());

                  headers.forEach((_, i) => {
                    const td = document.createElement("td");

                    const value = cells[i] || "";

                    td.textContent = value;

                    row.appendChild(td);
                  });
                  console.log(row);
                  tbody.appendChild(row);
                });

                table.appendChild(thead);
                table.appendChild(tbody);
                return table;
              }

              document
                .getElementById("table-container")
                .appendChild(renderCsvTable(input));
            </script>
          {% else %}
            {{ job.parsed_result }}
          {% endif %}
        </div>
      </div>
    {% endif %}
    <script>
      // Show actions button if there are actions
      document.addEventListener("DOMContentLoaded", function () {
        const actionsButton = document.getElementById("actionsButton");
        const actionsList = actionsButton?.querySelector("ul");
        if (actionsList && actionsList.children.length > 0) {
          actionsButton?.classList.remove("hidden");
        }
      });
    </script>
  </div>
{% endblock content %}
