{% extends "base.html" %}

{% block content %}
<div class="container-fluid py-4">



  {# 헤더 섹션 #}
  <div class="row mb-4">
    <div class="col">
      <h2 class="fw-bold mb-1">
        <i class="bi bi-server text-primary me-2"></i>
        서버 관리 대시보드
      </h2>
      <p class="text-muted mb-0">IP 처리 및 파일 관리를 위한 통합 관리 도구</p>
    </div>
  </div>

  {# 메인 작업 영역 #}
  <div class="row g-4 mb-4">
    {# IP 처리 카드 #}
    <div class="col-lg-6">
      <div class="card border shadow-sm h-100">
        <div class="card-header bg-primary text-white border-0 py-2">
          <h6 class="mb-0 fw-semibold">
            <i class="bi bi-hdd-network me-2"></i>
            IP 처리
          </h6>
        </div>
        <div class="card-body p-4">
          <form id="ipForm" method="post" action="{{ url_for('main.process_ips') }}">
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">

            {# 스크립트 선택 #}
            <div class="mb-3">
              <label for="script" class="form-label">스크립트 선택</label>
              <select id="script" name="script" class="form-select" required>
                <option value="">스크립트를 선택하세요</option>
                {% for script in scripts %}
                <option value="{{ script }}">{{ script }}</option>
                {% endfor %}
              </select>
            </div>

            {# XML 파일 선택 (조건부) #}
            <div class="mb-3" id="xmlFileGroup" style="display:none;">
              <label for="xmlFile" class="form-label">XML 파일 선택</label>
              <select id="xmlFile" name="xmlFile" class="form-select">
                <option value="">XML 파일 선택</option>
                {% for xml_file in xml_files %}
                <option value="{{ xml_file }}">{{ xml_file }}</option>
                {% endfor %}
              </select>
            </div>

            {# IP 주소 입력 #}
            <div class="mb-3">
              <label for="ips" class="form-label">
                IP 주소 (각 줄에 하나)
                <span class="badge bg-secondary ms-2" id="ipLineCount">0 대설정</span>
              </label>
              <textarea id="ips" name="ips" rows="4" class="form-control font-monospace"
                placeholder="예:&#10;192.168.1.1&#10;192.168.1.2&#10;192.168.1.3" required></textarea>
            </div>

            <button type="submit" class="btn btn-primary w-100">
              처리
            </button>
          </form>
        </div>
      </div>
    </div>

    {# 공유 작업 카드 #}
    <div class="col-lg-6">
      <div class="card border shadow-sm h-100">
        <div class="card-header bg-success text-white border-0 py-2">
          <h6 class="mb-0 fw-semibold">
            <i class="bi bi-share me-2"></i>
            공유 작업
          </h6>
        </div>
        <div class="card-body p-4">
          <form id="sharedForm" method="post">
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">

            <div class="mb-3">
              <label for="server_list_content" class="form-label">
                서버 리스트 (덮어쓰기)
                <span class="badge bg-secondary ms-2" id="serverLineCount">0 대설정</span>
              </label>
              <textarea id="server_list_content" name="server_list_content" rows="8" class="form-control font-monospace"
                style="font-size: 0.95rem;" placeholder="서버 리스트를 입력하세요..."></textarea>
            </div>

            <div class="d-flex gap-2">
              <button type="submit" formaction="{{ url_for('utils.update_server_list') }}" class="btn btn-secondary">
                MAC to Excel
              </button>
              <button type="submit" formaction="{{ url_for('utils.update_guid_list') }}" class="btn btn-success">
                GUID to Excel
              </button>
              <button type="submit" formaction="{{ url_for('utils.update_gpu_list') }}" class="btn btn-warning">
                GPU to Excel
              </button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>

  {# 진행바 #}
  <div class="row mb-4">
    <div class="col">
      <div class="card border-0 shadow-sm">
        <div class="card-body p-3">
          <div class="d-flex align-items-center mb-2">
            <i class="bi bi-activity text-primary me-2"></i>
            <span class="fw-semibold">처리 진행률</span>
          </div>
          <div class="progress" style="height: 25px;">
            <div id="progressBar" class="progress-bar progress-bar-striped progress-bar-animated bg-success"
              role="progressbar" style="width:0%;" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">
              <span class="fw-semibold">0%</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  {# 파일 관리 도구 #}
  <div class="row mb-4">
    <div class="col">
      <div class="card border shadow-sm">
        <div class="card-header bg-light border-0 py-2">
          <h6 class="mb-0">
            <i class="bi bi-tools me-2"></i>
            파일 관리 도구
          </h6>
        </div>

        <div class="card-body p-4 file-tools">
          <div class="row row-cols-1 row-cols-md-2 row-cols-lg-3 row-cols-xxl-5 g-3 align-items-end">

            <!-- ZIP 다운로드 -->
            <div class="col">
              <label class="form-label text-nowrap">ZIP 다운로드</label>
              <form method="post" action="{{ url_for('main.download_zip') }}">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                <div class="input-group">
                  <input type="text" class="form-control" name="zip_filename" placeholder="파일명" required>
                  <button class="btn btn-primary" type="submit">다운로드</button>
                </div>
              </form>
            </div>

            <!-- 파일 백업 -->
            <div class="col">
              <label class="form-label text-nowrap">파일 백업</label>
              <form method="post" action="{{ url_for('main.backup_files') }}">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                <div class="input-group">
                  <input type="text" class="form-control" name="backup_prefix" placeholder="PO로 시작">
                  <button class="btn btn-success" type="submit">백업</button>
                </div>
              </form>
            </div>

            <!-- MAC 파일 이동 -->
            <div class="col">
              <label class="form-label text-nowrap">MAC 파일 이동</label>
              <form id="macMoveForm" method="post" action="{{ url_for('utils.move_mac_files') }}">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                <button class="btn btn-warning w-100" type="submit">MAC Move</button>
              </form>
            </div>

            <!-- GUID 파일 이동 -->
            <div class="col">
              <label class="form-label text-nowrap">GUID 파일 이동</label>
              <form id="guidMoveForm" method="post" action="{{ url_for('utils.move_guid_files') }}">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                <button class="btn btn-info w-100" type="submit">GUID Move</button>
              </form>
            </div>

            <!-- GPU 파일 이동 -->
            <div class="col">
              <label class="form-label text-nowrap">GPU 파일 이동</label>
              <form id="gpuMoveForm" method="post" action="{{ url_for('utils.move_gpu_files') }}">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                <button class="btn btn-secondary w-100" type="submit">GPU Move</button>
              </form>
            </div>

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

  {# 처리된 파일 목록 #}
  <div class="row mb-4 processed-list">
    <div class="col">
      <div class="card border shadow-sm">
        <div class="card-header bg-light border-0 py-2 d-flex justify-content-between align-items-center">
          <h6 class="mb-0">
            <i class="bi bi-files me-2"></i>
            처리된 파일 목록
          </h6>
        </div>
        <div class="card-body p-4">
          {% if files_to_display and files_to_display|length > 0 %}
          <div class="row g-3">
            {% for file_info in files_to_display %}
            <div class="col-auto">
              <div class="file-card-compact border rounded p-2 text-center">
                <a href="{{ url_for('main.download_file', filename=file_info.file) }}"
                  class="text-decoration-none text-dark fw-semibold d-block mb-2 text-nowrap px-2" download
                  title="{{ file_info.name or file_info.file }}">
                  {{ file_info.name or file_info.file }}
                </a>
                <div class="file-card-buttons d-flex gap-2 justify-content-center">
                  <button type="button" class="btn btn-sm btn-outline btn-view-processed flex-fill"
                    data-bs-toggle="modal" data-bs-target="#fileViewModal" data-folder="idrac_info"
                    data-filename="{{ file_info.file }}">
                    보기
                  </button>
                  <form action="{{ url_for('main.delete_file', filename=file_info.file) }}" method="post"
                    class="d-inline">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                    <button type="submit" class="btn btn-sm btn-outline btn-delete-processed flex-fill"
                      onclick="return confirm('삭제하시겠습니까?');">
                      삭제
                    </button>
                  </form>
                </div>
              </div>
            </div>
            {% endfor %}
          </div>

          <!-- 페이지네이션 -->
          {% if total_pages > 1 %}
          <nav aria-label="Processed files pagination" class="mt-4">
            <ul class="pagination justify-content-center mb-0">

              <!-- 이전 페이지 -->
              {% if page > 1 %}
              <li class="page-item">
                <a class="page-link" href="{{ url_for('main.index', page=page-1) }}">
                  <i class="bi bi-chevron-left"></i> 이전
                </a>
              </li>
              {% else %}
              <li class="page-item disabled">
                <span class="page-link"><i class="bi bi-chevron-left"></i> 이전</span>
              </li>
              {% endif %}

              <!-- 페이지 번호 (최대 10개 표시) -->
              {% set start_page = ((page - 1) // 10) * 10 + 1 %}
              {% set end_page = [start_page + 9, total_pages]|min %}
              {% for p in range(start_page, end_page + 1) %}
              <li class="page-item {% if p == page %}active{% endif %}">
                <a class="page-link" href="{{ url_for('main.index', page=p) }}">{{ p }}</a>
              </li>
              {% endfor %}

              <!-- 다음 페이지 -->
              {% if page < total_pages %} <li class="page-item">
                <a class="page-link" href="{{ url_for('main.index', page=page+1) }}">
                  다음 <i class="bi bi-chevron-right"></i>
                </a>
                </li>
                {% else %}
                <li class="page-item disabled">
                  <span class="page-link">다음 <i class="bi bi-chevron-right"></i></span>
                </li>
                {% endif %}

            </ul>
          </nav>
          {% endif %}
          <!-- /페이지네이션 -->

          {% else %}
          <div class="text-center py-5">
            <i class="bi bi-inbox fs-1 text-muted mb-3"></i>
            <p class="text-muted mb-0">표시할 파일이 없습니다.</p>
          </div>
          {% endif %}
        </div>
      </div>
    </div>
  </div>

  {# 백업된 파일 목록 #}
  <div class="row backup-list">
    <div class="col">
      <div class="card border shadow-sm">
        <div class="card-header bg-light border-0 py-2">
          <h6 class="mb-0">
            <i class="bi bi-archive me-2"></i>
            백업된 파일 목록
          </h6>
        </div>
        <div class="card-body p-4">
          {% if backup_files and backup_files|length > 0 %}
          <div class="list-group">
            {% for date, info in backup_files.items() %}
            <div class="list-group-item border rounded mb-2 p-0 overflow-hidden">
              <div class="d-flex justify-content-between align-items-center p-3 bg-light">
                <div class="d-flex align-items-center">
                  <i class="bi bi-calendar3 text-primary me-2"></i>
                  <strong>{{ date }}</strong>
                  <span class="badge bg-primary ms-3">{{ info.count }} 파일</span>
                </div>
                <button class="btn btn-sm btn-outline-secondary" type="button" data-bs-toggle="collapse"
                  data-bs-target="#collapse-{{ loop.index }}" aria-expanded="false">
                  <i class="bi bi-chevron-down"></i>
                </button>
              </div>
              <div id="collapse-{{ loop.index }}" class="collapse">
                <div class="p-3">
                  <div class="row g-3">
                    {% for file in info.files %}
                    <div class="col-auto">
                      <div class="file-card-compact border rounded p-2 text-center">
                        <a href="{{ url_for('main.download_backup_file', date=date, filename=file) }}"
                          class="text-decoration-none text-dark fw-semibold d-block mb-2 text-nowrap px-2" download
                          title="{{ file }}">
                          {{ file.rsplit('.', 1)[0] }}
                        </a>
                        <div class="file-card-single-button">
                          <button type="button" class="btn btn-sm btn-outline btn-view-backup w-100"
                            data-bs-toggle="modal" data-bs-target="#fileViewModal" data-folder="backup"
                            data-date="{{ date }}" data-filename="{{ file }}">
                            보기
                          </button>
                        </div>
                      </div>
                    </div>
                    {% endfor %}
                  </div>
                </div>
              </div>
            </div>
            {% endfor %}
          </div>
          {% else %}
          <div class="text-center py-5">
            <i class="bi bi-inbox fs-1 text-muted mb-3"></i>
            <p class="text-muted mb-0">백업된 파일이 없습니다.</p>
          </div>
          {% endif %}
        </div>
      </div>
    </div>
  </div>

</div>

{# 파일 보기 모달 #}
<div class="modal fade" id="fileViewModal" tabindex="-1" aria-labelledby="fileViewModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-xl modal-dialog-scrollable">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="fileViewModalLabel">
          <i class="bi bi-file-text me-2"></i>
          파일 보기
        </h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="닫기"></button>
      </div>
      <div class="modal-body bg-light">
        <pre id="fileViewContent" class="mb-0 p-3 bg-white border rounded font-monospace"
          style="white-space:pre-wrap;word-break:break-word;max-height:70vh;">불러오는 중...</pre>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
          <i class="bi bi-x-circle me-1"></i>닫기
        </button>
      </div>
    </div>
  </div>
</div>
{% endblock %}

{% block extra_css %}
<link rel="stylesheet" href="{{ url_for('static', filename='css/index.css') }}">
{% endblock %}

{% block scripts %}
{{ super() }}

<script src="{{ url_for('static', filename='js/index.js') }}"></script>

<!-- 외부 script.js 파일 (IP 폼 처리 로직 포함) -->
<script src="{{ url_for('static', filename='script.js') }}"></script>
{% endblock %}