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

if (isset($_POST['updateprice'])) {
        //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;
        $oldopcost=$_POST['oldopcost'];
        $updateprice=$_POST['updateprice'];
        $oldop=$_POST['oldop'];

        $op1q=$_POST['op1q'];
        $op1v=$_POST['op1v'];
        $op2q=$_POST['op2q'];
        $op2v=$_POST['op2v'];
        $op3q=$_POST['op3q'];
        $op3v=$_POST['op3v'];
        $op4q=$_POST['op4q'];
        $op4v=$_POST['op4v'];
        $op5q=$_POST['op5q'];
        $op5v=$_POST['op5v'];
        $op6q=$_POST['op6q'];
        $op6v=$_POST['op6v'];
        $op7q=$_POST['op7q'];
        $op7v=$_POST['op7v'];
        $newop="1:".$op1q.";2:".$op2q.";3:".$op3q.";4:".$op4q.";5:".$op5q.";6:".$op6q.";7:".$op7q;
        $totcost=$op1v+$op2v+$op3v+$op4v+$op5v+$op6v+$op7v;

        if ($updateprice=="no"){
            $pricematter="Without Updating Reservation Price";
            $extcost=$oldopcost;
        }
        else{
            $pricematter="And Updating Extra Cost Finance data to ".$totcost." from ".$oldopcost;
            $extcost=$totcost;
        }
        $action="Changed Reservation";
        $item="Optionals Extra";
        $details="Changed ".$item." from ".$oldop. " to ".$newop." ".$pricematter;  

        $newop = $db2->real_escape_string($newop);
        $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 optionals=?, extracosts=? WHERE id=?");
            $sh->execute([$newop, $extcost, $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();
} ?>      