/**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     //
     $advice = new healthadvice();
     $advice->doctor_id = Auth::user()->id;
     $advice->player_id = $request->input('player_id');
     $advice->content = $request->input('content');
     $advice->save();
     return Response::json(array('success' => true, 'doctor_id' => $advice->doctor_id, 'doctor_name' => Auth::user()->true_name, 'time' => $advice->created_at->format('Y-m-d H:m:s'), 'content' => $advice->content));
 }
 /**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index()
 {
     //
     $last_height = healthentry::where('user_id', Auth::user()->id)->where('type', healthentry::TYPE_HEIGHT)->orderBy('begin_time', 'desc')->first();
     $last_weight = healthentry::where('user_id', Auth::user()->id)->where('type', healthentry::TYPE_WEIGTH)->orderBy('begin_time', 'desc')->first();
     $last_bp = healthentry::where('user_id', Auth::user()->id)->where('type', healthentry::TYPE_BLOODPRESSURE)->orderBy('begin_time', 'desc')->first();
     $last_hr = healthentry::where('user_id', Auth::user()->id)->where('type', healthentry::TYPE_HEARTRATE)->orderBy('begin_time', 'desc')->first();
     $weights = healthentry::where('user_id', Auth::user()->id)->where('type', healthentry::TYPE_WEIGTH)->orderBy('begin_time')->get();
     // Good&avg:h,w,bpl,bph,hr
     $goods = [185, 70, 90, 140, 70];
     $avgs = [170, 64, 95, 150, 80];
     $health_advices = healthadvice::where('player_id', Auth::user()->id)->get();
     return view('player.health.user_health', ['health_advices' => $health_advices, 'last_height' => $last_height, 'last_weight' => $last_weight, 'last_bp' => $last_bp, 'last_hr' => $last_hr, 'weights' => $weights, 'goods' => $goods, 'avgs' => $avgs]);
 }
 /**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index()
 {
     // health
     //$health_entries     = healthentry::where('user_id', Auth::user()->id)->orderBy('id')->get();
     // sleep
     // if(Cache::has('bs'.Auth::user()->id)){
     // $bad_sleep = Cache::get('bs'.Auth::user()->id);
     // $mid_sleep = Cache::get('ms'.Auth::user()->id);
     // $good_sleep = Cache::get('gs'.Auth::user()->id);
     // }else{
     $sleep_entries = healthentry::where('user_id', Auth::user()->id)->where('type', healthentry::TYPE_SLEEP)->where('begin_time', '>=', date('Y-m-d') . ' 00:00:00')->orderBy('id')->get();
     $bad_sleep = 0;
     $mid_sleep = 0;
     $good_sleep = 0;
     foreach ($sleep_entries as $sleep_entry) {
         if ($sleep_entry->level == healthentry::LEVEL_BAD) {
             $bad_sleep += $sleep_entry->value;
         }
         if ($sleep_entry->level == healthentry::LEVEL_MID) {
             $mid_sleep += $sleep_entry->value;
         }
         if ($sleep_entry->level == healthentry::LEVEL_GOOD) {
             $good_sleep += $sleep_entry->value;
         }
     }
     // Cache::put('bs'.Auth::user()->id, $bad_sleep, '10');
     // Cache::put('ms'.Auth::user()->id, $mid_sleep, '10');
     // Cache::put('gs'.Auth::user()->id, $good_sleep, '10');
     // }
     // temperature
     $t_entries = healthentry::where('user_id', Auth::user()->id)->where('type', healthentry::TYPE_TEMPERATURE)->where('begin_time', '>=', date('Y-m-d') . ' 00:00:00')->get();
     $avg_t = 0;
     foreach ($t_entries as $t_entry) {
         $avg_t += $t_entry->value;
     }
     if ($t_entries->count() == 0) {
         $avg_t = 0;
     } else {
         $avg_t = round($avg_t / $t_entries->count(), 1);
     }
     // heartrate
     $hr_entries = healthentry::where('user_id', Auth::user()->id)->where('type', healthentry::TYPE_HEARTRATE)->where('begin_time', '>=', date('Y-m-d') . ' 00:00:00')->get();
     $avg_hr = 0;
     foreach ($hr_entries as $hr_entry) {
         $avg_hr += $hr_entry->value;
     }
     if ($hr_entries->count() == 0) {
         $avg_hr = 0;
     } else {
         $avg_hr = round($avg_hr / $hr_entries->count(), 1);
     }
     // blood_pressure
     $bp_entries = healthentry::where('user_id', Auth::user()->id)->where('type', healthentry::TYPE_BLOODPRESSURE)->where('begin_time', '>=', date('Y-m-d') . ' 00:00:00')->get();
     $avg_bp_high = 0;
     $avg_bp_low = 0;
     foreach ($bp_entries as $bp_entry) {
         $avg_bp_high += $bp_entry->value;
         $avg_bp_low += $bp_entry->value2;
     }
     if ($bp_entries->count() == 0) {
         $avg_bp_high = 0;
         $avg_bp_low = 0;
     } else {
         $avg_bp_high = round($avg_bp_high / $bp_entries->count(), 1);
         $avg_bp_low = round($avg_bp_low / $bp_entries->count(), 1);
     }
     // sports
     $sports_entries = sportsentry::where('user_id', Auth::user()->id)->where('start_time', '>=', date('Y-m-d') . ' 00:00:00')->orderBy('id')->get();
     $running_distance = 0;
     $calories = 0;
     $running_time = 0;
     foreach ($sports_entries as $sports_entry) {
         if ($sports_entry->type == sportsentry::TYPE_RUN) {
             $running_distance += $sports_entry->value;
             $running_time += $sports_entry->last_time;
         }
         $calories += $sports_entry->calories;
     }
     if ($running_time == 0) {
         $running_speed = 0;
     } else {
         $running_speed = round($running_distance / $running_time * 3.6, 2);
     }
     // advice
     if (Cache::has('adviceOf' . Auth::user()->id)) {
         $health_advices = Cache::get('adviceOf' . Auth::user()->id);
     } else {
         $health_advices = healthadvice::where('player_id', Auth::user()->id)->get();
         Cache::put('adviceOf' . Auth::user()->id, $health_advices, '10');
     }
     return view('player.index', ['sports_entries' => $sports_entries, 'running_distance' => $running_distance, 'running_speed' => $running_speed, 'calories' => $calories, 'bad_sleep' => $bad_sleep, 'mid_sleep' => $mid_sleep, 'good_sleep' => $good_sleep, 'avg_t' => $avg_t, 'avg_hr' => $avg_hr, 'avg_bp_high' => $avg_bp_high, 'avg_bp_low' => $avg_bp_low, 'health_advices' => $health_advices]);
 }
 public function getHealth($id)
 {
     $advices = healthadvice::where('doctor_id', Auth::user()->id)->where('player_id', $id)->get();
     $player = player::find($id);
     return view('doctor.player.user_health', ['advices' => $advices, 'player' => $player]);
 }