<?php

namespace App\Http\Controllers;

use App\Models\DetailMakanan;
use App\Models\Makanan;
use App\Models\Pasien;
use id;
use Illuminate\Http\Request;

class PerhitunganController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index($id)
    {
        //menampilkan menu yang sesuai dengan kebutuhan kalori pasien
        $pasien = Pasien::find($id);
        $makanans = Makanan::whereBetween('kalori', [$pasien->kebutuhanKalori->kebutuhan_kalori - 30, $pasien->kebutuhanKalori->kebutuhan_kalori])->exists();
        if ($makanans) {
            $makanan = Makanan::whereBetween('kalori', [$pasien->kebutuhanKalori->kebutuhan_kalori - 30, $pasien->kebutuhanKalori->kebutuhan_kalori])->get();
            return view('pages.perhitungan.index', compact('makanan', 'pasien'));
        } else {
            return redirect()->route('pasien.index')->with('error', 'Tambahkan makanan terlebih dahulu.');
        }
    }

    public function cetak($id, $pasien)
    {
        //cetak hasil perankingan
        $makanan = Makanan::find($id);
        $pagi = DetailMakanan::where('makanan_id', $makanan->id)->where('waktu', 'Pagi')->get();
        $snackpagi = DetailMakanan::where('makanan_id', $makanan->id)->where('waktu', 'Snack Pagi')->get();
        $siang = DetailMakanan::where('makanan_id', $makanan->id)->where('waktu', 'Siang')->get();
        $snacksiang = DetailMakanan::where('makanan_id', $makanan->id)->where('waktu', 'Snack Siang')->get();
        $malam = DetailMakanan::where('makanan_id', $makanan->id)->where('waktu', 'Malam')->get();
        $pasien = Pasien::find($pasien);
        //menampilkan halaman cetak
        return view('pages.perhitungan.cetak', compact('makanan', 'pasien', 'pagi', 'snackpagi', 'siang', 'snacksiang', 'malam'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
