<?php
/**
 * kirim.php — Penerima form kontak website SatNet
 * Mengirim email via SMTP (PHPMailer) ke info@sat.net.id
 * Letakkan di: public_html/kirim.php
 */

header('Content-Type: application/json');
// --- CORS: sesuaikan dengan domain website Anda ---
header('Access-Control-Allow-Origin: https://sat.net.id');
header('Access-Control-Allow-Methods: POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');

// Tangani preflight request (OPTIONS)
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(204);
    exit;
}

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode(['success' => false, 'error' => 'Method not allowed']);
    exit;
}

// --- Ambil & validasi data ---
$data = json_decode(file_get_contents('php://input'), true);
$nama       = trim($data['nama'] ?? '');
$perusahaan = trim($data['perusahaan'] ?? '');
$email      = trim($data['email'] ?? '');
$telepon    = trim($data['telepon'] ?? '');
$topik      = trim($data['topik'] ?? '');
$pesan      = trim($data['pesan'] ?? '');

if ($nama === '' || $pesan === '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo json_encode(['success' => false, 'error' => 'Data tidak lengkap atau email tidak valid']);
    exit;
}

// --- Muat PHPMailer ---
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// Cara A (file manual di folder /PHPMailer/):
require _DIR_ . '/PHPMailer/Exception.php';
require _DIR_ . '/PHPMailer/PHPMailer.php';
require _DIR_ . '/PHPMailer/SMTP.php';
// Cara B (Composer): hapus 3 baris di atas, ganti dengan:
// require _DIR_ . '/vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    // ====== KONFIGURASI SMTP — SESUAIKAN DENGAN MAIL SERVER SATNET ======
    $mail->isSMTP();
    $mail->Host       = 'smtp.sat.net.id';        // host SMTP (mis. mail.sat.net.id / smtp.sat.net.id)
    $mail->SMTPAuth   = true;
    $mail->Username   = 'info@sat.net.id';        // akun email pengirim
    $mail->Password   = 'ikangoreng';  // password akun email
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // STARTTLS (port 587) atau ENCRYPTION_SMTPS (port 465)
    $mail->Port       = 587;                       // 587 utk STARTTLS, 465 utk SMTPS
    $mail->CharSet    = 'UTF-8';
    // ====================================================================

    // Pengirim & penerima
    $mail->setFrom('info@sat.net.id', 'SatNet Website');
    $mail->addAddress('info@sat.net.id', 'SatNet Info');
    $mail->addReplyTo($email, $nama);   // agar bisa balas langsung ke pengirim form

    // Isi email
    $mail->isHTML(false);
    $mail->Subject = "Permintaan Kontak Website - {$nama} ({$topik})";
    $mail->Body =
        "Pesan baru dari form kontak website SatNet:\n\n" .
        "Nama       : {$nama}\n" .
        "Perusahaan : " . ($perusahaan ?: '-') . "\n" .
        "Email      : {$email}\n" .
        "Telepon    : " . ($telepon ?: '-') . "\n" .
        "Topik      : {$topik}\n\n" .
        "Pesan:\n{$pesan}\n";

    $mail->send();
    echo json_encode(['success' => true]);
} catch (Exception $e) {
    echo json_encode(['success' => false, 'error' => 'Gagal mengirim: ' . $mail->ErrorInfo]);
}
?>
