<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

include 'koneksi.php';

$rating  = $_POST['rating'] ?? '';
$layanan = $_POST['layanan'] ?? '';
$alasan  = $_POST['alasan'] ?? '';

if ($rating && $layanan && $alasan) {
    $stmt = $conn->prepare("INSERT INTO survey (rating, layanan, alasan) VALUES (?, ?, ?)");
    $stmt->bind_param("sss", $rating, $layanan, $alasan);
    $stmt->execute();
    $stmt->close();
    ?>
    <!DOCTYPE html>
    <html lang="id">
    <head>
      <meta charset="UTF-8">
      <title>Terima Kasih</title>
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    </head>
    <body>
    <script>
      Swal.fire({
        icon: 'success',
        title: 'Terima kasih!',
        text: ' Atas Partisipasi Anda 😊',
        showConfirmButton: false,
        timer: 2000
      }).then(() => {
        window.location.href = 'index.php';
      });
    </script>
    </body>
    </html>
    <?php
} else {
    ?>
    <!DOCTYPE html>
    <html lang="id">
    <head>
      <meta charset="UTF-8">
      <title>Error</title>
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    </head>
    <body>
    <script>
      Swal.fire({
        icon: 'error',
        title: 'Oops!',
        text: 'Data tidak lengkap. Silakan isi semua form.',
        confirmButtonText: 'Kembali'
      }).then(() => {
        window.history.back();
      });
    </script>
    </body>
    </html>
    <?php
}
?>
