<?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.');
$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 (a.id LIKE '%{$search}%' OR a.name LIKE '%{$search}%' OR b.name LIKE '%{$search}%' OR c.name LIKE '%{$search}%')" : '';
$order = empty($_GET['order']) ? 'a.id' : substr(insql(str_replace('-', '.', $_GET['order'])), 0, 10);
if ($adminPowerLB <= 1) {
    $query = "SELECT COUNT(*) "
            . "FROM `${cfg_dbprefix}admin` AS a "
            . "LEFT JOIN `${cfg_dbprefix}admin_power` AS b ON a.power_id=b.id "
            . "LEFT JOIN `${cfg_dbprefix}admin_lb` AS c ON a.lb_id=c.id "
            . "WHERE a.id<>0 AND a.lb_id=${adminLBID}$searchSql;";
} else {
    $query = "SELECT COUNT(*) "
            . "FROM `${cfg_dbprefix}admin` AS a "
            . "LEFT JOIN `${cfg_dbprefix}admin_power` AS b ON a.power_id=b.id "
            . "LEFT JOIN `${cfg_dbprefix}admin_lb` AS c ON a.lb_id=c.id "
            . "WHERE a.id<>0$searchSql;";
}
$total = $nxsql->oneRowVal($query);
$pages = ceil($total / $limit);
$page > $pages && $page = $pages;
$begin = $page * $limit - $limit;
if ($adminPowerLB <= 1) {
    $query = "SELECT a.id,a.name,b.name,a.power_lb,c.name,a.time "
            . "FROM `${cfg_dbprefix}admin` AS a "
            . "LEFT JOIN `${cfg_dbprefix}admin_power` AS b ON a.power_id=b.id "
            . "LEFT JOIN `${cfg_dbprefix}admin_lb` AS c ON a.lb_id=c.id "
            . "WHERE a.id<>0 AND a.lb_id=${adminLBID}$searchSql ORDER BY $order $desc LIMIT $begin, $limit;";
} else {
    $query = "SELECT a.id,a.name,b.name,a.power_lb,c.name,a.time "
            . "FROM `${cfg_dbprefix}admin` AS a "
            . "LEFT JOIN `${cfg_dbprefix}admin_power` AS b ON a.power_id=b.id "
            . "LEFT JOIN `${cfg_dbprefix}admin_lb` AS c ON a.lb_id=c.id "
            . "WHERE a.id<>0$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 tc">' . htmlspecialchars($row[1]) . '</td>';
        $tmp .= '<td class="ui-widget-content tc">' . htmlspecialchars($row[2]) . '</td>';
        $tmp .= '<td class="ui-widget-content tc">' . ($row[3] == 0 ? '自己' : ($row[3] == 1 ? '同群' : '全部')) . '</td>';
        $tmp .= '<td class="ui-widget-content tc">' . ($row[3] == 1 ? htmlspecialchars($row[4]) : '') . '</td>';
        $tmp .= '<td class="ui-widget-content tc"><a href="sz_admin_edit.php?id=' . $row[0] . '" title="' . htmlspecialchars($row[1]) . '">修改</a></td>';
        $tmp .= '<td class="ui-widget-content tc">' . $row[5] . '</td>';
        $tmp .= '</tr>';
    }
    $result->close();
}
$subPageNums = $page < 1000 ? 10 : ($page < 10000 ? 7 : 5);
$subPageLink = 'sz_admin_list.php?desc=' . ($desc ? '' : 1) . '&limit=' . $limit . '&order=' . $order . '&search=' . $search . '&page=';
$fenye = new Fenye($total, $limit, $page, $subPageNums, $subPageLink, 6);
