<?php
require_once 'conn.php';
checkLogin("信息管理");

$qid = $_GET['qid'] ?? '';

// Check qid and redirect if invalid - do this before any output
if (empty($qid) || !is_numeric($qid)) {
    $conn->close();
    header("Location: /");
    exit;
}

// Now start HTML output
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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>
</head>
<body>
<div id="man_zone" style="margin:0">
<?php
$Keys = $_GET['Keys'] ?? '';
$Keyscode = textEncode($Keys);
$Ord = $_GET['Ord'] ?? '';
$Page = $_GET['Page'] ?? '';

$OrdStr = !empty($Ord) ? $Ord . "," : "";

// Get product IDs for this question
$result = $conn->query("SELECT productId FROM productFAQ WHERE questionId=" . (int)$qid);
$productStr = ",";
while ($row = $result->fetch_assoc()) {
    $productStr .= $row['productId'] . ",";
}

// Get products
$sql = "SELECT id, productName, productPic, productAddtime FROM product 
        WHERE productName LIKE '%" . $conn->real_escape_string($Keyscode) . "%' 
        ORDER BY productAddtime DESC";
$result = $conn->query($sql);

$Keys = urlencode($Keys);
$Ord = urlencode($Ord);
$hrefstr = "?keys=" . $Keys;
?>

<table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
    <thead>
        <tr>
            <th width="4%"></th>
            <th width="6%">序号</th>
            <th width="30%">产品名称</th>
            <th width="30%">图片</th>
            <th width="20%">添加时间</th>
        </tr>
    </thead>
    <tbody>
<?php
if ($result->num_rows > 0) {
    $pageSize = 7;
    $totalRows = $result->num_rows;
    $totalPages = ceil($totalRows / $pageSize);
    
    if ($Page == "") $Page = 1;
    if ($Page == "end") $Page = $totalPages;
    if (!is_numeric($Page) || $Page < 1) $Page = 1;
    $Page = (int)$Page;
    if ($Page > $totalPages) $Page = $totalPages;
    
    $offset = $pageSize * ($Page - 1);
    $sql .= " LIMIT $offset, $pageSize";
    $result = $conn->query($sql);
    
    $TempNum = $offset;
    
    while ($row = $result->fetch_assoc()) {
        $TempNum++;
        $checked = strpos($productStr, "," . $row['id'] . ",") !== false ? ' checked="checked"' : '';
?>
        <tr onMouseOver="this.style.background='#F7FCFF'" onMouseOut="this.style.background='#FFFFFF'">
            <td align="center"><input type="checkbox"<?php echo $checked; ?> name="chkbox" class="relatedProducts" onChange="relatedProducts(<?php echo $qid; ?>,<?php echo $row['id']; ?>)" /></td>
            <td align="center"><?php echo $TempNum; ?></td>
            <td align="center"><?php echo $row['productName']; ?></td>
            <td align="center"><img src="<?php echo $row['productPic']; ?>" height="60px"></td>
            <td align="center"><?php echo $row['productAddtime']; ?></td>
        </tr>
<?php
    }
} else {
    if (empty($Keys)) {
?>
        <tr>
            <td align="center" colspan="8">Sorry,当前暂无信息</td>
        </tr>
<?php
    } else {
?>
        <tr>
            <td align="center" colspan="8"><a href="?">Sorry,没有找到"<?php echo $Keyscode; ?>"相关的信息,点击返回</a></td>
        </tr>
<?php
    }
}
?>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="8">
                <div class="showpagebox">
<?php
if ($totalPages > 1) {
    $PageName = "?qid={$qid}&Keys={$Keys}&Ord={$Ord}&";
    $Pagelen = 3;
    
    if ($Page > 1) {
        echo "<a href=\"{$PageName}Page=1\">首页</a>";
        echo "<a href=\"{$PageName}Page=" . ($Page-1) . "\">上一页</a>";
    }
    
    if ($Pagelen * 2 + 1 >= $totalPages) {
        $StartPage = 1;
        $EndPage = $totalPages;
    } else {
        if ($Page <= $Pagelen + 1) {
            $StartPage = 1;
            $EndPage = $Pagelen * 2 + 1;
        } else {
            $StartPage = $Page - $Pagelen;
            $EndPage = $Page + $Pagelen;
        }
        if ($Page + $Pagelen > $totalPages) {
            $StartPage = $totalPages - $Pagelen * 2;
            $EndPage = $totalPages;
        }
    }
    
    for ($i = $StartPage; $i <= $EndPage; $i++) {
        if ($i == $Page) {
            echo "<a class=\"current\">{$i}</a>";
        } else {
            echo "<a href=\"{$PageName}Page={$i}\">{$i}</a>";
        }
    }
    
    if ($Page < $totalPages) {
        if ($totalPages - $Page > $Pagelen) {
            echo "<a href=\"{$PageName}Page={$totalPages}\">...{$totalPages}</a>";
        }
        echo "<a href=\"{$PageName}Page=" . ($Page+1) . "\">下一页</a>";
        echo "<a href=\"{$PageName}Page={$totalPages}\">尾页</a>";
    }
    
    $pageInputJs = "if(event.keyCode==13){location.href='{$PageName}Page='+document.getElementById('Pagego').value}";
    echo "<input type=\"text\" id=\"Pagego\" value=\"{$Page}\" onFocus=\"if(this.value == '{$Page}'){this.value='';}\" onBlur=\"if(this.value == ''){this.value='{$Page}';}\" onKeyUp=\"this.value=this.value.replace(/\\D/g,'')\" onKeyDown=\"{$pageInputJs}\" />";
}
?>
                </div>
                <div class="searchbox">
                    <input type="text" id="keys" value="<?php echo empty($Keyscode) ? '请输入搜索关键词' : $Keyscode; ?>" 
                           onFocus="if(this.value == '<?php echo empty($Keyscode) ? '请输入搜索关键词' : $Keyscode; ?>'){this.value='';}" 
                           onBlur="if(this.value == ''){this.value='<?php echo empty($Keyscode) ? '请输入搜索关键词' : $Keyscode; ?>';}" 
                           onKeyDown="if(event.keyCode==13){location.href='?qid=<?php echo $qid; ?>&Keys='+encodeURIComponent(document.getElementById('keys').value)}" />
                    <input type="button" id="searchgo" value="go" onClick="location.href='?qid=<?php echo $qid; ?>&Keys='+encodeURIComponent(document.getElementById('keys').value)" />
                </div>
                <div class="postchkbox">
                    <select id="chkact" name="chkact">
                        <option value="1">显示</option>
                        <option value="0">隐藏</option>
                        <option value="-1">删除</option>
                    </select>
                    <input type="button" value="执行" onClick="postchk(1)" class="btn1" />
                    <input type="button" value="新增" onClick="location.href='?act=add'" class="btn1" />
                </div>
            </td>
        </tr>
    </tfoot>
</table>
<?php 
$conn->close();
?>
</div>
</body>
</html>