<?php
require_once('conn.php');

// Check login status (assuming you have a similar function in PHP)
checkLogin("信息管理");

// Initialize variables to avoid undefined warnings
$page = isset($_GET['Page']) ? $_GET['Page'] : 1;
$keys = isset($_GET['Keys']) ? urlencode($_GET['Keys']) : '';
$keyscode = isset($_GET['Keys']) ? htmlspecialcharsFix($_GET['Keys']) : '';

// Prepare the SQL query with proper escaping
$search_term = mysqli_real_escape_string($conn, $keyscode);
$sql = "SELECT * FROM logrecord WHERE loginAct LIKE '%$search_term%' ORDER BY id DESC";
$result = mysqli_query($conn, $sql);

// Get total records for pagination
$total_records = mysqli_num_rows($result);
$records_per_page = 20;
$total_pages = ceil($total_records / $records_per_page);

// Validate page number
if ($page === 'end') {
    $page = $total_pages;
}
if (!is_numeric($page) || $page < 1) {
    $page = 1;
}
if ($page > $total_pages) {
    $page = $total_pages;
}

// Calculate offset for pagination
$offset = ($page - 1) * $records_per_page;
$sql .= " LIMIT $offset, $records_per_page";
$result = mysqli_query($conn, $sql);
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>管理区域</title>
    <link rel="stylesheet" href="css/common.css" type="text/css" />
    <link rel="stylesheet" href="css/jquery.galpop.css" type="text/css" />
    <script language="javascript" src="js/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="js/js.js"></script>
    <script type="text/javascript" src="xheditor-1.1.9/xheditor-1.1.9-zh-cn.min.js"></script>
    <script type="text/javascript" src="js/jquery.galpop.min.js"></script>
</head>
<body>
<div id="man_zone">
    <table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
        <thead>
            <tr>
                <th width="10%">序号</th>
                <th width="20%">时间</th>
                <th width="70%">日志</th>
            </tr>
        </thead>
        <tbody>
        <?php
        if (mysqli_num_rows($result) > 0) {
            $temp_num = $offset;
            while ($row = mysqli_fetch_assoc($result)) {
                $temp_num++;
                ?>
                <tr onMouseOver="this.style.background='#F7FCFF'" onMouseOut="this.style.background='#FFFFFF'">
                    <td align="center"><?php echo $temp_num; ?></td>
                    <td align="center"><?php echo htmlspecialcharsFix($row['loginTime']); ?></td>
                    <td align="center"><?php echo htmlspecialcharsFix($row['loginAct']); ?></td>
                </tr>
                <?php
            }
        } else {
            ?>
            <tr>
                <td colspan="4">暂无相关记录</td>
            </tr>
            <?php
        }
        ?>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="4">
                    <div class="showpagebox">
                    <?php
                    if ($total_pages > 1) {
                        $page_name = "?Keys=" . $keys . "&";
                        $page_len = 3;

                        // Previous page links
                        if ($page > 1) {
                            echo "<a href=\"{$page_name}Page=1\">首页</a>";
                            echo "<a href=\"{$page_name}Page=" . ($page - 1) . "\">上一页</a>";
                        }

                        // Calculate page range
                        if ($page_len * 2 + 1 >= $total_pages) {
                            $start_page = 1;
                            $end_page = $total_pages;
                        } else {
                            if ($page <= $page_len + 1) {
                                $start_page = 1;
                                $end_page = $page_len * 2 + 1;
                            } else {
                                $start_page = $page - $page_len;
                                $end_page = $page + $page_len;
                            }
                            if ($page + $page_len > $total_pages) {
                                $start_page = $total_pages - $page_len * 2;
                                $end_page = $total_pages;
                            }
                        }

                        // Page numbers
                        for ($i = $start_page; $i <= $end_page; $i++) {
                            if ($i == $page) {
                                echo "<a class=\"current\">{$i}</a>";
                            } else {
                                echo "<a href=\"{$page_name}Page={$i}\">{$i}</a>";
                            }
                        }

                        // Next page links
                        if ($page < $total_pages) {
                            if ($total_pages - $page > $page_len) {
                                echo "<a href=\"{$page_name}Page={$total_pages}\">...{$total_pages}</a>";
                            }
                            echo "<a href=\"{$page_name}Page=" . ($page + 1) . "\">下一页</a>";
                            echo "<a href=\"{$page_name}Page={$total_pages}\">尾页</a>";
                        }
                    }
                    ?>
                    </div>
                    <div class="searchbox">
                        <input type="text" id="keys" class="inputTxt" 
                            value="<?php echo empty($keyscode) ? '请输入搜索关键词' : htmlspecialcharsFix($keyscode); ?>" 
                            onFocus="if(this.value == '<?php echo empty($keyscode) ? '请输入搜索关键词' : htmlspecialcharsFix($keyscode); ?>'){this.value='';}" 
                            onBlur="if(this.value == ''){this.value='<?php echo empty($keyscode) ? '请输入搜索关键词' : htmlspecialcharsFix($keyscode); ?>';}" 
                            onKeyDown="if(event.keyCode==13){location.href='?Keys='+encodeURIComponent(document.getElementById('keys').value)+'&Page=<?php echo $page; ?>'}" />
                        <input type="button" id="searchgo" class="searchgo" value="go" 
                            onClick="location.href='?Keys='+encodeURIComponent(document.getElementById('keys').value)+'&Page=<?php echo $page; ?>'" />
                    </div>
                </td>
            </tr>
        </tfoot>
    </table>
</div>
<?php mysqli_close($conn); ?>
</body>
</html>