<?php

namespace Modules\User\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Jason\Api\Api;
use Modules\Mall\Models\Order;
use Modules\Mall\Models\Refund as RefundModel;
use Modules\User\Http\Resources\Account\UserAccountResource;
use Modules\User\Models\Identity;
use Vinkla\Hashids\Facades\Hashids;

class UserInfoResource extends JsonResource
{

    public function toArray($request): array
    {
        $identity = $this->identityFirst();
        if ($identity->id > 2) {
            $order = true;
        } else {
            $order = false;
        }

        return [
            'user_id'      => $this->id,
            'username'     => $this->username,
            'nickname'     => $this->info->nickname ?? '',
            'avatar'       => $this->info->avatar ?? '',
            'account'      => new UserAccountResource($this->account),
            'identity'     => new UserIdentityBaseResource($identity),
            //            'service'      => new  UserServiceResource($identity->identities()->first()),
            //            'water_mobile' => app('Conf_mall')['water_mobile'] ?? '',
            'invite'       => Hashids::connection('code')->encode($this->id),
            'area_code'    => $this->info->area_code,
            //            'case'         => $this->getStockData(),
            'created_at'   => (string) $this->created_at,
            'status'       => $this->getStateData(),
            //            'nowStatus'    => $this->getNowStatus(),
            //            'canPick'      => $this->canPick(),
            'count'        => [
                'relation' => $this->getRelationCount(),//下级
                'orders'   => $this->getOrderCount(),
            ],
            'can'          => [
                'area' => $this->areas()->exists(),
            ],
            'identityShow' => $this->when(true, function () use ($identity) {
                return [
                    'right'  => new UserIdentityRightsResource($identity),
                    'times'  => new IdentityMiddleResource($this->identityMiddle()->first()),
                    'is_top' => $identity->id == 4,
                ];
            }),
        ];
    }

}
