<?php
session_start();
require_once "config.php";

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Function to sanitize input
function sanitizeInput($input) {
    return htmlspecialchars(strip_tags(trim($input)));
}


if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
    $product_name = sanitizeInput($_POST['product_name']);
    $sku = sanitizeInput($_POST['sku']);
    $product_type = sanitizeInput($_POST['product_type']);
    $vendor = sanitizeInput($_POST['vendor']);
    $yom = sanitizeInput($_POST['yom']);
    $unit = sanitizeInput($_POST['unit']);
    $size = sanitizeInput($_POST['size']);
    $weight = sanitizeInput($_POST['weight']);
    $features = sanitizeInput($_POST['features']);
    $purchase_price = sanitizeInput($_POST['purchase_price']);
    $purchase_mode = sanitizeInput($_POST['purchase_mode']);
    $gst_percentage = isset($_POST['gst_percentage']) ? sanitizeInput($_POST['gst_percentage']) : null;
    $hsn_number = sanitizeInput($_POST['hsn_number']);
    $depot_name = sanitizeInput($_POST['depot_name']);
    $line = sanitizeInput($_POST['line']);
    $status = sanitizeInput($_POST['status']);

   
    if (isset($_GET['action'])) {
        $action = $_GET['action'];

        if ($action == 'add') {
            
              $sql = "INSERT INTO products (name, sku, type, vendor, yom, unit, size, weight, features, price, purchase_mode, gst_percentage, hsn_number, depot_name, line, status, created_at, updated_at) 
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        
             if ($stmt = mysqli_prepare($conn, $sql)) {
            $date = date("Y-m-d H:i:s"); // current date and time
             mysqli_stmt_bind_param($stmt, "ssssssssssssssssss", 
                           $product_name, $sku, $product_type, $vendor, $yom, $unit, $size, $weight, 
                           $features, $purchase_price, $purchase_mode, $gst_percentage, 
                           $hsn_number, $depot_name, $line, $status, $date, $date);

                if (mysqli_stmt_execute($stmt)) {
                    mysqli_stmt_close($stmt);
                    mysqli_close($conn);
                    $_SESSION['success_message'] = "Product added successfully.";
                    header("location: inventory.php");
                    exit();
                } else {
                    echo "Error: " . mysqli_error($conn);
                }
            } else {
                echo "Error: " . mysqli_error($conn);
            }
        } 
        
        if ($action == 'edit') {
            
            // print_r($_POST);die;

           $product_id = sanitizeInput($_POST['product_id']); 

           $sql = "UPDATE products SET 
                    name=?, sku=?, type=?, vendor=?, yom=?, unit=?, size=?, weight=?, features=?, price=?, 
                    purchase_mode=?, gst_percentage=?, hsn_number=?, depot_name=?, line=?, status=?, updated_at=? 
                    WHERE id=?";
            if ($stmt = mysqli_prepare($conn, $sql)) {
                 mysqli_stmt_bind_param($stmt, "sssssssssssssssssi", 
                    $product_name, $sku, $product_type, $vendor, $yom, $unit, $size, $weight, $features, 
                    $purchase_price, $purchase_mode, $gst_percentage, $hsn_number, $depot_name, $line, 
                    $status, $date, $product_id);
                    
                if (mysqli_stmt_execute($stmt)) {
                    mysqli_stmt_close($stmt);
                    mysqli_close($conn);
                    $_SESSION['success_message'] = "Products updated successfully.";
                    header("location: inventory.php");
                    exit();
                } else {
                    echo "Error: " . mysqli_error($conn);
                }
            } else {
                echo "Error: " . mysqli_error($conn);
            }
        }
        
        if ($action == 'delete') {
            $vendor_id = sanitizeInput($_POST['delete_id']); 
            $sql = "DELETE FROM vendors WHERE vendor_id=?";
            
            if ($stmt = mysqli_prepare($conn, $sql)) {
                mysqli_stmt_bind_param($stmt, "i", $vendor_id);
                if (mysqli_stmt_execute($stmt)) {
                    mysqli_stmt_close($stmt);
                    mysqli_close($conn);
                    header("Location: inventory.php");
                    exit();
                } else {
                    echo "Error: " . mysqli_error($conn);
                }
            }
        }
    }
    else{
        
    }
}
?>