<?php include('include/auth.php'); 
 //$now = new DateTime();
 //$today=$now->getTimestamp();

 $dateTimeZone = new DateTimeZone("Europe/Athens");
 $dateTime = new DateTime("now", $dateTimeZone);
 $timeOffset = $dateTimeZone->getOffset($dateTime);
 $today = time() + $timeOffset;
if (isset($_GET['rtype'])  && $_GET['rtype']!=1) {
    $rtype=$_GET['rtype'];
}
else {
    $rtype=1;
}
if (isset($_GET['status'])  && $_GET['status']!='0') {
        $status=$_GET['status'];
}
else {
    $status='0';
}

if (isset($_GET['veh'])  && $_GET['veh']!='0') {
    $veh=$_GET['veh'];
}
else {
$veh='0';
}
if (isset($_GET['ploc'])  && $_GET['ploc']!='0') {
    $ploc=$_GET['ploc'];
}
else {
$ploc='0';
}
if (isset($_GET['dloc'])  && $_GET['dloc']!='0') {
    $dloc=$_GET['dloc'];
}
else {
$dloc='0';
}

if (isset($_GET['picd'])  && $_GET['picd']!=0) {
    $picd=$_GET['picd'];
}
else {
    $picd=0;
}
if (isset($_GET['dropd'])  && $_GET['dropd']!=0) {
    $dropd=$_GET['dropd'];
}
else {
    $dropd=0;
}
$pick =strtotime($picd); 
$drop = strtotime($dropd)
?>
<!doctype html>
<html class="fixed dark">
<?php 
	$pagerole="lists";
	include('include/head.php');?>
<style>
    .green {
        color: green !important;
    }

    .red {
        color: red !important;
    }
    .lightyellow {color:lightyellow!important;}
    .lightblue {color:lightblue!important;}
    .rd-container-attachment{z-index: 10002;zoom:80%}
   </style>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/rome/3.0.2/rome.css"
    integrity="sha512-TAAJeGIyqkrJ4o4ZkIPesmqNKH9zowoRBGHkHUf1s4vWeII8Dv3NayKIq8Hws02HWT3OpocxeSaOQEQo9SH7ZQ=="
    crossorigin="anonymous" />
