<?php
include '../managers/menu.php';

if (!(UserHasPerm("god_profile") && $userID == 1)) {
  StopAndDie();
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && UserHasPerm("god_profile") && $userID = 1 && isset($_POST['anticsrfid']) && $_POST['anticsrfid'] == $_SESSION["anticsrfid"]) {
  $perm_id = $_POST['perm_id'] ?? '';
  $short_name = $coderclass->encode($_POST['short_name'] ?? '', 'HA98');
  $perm_desc = $coderclass->encode($_POST['perm_desc'] ?? '', 'H3AH');
  $perm_status = $_POST['perm_status'] ?? '';
  $perm_category = $_POST['perm_category'] ?? '';
  $risk_factor = $_POST['risk_factor'] ?? '';

  $sql = mysqli_query($conn,"SELECT perm_id FROM perm_database WHERE perm_id = '".$perm_id."'");
  if(!mysqli_fetch_array($sql)) {
    $sql = mysqli_query($conn,"INSERT INTO perm_database (perm_id, short_name, perm_desc, perm_status, perm_category, risk_factor) VALUES ('$perm_id', '$short_name', '$perm_desc', '$perm_status', '$perm_category', '$risk_factor')");
    echo "<script>alert('Jog sikeresen létrehozva!');</script>";
  } else {
    $sql = mysqli_query($conn,"UPDATE perm_database SET short_name='$short_name', perm_desc='$perm_desc', perm_status='$perm_status', perm_category='$perm_category', risk_factor='$risk_factor' WHERE perm_id = '$perm_id'");
    echo "<script>alert('Jog sikeresen módosítva!');</script>";
  }
}

$anticsrfid = bin2hex(random_bytes(24));
$_SESSION["anticsrfid"] = $anticsrfid;
?>
<!DOCTYPE html>
<html lang="hu" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Jog hozzáadása</title>
  </head>
  <style>
    body {
      font-family: Arial, sans-serif;
      background: #f2f2f2;
      display: flex;
      justify-content: center;
      padding: 50px;
    }

    form {
      background: white;
      padding: 30px;
      border-radius: 10px;
      width: 100%;
      max-width: 400px;
      box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
      display: flex;
      flex-direction: column;
    }

    h2 {
      text-align: center;
      margin-bottom: 30px;
    }

    .form-group {
      position: relative;
      margin-bottom: 30px;
    }

    input {
      width: calc(100% - 30px);
      padding: 12px 10px;
      font-size: 16px;
      border: 1px solid #ccc;
      border-radius: 5px;
      background: none;
      outline: none;
    }

    label {
      position: absolute;
      top: 12px;
      left: 10px;
      color: #aaa;
      font-size: 16px;
      pointer-events: none;
      transition: 0.2s ease all;
      background-color: white;
      padding: 0 5px;
    }

    input:focus + label,
    input:not(:placeholder-shown) + label {
      top: -10px;
      left: 8px;
      font-size: 12px;
      color: #48a14d;
    }

    button {
      padding: 12px;
      font-size: 16px;
      background-color: #48a14d;
      color: white;
      border: none;
      border-radius: 5px;
      cursor: pointer;
      margin-top: 10px;
    }

    button:hover {
      opacity: 0.8;
    }
  </style>
  <body>
    <form action="addperm.php" method="post">
      <h2>Jogosultság hozzáadása</h2>

      <div class="form-group">
        <input type="text" required placeholder=" " id="perm_id" name="perm_id" autocomplete="off" autocapitalize="off" spellcheck="false" autocorrect="off">
        <label for="perm_id">Jog ID</label>
      </div>

      <div class="form-group">
        <input type="text" required placeholder=" " id="short_name" name="short_name" autocomplete="off" autocapitalize="off" spellcheck="false" autocorrect="off">
        <label for="short_name">Rövid neve</label>
      </div>

      <div class="form-group">
        <input type="text" required placeholder=" " id="perm_desc" name="perm_desc" autocomplete="off" autocapitalize="off" spellcheck="false" autocorrect="off">
        <label for="perm_desc">Leírása</label>
      </div>

      <div class="form-group">
        <input type="number" required placeholder=" " id="perm_status" name="perm_status" value="1" min="0" max="2" autocomplete="off" autocapitalize="off" spellcheck="false" autocorrect="off">
        <label for="perm_status">Státusza</label>
      </div>

      <div class="form-group">
        <input type="text" required placeholder=" " id="perm_category" name="perm_category" autocomplete="off" autocapitalize="off" spellcheck="false" autocorrect="off" list="category">
        <label for="perm_category">Kategória</label>
        <datalist id="category">
          <?php

          $query = "SELECT DISTINCT perm_category FROM perm_database ORDER BY perm_category ASC";
          if ($result = $conn->query($query)) {
            while ($warehouse = $result->fetch_assoc()) {
              echo '<option value="'.$warehouse['perm_category'].'">';
            }
          }

          ?>

      	</datalist>
      </div>

      <div class="form-group">
        <input type="text" required placeholder=" " id="risk_factor" name="risk_factor" autocomplete="off" autocapitalize="off" spellcheck="false" autocorrect="off">
        <label for="risk_factor">Rizikófaktor</label>
      </div>

      <input type="hidden" name="anticsrfid" value="<?php echo $anticsrfid; ?>">
      <button type="submit">Hozzáadás</button>
    </form>

    <script src="../js/default.js" type="text/javascript"></script>
    <script type="text/javascript">
      document.querySelector("form").addEventListener("submit", function(e) {
        const inputs = this.querySelectorAll("input");
        let allFilled = true;

        inputs.forEach(input => {
          if (input.value.trim() === "") {
            allFilled = false;
          }
        });

        if (!allFilled) {
          e.preventDefault();
          alert("Tölts ki minden mezőt!");
        }
      });

    </script>

  </body>
</html>
