<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\Models\RekamMedis;

class LaporanController extends Controller
{
  public function index(Request $request)
  {
    $date = Carbon::now();
    $DSAwal = $request->input('tgl_awal');
    $DSAkhir = $request->input('tgl_akhir');
    $dDisplay = $date->format('d-m-Y');

    if ($DSAkhir == null || $DSAwal == null) {
      $dAwal = $dDisplay;
      $dAkhir = $dDisplay;
    } else {
      $dAwal = Carbon::parse($DSAwal)->format('d-m-Y');
      $dAkhir = Carbon::parse($DSAkhir)->format('d-m-Y');
    }

    $totalPendapatan = $this->getTotalPendapatan($dAwal, $dAkhir);
    $totalPasien = $this->getTotalPasien($dAwal, $dAkhir);

    if ($dAwal > $dAkhir) {
      $dataLaporanPasien = RekamMedis::all()->load('dataPasien', 'pemeriksaan')
        ->where('pemeriksaan.p_status', '=', 'Ditangani')
        ->whereBetween('rm_created_at', [Carbon::parse($dAkhir)->startOfDay(), Carbon::parse($dAwal)->endOfDay()]);
    } else {
      $dataLaporanPasien = RekamMedis::all()->load('dataPasien', 'pemeriksaan')
        ->where('pemeriksaan.p_status', '=', 'Ditangani')
        ->whereBetween('rm_created_at', [Carbon::parse($dAwal)->startOfDay(), Carbon::parse($dAkhir)->endOfDay()]);
    }

    return view(
      'laporan.laporan',
      compact('dataLaporanPasien'),
      [
        'date' => $dDisplay,
        'DSAwal' => $dAwal,
        'DSAkhir' => $dAkhir,
        'totalPendapatan' => $totalPendapatan,
        'totalPasien' => $totalPasien
      ]
    );
  }


  public function getTotalPendapatan($dAwal, $dAkhir)
  {
    if ($dAwal > $dAkhir) {
      $totalPendapatan = DB::table('rekam_medis')
        ->select(DB::raw('SUM(rm_pembayaran) as total'))
        ->where('pemeriksaan.p_status', '=', 'Ditangani')
        ->join('pemeriksaan', 'rekam_medis.p_id', '=', 'pemeriksaan.p_id')
        ->whereBetween('rm_created_at', [Carbon::parse($dAkhir)->startOfDay(), Carbon::parse($dAwal)->endOfDay()])
        ->groupBy('pemeriksaan.p_status')
        ->value('total');
    } else {
      $totalPendapatan = DB::table('rekam_medis')
        ->select(DB::raw('SUM(rm_pembayaran) as total'))
        ->where('pemeriksaan.p_status', '=', 'Ditangani')
        ->join('pemeriksaan', 'rekam_medis.p_id', '=', 'pemeriksaan.p_id')
        ->whereBetween('rm_created_at', [Carbon::parse($dAwal)->startOfDay(), Carbon::parse($dAkhir)->endOfDay()])
        ->groupBy('pemeriksaan.p_status')
        ->value('total');
    }
    return $totalPendapatan;
  }

  public function getTotalPasien($dAwal, $dAkhir)
  {
    if ($dAwal > $dAkhir) {
      $totalPasien = DB::table('rekam_medis')
        ->select(DB::raw('COUNT(pemeriksaan.p_id) as total'))
        ->where('pemeriksaan.p_status', '=', 'Ditangani')
        ->join('pemeriksaan', 'rekam_medis.p_id', '=', 'pemeriksaan.p_id')
        ->whereBetween('rm_created_at', [Carbon::parse($dAkhir)->startOfDay(), Carbon::parse($dAwal)->endOfDay()])
        ->groupBy('pemeriksaan.p_status')
        ->value('total');
    } else {
      $totalPasien = DB::table('rekam_medis')
        ->select(DB::raw('COUNT(pemeriksaan.p_id) as total'))
        ->where('pemeriksaan.p_status', '=', 'Ditangani')
        ->join('pemeriksaan', 'rekam_medis.p_id', '=', 'pemeriksaan.p_id')
        ->whereBetween('rm_created_at', [Carbon::parse($dAwal)->startOfDay(), Carbon::parse($dAkhir)->endOfDay()])
        ->groupBy('pemeriksaan.p_status')
        ->value('total');
    }

    return $totalPasien;
  }
}
