<?php


namespace app\admin\controller\consultant;

use app\admin\service\consultant\TeamService;
use app\common\Tree;
use app\controller\Curd;
use app\model\Consultant;
use app\model\SysDept;
use app\model\SysUser;
use support\Request;
use support\Response;
use Tinywan\Jwt\JwtToken;

class TeamController extends Curd
{

    public function __construct()
    {
        $this->model = new SysDept();
    }

    public function select(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $format = 'tree';
        $order = $request->get('order', 'desc');
        $field = $field ?? 'dept_addtimes';
        $where['dept_category'] = '获客团队';
        $deptIds = TeamService::getIdsByUser();
        if (false === $deptIds) {
            //无权限
            $where['dept_id'] = ['=', 0];
        } else if (is_array($deptIds)) {
            //指定团队下的权限
            $where['dept_id'] = ['in', $deptIds];
        }
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, 1000);
    }

    protected function afterQuery($items)
    {
        foreach ($items as &$item) {
            $item->nums = Consultant::where('dept_id',$item->dept_id)->orWhere('top_dept_id', $item->dept_id)->count();
        }
        return $items;
    }

    protected function formatTree($items): Response
    {
        $format_items = [];
        foreach ($items as $item) {
            $format_items[] = [
                'name' => $item->dept_name,
                'value' => (string)$item->dept_id,
                'id' => $item->id ?? $item->dept_id,
                'pid' => $item->pid ?? $item->dept_super_id,
                'dept_id' => $item->dept_id,
                'dept_name' => $item->dept_name,
                'dept_super_id' => $item->dept_super_id,
                'dept_status' => $item->dept_status,
                'dept_category' => $item->dept_category,
                'dept_code' => $item->dept_code,
                'dept_telephone' => $item->dept_telephone,
                'category_addtimes' => $item->category_addtimes,
                'nums' => $item->nums
            ];
        }
        $tree = new Tree($format_items);
        return json_success('success', $tree->getTree());
    }

    /**
     * Notes: 上级团队
     * User: yb
     * Date: 2024/8/1
     * Time: 17:16
     */
    public function parentList()
    {
        return TeamService::parentList();
    }

    /**
     * @Desc 部门详情
     * @Author Gorden
     * @Date 2024/2/21 10:35
     *
     * @param $id
     * @return \support\Response
     */
    public function deptInfo($id)
    {
        return TeamService::deptInfo($id);
    }

    /**
     * @Desc 创建部门
     * @Author Gorden
     * @Date 2024/2/21 9:22
     *
     * @param Request $request
     * @return \support\Response
     */
    public function addDept(Request $request)
    {
        $params = $request->post();
        if (empty($params['dept_name'])) {
            return json_fail('团队名称不能为空');
        }
        return TeamService::insertDept($params);
    }

    /**
     * @Desc 修改部门
     * @Author Gorden
     * @Date 2024/2/21 10:15
     *
     * @param $id
     * @param Request $request
     * @return \support\Response
     */
    public function updateDept(Request $request)
    {
        $params = $request->post();
        if (empty($params['id'])) {
            return json_fail('团队id不能为空');
        }
        if (empty($params['dept_name'])) {
            return json_fail('团队名称不能为空');
        }
        return TeamService::updateDept($params);
    }

    public function delDept(Request $request)
    {
        return TeamService::delDept($request);
    }

    /**
     * Notes: 检查管理员的部门信息
     * User: yb
     * Date: 2024/8/16
     * Time: 17:28
     * @return Response
     */
    public function checkDept()
    {
        $deptIds = TeamService::getIdsByUser(1);
        if (false === $deptIds) {
            //无权限
            $authDeptId = -1;
        } else if (is_array($deptIds)) {
            $userId = JwtToken::getCurrentId();
            $userInfo = SysUser::firstWhere(['user_id' => $userId]);
            $authDeptId = $userInfo->join_user_dept_id;
        } else {
            //超管权限
            $authDeptId = -2;
        }

        return json_success('请求成功', ['auth_dept_id' =>$authDeptId]);
    }
}