<?php include "../db.php"?>
<?php
$connection=new mysqli($dbhost,$dbusername,$dbpassword,$dbname);
if($_SERVER["REQUEST_METHOD"]=="POST" && isset($_POST["add_voucher_btn"])){
$name=$_POST["add_voucher_name"];
$discount=$_POST["add_voucher_discount"];
$status=$_POST["add_voucher_status"];
$discountType=$_POST["add_voucher_discountType"];
if(empty($name) || empty($discount) || empty($status) || empty($discountType)){
    session_start();
    $_SESSION["voucher_add_error"]="All Fields Are Required!!";
    header("Location: ../vouchers.php");
    exit();
}
else{
    
    $addVoucherQuery="INSERT into voucher (VoucherName,Discount,IsActive,Discount_Type) VALUES ('$name','$discount','$status','$discountType')";
    $connection->query($addVoucherQuery);
    session_start();
    $_SESSION["voucher_add_alert"]="Voucher Added Successfully!!";
    header("Location: ../vouchers.php");
    exit();
}
}
if($_SERVER["REQUEST_METHOD"]=="POST" && isset($_POST["edit_voucher_btn"])){
$id=$_POST["edit_voucher_id"];
$name=$_POST["edit_voucher_name"];
$discount=$_POST["edit_voucher_discount"];
$status=$_POST["edit_voucher_status"];
$discountType=$_POST["edit_voucher_discountType"];
if(empty($name) || empty($discount) || empty($status) || empty($discountType)){
    session_start();
    $_SESSION["voucher_edit_error"]="All Fields Are Required!!";
    header("Location: ../vouchers.php");
    exit();
}
else{
    
    $editVoucherQuery="UPDATE voucher SET VoucherName='$name',Discount='$discount',IsActive='$status',Discount_Type='$discountType' WHERE ID='$id'";
    $connection->query($editVoucherQuery);
    session_start();
    $_SESSION["voucher_edit_alert"]="Voucher Added Successfully!!";
    header("Location: ../vouchers.php");
    exit();
}
}
if($_POST["id"]){
    $ID=$_POST["id"];
    $dltVoucherQuery="DELETE from voucher WHERE ID='$ID'";
    $connection->query($dltVoucherQuery);
}
?>