<?php
session_start();

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

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

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

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

        // 🔹 Check service exists
        $check = $pdo->prepare("SELECT id FROM services WHERE id = ?");
        $check->execute([$service_id]);

        if ($check->fetch()) {

            // 🔹 Update service
            $update = $pdo->prepare(
                "UPDATE services 
                 SET service_code = ?, service_name = ?, price = ?
                 WHERE id = ?"
            );
            $update->execute([$service_code, $name, $price, $service_id]);

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

        } else {
            echo '<script>
                $(document).ready(function() {
                    Swal.fire({
                        title: "Warning!",
                        text: "Invalid Service Id",
                        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>';
}
?>
