{% extends 'components/base_nhncloud.html' %}
{% load static %}
{% block main_area %}
  <section class="pt-3">
    {% if clusters %}
      <div class="col-lg-12">
        <h1>Result Message</h1>
        <h6>Cluster를 삭제 전 Cluster에서사용 중인 리소스를 먼저 제거해야 합니다.</h6>
        <hr>
      </div>
      {% for var in clusters %}
        <div class="col-lg-12">
          <table class="table table-striped">
            <thead>
              <tr>
                <th scope="col">No</th>
                <th scope="col">Region</th>
                {% for key in var.keys %}
                  <th scope="col">{{ key }}</th>
                {% endfor %}
                <th scope="col">
                  delete
                </th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>{{ forloop.counter }}</td>
                <td id="region-{{ forloop.counter }}">{{ region }}</td>
                {% for key, value in var.items %}
                  {% if key == "kubeconfig" and value != "" %}
                    <td id="{{ key }}-{{ forloop.parentloop.counter }}">
                      <!-- 링크에 고유 ID를 부여하여 중복 방지 -->
                      <a id="download-link-{{ forloop.parentloop.counter }}" href="#" type="button" class="btn btn-primary justify-content-end" role="button" download="kubeconfig-{{ forloop.parentloop.counter }}.yaml">config</a>
                      <script>
                        (function (id, textContent) {
                          if (textContent && textContent.trim() !== "") {
                            var blob = new Blob([textContent], {type: 'text/plain'});
                            var url = URL.createObjectURL(blob);
                            document
                              .getElementById('download-link-' + id)
                              .href = url;
                          }
                        })('{{ forloop.parentloop.counter }}', `{{ value|escapejs }}`);
                      </script>
                    </td>
                  {% else %}
                    <td id="{{ key }}-{{ forloop.parentloop.counter }}">{{ value }}</td>
                  {% endif %}
                {% endfor %}
                <td>
                  <a type="submit" id="delete-{{ forloop.counter }}" class="btn btn-info justify-content-end" href="#" data-bs-toggle="modal" data-bs-target="#deleteModal" onclick="tenantIdToModal();">Delete</a>
                </td>
              </tr>
            </tbody>
          </table>

        </div>
      {% endfor %}
    {% else %}
      <div class="col-lg-12">
        <h1>Result Message</h1>
        <hr>
      </div>
      <div class="row">
        <div class="col-lg-6">
          <h3>리전에 생성된 Cluster가 없습니다...</h3>
        </div>
      </div>
    {% endif %}
    <!-- End Cluster All list -->
    <div class="col-lg-12 d-flex justify-content-end">
      <a class="btn btn-primary" href="/nhncloud/createClusterOnlyRequest">create nks</a>
    </div>
  </section>

  <!-- Delete kubernetes Cluster -->
  <form method="post" action="{% url 'nhncloud:removeCluster' %}">{% csrf_token %}
    <div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="deleteModal" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="deleteModalTitle">API 비밀번호를 입력해주세요.</h5>
          </div>
          <div class="modal-body">
            <div class="mb-3">
              <div class="row">
                <div class="col-md-4 form-group">
                  <label for="region">Region</label>
                </div>
                <div class="col-md-8">
                  <select class="form-control" name="region" id="region" readonly="readonly">
                    <option value="select...">select...</option>
                    <option value="kr1">판교</option>
                    <option value="kr2">평촌</option>
                  </select>
                </div>
              </div>
            </div>
            <div class="mb-3">
              <div class="row">
                <div class="col-md-4">
                  <label for="clusterName">Cluster Name</label>
                </div>
                <div class="col-md-8">
                  <input type="text" class="form-control" name="clusterName" id="clusterName" readonly="readonly">
                </div>
              </div>
            </div>
            <div class="mb-3">
              <div class="row">
                <div class="col-md-4">
                  <label for="tenantId">Tenant ID</label>
                </div>
                <div class="col-md-8">
                  <input type="password" class="form-control" name="tenantId" id="tenantId" readonly="readonly">
                </div>
              </div>
            </div>
            <div class="mb-3">
              <div class="row">
                <div class="col-md-4">
                  <label for="usrEmail">User Email</label>
                </div>
                <div class="col-md-8">
                  <input type="text" class="form-control" name="usrEmail" id="usrEmail" value="{{ user.nhnc_id }}">
                </div>
              </div>
            </div>
            <div class="mb-3">
              <div class="row">
                <div class="col-md-4">
                  <label for="apiPw">API Password</label>
                </div>
                <div class="col-md-8">
                  <input type="password" class="form-control" name="apiPw" id="apiPw">
                </div>
              </div>
            </div>
          </div>
          <div class="modal-footer">
            <button type="submit" class="btn btn-info justify-content-end" onclick="alert('삭제 명령 전송 후 Cluster 리스트가 조회됩니다...')">Request</button>
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
          </div>
        </div>
      </div>
    </div>
    <script>
      function tenantIdToModal() {
        id = event.srcElement.id
        var idx = id.indexOf('-') + 1
        tenantId = "tenant_id-" + id.substring(idx)
        clusterNameId = "name-" + id.substring(idx)
        regionId = "region-" + id.substring(idx)
        document
          .getElementById('region')
          .value = document
          .getElementById(regionId)
          .innerText
          document
          .getElementById('clusterName')
          .value = document
          .getElementById(clusterNameId)
          .innerText
          document
          .getElementById('tenantId')
          .value = "{{ user.nhnc_api_tenant_id }}";
      }
    </script>
  {% endblock %}