<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;
use app\admin\model\User as UserModel;

class Pilot extends Backend
{
    protected $model = null;
    
    protected $withJoinTable = ['group'];
    
    // 排除字段
    protected $preExcludeFields = ['lastlogintime', 'loginfailure', 'password', 'salt', 'money', 'score'];
    
    protected $quickSearchField = ['username', 'nickname', 'id'];
    
    public function initialize()
    {
        parent::initialize();
        $this->model = new UserModel();
    }
    
    /**
     * 查看
     */
    public function index()
    {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->param('select')) {
            $this->select();
        }
        
        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $res = $this->model
            ->withoutField('password,salt')
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where)
            ->where(['group_id' => 2, 'real_name_status' => 1, 'certified' => 1])
            ->order($order)
            ->paginate($limit);
        
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }
    
    /**
     * 加载为select(远程下拉选择框)数据，默认还是走$this->index()方法
     * 必要时请在对应控制器类中重写
     */
    public function select()
    {
        $this->request->filter(['strip_tags', 'trim']);
        
        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $res = $this->model
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where)
            ->order($order)
            ->paginate($limit);
        
        foreach ($res as $re) {
            $re->nickname_text = $re->username . '(ID:' . $re->id . ')';
        }
        
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }
    
}