<?php
    session_start();

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

    $electrical_id = $_POST['electrical_id'];
    $timestamp =date('Y-m-d H:i:s');

    //var_dump($_POST);
    //die();

    $latest_history_sql = "select * from electrical_history where electrical_id='$electrical_id' order by id desc limit 1";
    $result = $conn->query($latest_history_sql);
    $latest_history =  $result->fetch_all(MYSQLI_ASSOC);
    $latest_id = $latest_history[0]['id'];

    $sql = "UPDATE electrical_history SET available_at='$timestamp' where id='$latest_id'";
    $sql2 = "update electrical set status='available' where id='$electrical_id'";

    if ($conn->query($sql)) {
        $conn->query($sql2);
        $_SESSION['success'] = "Make Available successfull!";
    } else {
        $_SESSION['error'] = "Error updating record: " . $conn->error;
    }
    
    header('location: ../electrical_listing.php?type='.$_POST['type']);
?>