<?php
session_start();
require 'config.php';
require 'db.php';

// Prüfen ob das Formular abgeschickt wurde
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    
    if ($dbhandle->connect_error) {
        die("Verbindung fehlgeschlagen: " . $dbhandle->connect_error);
    }

    // Daten holen & absichern
    $clientid = $_SESSION['user']['id'];              // Discord-ID
    $clientname = $_SESSION['user']['nickname']; // Server-Nickname
    $service = $dbhandle->real_escape_string($_POST['Services']);
    $preis = $dbhandle->real_escape_string($_POST['preis']);
    $modell = $dbhandle->real_escape_string($_POST['modell']);
    $kennzeichen = $dbhandle->real_escape_string($_POST['kennzeichen']);

    // Eintrag speichern
    $stmt = $dbhandle->prepare("INSERT INTO contracts (user_id, service, preis, modell, kennzeichen) VALUES (?, ?, ?, ?, ?)");
    $stmt->bind_param("isiss", $clientid, $service, $preis, $modell, $kennzeichen);


    if ($stmt->execute()) {
        $meldung = "✅ Eintrag erfolgreich gespeichert!";
    } else {
        $meldung = "❌ Fehler: " . htmlspecialchars($stmt->error);
    }

    $stmt->close();
    $dbhandle->close();
}
?>



<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Eintragung</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <?php include "header.php"?>
<div id="formdiv">3
  <div class="form-group">
    <?php if (isset($meldung)): ?>
      <p style="color: lime; font-weight: bold; text-align: center;"><?php echo $meldung; ?></p>
    <?php endif; ?>
  </div class="form-group">
  <form name="entryform" id="entryform" method="post" action="">
    <h1>Eintragung</h1>

  <div class="form-group">
    <label for="service">Wähle eine Dienstleistung:</label>
    <select name="Services" id="service">
      <option value="Reparatur">Reparatur</option>
      <option value="Carplay">Carplay</option>
      <option value="Tuning">Tuning</option>
    </select>
  </div>

  <div class="form-group">
    <label for="name">Preis:</label>
    <input type="number" name="preis" id="preis" placeholder="0">
  </div>

  <div class="form-group">
    <label for="modell">Automodell:</label>
    <input type="text" name="modell" id="name" placeholder="infernus">
  </div>
  <div class="form-group">
    <label for="kennzeichen">Kennzeichen:</label>
    <input type="text" name="kennzeichen" id="kennzeichen" placeholder="RXJ 661">
  </div>
    <input type="submit" name="button" id="button" value="Absenden">
  </form>
  
</div>


<script>
document.getElementById('service').addEventListener('change', function () {
    const preisField = document.getElementById('preis');
    if (this.value === 'Carplay') {
        preisField.value = 3000;
    } else {
        preisField.value = ''; // Lösche den Wert, falls etwas anderes gewählt wird
    }
});
</script>

</body>
</html>