<?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.');
$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);
$leiID = empty($_GET['lei3']) ? (empty($_COOKIE['lei3']) ? 0 : abs(intval($_COOKIE['lei3']))) : abs(intval($_GET['lei3']));
$leiSql = $leiID ? " AND g.lei_id=${leiID}" : '';
$zuID = empty($_GET['zu3']) ? (empty($_COOKIE['zu3']) ? 0 : abs(intval($_COOKIE['zu3']))) : abs(intval($_GET['zu3']));
$zuSql = $zuID ? " AND g.zu_id=${zuID}" : '';
$search = empty($_GET['search']) ? '' : substr(insql($_GET['search']), 0, 255);
$searchSql = $search ? " AND (g.title LIKE '%{$search}%' OR g.ggcl LIKE '%{$search}%' OR g.bjgg LIKE '%{$search}%' OR g.id LIKE '%{$search}%' OR g.gid LIKE '%{$search}%')" : '';
$order = empty($_GET['order']) ? 'id' : substr(insql($_GET['order']), 0, 8);
if ($adminPowerLB == 1) {
    $query = "SELECT COUNT(g.id) FROM ${cfg_dbprefix}ggw AS g "
            . "INNER JOIN ${cfg_dbprefix}lei AS l ON g.lei_id=l.id "
            . "INNER JOIN ${cfg_dbprefix}zu AS z ON g.zu_id=z.id "
            . "WHERE g.recycle=${recycle} AND l.type=3 AND z.type=3 AND l.lb_id=${adminLBID} AND z.lb_id=${adminLBID}$leiSql$zuSql$searchSql;";
} elseif ($adminPowerLB == 0) {
    $query = "SELECT COUNT(g.id) FROM ${cfg_dbprefix}ggw AS g "
            . "WHERE g.recycle=${recycle} AND g.admin_id='${adminID}'$leiSql$zuSql$searchSql;";
} else {
    $query = "SELECT COUNT(g.id) FROM ${cfg_dbprefix}ggw AS g "
            . "WHERE g.recycle=${recycle}$leiSql$zuSql$searchSql;";
}
$total = $nxsql->oneRowVal($query);
$pages = ceil($total / $limit);
$page > $pages && $page = $pages;
$begin = $page * $limit - $limit;
if ($adminPowerLB == 1) {
    $query = "SELECT g.id,g.title,g.gid,g.ggcl,g.bjgg,g.type,g.width,g.height,g.admin_id,g.time,l.name,z.name "
            . "FROM ${cfg_dbprefix}ggw AS g "
            . "INNER JOIN ${cfg_dbprefix}lei AS l ON g.lei_id=l.id "
            . "INNER JOIN ${cfg_dbprefix}zu AS z ON g.zu_id=z.id "
            . "WHERE g.recycle=${recycle} AND l.type=3 AND z.type=3 AND l.lb_id=${adminLBID} AND z.lb_id=${adminLBID}$leiSql$zuSql$searchSql ORDER BY g.$order $desc LIMIT $begin, $limit;";
} elseif ($adminPowerLB == 0) {
    $query = "SELECT g.id,g.title,g.gid,g.ggcl,g.bjgg,g.type,g.width,g.height,g.admin_id,g.time,l.name,z.name "
            . "FROM ${cfg_dbprefix}ggw AS g "
            . "LEFT JOIN ${cfg_dbprefix}lei AS l ON g.lei_id=l.id "
            . "LEFT JOIN ${cfg_dbprefix}zu AS z ON g.zu_id=z.id "
            . "WHERE g.recycle=${recycle} AND g.admin_id='${adminID}'$leiSql$zuSql$searchSql ORDER BY g.$order $desc LIMIT $begin, $limit;";
} else {
    $query = "SELECT g.id,g.title,g.gid,g.ggcl,g.bjgg,g.type,g.width,g.height,g.admin_id,g.time,l.name,z.name "
            . "FROM ${cfg_dbprefix}ggw AS g "
            . "LEFT JOIN ${cfg_dbprefix}lei AS l ON g.lei_id=l.id "
            . "LEFT JOIN ${cfg_dbprefix}zu AS z ON g.zu_id=z.id "
            . "WHERE g.recycle=${recycle}$leiSql$zuSql$searchSql ORDER BY g.$order $desc LIMIT $begin, $limit;";
}
