<?php
// +----------------------------------------------------------------------
// | 管理员操作日志查询
// +----------------------------------------------------------------------
// | Copyright (c) 2018 https://blog.junphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小黄牛 <1731223728@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\admin\auth;
use app\controller\SuperClass\Admin;

class Admin_action_log extends Admin
{

    /**
     * @AopBefore(class="app\aop\AdminLogin", function="before")
     * @RequestMapping(route="/admin/auth/admin_action_log", method="GET|POST", title="管理员操作日志查询")
     * @Ioc(class="\x\Db", name="Db")
    */
    public function run() {
        if (\x\Request::is_ajax()) {
            return $this->showlist();
        }
        $param = \x\Request::get();
        $this->assign('id', !empty($param['id']) ? $param['id'] : 0);

        $admin_name = '所有人';
        if (!empty($param['id'])) {
            $admin_name = $this->Db->name('auth_admin')->where('id', $param['id'])->value('nick');
        }
        $this->assign('admin_name', $admin_name);

        $this->Db->return();
        return $this->display();
    }

    // 菜单列表
    private function showlist() {
        $param = \x\Request::get();
        $where = $this->_where($param);

        $list = $this->Db->name('auth_admin_action')->alias('A')
                ->join('auth_admin B', 'A.admin = B.id')
                ->field('A.*, B.nick')
                ->where($where)
                ->order('A.id DESC')
                ->page($param['page'], $param['limit'])
                ->select();
        $count = $this->Db->name('auth_admin_action')->alias('A')
                ->join('auth_admin B', 'A.admin = B.id')
                ->field('A.*, B.nick')
                ->where($where)
                ->count();

        foreach ($list as $k=>$v) {
            $list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            $list[$k]['update_time'] = $v['update_time'] ? date('Y-m-d H:i:s', $v['update_time']) : '';
        }
        $this->Db->return();
        return $this->_list('0', '', $count, $list);
    }

    // 搜索条件
    private function _where($param=[]) {
        $where = [];

        // 指定管理员日志查询
        if (!empty($param['id'])) {
            $where[] = ['A.admin', '=', $param['id']];
        }
        if (!empty($param['msg'])) {
            $where[] =  ['A.msg','like', "%".$param['msg']."%"];
        }
        if (!empty($param['url'])) {
            $where[] = ['A.url','like', "%".$param['url']."%"];
        }
        return $where;
    }
}