<!DOCTYPE html>
<html th:lang="${#locale.language}" th:dir="#{language.direction}" th:data-language="${#locale.toString()}" xmlns:th="https://www.thymeleaf.org">
  <head>
  <th:block th:insert="~{fragments/common :: head(title=#{split-by-size-or-count.title}, header=#{split-by-size-or-count.header})}"></th:block>
  </head>

  <body>
    <th:block th:insert="~{fragments/common :: game}"></th:block>
    <div id="page-container">
      <div id="content-wrap">
        <th:block th:insert="~{fragments/navbar.html :: navbar}"></th:block>
        <br><br>
        <div class="container">
          <div class="row justify-content-center">
            <div class="col-md-6 bg-card">
              <div class="tool-header">
                <span class="material-symbols-rounded tool-header-icon advance">layers</span>
                <span class="tool-header-text" th:text="#{overlay-pdfs.header}"></span>
              </div>
              <form id="overlayForm" method="post" enctype="multipart/form-data" th:action="@{'/api/v1/general/overlay-pdfs'}">
                <div th:replace="~{fragments/common :: fileSelector(name='fileInput', multipleInputsForSingleRequest=false, accept='application/pdf')}"></div>
                <div th:replace="~{fragments/common :: fileSelector(name='overlayFiles', multipleInputsForSingleRequest=true, accept='application/pdf')}"></div>

                <label for="overlayMode" th:text="#{overlay-pdfs.mode.label}">Overlay Mode</label>
                <select id="overlayMode" name="overlayMode" class="form-control">
                  <option value="SequentialOverlay" th:text="#{overlay-pdfs.mode.sequential}">Sequential Overlay</option>
                  <option value="InterleavedOverlay" th:text="#{overlay-pdfs.mode.interleaved}">Interleaved Overlay</option>
                  <option value="FixedRepeatOverlay" th:text="#{overlay-pdfs.mode.fixedRepeat}">Fixed Repeat Overlay</option>
                </select>
                <br>
                <label for="overlayPosition" th:text="#{overlay-pdfs.position.label}">Overlay Position</label>
                <select id="overlayPosition" name="overlayPosition" class="form-control">
                  <option value="0" th:text="#{overlay-pdfs.position.foreground}">Foreground</option>
                  <option value="1" th:text="#{overlay-pdfs.position.background}">Background</option>
                </select>
                <div id="countsContainer" style="display: none;">
                  <label th:text="#{overlay-pdfs.counts.label}">Overlay Counts</label>
                  <!-- Inputs for counts will be dynamically added here -->
                </div>

                <button type="submit" id="submitBtn" class="btn btn-primary" th:text="#{overlay-pdfs.submit}">Submit</button>
              </form>
              <script>
                function updateCountsInputs() {
                  const mode = document.getElementById('overlayMode').value;
                  console.log("mode",mode);
                  const countsContainer = document.getElementById('countsContainer');
                  console.log("countsContainer",countsContainer);
                  countsContainer.innerHTML = ''; // Clear previous inputs

                  if (mode === 'FixedRepeatOverlay') {
                    const fileInput = document.getElementById('overlayFiles-input');
                    console.log("fileInput",fileInput);
                    if(fileInput){
                      const files = fileInput.files
                      console.log("files",files);
                      if(files) {
                        const fileCount = files.length;
                        countsContainer.appendChild(document.createElement("br"));
                        const label = document.createElement('label');
                        label.setAttribute('th:text', '#{overlay-pdfs.counts.label}');
                        label.textContent = "Overlay Counts";
                        countsContainer.appendChild(label);
                        for (let i = 0; i < fileCount; i++) {
                          const input = document.createElement('input');
                          input.type = 'text';
                          input.name = 'counts';
                          input.classList.add('form-control');
                          input.placeholder = 'Count for file ' + (i + 1);
                          countsContainer.appendChild(input);
                        }
                        countsContainer.style.display = 'block';
                      }
                    }
                  } else {
                    countsContainer.style.display = 'none';
                  }
                }

                document.addEventListener('DOMContentLoaded', (event) => {
                  var fileInput = document.getElementById('overlayFiles-input');
                  console.log("fileInput2",fileInput);
                    if (fileInput) {
                      fileInput.addEventListener('change', updateCountsInputs);
                    }
                });
                document.addEventListener('DOMContentLoaded', (event) => {
                  var overlay = document.getElementById('overlayMode');
                  console.log("overlay2",overlay);
                    if (overlay) {
                      overlay.addEventListener('change', updateCountsInputs);
                    }
                });
              </script>
            </div>
          </div>
        </div>
      </div>
      <th:block th:insert="~{fragments/footer.html :: footer}"></th:block>
    </div>
  </body>
</html>