<?php declare(strict_types=1);
    require_once __DIR__ . '/src/QrbApi.php';

    // factor for sleepAfterSend's display (tenths of seconds) versus storage (microseconds) 
    const SLEEP_FACTOR = 100000;

    // create QR Bills API and get configuration of current profile
    $qrbApi = new QrbApi(Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']));
    $qrbCfg = $qrbApi->getProfileConfiguration();

    $qrbApi->writeLog("Loading index.php");
?>

<!DOCTYPE html>
<html>
<head>
  <title>QR BILLS MASS MAILER</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
  <link rel="stylesheet" type="text/css" href="css/user.css" />
  
<?php
    // the script "QrBillsProcess.js" handles POST for 3. Execution
?>
  <script type="text/javascript" src="js/QrBillsProcess.js"></script>
  <script type="text/javascript" src="js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div id="Content">
  <h1>
<?php 
    print $qrbApi->xl('title.massMailer'); 
?>
    <br />
<?php    
    print ($qrbCfg->getCreatePdf() ? ($qrbCfg->getPrintPaymentSlip() ? $qrbApi->xl('title.qrBills')
                                                                     : $qrbApi->xl('title.pdfMail'))
                                   : $qrbApi->xl('title.htmlMail'));
?>
  </h1>

  <p>
<?php
    print $qrbApi->xl('readme.hint1') 
          . '<a href="README.html" target="_blank">' . $qrbApi->xl('readme.hintLink') . '</a>' 
          . $qrbApi->xl('readme.hint2');
?>
  </p>

<?php
    // Handles POST for use case selection and configuration, 
    // must be included before any access to the configuration (including UseCaseSelection).
    // Inserts a script tag with the Javascript method reloadPage().
    include 'QrBillsSaveCfg.php';
    
    // profile selection
    include 'QrBillsProfileSelection.php';
    
    // use case selection
    include 'QrBillsUseCaseSelection.php';
?>
  
<?php
    // Start accordion
?>
  <div id="accordionSteps" 
       class="accordion">


<?php
    // Configuration
?>
    <div class="accordion-item">
      <h2 id="headingOne"
          class="accordion-header">
        <button id="butOne" 
                class="accordion-button collapsed" 
                type="button" 
                data-bs-toggle="collapse" 
                data-bs-target="#collapseOne" 
                aria-expanded="false" 
                aria-controls="collapseOne">
<?php
    print $qrbApi->xl('cfg.title'); 
?>
        </button>
      </h2>
      <div id="collapseOne" 
           class="accordion-collapse collapse" 
           aria-labelledby="headingOne" 
           data-bs-parent="#accordionSteps">
        <div class="accordion-body">
          <p>
<?php
    print $qrbApi->xl('cfg.hint'); 
?>
          </p>
<?php
    // configuration selection
    include 'QrBillsConfigurationSelection.php';
?>
        </div>
      </div>
    </div>
  

<?php
    // Input files
?>
    <div class="accordion-item">
      <h2 id="headingTwo" 
          class="accordion-header">
        <button id="butTwo" 
                class="accordion-button collapsed" 
                type="button" 
                data-bs-toggle="collapse" 
                data-bs-target="#collapseTwo" 
                aria-expanded="false" 
                aria-controls="collapseTwo">
<?php
    print $qrbApi->xl('upload.title'); 
?>
        </button>
      </h2>
      <div id="collapseTwo" 
           class="accordion-collapse collapse" 
           aria-labelledby="headingTwo" 
           data-bs-parent="#accordionSteps">
        <div class="accordion-body">
          <div>
            <p>
<?php
    print $qrbApi->xl('upload.hint1'); 
?>
              <a href="docs/README_File_Formats.html#input-files">
<?php
    print $qrbApi->xl('upload.hintLink'); 
?>
              </a> 
<?php
    print $qrbApi->xl('upload.hint2'); 
?>
            </p>

<?php
    // Delete Input Files
    include 'QrBillsDelete.php';
?>

            <div id="upload-result">
<?php
    // handles POST for "submit" in QrBillsUploadForm.php (inserts a DIV with the result)
    // must be included before any include 'QrBillsUploadForm.php'
    include 'QrBillsUpload.php';
?>
            </div>
            
<?php
    // PDF
    if ($qrbCfg->getCreatePdf()) {    
?>
            <div class="highlight">
              <h3 id="upload-pdf">
<?php   print $qrbApi->xl('upload.pdf.title'); ?>
              </h3>
              
<?php           
        $titleText     = 'upload.pdf.data.title';
        $submitText    = 'upload.pdf.data.submit';
        $inputDataType = QrbDataType::PDF_DATA;
        $language      = "";
        $inputFileKey  = PDF_DATA_KEY; // defined in QrBillsUpload
        include 'QrBillsUploadForm.php';
                
        $titleText     = 'upload.pdf.template.title';
        $submitText    = 'upload.pdf.template.submit';
        $showDefLang   = true;
        $inputDataType = QrbDataType::PDF_TEMPLATE;
        $inputFileKey  = PDF_TMPL_KEY; // defined in QrBillsUpload
        $language      = "";
        include 'QrBillsUploadForm.php';

        if ($qrbCfg->getMultiLanguage())  {
            if (in_array("de", $qrbCfg->getAdditionalLanguages(), true)) { 
                $titleText     = 'upload.pdf.template.de.title';
                $submitText    = 'upload.pdf.template.de.submit';
                $inputDataType = QrbDataType::PDF_TEMPLATE;
                $language      = "de";
                $inputFileKey  = PDF_TMPL_DE_KEY; // defined in QrBillsUpload
                include 'QrBillsUploadForm.php';
            }
          
            if (in_array("fr", $qrbCfg->getAdditionalLanguages(), true)) { 
                $titleText     = 'upload.pdf.template.fr.title';
                $submitText    = 'upload.pdf.template.fr.submit';
                $inputDataType = QrbDataType::PDF_TEMPLATE;
                $language      = "fr";
                $inputFileKey  = PDF_TMPL_FR_KEY; // defined in QrBillsUpload
                include 'QrBillsUploadForm.php';
            }
          
            if (in_array("it", $qrbCfg->getAdditionalLanguages(), true)) {
                $titleText     = 'upload.pdf.template.it.title';
                $submitText    = 'upload.pdf.template.it.submit';
                $inputDataType = QrbDataType::PDF_TEMPLATE;
                $language      = "it";
                $inputFileKey  = PDF_TMPL_IT_KEY; // defined in QrBillsUpload
                include 'QrBillsUploadForm.php';
            }
          
            if (in_array("en", $qrbCfg->getAdditionalLanguages(), true)) {
                $titleText     = 'upload.pdf.template.en.title';
                $submitText    = 'upload.pdf.template.en.submit';
                $inputDataType = QrbDataType::PDF_TEMPLATE;
                $language      = "en";
                $inputFileKey  = PDF_TMPL_EN_KEY; // defined in QrBillsUpload
                include 'QrBillsUploadForm.php';
            }
        } // if ($qrbCfg->getMultiLanguage())
?>
            </div>

<?php
        // Payment slip
        if ($qrbCfg->getPrintPaymentSlip()) {    
?>
            <div class="highlight">
              <h3 id="upload-payment-slip">
<?php       print $qrbApi->xl('upload.paymentSlip.title'); ?>
              </h3>

<?php
            $titleText     = 'upload.paymentSlip.credInfo.title';
            $submitText    = 'upload.paymentSlip.credInfo.submit';
            $inputDataType = QrbDataType::CRED_INFO;
            $language      = "";
            $inputFileKey  = CRED_INFO_KEY; // defined in QrBillsUpload
            include 'QrBillsUploadForm.php';
                  
            $titleText     = 'upload.paymentSlip.addInfo.title';
            $submitText    = 'upload.paymentSlip.addInfo.submit';
            $infoText      = 'upload.paymentSlip.addInfo.info';
            $inputDataType = QrbDataType::ADD_INFO;
            $language      = "";
            $inputFileKey  = ADD_INFO_KEY; // defined in QrBillsUpload
            include 'QrBillsUploadForm.php';
?>
            </div>
<?php
        } // if ($qrbCfg->getPrintPaymentSlip())
    } // if ($qrbCfg->getCreatePdf())
?>

<?php
    // Mail
    if ($qrbCfg->getSendMail()) {    
?>
            <div class="highlight">
              <h3 id="upload-mail">
<?php   print $qrbApi->xl('upload.mail.title'); ?>
              </h3>

<?php
        $titleText     = 'upload.mail.data.title';
        $submitText    = 'upload.mail.data.submit';
        $infoText      = 'upload.mail.data.info';
        $inputDataType = QrbDataType::MAIL_DATA;
        $language      = "";
        $inputFileKey  = MAIL_DATA_KEY; // defined in QrBillsUpload
        include 'QrBillsUploadForm.php';
                
        $titleText     = 'upload.mail.body.title';
        $submitText    = 'upload.mail.body.submit';
        $showDefLang   = true;
        $inputDataType = QrbDataType::MAIL_BODY;
        $language      = "";
        $inputFileKey  = MAIL_BODY_KEY; // defined in QrBillsUpload
        include 'QrBillsUploadForm.php';
                
        if ($qrbCfg->getMultiLanguage())  { 
            if (in_array("de", $qrbCfg->getAdditionalLanguages(), true)) { 
                $titleText     = 'upload.mail.body.de.title';
                $submitText    = 'upload.mail.body.de.submit';
                $inputDataType = QrbDataType::MAIL_BODY;
                $language      = "de";
                $inputFileKey  = MAIL_BODY_DE_KEY; // defined in QrBillsUpload
                include 'QrBillsUploadForm.php';
            }

            if (in_array("fr", $qrbCfg->getAdditionalLanguages(), true)) {
                $titleText     = 'upload.mail.body.fr.title';
                $submitText    = 'upload.mail.body.fr.submit';
                $inputDataType = QrbDataType::MAIL_BODY;
                $language      = "fr";
                $inputFileKey  = MAIL_BODY_FR_KEY; // defined in QrBillsUpload
                include 'QrBillsUploadForm.php';
            }

            if (in_array("it", $qrbCfg->getAdditionalLanguages(), true)) {
                $titleText     = 'upload.mail.body.it.title';
                $submitText    = 'upload.mail.body.it.submit';
                $inputDataType = QrbDataType::MAIL_BODY;
                $language      = "it";
                $inputFileKey  = MAIL_BODY_IT_KEY; // defined in QrBillsUpload
                include 'QrBillsUploadForm.php';
            }

            if (in_array("en", $qrbCfg->getAdditionalLanguages(), true)) {
                $titleText     = 'upload.mail.body.en.title';
                $submitText    = 'upload.mail.body.en.submit';
                $inputDataType = QrbDataType::MAIL_BODY;
                $language      = "en";
                $inputFileKey  = MAIL_BODY_EN_KEY; // defined in QrBillsUpload
                include 'QrBillsUploadForm.php';
            }
        } // if ($qrbCfg->getMultiLanguage())
?>
            </div>
<?php
    } // if ($qrbCfg->getSendMail())
?>

<?php
  // Recipients
?>
            <div class="highlight">
              <h3 id="upload-recipient">
<?php
    print $qrbApi->xl('upload.recipient.title'); 
?>
              </h3>

<?php
    $titleText     = 'upload.recipient.recipients.title';
    $submitText    = 'upload.recipient.recipients.submit';
    $inputDataType = QrbDataType::RECIPIENTS;
    $language      = "";
    $inputFileKey  = RCPT_INFO_KEY;
    include 'QrBillsUploadForm.php';
?>
              
            </div>
            
          </div>
        </div>
      </div>
    </div>
    

<?php
    // Processing
?>
    <div class="accordion-item">
      <h2 id="headingThree"
          class="accordion-header">
        <button id="butThree" 
                class="accordion-button collapsed" 
                type="button" 
                data-bs-toggle="collapse" 
                data-bs-target="#collapseThree" 
                aria-expanded="false" 
                aria-controls="collapseThree">
<?php
    print $qrbApi->xl('execution.title'); 
?>
        </button>
      </h2>
      <div id="collapseThree" 
           class="accordion-collapse collapse" 
           aria-labelledby="headingThree" 
           data-bs-parent="#accordionSteps">
        <div class="accordion-body">
          <div>
            <p>
<?php
    print $qrbApi->xl('execution.hint'); 
?>
            </p>

<?php
    // Processing DryRun
?>
            <form method="post">
              <div class="highlight">
                <div class="row">
                  <div class="col-4">
                    <h3 id="execution-dry-run">
<?php
    print $qrbApi->xl('execution.dryRun.title'); 
?>
                      <span class="iconPopover">
                        <a class="btn btn-dark" 
                           tabindex="0" 
                           role="button"
                           data-bs-html="true"
                           data-bs-toggle="popover"
                           data-bs-trigger="focus"
                           data-bs-title="
<?php
    print $qrbApi->xl('execution.dryRun.title'); 
?>
                           "
                           data-bs-content="
<?php
    print $qrbApi->xl('execution.dryRun.info'); 
?>
                           "
                        >
                          i
                        </a>
                      </span>
                    </h3>
                  </div>
                  <div class="col-8 cfg">
                    <div class="cfgItem">
                      <p>
<?php
    print $qrbApi->xl('execution.dryRun.hint'); 
?>
                      </p>

                      <div class="SubmitButton">
                        <input type="button" 
                               class="btn btn-dark" 
                               onclick="dryRun()"
<?php
    print ' value="' . $qrbApi->xl('execution.dryRun.submit') . '" '; 
?>
                        />
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </form>

<?php
    // Processing TestRun
?>
<?php
    if ($qrbCfg->getSendMail()) {    
?>
            <form method="post">
              <div class="highlight">
                <div class="row">
                  <div class="col-4">
                    <h3 id="execution-test-run">
<?php   print $qrbApi->xl('execution.testRun.title'); ?>
                      <span class="iconPopover">
                        <a class="btn btn-dark" 
                           tabindex="0" 
                           role="button"
                           data-bs-html="true"
                           data-bs-toggle="popover"
                           data-bs-trigger="focus"
                           data-bs-title="
<?php   print $qrbApi->xl('execution.testRun.title'); ?>
                           "
                           data-bs-content="
<?php   print $qrbApi->xl('execution.testRun.info'); ?>
                           "
                        >
                          i
                        </a>
                      </span>
                    </h3>
                  </div>
                  <div class="col-8 cfg">
                    <div class="cfgItem">
                      <p>
                        <label for="testEmailAddress">
<?php   print $qrbApi->xl('execution.testRun.hint'); ?>
                        </label>
                        <input class="form-control"
                               type="email"
                               id="testEmailAddress"
                               name="testEmailAddress"
<?php   print ' value="' . $qrbCfg->getPresetMail() . '" ' ?> 
                        />
                      </p>
                      <div class="SubmitButton">
                        <input type="button"
                               class="btn btn-dark"
                               onclick="testRun()" 
<?php   print ' value="' . $qrbApi->xl('execution.testRun.submit') . '" '; ?>
                        />
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </form>
<?php
    } // if ($qrbCfg->getSendMail())
?>

<?php
    // Processing really
?>
            <form method="post">
              <div class="highlight">
                <div class="row">
                  <div class="col-4">
                    <h3 id="execution-execute">
<?php
    print $qrbApi->xl('execution.execute.title'); 
?>
                      <span class="iconPopover">
                        <a class="btn btn-dark"
                           tabindex="0"
                           role="button"
                           data-bs-html="true"
                           data-bs-toggle="popover"
                           data-bs-trigger="focus"
                           data-bs-title="
<?php
    print $qrbApi->xl('execution.execute.title'); 
?>
                           "
                           data-bs-content="
<?php
    print $qrbApi->xl('execution.execute.info'); 
?>
                           "
                        >
                          i
                        </a>
                      </span>
                    </h3>
                  </div>
                  <div class="col-8 cfg">
                    <div class="cfgItem">
                      <p>
<?php
    print $qrbApi->xl('execution.execute.hint'); 
?>
                      </p>
                      <div class="SubmitButton">
                        <input type="button" 
                               class="btn btn-dark"
                               onclick="processRun()" 
<?php
    print ' value="' . $qrbApi->xl('execution.execute.submit') . '" '; 
?>
                        />
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </form>
                

            <br />
<?php
    // the status and debug sections are used by QrBillsProcess.js    
?>
            <div id="statusContent">
              <h2>
<?php
    print $qrbApi->xl('execution.status'); 
?>
              </h2>
              <p id="status">
              </p>
            </div>
            <div id="debug">
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <div id="footer">
    v1.4, 2024-10-27
  </div>
</div>

<script>
    /*
     * Additional languages
     */
    // disables the additional language that is the same as the selected default language
    function checkAddLang()
    {
        var defaultLangValue = document.getElementById('defaultLang').value;
    
        // check which default language is selected and activate additional languages
        if (defaultLangValue)
        {
            document.getElementById("addLang_de").disabled = false;
            document.getElementById("addLang_fr").disabled = false;
            document.getElementById("addLang_it").disabled = false;
            document.getElementById("addLang_en").disabled = false;
            var elementToDisable  = "addLang_" + defaultLangValue;
            document.getElementById(elementToDisable).disabled = true;
            document.getElementById(elementToDisable).checked = false;
        }
    }
    
    /*
     * Popover
     */
    // activate popover
    var popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]'));
    var popoverList = popoverTriggerList.map(
        function (popoverTriggerEl)
        {
            return new bootstrap.Popover(popoverTriggerEl);
        });

    /*
     * Collapse
     */
    // determine collapse state
    const STORED_NAME = "shownCollapse";
    var shownCollapse = window.sessionStorage.getItem(STORED_NAME);
  
    if (shownCollapse === null)         // there isn't a stored last state
    {
        shownCollapse = "collapseOne";  // default to first collapsible item
    }
  
    // show collapsible item according to collapse state
    const shownCollapsible = document.getElementById(shownCollapse);
    const shownInstance = bootstrap.Collapse.getOrCreateInstance(shownCollapsible);
    shownInstance.show();

    // add listener to save collapse state
    const collapsible = document.getElementById('accordionSteps');
    collapsible.addEventListener('show.bs.collapse', event =>
        {
            window.sessionStorage.setItem(STORED_NAME, event.srcElement.id);
        });

    // scroll target of action into view (if there is one)
    collapsible.addEventListener('shown.bs.collapse', event =>
        {
            var hashPos = window.location.href.indexOf('#');
            if (hashPos !== -1 )
            {
                var target = window.location.href.split("#")[1];
                var el = document.getElementById(target);
                el.scrollIntoView({block:"center", inline:"nearest", behavior:"smooth"});
            }
        });
</script>

</body>
</html>
