<?php
include "conn.php";
checkLogin("信息管理");
?>
<!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>
<script type="text/javascript" src="js/jquery.galpop.min.js"></script>
</head>
<body>
<div id="man_zone">
<?php
$act = $_GET['act'] ?? '';

if ($act == "save") {
    $isedit = false;
    $id = $_POST['id'] ?? '';
    if ($id != "" && is_numeric($id)) {
        $isedit = true;
    }
    $ch_name = textEncode($_POST['ch_name'] ?? '');

    if ($isedit) {
        $sql = "Select * From qudao Where id=" . $id;
        $rs = $conn->query($sql);
        if ($rs->rowCount() == 0) {
            $sql = "Insert Into qudao(ch_name) values('" . $ch_name . "')";
        } else {
            $sql = "Update qudao Set ch_name='" . $ch_name . "' Where id=" . $id;
        }
        $conn->query($sql);

        $page = $_GET['Page'] ?? '';
        $keys = urlencode($_GET['Keys'] ?? '');
        $ord = urlencode($_GET['Ord'] ?? '');
        header("Location: ?keys=" . $keys . "&Ord=" . $ord . "&Page=" . $page);
        exit;
    } else {
        $sql = "Insert Into qudao(ch_name) values('" . $ch_name . "')";
        $conn->query($sql);
        header("Location: ?");
        exit;
    }
}

if ($act == "add" || $act == "edit") {
    $id = $_GET['id'] ?? '';
    $isedit = false;
    $ch_name = '';
    
    if ($id != "" && is_numeric($id)) {
        $isedit = true;
        $sql = "Select * From qudao Where id=" . $id;
        $rs = $conn->query($sql);
        if ($row = $rs->fetch()) {
            $ch_name = textUncode($row['ch_name']);
        } else {
            $isedit = false;
        }
    }

    $page = $_GET['Page'] ?? '';
    $keys = urlencode($_GET['Keys'] ?? '');
    $ord = urlencode($_GET['Ord'] ?? '');
    $hrefstr = "?keys=" . $keys . "&Ord=" . $ord . "&Page=" . $page;
?>
    <form name="form1" method="post" action="<?php echo $hrefstr; ?>&act=save">
    <table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
        <tbody>
            <tr>
                <th width="8%">来源</th>
                <td><input type="text" id="ch_name" name="ch_name" value="<?php echo $ch_name; ?>" class="txt1" />
                    <input type="hidden" name="id" value="<?php echo $id; ?>" /></td>
            </tr>
            <tr>
                <th></th>
                <td><input type="submit" name="save" id="save" value="确定" class="btn1" /> 
                    <input type="reset" name="save" id="save" value="重置" class="btn1" /> 
                    <input type="button" value="返回" class="btn1" onClick="location.href='<?php echo $hrefstr; ?>'" /></td>
            </tr>
        </tbody>
    </table>
    </form>
<?php
    exit;
}

if ($act == "postchk") {
    $keys = urlencode($_GET['Keys'] ?? '');
    $ord = urlencode($_GET['Ord'] ?? '');
    $page = $_GET['Page'] ?? '';
    $chkact = $_POST['chkact'] ?? '';
    $sqlStr = "";
    
    if (isset($_POST['chkbox'])) {
        foreach ($_POST['chkbox'] as $value) {
            if ($sqlStr != "") {
                $sqlStr .= " Or id=" . $value;
            } else {
                $sqlStr = " Where id=" . $value;
            }
        }

        $sqlStr = "Delete From qudao" . $sqlStr;
        $conn->query($sqlStr);
    }
    
    header("Location: ?Keys=" . $keys . "&Ord=" . $ord . "&Page=" . $page);
    exit;
}

$keys = $_GET['Keys'] ?? '';
$keyscode = textEncode($keys);
$ord = $_GET['Ord'] ?? '';
$page = $_GET['Page'] ?? '';

$ordStr = "";
if ($ord != "") {
    $ordStr = $ord . ",";
}

$sql = "Select * From qudao Where ch_name like '%" . $keyscode . "%' Order By id Desc";
$rs = $conn->query($sql);

