<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta charset="utf-8">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
  <link rel="stylesheet" href="custom.css"> 
  <link rel="icon" href="favicon.ico" type="image/x-icon">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
  <div class="d-flex-center">
      <div class="container">
        <div class="row">
          <div class="col-12 col-lg-6 offset-lg-3">
            <!-- <table class="table" id="serverTable"> -->
            <?php
              $file = "serveri.csv";
              $html = "";
              $data = file_get_contents($file);
              $dataIds = explode("\n", $data);

              foreach ($dataIds as $dataId) {
                  $dataId = trim($dataId);
                  if ($dataId !== "" && $dataId[0] !== "#") {
                      $delimiterIndex = strpos($dataId, ";");
                      $prefix = "";
                      if ($delimiterIndex !== false) {
                          $prefix = trim(substr($dataId, $delimiterIndex + 1));
                          $dataId = trim(substr($dataId, 0, $delimiterIndex));
                      }

                      $tdData = $dataId . ": <button class='btn btn-primary webs-button' data-id='" . $dataId . "' data-prefix='" . $prefix . "'>Test</button><span class='time'></span>";
                      $html .= "<tr><td>" . $tdData . "</td></tr>";
                  }
              }
              echo "<table id='serverTable'>" . $html . "</table";
            ?>

            </table>
          </div>
        </div>
        <div class="row">
          <div class="col-12 col-lg-6 offset-lg-3">
            <hr style="border-top: 2px solid black;">
            <div class="text-center mt-3">
              <button class="btn btn-primary send-all-button center-button">Testiraj sve</button>
              <div class="form-group mt-3">
                <input type="text" id="textbox" class="form-control border-primary" placeholder="Upišite mail adresu ili korisničko ime">
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

  <script>
  $(document).ready(function () {
    $(document).on('click', '.webs-button', function () {
      var button = $(this);
      var buttonId = button.data('id');
      var buttonPrefix = button.data('prefix');
      var requestData = {
        id: buttonId,
        prefix: buttonPrefix,
      };
      sendRequest(requestData, button);
    });

  $('.send-all-button').click(function () {
    $('.webs-button').each(function () {
        var button = $(this);
        var buttonId = button.data('id');
        var buttonPrefix = button.data('prefix');
        var requestData = {
          id: buttonId,
          prefix: buttonPrefix,
        };
        sendRequest(requestData, button);
      });
    });
  });

  function sendRequest(requestData, button) {
    var id = requestData.id;
    var prefix = requestData.prefix;
    var url = 'https://' + id + '-demo.cdu.gov.hr/cpanel-test-cdu/test.php';
    var email = $('#textbox').val();
    requestData.email = email;

    $.ajax({
      url: url,
      type: 'POST',
      contentType: 'application/json',
      data: JSON.stringify(requestData),
      success: function (response) {
        var tempContainer = document.createElement('div');
        tempContainer.innerHTML = response;
        var time = tempContainer.textContent.trim();
        time = time.replace(/"/g, '');

        if (/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(time)) {
          button.closest('tr').find('.time').text('Poslano u ' + time);
        } else if (time === "Connection failed") {
          button.closest('tr').find('.time').text('Poslano, neuspješna konekcija');
        } else {
          button.closest('tr').find('.time').text(time);
        }

        tempContainer.remove();
      },
      error: function () {
        button.closest('tr').find('.time').text('Neuspješno slanje zahtjeva!');
      }
    });
  }

</script>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>

</body>
</html>
