<?php

namespace App\Http\Controllers\Admin;

use App\Exports\FatLogsExport;
use App\Exports\OrderMedicalReportExport;
use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Models\FatLog;
use App\Models\HealthLog;
use App\Models\NewFatLog;
use App\Models\User;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class FatController extends Controller
{
    use ResponseJson;

    public function userFatLogs(Request $request, User $user)
    {
//        $type = $request->get('type');
//        if($type == User::FAT_DEVICE_YUNKANGBAO){
//            $logs = HealthLog::join('health_kinds','health_kinds.e_name','=','health_logs.e_name')->where('user_id', $user->id)
//                ->where('health_logs.e_name', 'Weight')->orderByDesc('tested_at')
//                ->select('user_id', 'health_logs.e_name as fat_name', 'score as fat_data', 'unit', 'tested_at')->paginate();
//        }else{
//            $logs = FatLog::where('user_id', $user->id)->where('fat_name', '体重')->orderByDesc('tested_at')->select('user_id', 'fat_name', 'fat_data', 'unit', 'tested_at')->paginate();
//        }
        $type = $request->get('type');
        $is_export = $request->get('is_export');
        $query = NewFatLog::query()->where('user_id', $user->id)
            ->where('type',$type)
            ->orderByDesc('id')
            ->select('id','user_id', 'data','created_at');
        if($is_export){
            $logs = $query->get();
        }else{
            $logs = $query->paginate();
        }
        foreach ($logs as $log){
            $data = json_decode($log->data);
            foreach ($data as $item){
                if(!isset($item->e_name) || $item->e_name != "Weight"){
                    continue;
                }
                $log->e_name = $item->e_name;
                $log->fat_name = $item->fat_name;
                $log->fat_data = $item->fat_data;
                $log->unit = $item->unit;
            }
        }

        if($is_export){
            return Excel::download(new FatLogsExport($logs), '体检报告.xlsx');
        }
        return $this->success('ok', $logs);
    }

    public function userFatLog(Request $request, $id)
    {
//        $tested_at = $request->input('tested_at');
//        $type = $request->get('type');
//        if($type == User::FAT_DEVICE_YUNKANGBAO){
//            $logs = HealthLog::join('health_kinds','health_kinds.e_name','=','health_logs.e_name')->where('user_id', $user->id)->where('tested_at', $tested_at)
//                ->orderByDesc('tested_at')
//                ->select('user_id', 'health_logs.e_name as fat_name', 'score as fat_data', 'unit', 'tested_at')->get();
//        }else{
//            $logs = FatLog::with('fatKind')->where('user_id', $user->id)->where('tested_at', $tested_at)->orderByDesc('tested_at')->select('user_id', 'fat_name', 'fat_data', 'unit', 'tested_at')->get();
//        }
        $logs = NewFatLog::find($id);
        $logs->data = json_decode($logs->data);
        return $this->success('ok', $logs);
    }
}
