<?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['orderStatus'])) {
        //variables to hold our submitted data with post     
       
        $status2 = $_POST['orderStatus'];
        
        if ($status2=="on-hold"){
            $status="standby";
        }
        elseif($status2=="completed"){
            $status="confirmed";
        }
        
    

        $dateTimeZone = new DateTimeZone("Europe/Athens");
        $dateTime = new DateTime("now", $dateTimeZone);
        $timeOffset = $dateTimeZone->getOffset($dateTime);
        $date = time() + $timeOffset;
        $days = $_POST['days'];
        $source=$_POST['source'];
        $employee = $_POST['employee'];
        $nominative =$_POST['nominative'];
        $phone =$_POST['phone'];
        $custmail = $_POST['custmail'];
        $custdata = $_POST['custdata'];
        $ritiro2 = $_POST['ritiro2'];					 
        $idplace = $_POST['idplacer'];
        $idcar = $_POST['idcar'];
        $idtar=$_POST['idtar'];
        $op1q=$_POST['op1q'];
        
        $op2q=$_POST['op2q'];
        
        $op3q=$_POST['op3q'];
        
        $op4q=$_POST['op4q'];
       
        $op5q=$_POST['op5q'];
       
        $op6q=$_POST['op6q'];
       
        $op7q=$_POST['op7q'];
       
        $newop="1:".$op1q.";2:".$op2q.";3:".$op3q.";4:".$op4q.";5:".$op5q.";6:".$op6q.";7:".$op7q;
        $consegna2 = $_POST['consegna2'];
        $idreturnplace = $_POST['idreturnplacer'];					 
        $order_total = $_POST['order_total'];
        $totpaid = $_POST['totpaid'];
        $cust_cost = $_POST['cust_cost'];
        $extracosts = $_POST['extracosts'];      
        $idpayment = $_POST['idpayment'];
        $adminnotes = $_POST['adminnotes'];      
        $email_text = $_POST['email_text'];
        $ts = $_POST['ts'];      
        $cd_dob=$_POST['cd_dob'];
        $cd_flight=$_POST['flight'];
        $cd_hotel=$_POST['hotel'];
        $cd_hotelroom=$_POST['hotelroom'];
        $cd_dlnumber=$_POST['cd_dlnumber'];
        $cd_passnum=$_POST['cd_passnum'];
        $cd_address=$_POST['cd_address'];

        $ritiro= \DateTime::createFromFormat('d-m-Y H:i', $ritiro2)->getTimestamp(); 
        $consegna= \DateTime::createFromFormat('d-m-Y H:i', $consegna2)->getTimestamp(); 
        $custdata=preg_replace('/\r?\n$/', ' ', $custdata);
        $adminnotes=preg_replace('/\r?\n$/', ' ', $adminnotes);
        $email_text=preg_replace('/\r?\n$/', ' ', $email_text);
        $totpaid=number_format($totpaid,2);
        $cust_cost=number_format($cust_cost,2);
        $extracosts=number_format($extracosts,2);
        $order_total=number_format($order_total,2);

        $db3->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        
        try {
            $db3->beginTransaction();          
        
            $sh = $db3->prepare("INSERT INTO jostj_vikrentcar_orders (id, ts, status, idcar, days, ritiro, consegna, idtar, optionals, custmail, idplace, idreturnplace, totpaid, idpayment, order_total, phone, nominative, adminnotes, cust_cost, extracosts, source, employee, email_text, cd_dob, cd_flight, cd_hotel,  cd_hotelroom, cd_dlnumber, cd_passnum, cd_address, custdata) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?  )");
            $sh->execute([NULL, $ts, $status, $idcar, $days, $ritiro, $consegna, $idtar, $newop, $custmail, $idplace, $idreturnplace, $totpaid, $idpayment, $order_total, $phone, $nominative, $adminnotes, $cust_cost, $extracosts, $source, $employee, $email_text, $cd_dob, $cd_flight, $cd_hotel,  $cd_hotelroom, $cd_dlnumber, $cd_passnum, $cd_address, $custdata]);

                        $fid=$db3->lastInsertId();
            $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 ( PDOException  $e ) {
            $db3->rollBack();
            echo "<div class='alert alert-danger'>Error updating record: </div>" . $e->getMessage();
        }
        $db3= null;
    }
    ?>      