<?php

namespace App\Http\Controllers\EndoBOOK;
use App\Http\Controllers\Controller;
use App\Models\Datacase;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use App\Models\Server;
use App\Models\Department;


class FollowupController extends Controller
{

    public function __construct(Request $r){checklogin();}

    public function index(){
        $view['doctor']         = Department::user('doctor');
        $view['procedure']      = Department::procedure(uid());
        $view['tb_case']        = $this->statusholding();
        return view('endobook.followup.index',$view);
    }

    public function statusholding(){

        $tb_case = Server::table('tb_case')->orderBy('_id','DESC')->limit(100)->get();
        $arr = array();
        foreach($tb_case as $data){
            $data = (object) $data;
            if (isset($data->appointment)) {
                $case_hn      = isset($data->case_hn)       ? $data->case_hn     : $data->hn;
                $patient_name = isset($data->patientname)   ? $data->patientname : $data->firstname.' '.$data->lastname;
                $doctor1      = isset($data->doctorname)    ? $data->doctorname  : $data->physician;
                $appointment  = explode(' ',$data->appointment);

                $note = Server::table('tb_casenote')->where('_id', $data->noteid)->first();
                $arr[$appointment[0]][$case_hn]['diagnosis']         = @$note['history']['provisional_other']."";
                $arr[$appointment[0]][$case_hn]['_id']               = $data->_id;
                $arr[$appointment[0]][$case_hn]['hn']                = $case_hn;
                $arr[$appointment[0]][$case_hn]['noteid']            = @$data->noteid;
                $arr[$appointment[0]][$case_hn]['patientname']       = @$patient_name."";
                $arr[$appointment[0]][$case_hn]['physician']         = @$doctor1."";
                $arr[$appointment[0]][$case_hn]['procedure'][]       = @$data->procedurename."";
                $arr[$appointment[0]][$case_hn]['description']       = @$data->description."";
                $arr[$appointment[0]][$case_hn]['waitinglocation']   = @$data->waitinglocation."";
                $arr[$appointment[0]][$case_hn]['statusjob'][]       = @$data->statusjob."";
                $arr[$appointment[0]][$case_hn]['room']              = @$data->room."";
                $arr[$appointment[0]][$case_hn]['appointment']       = @$data->appointment."";
            }
        }
        return $arr;
    }


    public function show($id){

        $view['tb_booking']         = Server::table('tb_casenote')->where('_id', $id)->first();

        $view['note']               = (object) Server::table('tb_casenote')->where('_id',$id)->first();
        $w[] = array("appointment","like",@$view['note']->appointment.""."%");
        $w[] = array("case_hn",@$view['note']->hn."");
        $w[] = array('statusjob', '!=', 'delete');
        $w[] = array('statusjob', '!=', 'cancel');
        $view['tb_casefirst']       = (object) Server::table('tb_case')->where($w)->first();
        $view['tb_case']            = (object) Server::table('tb_case')->where($w)->get();
        $view['treatment']  = (object) Server::table('tb_casenote')->where('_id', @$view['case']->noteid."")->first();
        $view['tb_treatmentcoverage']   = (object) Server::table('tb_treatmentcoverage')->get();


        $view['patient']            = (object) Server::table('tb_patient')->where('hn',@$view['tb_casefirst']->case_hn."")->first();
        $view['cid']                = @$view['tb_casefirst']->_id."";
        $view['nid']                = $id;


        return view('endobook.followup.show',$view);
    }

    public function store(Request $r){
        if(isset($r->event)){
            if($r->event == 'update_case')                    {return $this->update_case_status($r);}
        }
    }


    public function update_case_status($r){
        $status = 0;
        if(isset($r->case_hn)){
            $w[0] = array('case_hn', $r->case_hn);
            $u['case_status'] = 3;
            Server::table('tb_case')->where($w)->update($u);
            $status = 1;
        }
        echo $status;
    }


}
