<?php

namespace plugin\admin\app\controller;
use plugin\admin\app\controller\Crud;
use plugin\admin\app\model\User;
use support\exception\BusinessException;
use support\Request;
use support\Response;

/**
 * 团队管理 
 */
class TeamController extends Crud
{
    
    /**
     * @var User
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    

     function __construct()
     {
         $this->model = new User();
     }
    public function select(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        //log_alert($where);
        $user_id = 0;
        if($where['user_id']['value1']){
            $user_id = $where['user_id']['value1'];
        }
        if($where['type']['value1'] == 'child'){
            $list = \app\model\UserTeam::alias('ut')
                ->join('user u', 'ut.descendant_id = u.id')
                ->where('ut.ancestor_id', $user_id)
                ->field('ut.depth,u.*')
                ->order('ut.depth asc')
                ->paginate(10);
        }
        if($where['type']['value1'] == 'tree'){
            $list = \app\model\UserTeam::alias('ut')
                ->join('user u', 'ut.ancestor_id = u.id')
                ->where('ut.descendant_id', $user_id)
                ->field('ut.depth,u.*')
                ->order('ut.depth asc')
                ->paginate(10);
        }
        $total = $list->total();
        $items = $list->items();
        return $this->formatNormal($items,$total);
    }
}
