<?php
header('Content-Type: application/json');

// Get POST data
$fullName = isset($_POST['fullName']) ? trim($_POST['fullName']) : '';
$email = isset($_POST['email']) ? trim($_POST['email']) : '';
$phone = isset($_POST['phone']) ? trim($_POST['phone']) : '';
$lga = isset($_POST['lga']) ? trim($_POST['lga']) : '';
$role = isset($_POST['role']) ? trim($_POST['role']) : 'Member';

if (empty($fullName) || empty($phone) || empty($lga)) {
    echo json_encode(["status" => "error", "message" => "Please fill in all required fields."]);
    exit;
}

$date = date('Y-m-d h:i A');
$entry = "[$date]\nName: $fullName\nEmail: $email\nPhone: $phone\nLGA: $lga\nRole: $role\n";
$entry .= str_repeat("=", 50) . "\n\n";

// Append to cbmekiti.txt
$file = 'cbmekiti.txt';
if (file_put_contents($file, $entry, FILE_APPEND | LOCK_EX)) {
    echo json_encode(["status" => "success"]);
} else {
    echo json_encode(["status" => "error", "message" => "Failed to save data. Permissions issue."]);
}
?>
