<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get form data from AJAX request
    $host = $_POST['whatsapp_gwhost'];
    $database = $_POST['whatsapp_gwdb'];
    $username = $_POST['whatsapp_gwuser'];
    $password = $_POST['whatsapp_gwpass'];

    // Create the content for the config.php file
    $configContent = "<?php\n";
    $configContent .= "// Database connection\n";
    $configContent .= "\$host = '$host'; // Your database host\n";
    $configContent .= "\$username = '$username'; // Your database username\n";
    $configContent .= "\$password = '$password'; // Your database password\n";
    $configContent .= "\$database = '$database'; // Your database name\n\n";
    $configContent .= "\$mysqli = new mysqli(\$host, \$username, \$password, \$database);\n\n";
    $configContent .= "// Check connection\n";
    $configContent .= "if (\$mysqli->connect_error) {\n";
    $configContent .= "    die(\"Connection failed: \" . \$mysqli->connect_error);\n";
    $configContent .= "}\n";
    $configContent .= "?>";

    // Write the config.php file
    $file = fopen('cron/config.php', 'w');
    if ($file) {
        fwrite($file, $configContent);
        fclose($file);
        echo "Config file created successfully!";
    } else {
        echo "Unable to create config file.";
    }
}
?>
