<?php
session_start();

$api_name = $_POST['api_name'] ?? '';
$api_url  = $_POST['api_url'] ?? '';
$api_key  = $_POST['api_key'] ?? '';
$id       = $_POST['id'] ?? '';
$country_code  = $_POST['country_code'] ?? '';
include("../auth.php"); // $pdo connection

// admin check


// validation
if ($api_name !== "" && $api_url !== "" && $api_key !== "" && $id !== "") {

    try {
        $stmt = $pdo->prepare(
            "UPDATE api_details
             SET name = ?, api_key = ?, api_url = ?, country_code = ?
             WHERE id = ?"
        );

        $stmt->execute([
            $api_name,
            $api_key,
            $api_url,
            $country_code,
            $id
        ]);

        echo '<script>
            $(document).ready(function() {
                Swal.fire({
                    title: "Success",
                    text: "Details Update Successful",
                    icon: "success",
                    confirmButtonText: "Ok"
                });
            });
        </script>';

    } catch (PDOException $e) {
        echo '<script>
            $(document).ready(function() {
                Swal.fire({
                    title: "Error",
                    text: "Database Error",
                    icon: "error",
                    confirmButtonText: "Ok"
                });
            });
        </script>';
    }

} else {
    echo '<script>
        $(document).ready(function() {
            Swal.fire({
                title: "Warning!",
                text: "Please fill all the fields first",
                icon: "warning",
                confirmButtonText: "Ok"
            });
        });
    </script>';
}
?>
