<?php 
    include "header.php";
   include "databaseConnect.php";  
   $cczCount ='';  
   $totalRecord='';
   $ccaCount='';

//    print_r($_SESSION);die;

    $numRecordPerPage = 15;
    include "pagination.php";

    $where = '1=1';
    $searchBySlug = '';
    if(isset($_REQUEST['searchBySlug']) && !empty($_REQUEST['searchBySlug'])){
        $searchBySlug = $_REQUEST['searchBySlug'];
        $where .= " AND slug = '$searchBySlug'";
    }

    if (empty($numrecords) || !is_numeric($numrecords)) {
        $sql1 = "SELECT count(*) as total FROM tblhoteldetails where $where";
        $result1 = mysqli_query($conn, $sql1);
        $row1 = mysqli_fetch_assoc($result1);
        $totalRecord = $row1["total"];
    }
    $queryLimit = $start.",".$numRecordPerPage;

    $query = "SELECT * FROM tblhoteldetails where $where order by addedDateTime desc limit $queryLimit"; 
    $result = mysqli_query($conn, $query);
?>
<style>
    .table-container {
        overflow-x: auto; 
        -webkit-overflow-scrolling: touch;
        max-width: 100%;  
    }
    table {
        width: 100%;
        border-collapse: collapse;
    }
    th, td {
        padding: 8px;
        text-align: left;
        border: 1px solid #ddd;
    }
    th {
        background-color: #f4f4f4;
    }
    .table th:nth-child(2), .table td:nth-child(2) { width: 150px; } /* Hotel Name */
    .table th:nth-child(3), .table td:nth-child(3) { max-width: 300px;max-height: 300px;overflow:auto; } /* Link */
    .table th:nth-child(4), .table td:nth-child(4) { width: 200px; } /* Rating Comment */
    .table th:nth-child(5), .table td:nth-child(5) { width: 100px; } /* Review Number */
    .table th:nth-child(6), .table td:nth-child(6) { width: 100px; } /* Pricing */
    .table th:nth-child(7), .table td:nth-child(7) { width: 150px; } /* Free Cancellation */
    .table th:nth-child(8), .table td:nth-child(8) { width: 150px; } /* No Pre-payment */
    .table th:nth-child(9), .table td:nth-child(9) { width: 200px; } /* Hotel Address */
    .table th:nth-child(10), .table td:nth-child(10) { max-width: 300px; overflow:auto;} /* Image URL */
    .table th:nth-child(11), .table td:nth-child(11) { width: 250px; } /* Amenities */
    .pagination {
        display: flex;
        list-style: none;
        padding: 0;
        margin: 0;
        justify-content: flex-end;
    }

    .pagination li {
        margin: 0 5px;
    }

    .pagination li a {
        color: #007bff;
        padding: 8px 12px;
        text-decoration: none;
        border: 1px solid #ddd;
        border-radius: 4px;
        transition: background-color 0.3s, color 0.3s;
    }

    .pagination li a:hover {
        background-color: #007bff;
        color: white;
        border-color: #007bff;
    }

    .pagination li.active a {
        background-color: #007bff;
        color: white;
        border-color: #007bff;
    }

    .pagination .prev i {
        margin-right: 5px;
    }

</style>

