<?php
ob_start();
include_once 'classes/common.php';
include_once 'classes/DB.class.php';
include_once 'session/admin-session.php';
ini_set("display_errors", 0);
$db = new DB();
$com = new common();

$admin_id = $_SESSION['rcuesr_room_admin_id'];

$start_date = DATE('d-m-Y');
$end_date = DATE('d-m-Y');

$start_date_val = DATE('Y-m-d', strtotime($start_date));
$end_date_val = DATE('Y-m-d', strtotime($end_date));

$booking_status = "1";
if ($booking_status == "1") {
    $dy_str = "1 AND book_status='$booking_status' AND (DATE(departure_date_time)>='$start_date_val' AND DATE(departure_date_time)<='$end_date_val')";
} else {
    $dy_str = "1 AND book_status='$booking_status' AND (DATE(arrival_date_time)>='$start_date_val' AND DATE(arrival_date_time)<='$end_date_val')";
}

if ($_POST['booking_history_btn']) {
    $start_date = $_REQUEST['start_date'];
    $start_date = preg_replace("/[^a-zA-Z0-9_-]/", "", $start_date);
    
    
    $end_date = $_REQUEST['end_date'];
    $end_date = preg_replace("/[^a-zA-Z0-9_-]/", "", $end_date);
    
    $booking_status = $_REQUEST['book_status'];
    $booking_status = preg_replace("/[^a-zA-Z0-9_-]/", "", $booking_status);
    

    $dy_str = "1";
    if (!empty($book_status)) {
        $dy_str .= " AND book_status='$booking_status'";
    }
    if (!empty($start_date) && !empty($end_date)) {
        $start_date_val = DATE('Y-m-d', strtotime($start_date));
        $end_date_val = DATE('Y-m-d', strtotime($end_date));
        if ($booking_status == "1") {
            $dy_str .= " AND book_status='$booking_status' AND (DATE(departure_date_time)>='$start_date_val' AND DATE(departure_date_time)<='$end_date_val')";
        } else {
            $dy_str .= " AND book_status='$booking_status' AND (DATE(arrival_date_time)>='$start_date_val' AND DATE(arrival_date_time)<='$end_date_val')";
        }
    }
}

//echo "$dy_str ORDER BY departure_date_time ASC";
// Getting the List Of Compnay
$RoomBookListArr = $db->selectWithCols("rcuesr_room_booking_tbl", "*", "$dy_str ORDER BY departure_date_time ASC");
?>
<div class="row">
    <div class="col-md-12 col-sm-12 col-xs-12 ">
        <h3>Booking History</h3>
        <div class="box box-primary">
            <div class="box-body">
                <form class="" method="POST" id="form_booking_history">
                    <div class="table-responsive ">
                        <table class="table table-striped table-hover table-bordered table-condensed" id="tbl_booking_history">
                            <tr>
                                <td>
                                    <label>Start Date</label>
                                    <input type="date" class="form-control" name="start_date" id="start_date" value="<?php echo $start_date; ?>" required=""/>
                                </td>
                                <td>
                                    <label>End Date</label>
                                    <input type="date" class="form-control" name="end_date" id="end_date" value="<?php echo $end_date; ?>" required=""/>
                                </td>
                                <td>
                                    <label>Check In/Out</label>
                                    <select name="book_status" id="book_status" class="form-control">
                                        <option value="2" <?php
                                        if ($booking_status == "2") {
                                            echo "SELECTED";
                                        }
                                        ?>>Check In</option>
                                        <option value="1" <?php
                                        if ($booking_status == "1") {
                                            echo "SELECTED";
                                        }
                                        ?>>Check Out</option>
                                    </select>
                                </td>
                                <td>
                                    <label>&nbsp;</label><br/>
                                    <input type="submit" class="btn btn-danger" name="booking_history_btn" id="booking_history_btn" value="Search"/>
                                </td>
                            </tr>
                        </table>
                    </div>
                </form>
                <div class="">
                    <p>       <h3>            
<?php echo "Booking List From " . DATE('d/M', strtotime($start_date_val)) . " To " . DATE('d/M', strtotime($end_date_val)); ?>
                    </h3>
                    </p>
                </div>

                <div class="table-responsive ">
                    <table class="table table-striped table-hover table-bordered table-condensed" id="tbl_booking_id">
                        <thead>
                            <tr>
                                <th width="3%">
                                    #
                                </th>
                                
                                <th width="10%">
                                    Bill Number
                                </th>
                                <th width="15%">
                                    Room Number
                                </th>
                                <th width="25%">
                                    Booking User Details
                                </th>
                                <th>
                                    ID
                                </th>
                                <th>
                                    Amount
                                </th>
                                <th>
                                    Check In
                                </th>
                                <th>
                                    Check Out
                                </th>
                                <th>
                                    Action
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $c = 1;
                            foreach ($RoomBookListArr as $RBLA) {
                                $BookingDetails = "<b>" . common::StrFromDb($RBLA['applicant_name']) . "</b>";
                                if (!empty($RBLA['applicant_post'])) {
                                    $BookingDetails .= "/" . common::StrFromDb($RBLA['applicant_post']);
                                }
                                if (!empty($RBLA['department'])) {
                                    $BookingDetails .= "/" . common::StrFromDb($RBLA['department']);
                                }
                                if (!empty($RBLA['mobile_number'])) {
                                    $BookingDetails .= "<br/>" . common::StrFromDb($RBLA['mobile_number']);
                                }
                                $paid_amount = $RBLA['paid_amount'];
                                $CheckInDateTime = DATE('d/m/Y H:i', strtotime($RBLA['arrival_date_time']));
                                $CheckOutDateTime = DATE('d/m/Y H:i', strtotime($RBLA['departure_date_time']));
                                $bookingUserId = "";
                                $bookingUserId = $RBLA['id_type'] . ":" . $RBLA['id_number'];
                                if ($RBLA['upload_file']) {
                                    $upload_file_str = "<br/><a href='updoc/" . $RBLA['upload_file'] . "' target='_blank' style='color: blue;'>Download</a>";
                                    $bookingUserId .= $upload_file_str;
                                }
                                ?>
                                <tr id="room_book_id_<?php echo $RBLA['room_book_pk_id']; ?>">
                                    <td><?php echo $c; ?></td>
                                    <td><?php echo common::StrFromDb($RBLA['bill_number']); ?></td>
                                    <td><?php echo common::StrFromDb($RBLA['room_number']); ?></td>
                                    <td><?php echo common::StrFromDb($BookingDetails); ?></td>
                                    <td><?php echo $bookingUserId; ?></td>
                                    <td><i class="fa fa-inr"></i><?php echo common::DecimalTwoPlace($paid_amount); ?></td>
                                    <td><?php echo $CheckInDateTime; ?></td>
                                    <td><?php echo $CheckOutDateTime; ?></td>
                                    <td>
                                        <a href="bill-generate-pdf.php?BookPkId=<?php echo $RBLA['room_book_pk_id']; ?>" style='color: blue;'>Download Bill</a>
                                    </td>
                                </tr>
    <?php
    $c = $c + 1;
}
?>
                        </tbody>
                    </table>
                </div>
            </div>

        </div>
    </div>
</div>
<div class="clearfix"></div><br/><br/>
<?php
$mainpagecontent = ob_get_contents();
ob_clean();
include 'admin-master.php';
?>