<body>
    <section class="body">

        <!-- start: header -->
        <header class="header">
            <div class="logo-container">
                <a href="index.php" class="logo">
                    <img src="img/logo-light.png" width="75" height="35" alt="Web Voyage" />
                </a>
                <div class="d-md-none toggle-sidebar-left" data-toggle-class="sidebar-left-opened" data-target="html"
                    data-fire-event="sidebar-left-opened">
                    <i class="fas fa-bars" aria-label="Toggle sidebar"></i>
                </div>
            </div>

            <!-- start: search & user box -->
            <div class="header-right">
                <?php include('include/search.php');?>


                <span class="separator"></span>
                <?php include('include/notifications.php');?>


                <span class="separator"></span>
                <?php include('include/userbox.php');?>

            </div>
            <!-- end: search & user box -->
        </header>
        <!-- end: header -->

        <div class="inner-wrapper">
            <!-- start: sidebar -->
            <?php include('include/nav.php');?>
            <!-- end: sidebar -->

            <section role="main" class="content-body">
                <header class="page-header">
                    <h2>Website Reservations <?php if($rtype==3 && ($picd!=0 || $dropd!=0)){ echo "From ".$picd." to ".$dropd;}?></h2>

                    <div class="right-wrapper text-right">
                        <ol class="breadcrumbs">
                            <li>
                                <a href="index.php">
                                    <i class="fas fa-home"></i>
                                </a>
                            </li>
                            <li><span>WV Admin</span></li>
                            <li><span>Website Reservations</span></li>
                        </ol>

                        <a class="sidebar-right-toggle" data-open="sidebar-right"><i
                                class="fas fa-chevron-left"></i></a>
                    </div>
                </header>
                <?php if($userrole==2){echo "Δεν έχετε Διακαιώματα για αυτή την λειτουργία";}
				else {

?>
               
               <h3>Filters</h3>
               <?php if($rtype==3 && ($picd!=0 || $dropd!=0)){?>
               <div class="col-md-auto text-right">
              <a class="btn btn-success " href="#modaldaterange" rel="modal:open"><i
                                                    class="fas fa-calendar-alt"></i>Change Dates Range</a>
                     </div><?php }?>
                <div class="row mb-0">

                    <div class="col-md-auto"> <label>Reservation Type</label>
                        <select  data-plugin-selectTwo class="form-control populate" onChange="window.location.href=this.value">
                        <option
                                value="?<?php echo http_build_query(array_merge($_GET, array("rtype"=>0)))?>"
                                <?php if($rtype==0) { echo "selected";} ?>>
                                All</option>
                                <option
                                value="?<?php echo http_build_query(array_merge($_GET, array("rtype"=>1)))?>"
                                <?php if($rtype==1) { echo "selected";} ?>>
                                Open Only</option>
                                <option
                                value="?<?php echo http_build_query(array_merge($_GET, array("rtype"=>2)))?>"
                                <?php if($rtype==2) { echo "selected";} ?>>
                                Past Only</option>
                                <option
                                value="?<?php echo http_build_query(array_merge($_GET, array("rtype"=>3)))?>"
                                <?php if($rtype==3) { echo "selected";} ?>>
                                Specific Date Range</option>
                               
                            </select>
                    </div>
                    
                    <div class="col-md-auto"> <label>Reservation Status</label>
                        <select  data-plugin-selectTwo class="form-control populate" onChange="window.location.href=this.value">
                        <option
                                value="?<?php echo http_build_query(array_merge($_GET, array("status"=>'0')))?>"
                                <?php if($status=='0') { echo "selected";} ?>>
                                All</option>
                                <option
                                value="?<?php echo http_build_query(array_merge($_GET, array("status"=>"confirmed")))?>"
                                <?php if($status=="confirmed") { echo "selected";} ?>>
                                Confirmed</option>
                                <option
                                value="?<?php echo http_build_query(array_merge($_GET, array("status"=>"standby")))?>"
                                <?php if($status=="standby") { echo "selected";} ?>>
                                StandBy</option>
                                <option
                                value="?<?php echo http_build_query(array_merge($_GET, array("status"=>"cancelled")))?>"
                                <?php if($status=="cancelled") { echo "selected";} ?>>
                                Cancelled</option>
                                <option
                                value="?<?php echo http_build_query(array_merge($_GET, array("status"=>"contract")))?>"
                                <?php if($status=="contract") { echo "selected";} ?>>
                                Contract</option>
                               
                            </select>
                    </div>
                   
                    
                    <div class="col-md-auto"> <label>Vehicle</label>
                        <select  data-plugin-selectTwo class="form-control populate" onChange="window.location.href=this.value">
                        <option
                                value="?<?php echo http_build_query(array_merge($_GET, array("veh"=>0)))?>"
                                <?php if($veh==0) { echo "selected";} ?>>
                                All</option>
                                <?php $sqlin3 = $db->query("SELECT * FROM jos8r_vikrentcar_cars ") or die(mysql_error()); 
					 while($rowin3 = mysqli_fetch_array($sqlin3)) {
                        
                         ?>
                                <option
                                value="?<?php echo http_build_query(array_merge($_GET, array("veh"=>$rowin3['id'])))?>"
                                <?php if($veh==$rowin3['id']) { echo "selected";} ?>>
                                <?php echo $rowin3['name'];?></option>
                             <?php } ?>
                               
                            </select>
                    </div>
                    <div class="col-md-auto"> <label>Pick Up Location</label>
                        <select  data-plugin-selectTwo class="form-control populate" onChange="window.location.href=this.value">
                        <option
                                value="?<?php echo http_build_query(array_merge($_GET, array("ploc"=>0)))?>"
                                <?php if($ploc==0) { echo "selected";} ?>>
                                All</option>
                                <?php $sqlin3 = $db->query("SELECT * FROM jos8r_vikrentcar_places ") or die(mysql_error()); 
					 while($rowin3 = mysqli_fetch_array($sqlin3)) {
                        
                         ?>
                                <option
                                value="?<?php echo http_build_query(array_merge($_GET, array("ploc"=>$rowin3['id'])))?>"
                                <?php if($ploc==$rowin3['id']) { echo "selected";} ?>>
                                <?php echo $rowin3['name'];?></option>
                             <?php } ?>
                               
                            </select>
                    </div>
                    <div class="col-md-auto"> <label>Drop Off Location</label>
                        <select  data-plugin-selectTwo class="form-control populate" onChange="window.location.href=this.value">
                        <option
                                value="?<?php echo http_build_query(array_merge($_GET, array("dloc"=>0)))?>"
                                <?php if($dloc==0) { echo "selected";} ?>>
                                All</option>
                                <?php $sqlin3 = $db->query("SELECT * FROM jos8r_vikrentcar_places ") or die(mysql_error()); 
					 while($rowin3 = mysqli_fetch_array($sqlin3)) {
                        
                         ?>
                                <option
                                value="?<?php echo http_build_query(array_merge($_GET, array("dloc"=>$rowin3['id'])))?>"
                                <?php if($dloc==$rowin3['id']) { echo "selected";} ?>>
                                <?php echo $rowin3['name'];?></option>
                             <?php } ?>
                               
                            </select>
                    </div>
                    
                    
                     </div>
                      
                <div class="row">
                    <div class="col">
                        <section class="card">
                            <header class="card-header">
                                <div class="card-actions">
                                    <a href="#" class="card-action card-action-toggle" data-card-toggle></a>
                                    <a href="#" class="card-action card-action-dismiss" data-card-dismiss></a>
                                </div>

                                <h2 class="card-title">Website Reservations</h2>
                            </header>

                            <div class="card-body">

                                <table class="table table-bordered table-striped mb-0" id="datatable-tabletools">
                                    <thead>
                                        <tr>
                                        <th>Res ID</th>
                                        <th>Ordered Date</th>
                                        <th>Client</th>
                                        <th>Vehicle</th>
                                        <th>Pick Up Location</th>
                                         <th>Pick Up Date/Time</th>
                                        <th>Drop Off Location</th>
                                        
                                        <th>Drop Off Date/Time</th>
                                        <th>Rental Price</th>
                                      
                                        <th>Status</th>
                                        <th>BackOffice</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>

                                    <?php 
$sql = "SELECT jos8r_vikrentcar_orders.id AS orderid, jos8r_vikrentcar_orders.*, jos8r_vikrentcar_places.*  FROM jos8r_vikrentcar_orders
LEFT JOIN jos8r_vikrentcar_places ON jos8r_vikrentcar_orders.idplace=jos8r_vikrentcar_places.id AND jos8r_vikrentcar_orders.idreturnplace=jos8r_vikrentcar_places.id
WHERE  jos8r_vikrentcar_orders.custdata!='Car Closed' ";

    if ($status!='0'){
        $sql .=" AND jos8r_vikrentcar_orders.status='$status'  ";
        }
        if ($rtype!=0){
        if ($rtype==1){
        $sql .=" AND jos8r_vikrentcar_orders.ritiro >'$today'  ";
        }
    elseif ($rtype==2){
            $sql .=" AND jos8r_vikrentcar_orders.ritiro <='$today'  ";
            }
    elseif ($rtype==3){
                $sql .=" AND jos8r_vikrentcar_orders.ritiro BETWEEN '$pick' AND '$drop' ";
                }
        }
        
    if ($veh!='0'){
         $sql .=" AND jos8r_vikrentcar_orders.idcar='$veh'  ";
                }
    if ($ploc!='0'){
         $sql .=" AND jos8r_vikrentcar_orders.idplace='$ploc'  ";
     } 
     if ($dloc!='0'){
        $sql .=" AND jos8r_vikrentcar_orders.idreturnplace='$dloc'  ";
    }                        
$result = mysqli_query($db,$sql);

if ($result->num_rows > 0) {
    // output data of each row
   while($row = mysqli_fetch_array($result)) {
	  	  if ($row["idplace"]!=NULL) {$idplace=$row["idplace"];} else {$idplace="1";}
	   if ($row["idreturnplace"]!=NULL) {$idreturnplace =$row["idreturnplace"];} else {$idreturnplace="1";}
	  $idsource=$row["source"];
	   $idcar=$row["idcar"];
	   
	  $sql4 = $db->query("SELECT * FROM jos8r_vikrentcar_cars WHERE jos8r_vikrentcar_cars.id=$idcar ") or die(mysql_error());
$row5 = mysqli_fetch_array( $sql4 ); 

	  $sql2 = $db->query("SELECT * FROM jos8r_vikrentcar_places WHERE jos8r_vikrentcar_places.id=$idplace ");
$row3 = mysqli_fetch_array( $sql2 ); 


	$sql3 = $db->query("SELECT * FROM jos8r_vikrentcar_places WHERE jos8r_vikrentcar_places.id=$idreturnplace ") or die(mysql_error());
$row4 = mysqli_fetch_array( $sql3 );
	   
    $picktime=date('d/m/Y H:i', $row['ritiro']);
    $droptime=date('d/m/Y H:i', $row['consegna']);

     ?>

                                        <tr>
                                            <td ><strong><?php echo $row["orderid"]; ?></strong></td>
                                            <td><?php echo date('d/m/Y H:i', $row['ts']);  ?></td>
                                            <td class="text-center"><?php if ($row["nominative"]!=NULL) {echo $row['nominative'];} else {echo $row['custdata'];} ?></td>
                                            <td class="text-center"><?php if ($row5['name']!=NULL) {echo $row5['name'];} else {echo "not set";} ?></td>

                                            <td class="text-center">
                                            <?php if ($row3["name"]!=NULL) {echo $row3["name"];} else {echo "not set";} ?></td>
                                            <td><?php echo $picktime;  ?></td>
                                            <td> <?php if ($row4['name']!=NULL) {echo $row4['name'];} else {echo "not set";} ?></td>
                                            <td><?php echo $droptime;  ?></td>
                                            <td><?php echo $row['order_total']; ?>€</td>
                                           
                                            <td class="<?php if($row["status"]=="cancelled"){echo "red";} elseif($row["status"]=="confirmed") {echo "green";} elseif($row["status"]=="standby"){echo "lightyellow";} elseif($row["status"]=="contract"){echo "lightblue";}?>"><?php echo $row['status']; ?></td>

                                            <td class="text-center"><?php  if ($row["backoffice"]=="0"){ if($row["status"]=="confirmed"){?><a href="newwebreservation.php?webid=<?php echo $row["orderid"];?>"
                                                    class="on-default edit-row"><i class="fa fa-2x danger fa-save"></i></a><?php } else {}  }  else {?><i class="fa fa-2x success green fa-check-circle"> <?php ;}?> </td>
                                                
                                            <td class="actions">
                                            <?php  if ($row["backoffice"]!="0"){ ?>
                                                <a href="reservation.php?webid=<?php echo $row["orderid"];?>"
                                                    class="on-default edit-row"><i class="fa fa-eye"></i></a><?php if($row["status"]=="confirmed"){?> <a href="newcontract.php?webid=<?php echo $row["orderid"];?>"
                                                    class="on-default edit-row"><i class="fas fa-file-signature"></i></a> <a href="resprint.php?webid=<?php echo $row["orderid"];?>"
                                                    class="on-default edit-row"><i class="fas fa-print"></i></a> <?php } } ?>

                                            </td>
                                        </tr>
                                        <?php } ?>
                                    </tbody>
                                </table>
                            </div>

                        </section>

                    </div>
                </div>
                <?php } }?>
                <!-- end: page -->
                <div id="modaldaterange" class="modal">
    <section class="card">
        <header class="card-header">
            <h2 class="card-title">Filter Reservation Dates?</h2>
        </header>
        <div class="card-body">
            <form method="get" action="reservations-local.php?">
                <div class="modal-wrapper">
                    <div class="modal-icon">
                        <i class="fas fa-question-circle"></i>
                    </div>

                    <div class="modal-text">
                        <div class="row">
                            <div class="col-md-12">



                                <div class="form-row">
                                    <div class="form-group col mb-1">
                                        <label>From</label>
                                        <input class="form-control" type="text" name="picd" id="picd"
                                            value="" required>
                                    </div>

                                </div>
                               
                                <div class="form-row">
                                    <div class="form-group col mb-1">
                                        <label>To</label>
                                        <input class="form-control" type="text" name="dropd" id="dropd"
                                            value="" required>
                                    </div>
                                </div>




                            </div>

                        </div>
                    </div>
                </div>
        </div>
        <footer class="card-footer">
            <div class="row">
                <div class="col-md-12 text-right">
                    <button class="btn btn-primary " type="submit">Filter</button>
                    <a class="btn btn-default" type="button" href="#" rel="modal:close">Cancel</a>
                </div>
                <input type="hidden" name="rtype" value="<?php echo $rtype;?>">
                <input type="hidden" name="stationid" value="<?php echo $stationid;?>">
                <input type="hidden" name="status" value="<?php echo $status;?>">

                <input type="hidden" name="source" value="<?php echo $source;?>">
                <input type="hidden" name="veh" value="<?php echo $veh;?>">
                <input type="hidden" name="ploc" value="<?php echo $ploc;?>">
                
                <input type="hidden" name="dloc" value="<?php echo $dloc;?>">
                </form>
            </div>
        </footer>
    </section>
