{% 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-light border-0 py-2">
          <h6 class="mb-0">
            <i class="bi bi-hdd-network me-2"></i>
            IP 처리
          </h6>
        </div>
        <div class="card-body p-4 h-100 d-flex flex-column">
          <form id="ipForm" method="post" action="{{ url_for('main.process_ips') }}" class="h-100 d-flex flex-column">
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">

            {# 스크립트 선택 #}
            <div class="mb-3">
              <select id="script" name="script" class="form-select" required autocomplete="off">
                <option value="">스크립트를 선택하세요</option>
                {% if grouped_scripts %}
                {% for category, s_list in grouped_scripts.items() %}
                <optgroup label="{{ category }}">
                  {% for script in s_list %}
                  <option value="{{ script }}">{{ script }}</option>
                  {% endfor %}
                </optgroup>
                {% endfor %}
                {% else %}
                {# 만약 grouped_scripts가 없는 경우(하위 호환) #}
                {% for script in scripts %}
                <option value="{{ script }}">{{ script }}</option>
                {% endfor %}
                {% endif %}
              </select>
            </div>

            {# XML 파일 선택 (조건부) #}
            <div class="mb-3" id="xmlFileGroup" style="display:none;">
              <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 flex-grow-1 d-flex flex-column">
              <label for="ips" class="form-label w-100 d-flex justify-content-between align-items-end mb-2">
                <span class="mb-1">
                  IP 주소
                  <span class="badge bg-secondary ms-1" id="ipLineCount">0</span>
                </span>
                <div class="d-flex align-items-center gap-1">
                  <button type="button" class="btn btn-sm btn-outline-secondary px-2 py-1" id="btnClearIps"
                    title="입력 내용 지우기" style="font-size: 0.75rem;">
                    <i class="bi bi-trash me-1"></i>지우기
                  </button>
                  <button type="button" class="btn btn-sm btn-outline-primary px-2 py-1" id="btnStartScan"
                    title="10.10.0.1 ~ 255 자동 스캔" style="font-size: 0.75rem;">
                    <i class="bi bi-search me-1"></i>IP 스캔
                  </button>
                </div>
              </label>
              <textarea id="ips" name="ips" class="form-control font-monospace flex-grow-1"
                placeholder="예:&#10;192.168.1.1&#10;192.168.1.2" required style="resize: none;"></textarea>
            </div>

            <div class="mt-auto">
              <button type="submit"
                class="btn btn-white bg-white border shadow-sm w-100 py-2 d-flex flex-column align-items-center justify-content-center gap-1 btn-quick-move">
                <div class="rounded-circle bg-primary bg-opacity-10 text-primary p-1">
                  <i class="bi bi-play-circle-fill fs-5"></i>
                </div>
                <span class="fw-medium text-dark" style="font-size: 0.8rem;">처리 시작</span>
              </button>
            </div>
          </form>
        </div>
      </div>
    </div>

    {# 공유 작업 카드 #}
    <div class="col-lg-6">
      <div class="card border shadow-sm h-100">
        <div class="card-header bg-light border-0 py-2">
          <h6 class="mb-0">
            <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="row g-2">
              <div class="col-4">
                <button type="submit" formaction="{{ url_for('utils.update_server_list') }}"
                  class="btn btn-white bg-white border shadow-sm w-100 py-2 d-flex flex-column align-items-center justify-content-center gap-1 btn-quick-move h-100">
                  <div class="rounded-circle bg-primary bg-opacity-10 text-primary p-1">
                    <i class="bi bi-file-earmark-spreadsheet fs-5"></i>
                  </div>
                  <span class="fw-medium text-dark" style="font-size: 0.8rem;">MAC to Excel</span>
                </button>
              </div>
              <div class="col-4">
                <button type="submit" formaction="{{ url_for('utils.update_guid_list') }}"
                  class="btn btn-white bg-white border shadow-sm w-100 py-2 d-flex flex-column align-items-center justify-content-center gap-1 btn-quick-move h-100">
                  <div class="rounded-circle bg-success bg-opacity-10 text-success p-1">
                    <i class="bi bi-file-earmark-excel fs-5"></i>
                  </div>
                  <span class="fw-medium text-dark" style="font-size: 0.8rem;">GUID to Excel</span>
                </button>
              </div>
              <div class="col-4">
                <button type="submit" formaction="{{ url_for('utils.update_gpu_list') }}"
                  class="btn btn-white bg-white border shadow-sm w-100 py-2 d-flex flex-column align-items-center justify-content-center gap-1 btn-quick-move h-100">
                  <div class="rounded-circle bg-danger bg-opacity-10 text-danger p-1">
                    <i class="bi bi-gpu-card fs-5"></i>
                  </div>
                  <span class="fw-medium text-dark" style="font-size: 0.8rem;">GPU to Excel</span>
                </button>
              </div>
            </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="d-flex flex-column gap-3">

            <!-- 상단: 입력형 도구 (다운로드/백업) -->
            <div class="row g-2">
              <!-- ZIP 다운로드 -->
              <div class="col-6">
                <div class="card h-100 border-primary-subtle bg-primary-subtle bg-opacity-10">
                  <div class="card-body p-2 d-flex flex-column justify-content-center">
                    <h6 class="card-title fw-bold text-primary mb-1 small" style="font-size: 0.75rem;">
                      <i class="bi bi-file-earmark-zip me-1"></i>ZIP
                    </h6>
                    <form method="post" action="{{ url_for('main.download_zip') }}">
                      <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                      <div class="input-group input-group-sm">
                        <input type="text" class="form-control border-primary-subtle form-control-sm"
                          name="zip_filename" placeholder="파일명" required
                          style="font-size: 0.75rem; padding: 0.2rem 0.5rem;">
                        <button class="btn btn-primary btn-sm px-2" type="submit">
                          <i class="bi bi-download" style="font-size: 0.75rem;"></i>
                        </button>
                      </div>
                    </form>
                  </div>
                </div>
              </div>

              <!-- 파일 백업 -->
              <div class="col-6">
                <div class="card h-100 border-success-subtle bg-success-subtle bg-opacity-10">
                  <div class="card-body p-2 d-flex flex-column justify-content-center">
                    <h6 class="card-title fw-bold text-success mb-1 small" style="font-size: 0.75rem;">
                      <i class="bi bi-hdd-network me-1"></i>백업
                    </h6>
                    <form method="post" action="{{ url_for('main.backup_files') }}">
                      <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                      <div class="input-group input-group-sm">
                        <input type="text" class="form-control border-success-subtle form-control-sm"
                          name="backup_prefix" placeholder="ex)PO-20251117-0015_20251223_판교_R6615(TY1A)"
                          style="font-size: 0.75rem; padding: 0.2rem 0.5rem;">
                        <button class="btn btn-success btn-sm px-2" type="submit">
                          <i class="bi bi-save" style="font-size: 0.75rem;"></i>
                        </button>
                      </div>
                    </form>
                  </div>
                </div>
              </div>
            </div>

            <!-- 하단: 원클릭 액션 (파일 정리) -->
            <div class="card bg-light border-0">
              <div class="card-body p-3">
                <small class="text-muted fw-bold text-uppercase mb-2 d-block">
                  <i class="bi bi-folder-symlink me-1"></i>파일 정리 (Quick Move)
                </small>
                <div class="row g-2">
                  <!-- MAC Move -->
                  <div class="col-4">
                    <form id="macMoveForm" method="post" action="{{ url_for('utils.move_mac_files') }}" class="h-100">
                      <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                      <button
                        class="btn btn-white bg-white border shadow-sm w-100 h-100 py-1 d-flex flex-column align-items-center justify-content-center gap-1 btn-quick-move"
                        type="submit">
                        <div class="rounded-circle bg-primary bg-opacity-10 text-primary p-1">
                          <i class="bi bi-cpu fs-6"></i>
                        </div>
                        <span class="fw-medium text-dark" style="font-size: 0.75rem;">MAC</span>
                      </button>
                    </form>
                  </div>

                  <!-- GUID Move -->
                  <div class="col-4">
                    <form id="guidMoveForm" method="post" action="{{ url_for('utils.move_guid_files') }}" class="h-100">
                      <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                      <button
                        class="btn btn-white bg-white border shadow-sm w-100 h-100 py-1 d-flex flex-column align-items-center justify-content-center gap-1 btn-quick-move"
                        type="submit">
                        <div class="rounded-circle bg-success bg-opacity-10 text-success p-1">
                          <i class="bi bi-fingerprint fs-6"></i>
                        </div>
                        <span class="fw-medium text-dark" style="font-size: 0.75rem;">GUID</span>
                      </button>
                    </form>
                  </div>

                  <div class="col-4">
                    <form id="gpuMoveForm" method="post" action="{{ url_for('utils.move_gpu_files') }}" class="h-100">
                      <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                      <button
                        class="btn btn-white bg-white border shadow-sm w-100 h-100 py-1 d-flex flex-column align-items-center justify-content-center gap-1 btn-quick-move"
                        type="submit">
                        <div class="rounded-circle bg-danger bg-opacity-10 text-danger p-1">
                          <i class="bi bi-gpu-card fs-6"></i>
                        </div>
                        <span class="fw-medium text-dark" style="font-size: 0.75rem;">GPU</span>
                      </button>
                    </form>
                  </div>
                </div>
              </div>
            </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') }}">
<!-- Tom Select CSS (Bootstrap 5 theme) -->
<link href="https://cdn.jsdelivr.net/npm/tom-select@2.2.2/dist/css/tom-select.bootstrap5.min.css" rel="stylesheet">
<style>
  /* Tom Select 미세 조정 */
  .ts-wrapper.form-select {
    padding: 0 !important;
    border: none !important;
  }

  .ts-control {
    border: 1px solid #dee2e6;
    border-radius: 0.375rem;
    padding: 0.375rem 0.75rem;
  }

  .ts-wrapper.focus .ts-control {
    border-color: #86b7fe;
    box-shadow: 0 0 0 0.25rem rgba(13, 110, 253, 0.25);
  }

  /* Quick Move 버튼 호버 효과 */
  .btn-quick-move {
    transition: all 0.2s ease-in-out;
  }

  .btn-quick-move:hover {
    transform: translateY(-3px);
    box-shadow: 0 .5rem 1rem rgba(0, 0, 0, .1) !important;
    background-color: #f8f9fa !important;
    border-color: #dee2e6 !important;
  }

  .btn-quick-move:active {
    transform: translateY(0);
  }
</style>
{% endblock %}

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

<!-- Tom Select JS -->
<script src="https://cdn.jsdelivr.net/npm/tom-select@2.2.2/dist/js/tom-select.complete.min.js"></script>

<script>
  document.addEventListener('DOMContentLoaded', function () {
    // Tom Select 초기화
    // 모바일 등 환경 고려, 검색 가능하게 설정
    if (document.getElementById('script')) {
      new TomSelect("#script", {
        create: false,
        sortField: {
          field: "text",
          direction: "asc"
        },
        placeholder: "스크립트를 검색하거나 선택하세요...",
        plugins: ['clear_button'],
        allowEmptyOption: true,
      });
    }
  });
</script>

<script src="{{ url_for('static', filename='js/index.js') }}?v={{ range(1, 100000) | random }}"></script>

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