<?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\Department;

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


    public function index(Request $r){

        $w[]    = array("hn",$r->hn);
        $w[]    = array("date",date('Y-m-d'));
        $tb_casenote = Mongo::table("tb_casenote")->where($w)->first();
        if(isset($tb_casenote['_id'])){
            $nid = (string) $tb_casenote['_id'];
        }else{
            $val['hn']     = $r->hn;
            $val['date']   = date('Y-m-d');
            $val['appointment'] = date('Y-m-d H:i');
            $nid = (string) Mongo::table("tb_casenote")->insertGetId($val);
        }
        if(isset($r->historytaking)){
            return redirect("book/registration/$nid?historytaking=true");
        }


        return redirect("book/registration/$nid");
    }


    public function store(Request $r){
        $val['date']   = date("Y-m-d");
        $val['hn']     = $r->patient_select_hn;
        $nid = Mongo::table('tb_casenote')->insertGetId($val);
        return redirect("book/registration/$nid");
    }


    public function show(Request $r, $id){

        // dd($id);
        function check_null_to($data){
            if(isset($data)){
                return $data;
            }else{
                return '-';
            }
        }

        $check = Mongo::table('tb_casenote')->where('_id',$id)->first();

        $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['patient']    = $patient;
        $view['view']       = $view;
        $view['tb_booking'] = (object) Mongo::table('tb_booking')->where('noteid',$id)->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['doctor']                 = Department::user('doctor', uid());
        // $view['nurse']                 = Department::user('nurse', uid());
        $view['tb_patienthistory'] = Mongo::table('tb_patienthistory')
        ->where('hn', $patient->hn)
        ->where('department' , uget('department'))
        ->orderByDesc("_id")
        ->first();

        // dd($view);


        // dd($r->patient_select_hn);
        // dd($view['patienthistory']);




        $booking = getCONFIG("booking");

        if(@$booking->noask){

        }else{
            return redirect("registration/$patient->_id?book");
        }


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






}
