<?php include('../include/auth.php');
include('../include/lskfjwfe.php');
include('../include/header.php');
$fid=$_GET['id'];
$user=$userid;
$status2=$_GET['orderStatus'];

if ($status2=="on-hold"){
    $status="standby";
}
elseif($status2=="completed"){
    $status="confirmed";
}
elseif($status2=="cancelled"){
    $status="cancelled";
}
elseif($status2=="refunded"){
    $status="contract";
}
if (isset($_POST['orderStatus'])) {
        //variables to hold our submitted data with post

        $status2 = $_POST['orderStatus'];
        
        if ($status2=="on-hold"){
            $status="standby";
        }
        elseif($status2=="completed"){
            $status="confirmed";
        }
        elseif($status2=="cancelled"){
            $status="cancelled";
        }
        elseif($status2=="refunded"){
            $status="contract";
        }
       $fid=$_POST['fid'];
        $oldstatus=$_POST['oldstatus'];
        $action="Changed Reservation";
        $item="Status";
        $details="Changed ".$item." from ".$oldstatus. " to ".$status;  
        $dateTimeZone = new DateTimeZone("Europe/Athens");
        $dateTime = new DateTime("now", $dateTimeZone);
        $timeOffset = $dateTimeZone->getOffset($dateTime);
        $date = time() + $timeOffset;
        $status = $db2->real_escape_string($status);
        $oldstatus = $db2->real_escape_string($oldstatus);
        $action = $db2->real_escape_string($action);
        $item = $db2->real_escape_string($item);
        $details = $db2->real_escape_string($details);
       

        $db3->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        
        try {
            $db3->beginTransaction();
        
            $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]);
        
            $sh = $db3->prepare("UPDATE jostj_vikrentcar_orders SET status=? WHERE id=?");
            $sh->execute([$status, $fid]);
        
            $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>" . $db3->error;
        }
$db3->close();
} ?>      