<?php
    session_start();

    require("../includes/database_connect.php");

    $tools_id = $_POST['tools_id'];
    $moveout_quantity = $_POST['moveout_quantity'];
    $total_available_quantity = $_POST['total_available_quantity'];
    $taken_by = $_POST['taken_by'];
    $used_by = $_POST['used_by'];
    $location = $_POST['location'];
    $timestamp =date('Y-m-d H:i:s');

    // var_dump($_POST);
    // die();
   
    $sql = "insert into  tools_history (tools_id, quantity, taken_by, used_by, type, created_at, location, current_quantity) values ('$tools_id','$moveout_quantity','$taken_by','$used_by','move_out','$timestamp', '$location', '$total_available_quantity')";
    $sql2 = "update tools set quantity='$total_available_quantity' where id='$tools_id'";

    if ($conn->query($sql)) {
        $conn->query($sql2);
        $_SESSION['success'] = "Stock Move Out successfully";
    } else {
        $_SESSION['error'] = "Error updating record: " . $conn->error;
    }
    
    header('location: ../tools_listing.php');
?>