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

if (isset($_POST['neworder_total'])) {
        //variables to hold our submitted data with post     
       
        $fid=$_POST['fid'];
        $dateTimeZone = new DateTimeZone("Europe/Athens");
        $dateTime = new DateTime("now", $dateTimeZone);
        $timeOffset = $dateTimeZone->getOffset($dateTime);
        $date = time() + $timeOffset;
        
        $neworder_total=$_POST['neworder_total'];
        $newtotpaid=$_POST['newtotpaid'];
        $newcust_cost=$_POST['newcust_cost'];
        $newextracosts=$_POST['newextracosts'];
        $newidpayment=$_POST['newidpayment'];

        $oldorder_total=$_POST['oldorder_total'];
        $oldtotpaid=$_POST['oldtotpaid'];
        $oldcust_cost=$_POST['oldcust_cost'];
        $oldextracosts=$_POST['oldextracosts'];
        $oldidpayment=$_POST['oldidpayment'];

        $action="Changed Reservation";
        $item="Finance Data";
        $details="Changed ".$item." Order Total from ".$oldorder_total. " to ".$neworder_total.", </br> Total Paid from ".$oldtotpaid. " to ".$newtotpaid.",</br> Pay Locally from ".$oldcust_cost. " to ".$newcust_cost.",</br> Extra Costs from ".$oldextracosts. " to ".$newextracosts.", </br> Payment Method from ".$oldidpayment. " to ".$newidpayment;  

        $newidpayment = $db2->real_escape_string($newidpayment);
        $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 order_total=?, totpaid=?, cust_cost=?, extracosts=?, idpayment=? WHERE id=?");
            $sh->execute([$neworder_total, $newtotpaid , $newcust_cost , $newextracosts, $newidpayment, $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();
} ?>      