<?php include "../db.php";?>
<?php
 $connection = new mysqli($dbhost,$dbusername,$dbpassword,$dbname);
if($_SERVER["REQUEST_METHOD"]=="POST" && isset($_POST["add_product"])){
    $name=$_POST["add_product_name"];
    $price=$_POST["add_product_price"];
    $category=$_POST["add_product_category"];
   

    if(empty($name) && empty($price) ){
        session_start();
        $_SESSION["product_error"]="All fields are required!!";
        header("Location: ../products.php");
        exit();
    }
    else{
        $addProductQuery="INSERT into product VALUES('','$name','$price','$category','')";
        
        $connection->query($addProductQuery);
        session_start();
        $_SESSION["product_alert"]="Product added successfully!!";
        header("Location: ../products.php");
        exit();
    }
}

if($_SERVER["REQUEST_METHOD"]=="POST"&& isset($_POST["edit_submit_product"])){
  $id=$_POST["edit_product_id"];
  $editName=$_POST["edit_product_name"];
  $editPrice=$_POST["edit_product_price"];
  $editProductCategory=$_POST["edit_product_category"];
  if(empty($editName) && empty($editPrice) && empty($editProductCategory)){
    session_start();
    $_SESSION["productediterror"]="Empty fields are not allowed for update!!";
    header("Location: ../products.php");
    exit();
  }
  else{
    $editProductQuery="UPDATE product SET Name='$editName',Price='$editPrice',Category='$editProductCategory' WHERE ID='$id'";
    $connection->query($editProductQuery);
    session_start();
    $_SESSION["producteditalert"]="Product Updated Successfully!!";
    header("Location: ../products.php");
    exit();
  }
}
    if(isset($_POST["id"])){
$id=$_POST["id"];
$deleteProductQuery="DELETE FROM product WHERE ID='$id'";
$connection->query($deleteProductQuery);
    }
?> 