<?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_list extends Admin
{

    /**
     * @AopBefore(class="app\aop\AdminLogin", function="before")
     * @RequestMapping(route="/admin/auth/admin_list", method="GET|POST", title="管理员列表")
    */
    public function run() {
        if (\x\Request::is_ajax()) {
            return $this->showlist();
        }
        return $this->display();
    }

    // 菜单列表
    private function showlist() {
        $Db = new \x\Db();
        $param = \x\Request::get();
        $where = $this->_where($param);
        $list = $Db->name('auth_admin')->alias('A')
                ->join('auth_role B', 'A.role = B.id', 'LEFT')
                ->where($where)
                ->order('id DESC')
                ->field('A.*, B.name as role_name')
                ->page($param['page'], $param['limit'])
                ->select();
        $count = $Db->name('auth_admin')->alias('A')->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']) : '';
            $list[$k]['status'] = $v['status']==1?'开启':'关闭';
            if ($v['role'] == 0) {
                $list[$k]['role_name'] = '超级管理员';
            }
        }
        $Db->return();
        return $this->_list('0', '', $count, $list);
    }

    // 搜索条件
    private function _where($param=[]) {
        $where = [];
        if (!empty($param['nick'])) {
            $where[] =  ['A.nick','like', "%".$param['nick']."%"];
        }
        if (!empty($param['name'])) {
            $where[] = ['A.name','like', "%".$param['name']."%"];
        }
        if (!empty($param['phone'])) {
            $where[] = ['A.phone', 'like', "%".$param['phone']."%"];
        }
        if (!empty($param['status'])) {
            $where[] = ['A.status', '=', $param['status']];
        }
        return $where;
    }

}