<?php
/**
 * Created by PhpStorm
 * DateTime: 2022/11/5 0:31.
 *
 * @description
 */

namespace App\Http\Controllers\V1\Share;

use App\Http\Controllers\V1\Controller;
use App\Models\User;
use Carbon\Carbon;
use Dingo\Api\Http\Request;
use Illuminate\Http\JsonResponse;

class TeamController extends Controller
{
    public function lists(Request $request): JsonResponse
    {
        $limit  = $request->input('limit', 10);
        $page   = $request->input('page', 1);
        $offset = ($page - 1) * 10;
        $user   = \user();

        $lists = User::withCount('child')
            ->withCount('childOrder')
            ->withSum('childOrder', 'income')
            ->where('parent_id', $user->id)
            ->limit($limit)
            ->offset($offset)
            ->get();

        /* @var User $list */
        foreach ($lists as $list) {
            $list->become_child_at        = Carbon::parse($list->become_child_at)->toDateString();
            $list->child_order_sum_income = $list->child_order_sum_income ?: 0;
            $list->child_order_count      = $list->child_order_count ?: 0;
        }

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