<?php
require 'koneksi.php';
require 'vendor/autoload.php'; // Pastikan sudah menginstall Twilio SDK

function sendOtp($to, $otp) {
    $sid = 'YOUR_TWILIO_SID';
    $token = 'YOUR_TWILIO_AUTH_TOKEN';
    $twilioNumber = 'YOUR_TWILIO_WHATSAPP_NUMBER';

    $client = new \Twilio\Rest\Client($sid, $token);
    $message = "Your OTP is: $otp";

    $client->messages->create("whatsapp:$to", [
        'from' => "whatsapp:$twilioNumber",
        'body' => $message,
    ]);
}

// Proses pengiriman OTP
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nama_pengguna = $_POST['nama_pengguna'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $otp = rand(100000, 999999); // Generate OTP

    // Simpan data KYC dan OTP ke database
    $stmt = $koneksi->prepare("INSERT INTO kyc (nama_pengguna_anggota, email, phone, otp) VALUES (?, ?, ?, ?)");
    $stmt->bind_param("ssss", $nama_pengguna, $email, $phone, $otp);
    $stmt->execute();

    // Kirim OTP ke WhatsApp
    sendOtp($phone, $otp);
    echo "OTP telah dikirim ke WhatsApp.";
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kirim OTP</title>
</head>
<body>
    <form action="send_otp.php" method="POST">
        <input type="text" name="nama_pengguna" placeholder="Nama Pengguna" required>
        <input type="email" name="email" placeholder="Email" required>
        <input type="tel" name="phone" placeholder="Nomor WhatsApp" required>
        <button type="submit">Kirim OTP</button>
    </form>
</body>
</html>
