<?php

/*
 * ----------------------------------------------------
 * 程序名称: 牛叉广告管理系统(NiuXams)
 * 程序功能: 实现专业的广告投放、管理和统计等管理系统！
 * 程序开发: 牛叉软件(NiuXSoft.com)
 * 版权所有: [NiuXams] (C)2013-2099 NiuXSoft.com
 * 官方网站: www.niuxsoft.com  Email: niuxsoft@163.com
 * ----------------------------------------------------
 * 使用条款:
 * 1.为构建创新型经济社会,请尊重保护知识产权！
 * 2.该软件个人非商业用途免费使用.
 * 3.免费使用禁止修改版权信息和官方推广链接.
 * 4.禁止任何衍生版本.禁止任何非法修改源代码贩卖.
 * ----------------------------------------------------
 */

defined('NIUX_INCLUDED') or exit('Access Denied.');
$leizu = empty($_GET['leizu']) ? '' : $_GET['leizu'];
$leizu == 'lei' || $leizu == 'zu' || errWin('非法输入！');
$type = empty($_GET['type']) ? '' : $_GET['type'];
is_int($type) || $type < 5 || $type > 0 || errWin('非法输入！');
$title = '';
$title .= $type == 1 ? '广告' : ($type == 2 ? '广告策略' : ($type == 3 ? '广告位' : '二段式代码'));
$title .= $leizu == 'lei' ? '分类' : '分组';
$title .= '列表';
$desc = empty($_GET['desc']) ? 'DESC' : '';
$limit = empty($_GET['limit']) ? 20 : (intval($_GET['limit']) ? abs(intval($_GET['limit'])) : 20);
$page = empty($_GET['page']) ? 1 : (intval($_GET['page']) ? abs(intval($_GET['page'])) : 1);
$search = empty($_GET['search']) ? '' : substr(insql($_GET['search']), 0, 255);
$searchSql = $search ? " AND (name LIKE '%{$search}%' OR id LIKE '%{$search}%' OR lb_id LIKE '%{$search}%')" : '';
$order = empty($_GET['order']) ? 'id' : substr(insql($_GET['order']), 0, 8);
if ($adminPowerLB == 1) {
    $query = "SELECT COUNT(id) FROM `${cfg_dbprefix}${leizu}` WHERE type=${type} AND lb_id=${adminLBID}$searchSql;";
} elseif ($adminPowerLB == 0) {
    $query = "SELECT COUNT(id) FROM `${cfg_dbprefix}${leizu}` WHERE type=${type} AND admin_id=${adminID}$searchSql;";
} else {
    $query = "SELECT COUNT(id) FROM `${cfg_dbprefix}${leizu}` WHERE type=${type}$searchSql;";
}
$total = $nxsql->oneRowVal($query);
$pages = ceil($total / $limit);
$page > $pages && $page = $pages;
$begin = $page * $limit - $limit;
if ($adminPowerLB == 1) {
    $query = "SELECT id,name,lb_id FROM `${cfg_dbprefix}${leizu}` WHERE type=${type} AND lb_id=${adminLBID}$searchSql ORDER BY $order $desc LIMIT $begin, $limit;";
} elseif ($adminPowerLB == 0) {
    $query = "SELECT id,name,lb_id FROM `${cfg_dbprefix}${leizu}` WHERE type=${type} AND admin_id=${adminID}$searchSql ORDER BY $order $desc LIMIT $begin, $limit;";
} else {
    $query = "SELECT id,name,lb_id FROM `${cfg_dbprefix}${leizu}` WHERE type=${type}$searchSql ORDER BY $order $desc LIMIT $begin, $limit;";
}
$tmp = '';
if ($result = $nxsql->query($query)) {
    while ($row = $result->fetch_row()) {
        $tmp .= '<tr>';
        $tmp .= '<td class="ui-widget-content tc"><input type="checkbox" class="check_id" value="' . $row[0] . '"></td>';
        $tmp .= '<td class="ui-widget-content tc">' . $row[0] . '</td>';
        $tmp .= '<td class="ui-widget-content plr">' . htmlspecialchars($row[1]) . '</td>';
        $tmp .= '<td class="ui-widget-content tc"><a href="leizu_edit.php?type=' . $type . '&leizu=' . $leizu . '&id=' . $row[0] . '" title="' . htmlspecialchars($row[1]) . '">修改</a></td>';
        $tmp .= '<td class="ui-widget-content tc">' . $row[2] . '</td>';
        $tmp .= '</tr>';
    }
    $result->close();
}
$subPageNums = $page < 1000 ? 10 : ($page < 10000 ? 7 : 5);
$subPageLink = 'leizu_list.php?type=' . $type . '&leizu=' . $leizu . '&desc=' . ($desc ? '' : 1) . '&limit=' . $limit . '&order=' . $order . '&search=' . $search . '&page=';
$fenye = new Fenye($total, $limit, $page, $subPageNums, $subPageLink, 6);
