<?php

namespace app\adminapi\controller;

use app\adminapi\model\AuthGroup;
use app\common\controller\AdminApi;
use app\adminapi\model\AdminLog as AdminLogModel;

/**
 * 管理员日志
 *
 * @icon   fa fa-users
 * @remark 管理员可以查看自己所拥有的权限的管理员日志
 */
class Adminlog extends AdminApi
{
    /**
     * @var \app\admin\model\AdminLog
     */
    protected $model = null;
    protected $childrenGroupIds = [];
    protected $childrenAdminIds = [];

    protected $groupdata = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new AdminLogModel;

        $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
        $this->childrenGroupIds = $this->auth->getChildrenGroupIds(true);

        $groupName = AuthGroup::where('id', 'in', $this->childrenGroupIds)
            ->column('id,name');

        $this->groupdata = $groupName;

    }



    /**
     * 管理员操作日志列表
     *
     * @ApiMethod (GET)
     * @ApiParams (name="limit", type="int", required=true, description="每页条数")
     * @ApiParams (name="page", type="int", required=true, description="页数")
     * @ApiParams (name="username", type="string", required=false, description="管理员名字")
     * @ApiParams (name="url", type="string", required=false, description="url")
     * @ApiParams (name="title", type="string", required=false, description="日志标题")
     * @ApiParams (name="content", type="string", required=false, description="日志内容")
     * @ApiParams (name="ip", type="string", required=false, description="IP")
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {

            $isSuperAdmin = $this->auth->isSuperAdmin();
            $childrenAdminIds = $this->childrenAdminIds;

            $where = [];
            $limit =  $this->request->get("limit/d",10);
            $username =  $this->request->get("username/s","");
            if($username){
                $where['username'] = ['like',"%{$username}%"];
            }

            $url =  $this->request->get("url/s","");
            if($url){
                $where['url'] = ['like',"%{$url}%"];
            }

            $title =  $this->request->get("title/s","");
            if($title){
                $where['title'] = ['like',"%{$title}%"];
            }

            $content =  $this->request->get("content/s","");
            if($content){
                $where['content'] = ['like',"%{$content}%"];
            }

            $ip =  $this->request->get("ip/s","");
            if($ip){
                $where['ip'] = ['like',"%{$ip}%"];
            }



            $list = $this->model
                ->where($where)
                ->where(function ($query) use ($isSuperAdmin, $childrenAdminIds) {
                    if (!$isSuperAdmin) {
                        $query->where('admin_id', 'in', $childrenAdminIds);
                    }
                })
                ->field('content,useragent', true)
                ->order("id desc")
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            $this->success("查询成功！",$result);
        }
    }



    /**
     * 日志详情
     *
     * @ApiMethod (GET)
     * @ApiRoute    (/adminapi/adminlog/detail/ids/{ids})
     * @ApiParams (name="ids", type="string", required=true, description="需要查看的ids")
     */
    public function detail($ids)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if (!$this->auth->isSuperAdmin()) {
            if (!$row['admin_id'] || !in_array($row['admin_id'], $this->childrenAdminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        $this->success("查询成功！",$row);
    }



}