</div>
            </section>
        </div>

        <?php include('include/aside.php');?>

    </section>


    <!-- Vendor -->
    <script src="vendor/jquery/jquery.js"></script>
    <script src="vendor/jquery-browser-mobile/jquery.browser.mobile.js"></script>
    <script src="vendor/popper/umd/popper.min.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.js"></script>
    <script src="vendor/bootstrap-datepicker/js/bootstrap-datepicker.js"></script>
    <script src="vendor/common/common.js"></script>
    <script src="vendor/nanoscroller/nanoscroller.js"></script>
    <script src="vendor/magnific-popup/jquery.magnific-popup.js"></script>
    <script src="vendor/jquery-placeholder/jquery.placeholder.js"></script>

    <!-- Specific Page Vendor -->
    <script src="vendor/select2/js/select2.js"></script>
    <script src="vendor/datatables/media/js/jquery.dataTables.min.js"></script>
    <script src="vendor/datatables/media/js/dataTables.bootstrap4.min.js"></script>
    <script src="vendor/datatables/extras/TableTools/Buttons-1.4.2/js/dataTables.buttons.min.js"></script>
    <script src="vendor/datatables/extras/TableTools/Buttons-1.4.2/js/buttons.bootstrap4.min.js"></script>
    <script src="vendor/datatables/extras/TableTools/Buttons-1.4.2/js/buttons.html5.min.js"></script>
    <script src="vendor/datatables/extras/TableTools/Buttons-1.4.2/js/buttons.print.min.js"></script>
    <script src="vendor/datatables/extras/TableTools/JSZip-2.5.0/jszip.min.js"></script>
    <script src="vendor/datatables/extras/TableTools/pdfmake-0.1.32/pdfmake.min.js"></script>
    <script src="vendor/datatables/extras/TableTools/pdfmake-0.1.32/vfs_fonts.js"></script>


    <!--(remove-empty-lines-end)-->

    <!-- Theme Base, Components and Settings -->
    <script src="js/theme.js"></script>

    <!-- Theme Custom -->
    <script src="js/custom.js"></script>

    <!-- Theme Initialization Files -->
    <script src="js/theme.init.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.css" />
    <!-- Examples -->
    <script src="js/examples/examples.datatables.default.js"></script>
    <script src="js/examples/examples.modals.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"
        integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ=="
        crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/rome/3.0.2/rome.standalone.min.js"
        integrity="sha512-cJ4aSZY0xBXrcH65MOwCloQAiTNCBm2F8/KlcBTlczgn4txZkT7h3YSXULN06pkUnsySMEg5BU4QCHAr97U7ng=="
        crossorigin="anonymous"></script>
        <?php if($rtype==3 && ($picd==0 || $dropd==0)){?>
    <script>
        $(window).on('load', function() {
        $('#modaldaterange').modal('show');
    });
        </script>
    <?php }?>
    <script>
    
    const options = {
"appendTo": document.body,
"autoClose": true,
"autoHideOnBlur": true,
"autoHideOnClick": true,
"date": true,
"dateValidator": Function.prototype,
"dayFormat": "DD",
"initialValue": null,
"inputFormat": "DD-MM-YYYY",
"invalidate": true,
"max": null,
"min": null,
"monthFormat": "MMMM YYYY",
"monthsInCalendar": 1,
"required": false,
"strictParse": false,
"styles": {
 "back": "rd-back",
 "container": "rd-container",
 "date": "rd-date",
 "dayBody": "rd-days-body",
 "dayBodyElem": "rd-day-body",
 "dayConcealed": "rd-day-concealed",
 "dayDisabled": "rd-day-disabled",
 "dayHead": "rd-days-head",
 "dayHeadElem": "rd-day-head",
 "dayRow": "rd-days-row",
 "dayTable": "rd-days",
 "month": "rd-month",
 "next": "rd-next",
 "positioned": "rd-container-attachment",
 "selectedDay": "rd-day-selected",
 "selectedTime": "rd-time-selected",
 "time": "rd-time",
 "timeList": "rd-time-list",
 "timeOption": "rd-time-option"
},
"time": false,
"timeFormat": "HH:mm",
"timeInterval": 900,
"timeValidator": Function.prototype,
"weekdayFormat": "min",
"weekStart": moment().weekday(0).day()
     }
     rome(picd, options);
     rome(dropd, options);
 </script>
    <script>
        (function ($) {

            'use strict';

            var datatableInit = function () {
                var $table = $('#datatable-tabletools');


                var table = $table.dataTable({
                    "order": [
                        [0, "desc"]
                    ],
                    "pageLength": 30,
                    "lengthMenu": [[10, 30, 50, -1], [10, 30, 50, "All"]],
                    sDom: '<"text-right mb-md"T><"row"<"col-lg-6"l><"col-lg-6"f>><"table-responsive"t>p',
                    buttons: [{
                            extend: 'print',
                            text: 'Print',
                            exportOptions: {
                    columns: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
                }
                        },
                        {
                            extend: 'excel',
                            text: 'Excel',
                            exportOptions: {
                    columns: [  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
                }
                        },
                        {
                            extend: 'pdf',
                            text: 'PDF',
                            exportOptions: {
                    columns: [  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11  ]
                },
                            customize: function (doc) {
                                var colCount = new Array();
                                $('#datatable-tabletools').find('tbody tr:first-child td').each(
                                    function () {
                                        if ($(this).attr('colspan')) {
                                            for (var i = 1; i <= $(this).attr(
                                                'colspan'); $i++) {
                                                colCount.push('*');
                                            }
                                        } else {
                                            colCount.push('*');
                                        }
                                    });
                                doc.content[1].table.widths = colCount;
                            }
                        }
                    ]
                });

                $('<div />').addClass('dt-buttons mb-2 pb-1 text-right').prependTo(
                    '#datatable-tabletools_wrapper');

                $table.DataTable().buttons().container().prependTo('#datatable-tabletools_wrapper .dt-buttons');

                $('#datatable-tabletools_wrapper').find('.btn-secondary').removeClass('btn-secondary').addClass(
                    'btn-default');
            };

            $(function () {
                datatableInit();
            });

        }).apply(this, [jQuery]);
    </script>

  
</body>

</html>
<?php  include('include/footer.php');?>