<?php

// 🔹 Sanitasi input (anti XSS)
function clean($data){
    return htmlspecialchars(strip_tags(trim($data)));
}

// 🔹 Ambil nama tamu dari URL
function getTamu(){
    if(isset($_GET['to'])){
        return clean($_GET['to']);
    }
    return "Tamu Undangan";
}

// 🔹 Format tanggal (Indonesia)
function tanggalIndo($tanggal){
    $bulan = [
        1 => 'Januari','Februari','Maret','April','Mei','Juni',
        'Juli','Agustus','September','Oktober','November','Desember'
    ];

    $split = explode('-', $tanggal);
    return $split[2] . ' ' . $bulan[(int)$split[1]] . ' ' . $split[0];
}

// 🔹 Simpan RSVP
function simpanRSVP($conn, $nama, $ucapan){
    $nama = clean($nama);
    $ucapan = clean($ucapan);

    $query = "INSERT INTO rsvp (nama, ucapan) VALUES ('$nama', '$ucapan')";
    return mysqli_query($conn, $query);
}

// 🔹 Ambil semua ucapan
function getUcapan($conn){
    $result = mysqli_query($conn, "SELECT * FROM rsvp ORDER BY id DESC");
    return $result;
}

// 🔹 Hitung jumlah tamu
function countTamu($conn){
    $result = mysqli_query($conn, "SELECT COUNT(*) as total FROM rsvp");
    $data = mysqli_fetch_assoc($result);
    return $data['total'];
}