<?php
session_start();

$server_id    = $_POST['server_id'] ?? '';
$service_code = $_POST['service_code'] ?? '';
$name         = $_POST['name'] ?? '';
$price        = $_POST['price'] ?? '';

include("../auth.php"); // yahan $pdo hona chahiye

// 🔹 Empty check
if ($server_id !== "" && $service_code !== "" && $name !== "" && $price !== "") {

    // 🔹 Numeric price check
    if (is_numeric($price)) {

        // 🔹 Check duplicate service
        $check = $pdo->prepare(
            "SELECT id FROM services WHERE service_code = ? AND api_id = ?"
        );
        $check->execute([$service_code, $server_id]);

        if (!$check->fetch()) {

            // 🔹 Insert service
            $insert = $pdo->prepare(
                "INSERT INTO services (api_id, price, service_name, service_code)
                 VALUES (?,?,?,?)"
            );
            $insert->execute([$server_id, $price, $name, $service_code]);

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

        } else {
            echo '<script>
                $(document).ready(function() {
                    Swal.fire({
                        title: "Warning!",
                        text: "Service id Already Added",
                        icon: "warning",
                        confirmButtonText: "Ok"
                    });
                });
            </script>';
        }

    } else {
        echo '<script>
            $(document).ready(function() {
                Swal.fire({
                    title: "Warning!",
                    text: "Please Enter Numerical Values",
                    icon: "warning",
                    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>';
}
?>
