<?php

namespace App\Http\Controllers\Endobook;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Mongo;
use App\Models\Note;
use App\Models\Datacase;
use App\Models\Department;
use GuzzleHttp\Client;
use PDF;
use App\Models\Mainpartsub;

class CasemonitorController extends Controller
{
    // public function __construct(Request $r){checklogin();}

    public function show(Request $r, $id)
    {
        function check_null_to($data)
        {
            if (isset($data)) {
                return $data;
            } else {
                return '-';
            }
        }

        $check = Mongo::table('tb_casenote')
            ->where("hn", $r->hn)
            ->where("date", $r->date)
            ->first();

        $view['date'] = $r->date;

        $id = @$check['_id'];

        // dd($check);

        if (!$check) {
            $val['hn'] = $r->hn;
            $val['date'] = $r->date;
            $val['department'] = uget("department");
            $id = Mongo::table('tb_casenote')->insertGetId($val);
            $check = Mongo::table('tb_casenote')
                ->where('_id', $id)
                ->first();
        }

        $view['nid'] = $id;


        $url = explode("/", url()->previous());

        if ($url[count($url) - 1] == 'control') {
            $view['process'] = 'history2';
        } else {
            $view['process'] = 'history1';
        }

        if (isset($r->from)) {
            $view['from']       = @$r->from . "";
            $view['process']    = 'history2';
        }

        $patient = (object) Mongo::table('tb_patient')
            ->where('hn', $check['hn'])
            ->first();

        $view['uid']        = uid();
        $view['id']         = $id;
        // $view['doctor']     = Department::user('doctor');
        // $view['nurse']      = Department::user('nurse');
        $view['note']       = $check;
        $view['note']       = (object) Mongo::table('tb_casenote')->where('_id', $id)->first();
        $view['casenote']   = (array) $view['note'];

        $view['nid']        = (string) $view['casenote']['_id'];
        // dd($view['casenote'],$check,$view['nid']);
        $view['patient']    = $patient;
        $view['view']       = $view;
        $view['tb_booking'] = (object) Mongo::table('tb_booking')
            ->where('hn', $r->hn)
            ->where('date', $r->date)
            ->first();
        $date1              = date_create($patient->birthdate);
        $date2              = date_create(date('Y-m-d'));
        $diff               = date_diff($date1, $date2);
        $view['hn']         = $patient->hn;
        $view['name']       = $patient->prefix . ' ' . $patient->firstname . ' ' . $patient->middlename . ' ' . $patient->lastname;
        $view['phone']      = check_null_to($patient->phone);
        $view['email']      = check_null_to($patient->email);
        $view['gender']     = check_null_to($patient->gender);
        $view['age']        = "($diff->y Y)";
        $view['dob']        = $date1->format('d M,Y');
        $view['procedure']  = Department::procedure(uid());
        $view['tb_treatmentcoverage'] = Mongo::table('tb_treatmentcoverage')->get();
        $view['doctor']                 = Department::user('doctor', uid());
        $view['nurse']                 = Department::user('nurse', uid());
        
        // dd($view['case']);


        // dd($view);
        // $this->checkvitalsign_softcon($r);

        return view('EndoBOOK.booking.registration', $view);
    }
}