<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <div class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1 class="m-0"><i class="nav-icon fas fa-tachometer-alt"></i> Dashboard</h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item">Home</li>
                    </ol>
                </div>
            </div>
        </div>
    </div>
    <!-- /.content-header -->
     
    <div class="card">
        <div class="card-header">
            <div class="row">
                <div class="col-sm-3">
                    <span>Total <strong><?=$totalRecord;?> </strong>Records Found.</strong></span>
                </div>
                <div class="col-sm-9 d-flex">
                    <form method="get">
                        <div class="col-sm-8 d-flex" style="gap:20px;">
                            <select class="form-control" name="searchBySlug">
                                <option value=""> Search Slug</option>
                                <?php 
                                    $sql = "select distinct slug from tblhoteldetails where slug != ''";
                                    $query = mysqli_query($conn, $sql);
                                    if(mysqli_num_rows($query)>0){
                                ?>
                                        <?php while($slugs = mysqli_fetch_assoc($query)){?>
                                            <option value="<?=$slugs['slug']?>" <?=$searchBySlug== $slugs['slug'] ? 'selected' : ''?>><?=$slugs['slug']?></option>
                                <?php }}?>
                            </select>
                            <button type="submit" class="btn btn-primary">Search</button>
                        </div>
                    </form>
                    <div class="col-sm-4 text-right">
                        <a class="btn btn-outline-primary" href = "add-hotels.php">Add Hotel</a>
                    </div>
                </div>
            </div>
        </div>
        <div class="card-body">
            <!-- Main content -->
            <section class="content">
                <div class="container-fluid">
                    <!-- Show data here -->
                <div class="table-responsive">
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <!-- <th>ID</th> -->
                                    <th>Action</th>
                                    <th>Page Slug</th>
                                    <th>Hotel Name</th>
                                    <th>Link</th>
                                    <th>Rating Comment</th>
                                    <th>Review Number</th>
                                    <th>Pricing (in $)</th>
                                    <th>Free Cancellation</th>
                                    <th>No Pre-payment</th>
                                    <th>Hotel Address</th>
                                    <th>Hotel Image URL</th>
                                    <th>Amenities</th>                                    
                                </tr>
                            </thead>
                            <tbody>
                                <?php while ($row = mysqli_fetch_assoc( $result)) { ?>
                                    <tr>
                                        <td><a href="edit-hotel-details.php?hotelId=<?=$row['hotel_id']?>">Edit</a></td>
                                        <td><?php echo $row['slug']; ?></td>
                                        <td><?php echo $row['hotel_name']; ?></td>
                                        <td><?php echo $row['hotel_link']; ?></td>
                                        <td><?php echo $row['rating_comment']; ?></td>
                                        <td><?php echo $row['review_number']; ?></td>
                                        <td><?php echo $row['pricing']; ?></td>
                                        <td>
                                            <?php echo ($row['free_cancellation'] == 1) ? 'Free Cancellations' : 'No Free Cancellations'; ?>
                                        </td>
                                        <td>
                                            <?php echo ($row['no_pre_payment'] == 1) ? 'No Pre-payment' : 'Pre-payment Required'; ?>
                                        </td>
                                        <td><?php echo $row['hotel_address']; ?></td>
                                        <td><?php echo $row['image_url']; ?></td>
                                        <td><?php echo $row['amenities']; ?></td>
                                        
                                    </tr>
                                <?php } ?>
                            </tbody>
                        </table>
                </div>
                </div><!-- /.container-fluid -->
            </section>
            <!-- /.content -->
        </div>
        <div class="card-footer">
            <div class="row">
                <!---<div class="col-lg-2 col-md-2" style="padding-top: 10px;">
                    <div class="form-group">   
                        <select class="form-control" name="rowcount" onchange="sortOrder(this.value)">
                            <?php
                        $numrows_arr = array("10","25","50","100","200");
                        foreach($numrows_arr as $nrow){ ?>
                            <option value="<?=$nrow?>"
                                <?php if($numRecordPerPage == $nrow){ echo 'selected'; } ?>><?=$nrow?>
                            </option>
                            <?php }
                        ?>
                        </select>
                    </div>
                </div>---->
                <div class="col-md-12 col-lg-12 dispCenter">
                    <div class="dataTables_paginate paging_simple_numbers">
                        <ul class="pagination">
                            <?php
                            $pageName = "admin.php";
                            $parameters = "searchBySlug=".$searchBySlug;// we can use this or
                            //$parameters .="&searchfaqPageType=" . urlencode($searchfaqPageType) ."&searchfaqFuelType=" . urlencode($searchfaqFuelType) ."&orderBy=".$order_by."&sort=".$sort;====can use this 
                            
                            printPagination($pageName,$numRecordPerPage,$totalRecord,$parameters);
                        ?>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <!-- <div class="card-footer"></div> -->
    </div>

    
</div>
<?php include "footer.php"?>
<script>
    function sortOrder(colName) {
    if ($.isNumeric(colName) == false) {
        $("#orderBy").val(colName);
        var sort = $("#sort").val();
        if (sort == 'desc') {
            $("#sort").val("asc");
        } else if (sort == 'asc') {
            $("#sort").val("desc");
        }
    } else {
        $("#rowcount").val(colName);
    }
    $("form#filterForm").submit();
}
</script>
