<?php

namespace Modules\User\Http\Controllers\Api\Relation;

use App\Api\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Jason\Api\Api;
use Modules\User\Http\Resources\RelationResource;
use Modules\User\Models\Relation;

class IndexController extends Controller
{

    /**
     * Notes: 我的下级
     *
     * @Author: 玄尘
     * @Date  : 2021/9/24 15:05
     * @param  Request  $request
     * @return JsonResponse
     */
    public function index(Request $request): JsonResponse
    {
        $layer = $request->larer ?? 1;

        $user  = Api::user();
        $layer = bcadd($user->relation->layer, $layer);

        $users = Relation::query()
            ->where('bloodline', 'like', "%,".$user->id.",%")
            ->when($layer, function ($q) use ($layer) {
                $q->where('layer', $layer);
            }, function ($q) {
                $q->whereIn('layer', [2, 3]);
            })
            ->paginate();
        $data  = [
            'count' => $user->getRelationCount(),
            'users' => RelationResource::collection($users),
        ];

        return $this->success($data);
    }

}