<?php

namespace App\Http\Resources;


use App\Models\User;
use Illuminate\Http\Resources\Json\JsonResource;

class UserInfoV1Resource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request): array
    {
        $user = $this->user;
        return [
            'use_id' => $this->user_id,
            'name' => $user->name ?? null,
            'mobile' => $user->mobile ?? null,
            'sex' => $user->sex ?? null,
            'avatar' => $user->avatar ?? null,
            'birthday' => $user->birthday ?? null,
            'stature' => $user->stature ?? null,
            'body_images' => User::bodyImages($user->body_images ?? []),
            'after_body_images' => User::bodyImages($user->after_body_images ?? []),
            'weight' => $this->weight ?? null,
            'address' => $this->address ?? null,
            'vegan' => $this->vegan ?? null,
            'egg' => $this->egg ?? null,
            'eco' => $this->eco ?? null,
            'stomach' => isset($this->stomach) ? json_decode($this->stomach) : [],
            'heart' => isset($this->heart) ? json_decode($this->heart) : [],
            'sleep' => isset($this->sleep) ? json_decode($this->sleep) : [],
            'immunity' => isset($this->immunity) ? json_decode($this->immunity) : [],
            'woman' => isset($this->woman) ? json_decode($this->woman) : [],
            'man' => isset($this->man) ? json_decode($this->man) : [],
            'other' => isset($this->other) ? json_decode($this->other) : [],
            'dietary' => isset($this->dietary) ? json_decode($this->dietary) : [],
            'living' => isset($this->living) ? json_decode($this->living) : [],
            'mental' => isset($this->mental) ? json_decode($this->mental) : [],
            'family' => isset($this->family) ? json_decode($this->family) : [],
            'family_member' => isset($this->family_member) ? json_decode($this->family_member) : [],
            'personal' => isset($this->personal) ? json_decode($this->personal) : [],
            'desc' => $this->desc ?? null,
            'anomaly' => $this->anomalyTranfer() ?? [],
            'nutrient' => isset($this->nutrient) ? json_decode($this->nutrient) : [],
            'scheme' => $this->scheme ?? [],
            'unit' => get_user_unit($this->user->language ?? 0, $this->user->unit ?? '斤') ?? "kg",
            'delivery_name' => $this->delivery_name ?? '',
            'delivery_number' => $this->delivery_number ?? '',
            'delivery_type' => $this->delivery_type ?? '',
            'language' => $user->language ?? 0,
        ];
    }

    public function anomalyTranfer()
    {
        $arr = [
            'sbp' => "收缩压",
            'dbp' => "舒张压",
            'fbg' => '空腹血糖',
            'urea' => '尿素',
            'ua' => '尿酸',
            'cre' => "肌酐",
            'cvsc' => '胱抑素C',
            'tc' => '总胆固醇',
            'tg' => '甘油三脂',
            'hdl' => '高密度脂蛋白',
            'ldl' => '低密度脂蛋白',
            'tbil' => '总胆红素',
            'dbil' => '直接胆红素',
            'alt' => '谷丙转氨酶',
            'ast' => '谷草转氨酶',
            'wbc' => '白细胞计数',
            'rbc' => '红细胞计数',
            'hgb' => '血红蛋白',
            'hct' => '红细胞比积',
            'mcv' => '平均RBC体积',
            'mch' => '平均RBC血红蛋白量',
            'mchc' => '平均RBC血红蛋白浓度',
            'rdw' => 'RBC分布宽度标准差',
            'rdwcv' => 'RBC分布宽度变异系数',
            'plt' => '血小板计数',
            'mpv' => '平均血小板体积',
            'pdw' => '血小板体积分布宽带',
            'plcr' => '大血小板比率',
            'pct' => '血小板比积',
            'gr' => '中性粒细胞计数',
            'gr_p' => '中性粒细胞比值',
            'ly' => '淋巴细胞计数',
            'ly_p' => '淋巴细胞比值',
            'mono' => '单核细胞计数',
            'mono_p' => '单核细胞比值',
            'eos' => '嗜酸粒细胞计数',
            'eos_p' => '嗜酸睡粒细胞比值',
            'baso' => '嗜碱性粒细胞计数',
            'baso_p' => '嗜碱性粒细胞比值'
        ];
        $anomaly_arr = isset($this->anomaly) ? json_decode($this->anomaly) : [];
        if (count($anomaly_arr)) {
            $data_arr = [];
            foreach ($anomaly_arr as $anomaly) {
                $data = [];
                foreach ($anomaly as $an) {
                    $data[] = $arr[$an];
                }
                $data_arr[] = $data;
            }
            $anomaly_arr = $data_arr;
        }
        return $anomaly_arr;
    }
}
