<?php include "../db.php" ;?>
<?php
$connection= new mysqli($dbhost,$dbusername,$dbpassword,$dbname);
if($_SERVER["REQUEST_METHOD"]=="POST" && isset($_POST["add_waiter_btn"])){
    $name=$_POST["add_waiter_name"];
    $status=$_POST["add_waiter_status"];
    if(empty($name) && empty($status)){
        session_start();
        $_SESSION["waiter_add_error"]="All Fields Are Required!!";
        header("Location: ../waiters.php");
        exit();
    }
    else{
        $addWaiterQuery="INSERT INTO waiters (Waiter_Name,isActive)
        VALUES ('$name','$status')";
        $connection->query($addWaiterQuery);
        session_start();
        $_SESSION["waiter_add_alert"]="Waiter Added Successfully!!";
        header("Location: ../waiters.php");
        exit();
    }
}
if($_SERVER["REQUEST_METHOD"]=="POST" && isset($_POST["edit_waiter_btn"])){
    
    $id=$_POST["edit_waiter_id"];
    $name=$_POST["edit_waiter_name"];
    $status=$_POST["edit_waiter_status"];
    if(empty($name) && empty($status)){
        session_start();
        $_SESSION["waiter_add_error"]="All Fields Are Required!!";
        header("Location: ../waiters.php");
        exit();
    }
    else{
        $updateWaiterQuery="UPDATE waiters SET Waiter_Name='$name',isActive='$status' WHERE ID='$id'";
        $connection->query($updateWaiterQuery);
        session_start();
        $_SESSION["waiter_add_alert"]="Waiter Added Successfully!!";
        header("Location: ../waiters.php");
        exit();
    }
}

if($_POST["id"]){
    $id=$_POST["id"];
    $dltWaiterQuery="DELETE FROM waiters WHERE ID='$id'";
    $connection->query($dltWaiterQuery);
}
?>