<?php 
    include "header.php";
   include "databaseConnect.php";  
   $cczCount ='';  
   $totalRecord='';
   $ccaCount='';
    function getCountry($ip){
        $url = "http://ip-api.com/json/$ip";

        $response = file_get_contents($url); 
        $data = json_decode($response, true); 
        if ($data && $data['status'] === 'success') {
            echo $data['country']; 
        } else {
            echo "Unable to fetch location.";
        }
    }

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

    $where = '1=1';
    $searchBySlug = '';
    if(isset($_REQUEST['searchBySlug']) && !empty($_REQUEST['searchBySlug'])){
        $searchBySlug = $_REQUEST['searchBySlug'];
        $dateRange = isset($_REQUEST['daterange'])?$_REQUEST['daterange']:'';
        list($startDate, $endDate) = explode(' - ', $dateRange);
        $type = isset($_REQUEST['type'])?$_REQUEST['type']:'';

        $where .= " AND type='$type' AND date(created_at) BETWEEN '$startDate' AND '$endDate' AND current_url LIKE '%$searchBySlug' ";
    }

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

    $query = "SELECT * FROM tbluseractivitylogs where $where order by id 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">
    <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>
        </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>Country</th> -->
                                    <th>Hotel</th> 
                                    <th>Current Url</th>
                                    <!-- <th>Redirect Url</th> -->
                                    <th>Data</th>
                                    <th>Date</th>
                                                                     
                                </tr>
                            </thead>
                            <tbody>
                                <?php while ($row = mysqli_fetch_assoc( $result)) { ?>
                                    <?php $data = json_decode($row['searchData'])?>
                                    <tr>
                                        <td><?= isset($data->hotel_name) && $data->hotel_name!='' && $data->hotel_name !='null' ?$data->hotel_name:'' ?></td>
                                        <!-- <td><?php getCountry($row['user_ip']) ?></td> -->
                                        <td><?php echo $row['current_url']; ?></td>
                                        <!-- <td><?php echo $row['redirect_url']; ?></td> -->
                                        <td><?php echo $row['searchData']; ?></td>                                      
                                        <td><?php echo date('Y-m-d H:i',strtotime($row['created_at'])); ?></td>
                                    </tr>
                                <?php } ?>
                            </tbody>
                        </table>
                </div>
                </div><!-- /.container-fluid -->
            </section>
            <!-- /.content -->
        </div>
        <div class="card-footer">
            <div class="row">
                <div class="col-md-12 col-lg-12 dispCenter">
                    <div class="dataTables_paginate paging_simple_numbers">
                        <ul class="pagination">
                            <?php
                            $pageName = "click-log.php";
                            $parameters = "searchBySlug=".$searchBySlug."&daterange=".$dateRange."&type=".$type;
                            
                            printPagination($pageName,$numRecordPerPage,$totalRecord,$parameters);
                        ?>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <!-- <div class="card-footer"></div> -->
    </div>

    
</div>
<?php include "footer.php"?>
