<?php 
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
error_reporting(E_ALL);
include('../include/auth.php');
include('../include/lskfjwfe.php');
include('../include/header.php');
$user=$userid;

if (isset($_POST['newpick'])) {
        //variables to hold our submitted data with post

       
       $fid=$_POST['fid'];
        $oldconsegna=$_POST['oldconsegna'];
        $oldveh=$_POST['oldveh'];
        $newconsegna=$_POST['newpick'];
        $newveh=$_POST['newveh'];
        $reason=$_POST['reason'];
        $notes=$_POST['notes'];
        $sql3 = $db2->query("SELECT * FROM fleet LEFT JOIN jostj_vikrentcar_cars ON jostj_vikrentcar_cars.id=fleet.veh_id WHERE fleet.fleet_id=$oldveh ");
        $row3 = mysqli_fetch_array( $sql3 );
        $sql4 = $db2->query("SELECT * FROM fleet LEFT JOIN jostj_vikrentcar_cars ON jostj_vikrentcar_cars.id=fleet.veh_id WHERE fleet.fleet_id=$newveh");
        $row4 = mysqli_fetch_array( $sql4 );
        $contid=$_POST['contid'];
      
        $newconsegna= \DateTime::createFromFormat('d-m-Y H:i', $newconsegna)->getTimestamp(); 
        $olddroptime=date('d-m-Y H:i', $oldconsegna);
        $newdroptime=date('d-m-Y H:i', $newconsegna);
        $action="Change";
        $item="Vehicle";
        $details="Change ".$item." from ".$row3['lic_num']." to ".$row4['lic_num']." on ".$date;  
        $dateTimeZone = new DateTimeZone("Europe/Athens");
        $dateTime = new DateTime("now", $dateTimeZone);
        $timeOffset = $dateTimeZone->getOffset($dateTime);
        $date = time() + $timeOffset;
        $totalcont=$extcost+$currcost;
           

        $db3->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        
        try {
            $db3->beginTransaction();
        
            $sh = $db3->prepare("INSERT INTO veh_change (change_id, contract_id, veh_id, change_from, reason, notes) VALUES (?, ?, ?, ?, ?, ?)");
$sh->execute([NULL, $contid, $newveh, $newconsegna, $reason, $notes]);
        
            $sh = $db3->prepare("UPDATE contracts SET cont_veh_change=? WHERE contract_id=?");
$sh->execute([1, $contid,]);

            $sh = $db3->prepare("INSERT INTO reservation_log (reservation_log_id, reservation_log_res, reservation_log_user, reservation_log_date, reservation_log_action, reservation_log_item, reservation_log_details) VALUES (?, ?, ?,?, ?, ?,? )");
            $sh->execute([NULL, $fid, $user, $date, $action, $item, $details]);

            $db3->commit();
            echo "<div class='alert alert-success'>Record Updated Succesfully</div><script>"; echo "window.location.replace('../reservation.php?id=$fid');";
            echo "</script>";   
        exit;
        } catch ( Exception $e ) {
            $db3->rollBack();
            echo "<div class='alert alert-danger'>Error updating record: </div>" . $e->getMessage();
        }
        $db3= null;
} ?>      