<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Carbon\Carbon;

use App\Models\DataPasien;
use App\Models\Antrian;

class DataPasienController extends Controller
{
  public function index()
  {
    $dataPasien = DataPasien::all()->sortBy('dp_no_rm');

    $riwayatMedisPasien = Antrian::all();

    return view('data_pasien.data_pasien', compact('dataPasien', 'riwayatMedisPasien'));
  }

  public function show($dp_id)
  {
    $dataPasien = DataPasien::find($dp_id);

    $riwayatMedisPasien = Antrian::where('dp_id', $dp_id)->get();

    return view('data_pasien.data_pasien_detail', compact('dataPasien', 'riwayatMedisPasien'));
  }

  public function edit($dp_id)
  {
    $dataPasien = DataPasien::find($dp_id);

    return view('data_pasien.data_pasien_edit', compact('dataPasien'));
  }

  public function update(Request $request, DataPasien $dataPasien)
  {
    $data = $request->only(
      'dp_no_rm',
      'dp_no_ktp',
      'dp_no_bpjs',
      'dp_nama',
      'dp_jenis_kelamin',
      'dp_no_hp',
      'dp_alamat',
    );

    $data['dp_tgl_lahir'] = Carbon::parse($request->dp_tgl_lahir)->startOfDay();

    $dataPasien->update($data);

    return redirect('data_pasien')->with(['notif' => 'updated']);
  }

  public function destroy(DataPasien $dataPasien)
  {
    $dataPasien->delete();

    return response()->json($dataPasien);
  }
}
