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

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

       
       $fid=$_POST['fid'];
        $oldconsegna=$_POST['oldconsegna'];
        $oldidreturnplace=$_POST['oldidreturnplace'];
        $newconsegna=$_POST['newconsegna3'];
        $newidreturnplace=$_POST['newidreturnplace'];
        $contid=$_POST['contid'];
        $extcost=$_POST['extcost'];
        $currcost=$_POST['currcost'];
        $sql3 = $db2->query("SELECT * FROM jostj_vikrentcar_places WHERE jostj_vikrentcar_places.id=$oldidreturnplace ");
        $row3 = mysqli_fetch_array( $sql3 );
        $sql4 = $db2->query("SELECT * FROM jostj_vikrentcar_places WHERE jostj_vikrentcar_places.id=$newidreturnplace ");
        $row4 = mysqli_fetch_array( $sql4 );
        $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="Extend";
        $item="Reservation";
        $details="Extended ".$item." from ".$row3['name']." ".$olddroptime." to ".$row4['name']." ".$newdroptime;  
        $dateTimeZone = new DateTimeZone("Europe/Athens");
        $dateTime = new DateTime("now", $dateTimeZone);
        $timeOffset = $dateTimeZone->getOffset($dateTime);
        $date = time() + $timeOffset;
        $totalcont=$extcost+$currcost;
        $extcost=number_format($extcost,2);
        $extcost2=number_format($totalcont,2);
        $nm1="1";
        $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 contract_extensions (cont_ext_id, cont_ext_res, cont_ext_cid, contract_ext_ts, contract_ext_newprice, ext_old_date, ext_old_return, ext_new_date, ext_new_rplace, ext_employee) VALUES (?, ?, ?,?, ?, ?, ?, ?,?,? )");
            $sh->execute([NULL, $fid, $contid, $date, $extcost, $oldconsegna, $oldidreturnplace, $newconsegna, $newidreturnplace, $user]);
        
            $sh = $db3->prepare("UPDATE jostj_vikrentcar_orders SET ext_old_date=?, ext_old_return=?, ext_on=?  WHERE id=?");
            $sh->execute([$oldconsegna,  $oldidreturnplace, $date, $fid,]);
                    
            $sh = $db3->prepare("UPDATE contracts SET cont_extended=?, contract_ext_ts=?, contract_ext_newprice=?, cont_return=? WHERE contract_id=?");
            $sh->execute([$nm1, $date, $extcost2, $newconsegna, $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;
} ?>      