$keys = urlencode($keys);
$ord = urlencode($ord);
$hrefstr = "?keys=" . $keys;
?>
<form id="form1" method="post" action="?act=postchk&Keys=<?php echo $keys; ?>&Ord=<?php echo $ord; ?>&Page=<?php echo $page; ?>" onSubmit="return false">
<table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
    <thead>
        <tr>
            <th width="4%"><input type="checkbox" name="chkall" id="chkall" onClick="chkboxall(this,'chkbox')" /></th>
            <th width="6%">序号</th>
            <th width="60%">渠道来源</th>
            <th width="30%">操作</th>
        </tr>
    </thead>
    <tbody>
    <?php
    if ($rs->rowCount() > 0) {
        $records_per_page = 10;
        
        if ($page == "") $page = 1;
        if ($page == "end") $page = ceil($rs->rowCount() / $records_per_page);
        if (!is_numeric($page) || $page < 1) $page = 1;
        $page = (int)$page;
        if ($page > ceil($rs->rowCount() / $records_per_page)) {
            $page = ceil($rs->rowCount() / $records_per_page);
        }
        
        $start = ($page - 1) * $records_per_page;
        $tempNum = $start;
        
        $count = 0;
        while ($row = $rs->fetch()) {
            $count++;
            if ($count > $start && $count <= $start + $records_per_page) {
                $tempNum++;
    ?>
                <tr onMouseOver="this.style.background='#F7FCFF'" onMouseOut="this.style.background='#FFFFFF'">
                    <td align="center"><input type="checkbox" name="chkbox[]" value="<?php echo $row['id']; ?>" /></td>
                    <td align="center"><?php echo $tempNum; ?></td>
                    <td align="center"><?php echo $row['ch_name']; ?></td>
                    <td align="center">
                        <a href="?Keys=<?php echo $keys; ?>&Ord=<?php echo $ord; ?>&Page=<?php echo $page; ?>&act=edit&id=<?php echo $row['id']; ?>" class="ico_edit ico">修改</a>
                    </td>
                </tr>
    <?php
            }
        }
    } else {
        if ($keys == "") {
    ?>
            <tr>
                <td align="center" colspan="4">Sorry,当前暂无信息</td>
            </tr>
    <?php
        } else {
    ?>
            <tr>
                <td align="center" colspan="4"><a href="?">Sorry,没有找到"<?php echo $keyscode; ?>"相关的信息,点击返回</a></td>
            </tr>
    <?php
        }
    }
    ?>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="4">
                <div class="showpagebox">
                <?php
                if ($rs->rowCount() > 0) {
                    $total_pages = ceil($rs->rowCount() / $records_per_page);
                    if ($total_pages > 1) {
                        $pageName = "?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 >= $total_pages) {
                            $startPage = 1;
                            $endPage = $total_pages;
                        } else {
                            if ($page <= $pagelen + 1) {
                                $startPage = 1;
                                $endPage = $pagelen * 2 + 1;
                            } else {
                                $startPage = $page - $pagelen;
                                $endPage = $page + $pagelen;
                            }
                            if ($page + $pagelen > $total_pages) {
                                $startPage = $total_pages - $pagelen * 2;
                                $endPage = $total_pages;
                            }
                        }

                        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 < $total_pages) {
                            if ($total_pages - $page > $pagelen) {
                                echo "<a href=\"{$pageName}Page=$total_pages\">...$total_pages</a>";
                            }
                            echo "<a href=\"{$pageName}Page=" . ($page+1) . "\">下一页</a>";
                            echo "<a href=\"{$pageName}Page=$total_pages\">尾页</a>";
                        }

                        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=\"if(event.keyCode==13){location.href='{$pageName}Page='+document.getElementById('Pagego').value}\" />";
                    }
                }
                ?>
                </div>
                <div class="searchbox">
                    <input type="text" id="keys" value="<?php echo ($keyscode == "") ? "请输入搜索关键词" : $keyscode; ?>" 
                           onFocus="if(this.value == '<?php echo ($keyscode == "") ? "请输入搜索关键词" : $keyscode; ?>'){this.value='';}" 
                           onBlur="if(this.value == ''){this.value='<?php echo ($keyscode == "") ? "请输入搜索关键词" : $keyscode; ?>';}" 
                           onKeyDown="if(event.keyCode==13){location.href='?Keys='+escape(document.getElementById('keys').value)}" />
                    <input type="button" id="searchgo" value="go" onClick="location.href='?Keys='+escape(document.getElementById('keys').value)" />
                </div>
                <div class="postchkbox">
                    <select id="chkact" name="chkact">
                        <option value="1">请选择</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>
</form>
</div>
<script>
$('.click-open-iframe').galpop({
    contentType: 'iframe',
});
</script>
</body>
</html>