<?php

namespace App\Http\Controllers\Tablet;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use App\Models\Datacase;
use App\Models\Casebooking;
use App\Models\Department;
use App\Models\Mongo;


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

    public function index(Request $r){
        $view['doctor']             = Department::user('doctor', uid());
        $view['procedure']          = Department::procedure(uid());
        $view['room']               = Department::room(uid());
        $str                        = file_get_contents("D:/laragon/htdocs/config/project/admin.txt");
        $view['config']             = jsonDecode($str);
        $view['date']               = date('Y-m-d');
        $view['set_page']           = jsonDecode($str);
        $view['book']               = Casebooking::booktoday();
        $view['i']                  = 0;
        $view['url']                = url('');
        $view['count_casetoday']    = Datacase::counttoday();
        $view['alltoday_case']      = $this->statusholding($r, $r->status);
        $view['all_case']           = $this->search_case($r);
        $view['configadmin']        = getCONFIG('admin');
        $view['feature']            = getCONFIG("feature");
        $view['tablet']            = getCONFIG("tablet");
        // dd($view['tablet']);
        $view['search']             = false;
        if(@$r->event == 'search_case'){
            $view['search'] = true;
            $view['search_type'] = @$r->type."";
            if(@$r->type == 'today'){
                $view['alltoday_case'] = $this->statusholding($r, $r->status);
            } else if(@$r->type == 'all') {
                $view['all_case'] = $this->search_case($r);
            }
            $view['filter']   = $this->get_search_var($r);
        }

            $view['menu']      = 'home';



        return view('EndoCAPTURE\home\Mockup\index',$view);
    }

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

    public function search_case($r){
        $department = [];
        $department = uget("department");

        $w[0] = array('status', '!=', 'cancel');
        $w[1] = array('status', '!=', 'delete');
        $w[2]   = array("department" , $department);
        $orw[0] = array('status', '!=', 'cancel');
        $orw[1] = array('status', '!=', 'delete');
        $orw[2] = array("department" , $department);

        $arr = [];
        if(@$r->hn."" != ''){
            $w[] = array('case_hn', 'like', '%'.$r->hn.'%');
            $orw[] = array('case_hn','like', '%'.$r->hn.'%');
        }

        if(@$r->name."" != ''){
            $w[] = array('patientname', 'like', '%'.$r->name.'%');
            $orw[] = array('patientname', 'like', '%'.$r->name.'%');
        }

        if(@$r->physician."" != ''){
            $w[] = array('case_physicians01', $r->physician);
            $orw[] = array('case_physicians01', $r->physician);
        }

        if(@$r->status."" != ''){
            $w[] = array('statusjob', $r->status);
            if(@$r->status."" == 'recovery'){
                $orw[] = array('statusjob', 'discharged');
            }
        }
        $arr = Mongo::table('tb_case')->where($w)->orWhere($orw)->orWhere($orw)->orderBy('_id','DESC')->limit(500)->get();
        return $arr;
    }

    public function get_search_var($r){
        $arr = [];
        if(@$r->hn."" != ''){
            $arr['hn'] = $r->hn;
        }

        if(@$r->name."" != ''){
            $arr['name'] = $r->name;
        }

        if(@$r->physician."" != ''){
            $arr['physician'] = $r->physician;
        }

        if(@$r->status."" != ''){
            $arr['status'] = $r->status;
        }
        return $arr;
    }

    public function show($id){
        $view['nid'] = $id;
        $view['note'] = (object) Mongo::table("tb_casenote")->where('noteid', $id)->first();
        return view('short.b',$view);
    }


    public function statusholding($r, $status){

        $department = [];
        $department = uget("department");
        if(!isset($r->status)){
            $status = 'all';
        }

        $w[0]       = array('appointment',"like",date('Y-m-d')."%");
        // $w[1] = array("statusjob" , $status);
        $w[5]       = array("department" , $department);
        $orw[0]     = array('appointment',"like",date('Y-m-d')."%");
        $orw1[0]    = array('appointment',"like",date('Y-m-d')."%");
        $orw[5]     = array("department" , $department);
        $orw1[5]    = array("department" , $department);
        $tb_case    = Mongo::table('tb_case')->where($w)->orderBy('_id','DESC')->limit(500)->get();

        if(@$r->hn."" != ''){
            $w[] = array('case_hn', 'like', '%'.$r->hn.'%');
            $orw[] = array('case_hn', 'like', '%'.$r->hn.'%');
            $orw1[] = array('case_hn', 'like', '%'.$r->hn.'%');
        }

        if(@$r->name."" != ''){
            $w[] = array('patientname', 'like', '%'.$r->name.'%');
            $orw[] = array('patientname', 'like', '%'.$r->name.'%');
            $orw1[] = array('patientname', 'like','%'.$r->name.'%');
        }

        if(@$r->physician."" != ''){
            $w[] = array('case_physicians01', $r->physician);
            $orw[] = array('case_physicians01', $r->physician);
            $orw1[] = array('case_physicians01', $r->physician);
        }

        if($status == 'holding'){
            $orw[0] = array('appointment',"like",date('Y-m-d')."%");
            $orw[1] = array("statusjob" , 'operation');
            $orw1[0] = array('appointment',"like",date('Y-m-d')."%");
            $orw1[1] = array("statusjob" , 'recovery');
            $tb_case = Mongo::table('tb_case')->where($w)->orWhere($orw)->orWhere($orw1)->orderBy('_id','DESC')->limit(500)->get();
        } elseif($status == 'operation'){
            $orw[0] = array('appointment',"like",date('Y-m-d')."%");
            $orw[1] = array("statusjob", "recovery");
            $orw1[0] = array('appointment',"like",date('Y-m-d')."%");
            $orw1[1] = array("statusjob", "holding");
            $tb_case = Mongo::table('tb_case')->where($w)->orWhere($orw)->orWhere($orw1)->orderBy('_id','DESC')->limit(500)->get();
        } elseif($status == 'recovery'){
            $orw[0] = array('appointment',"like",date('Y-m-d')."%");
            $orw[1] = array("statusjob" , 'discharged');
            $orw1[0] = array('appointment',"like",date('Y-m-d')."%");
            $orw1[1] = array("statusjob" , 'operation');
            $tb_case = Mongo::table('tb_case')->where($w)->orWhere($orw)->orWhere($orw1)->orderBy('_id','DESC')->limit(500)->get();
        } else if($status == 'all'){
            $tb_case = Mongo::table('tb_case')->where($w)->orderBy('_id','DESC')->limit(500)->get();
        }

        $arr = array();
        foreach($tb_case as $data){
            $data = (object) $data;

            if($data->statusjob == 'delete' || $data->statusjob == 'cancel'){
                continue;
            }

            $w1[0]        = array('caseuniq', $data->caseuniq);
            $case_monitor = (object) Mongo::table('tb_casemonitor')->where($w1)->first();

            $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->doctorname  : $data->physician;
            if(isset($data->physician) && @$data->physician."" !== ""){
                $doctor1  = $data->physician;
            }
            $room         = isset($data->room)          ? $data->room        : '';
            $pdfversion   = isset($data->case_pdfversion) ? $data->case_pdfversion : 0;

            $arr[$case_hn]['_id']                   = $data->_id;
            $arr[$case_hn]['case_id']               = @$data->case_id."";
            $arr[$case_hn]['hn']                    = $case_hn;
            $arr[$case_hn]['patientname']           = @$patient_name."";
            $arr[$case_hn]['physician']             = @$doctor1."";
            $arr[$case_hn]['procedure'][]           = @$data->procedurename."";
            $arr[$case_hn]['description']           = @$data->description."";
            $arr[$case_hn]['waitinglocation']       = @$data->waitinglocation."";
            $arr[$case_hn]['statusjob'][]           = @$data->statusjob."";
            $arr[$case_hn]['statuspacs'][]          = @$data->statuspacs;
            $arr[$case_hn]['statusreport'][]        = @$data->statusreport."";
            $arr[$case_hn]['prediagnosis_other']    = @$data->prediagnostic_other."";
            $arr[$case_hn]['rapid_urease_test'][]   = isset($data->rapid_urease_test) ? @$data->rapid_urease_test."" : '';
            $arr[$case_hn]['rapid_other'][]         = isset($data->rapid_other) ? @$data->rapid_other."" : '';
            $arr[$case_hn]['complication_other']    = @$data->complication_other."";

            if($pdfversion != 0 && is_array($pdfversion)){
                $arr[$case_hn]['case_pdfversion'][] = @$pdfversion;
            }

            $arr[$case_hn]['room']              = gettype($room) != 'array' ? @$room."" : join(" ", $room);
            $arr[$case_hn]['appointment']       = @$data->appointment."";
            $arr[$case_hn]['description']       = isset($case_monitor->monitor_description) ? $case_monitor->monitor_description : '';
        }

        $new_arr = [];
        foreach (isset($arr)?$arr:[] as $hn => $val) {
            if(is_array($val['statusjob'])){
                if(@$status == 'operation'){
                    if(in_array('operation', $val['statusjob'])){
                        $new_arr[$hn] = $val;
                    }
                }

                if(@$status == 'recovery'){
                    if(in_array('recovery', $val['statusjob']) || in_array('discharged', $val['statusjob'])){
                        $new_arr[$hn] = $val;
                    }
                }

                if(@$status == 'holding'){
                    $is_holding = true;
                    foreach (@$val['statusjob'] as $status) {
                        if($status != 'holding'){
                            $is_holding = false;
                        }
                    }
                    if($is_holding){
                        $new_arr[$hn] = $val;
                    }
                }

                if(@$status == 'all'){
                    $new_arr[$hn] = $val;
                }
            }
        }

        return $new_arr;
    }


}
