<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Log;

class MedicalReport extends BaseModel
{
    use HasFactory,SoftDeletes;

    const TYPE_BEFORE = 0;
    const TYPE_AFTER = 1;

    public function anomaly(): Attribute
    {
        return Attribute::make(
            get: fn($value) => $this->anomalyTranfer($value),
        );
    }

    public function anomalyTranfer($value)
    {
//        $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'=>'嗜碱性粒细胞比值'
//        ];
        $arr = [
            'sbp' => trans('app.blood_pressure.sbp'), 'dbp' => trans('app.blood_pressure.dbp'), 'fbg' => trans('app.blood_sugar.fbg'), 'urea' => trans('app.urinalysis.urea'), 'ua' => trans('app.urinalysis.ua'), 'cre' => trans('app.urinalysis.cre'), 'cvsc' => trans('app.urinalysis.cvsc'), 'tc' => trans('app.blood_fat.tc'), 'tg' => trans('app.blood_fat.tg'),
            'hdl' => trans('app.blood_fat.hdl'), 'ldl' => trans('app.blood_fat.ldl'), 'tbil' => trans('app.liver.tbil'), 'dbil' => trans('app.liver.dbil'), 'alt' => trans('app.liver.alt'), 'ast' => trans('app.liver.ast'), 'wbc' => trans('app.blood_routine.wbc'), 'rbc' => trans('app.blood_routine.rbc'), 'hgb' => trans('app.blood_routine.hgb'), 'hct' => trans('app.blood_routine.hct'), 'mcv' => trans('app.blood_routine.mcv'), 'mch' => trans('app.blood_routine.mch'),
            'mchc' => trans('app.blood_routine.mchc'), 'rdw' => trans('app.blood_routine.rdw'), 'rdwcv' => trans('app.blood_routine.rdwcv'), 'plt' => trans('app.blood_routine.plt'), 'mpv' => trans('app.blood_routine.mpv'), 'pdw' => trans('app.blood_routine.pdw'), 'plcr' => trans('app.blood_routine.plcr'), 'pct' => trans('app.blood_routine.plcr'), 'gr' => trans('app.blood_routine.gr'), 'gr_p' => trans('app.blood_routine.gr_p'), 'ly' => trans('app.blood_routine.ly'), 'ly_p' => trans('app.blood_routine.ly_p'),
            'mono' => trans('app.blood_routine.mono'), 'mono_p' => trans('app.blood_routine.mono_p'), 'eos' => trans('app.blood_routine.eos'), 'eos_p' => trans('app.blood_routine.eos_p'), 'baso' => trans('app.blood_routine.baso'), 'baso_p' => trans('app.blood_routine.baso_p')
        ];
        $anomaly_arr = json_decode($value) ?: [];
        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;
    }

}
