<!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=#{imageToPDF.title}, header=#{imageToPDF.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 convertto">picture_as_pdf</span>
              <span class="tool-header-text" th:text="#{imageToPDF.header}"></span>
            </div>
            <form id="imageToPDFForm" method="post" enctype="multipart/form-data"
              th:action="@{'/api/v1/convert/img/pdf'}">
              <div
                th:replace="~{fragments/common :: fileSelector(name='fileInput', multipleInputsForSingleRequest=false, accept='image/*,.psd', inputText=#{imgPrompt})}">
              </div>
              <div class="mb-3">
                <label for="fitOption" th:text="#{imageToPDF.selectLabel}">Fit Options</label>
                <select class="form-control" id="fitOption" name="fitOption">
                  <option value="fillPage" th:text="#{imageToPDF.fillPage}">Fill Page</option>
                  <option value="fitDocumentToImage" th:text="#{imageToPDF.fitDocumentToImage}">Fit Document to Image
                  </option>
                  <option value="maintainAspectRatio" th:text="#{imageToPDF.maintainAspectRatio}">Maintain Aspect Ratio
                  </option>
                </select>
              </div>

              <div class="form-check ms-3">
                <input id="autoRotate" name="autoRotate" type="checkbox">
                <label for="autoRotate" th:text=#{imageToPDF.selectText.2}></label>
              </div>
              <div class="mb-3">
                <label th:text="#{pdfToImage.colorType}"></label>
                <select class="form-control" id="colorType" name="colorType">
                  <option value="color" th:text="#{pdfToImage.color}"></option>
                  <option value="greyscale" th:text="#{pdfToImage.grey}"></option>
                  <option value="blackwhite" th:text="#{pdfToImage.blackwhite}"></option>
                </select>
              </div>
              <input type="hidden" id="override" name="override" value="multi">
              <div class="mb-3">
                <label th:text=#{imageToPDF.selectText.3}></label>
                <select class="form-control" id="conversionType" name="conversionType" disabled>
                  <option value="merge" th:text=#{imageToPDF.selectText.4}></option>
                  <option value="convert" th:text=#{imageToPDF.selectText.5} selected></option>
                </select>
              </div>
              <br>
              <button type="submit" id="submitBtn" class="btn btn-primary" th:text="#{imageToPDF.submit}"></button>
              <script>
                $('#fileInput-input').on('change', function (e) {
                  const fileInput = e.target;
                  fileInput.addEventListener('file-input-change', async (e) => {
                    const { allFiles } = e.detail;
                    var conversionType = document.getElementById("conversionType");
                    console.log("files.length=" + allFiles.length)
                    if (allFiles.length > 1) {
                      conversionType.disabled = false;
                    } else {
                      conversionType.disabled = true;
                    }
                  });
                });

                $('#conversionType').change(function () {
                  var selectedValue = $(this).val();
                  var override = document.getElementById("override");
                  console.log("selectedValue=" + selectedValue)
                  if (selectedValue === 'merge') {
                    override.value = "single";
                  } else if (selectedValue === 'convert') {
                    override.value = "multi";
                  }
                });
                document.getElementById("imageToPDFForm").addEventListener("submit", async function (e) {
                  e.preventDefault();

                  const form = e.target;
                  const formData = new FormData(form);

                  const fitOptionSelect = document.getElementById("fitOption");
                  const fitOptionValue = fitOptionSelect ? fitOptionSelect.value : "fit-to-page"; // Default value
                  formData.append("fitOption", fitOptionValue);

                  const overrideInput = document.getElementById("override");
                  console.log("Override value before submission:", overrideInput.value);

                  for (let [key, value] of formData.entries()) {
                    console.log(`${key}:`, value);
                  }

                  try {
                    const response = await fetch(form.action, {
                      method: "POST",
                      body: formData
                    });

                    if (response.ok) {
                      console.log("Form successfully submitted");
                    } else {
                      console.error("Failed to submit the form");
                    }
                  } catch (error) {
                    console.error("Error submitting the form:", error);
                  }
                });
              </script>
            </form>
          </div>
        </div>
      </div>
    </div>
    <th:block th:insert="~{fragments/footer.html :: footer}"></th:block>
  </div>
</body>

</html>