<?php
include("connection.php");

ob_start(); // Start output buffering

if (isset($_POST['add'])) {
    $vendor_name = $_POST['vendor_name'];
    $currentDateTime = date('Y-m-d H:i:s');
    // Prepare the insert statement
    $sql = "INSERT INTO vendor (vendor_name, date_time) VALUES ('$vendor_name', '$currentDateTime')";
    $result = mysqli_query($conn, $sql);
    if ($result) {
        header("Location: add_vender.php?success=Vendor Added Successfully!!!");
        exit(); // Exit to prevent further script execution
    } else {
        header("Location: add_vender.php?error=Failed insertion!!!");
        exit(); // Exit to prevent further script execution
    }
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $itemId = $_POST['item_id'];
    
    if (isset($_POST['activate'])) {
        $status = 1;
    } elseif (isset($_POST['block'])) {
        $status = 0;
    }
    
    $updateQuery = "UPDATE vendor SET status = $status WHERE id = $itemId";
    mysqli_query($conn, $updateQuery);
    
    // Redirect back to the view table page
    header('Location: add_vender.php');
    exit();
}

ob_end_flush(); // End output buffering and send headers
?>