<?php

namespace App\Http\Controllers\Tablet;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use App\Models\Department;
use App\Models\Mongo;
use Carbon\Carbon;
use App\Models\User;
use App\Models\Server;


class BookController extends Controller
{

    // public function index(Request $r){


    //     // if(isMobile()){return view('Endocapture.nurse_monitor.mockup.ipad.booking.index', $view);}
    //     return view('Endocapture.ipad.booking.index');
    // }

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

    public function store(Request $r){

        if($r->event=='edit_booking'){
            $this->edit_booking($r);
            return redirect('tablet/book');
        }

        if($r->event=='gen_calendar'){
            $view['tb_bookset_calendar_department'] = Mongo::table('tb_bookset_calendar_department')->get();
            if($r->status=='back'){
                $date = Carbon::create($r->year, $r->month, 1)->subMonth(1);
            }else{
                $date = Carbon::create($r->year, $r->month, 1)->addMonths(1);
            }
            $view['year']       = $date->year;
            $view['month']      = $date->month;
            $view['day']        = '01';
            $view['s_day']      = $r->s_day;
            $view['s_month']    = $r->s_month;
            $view['s_year']     = $r->s_year;


            return view('EndoBOOK.gen.calendar',$view)->render();

        }


    }

    public function index(Request $r){


        $view['booking']        = getCONFIG("booking");
        $view['userID']         = uid();
        $view['procedure']      = Department::procedure(uid());
        $view['doctor']         = Department::user('doctor');
        $view['procedure_ex']   = array("anes01","anes02","anes03","equ01","equ02","equ03");
        $view['user_type']      = uget("user_type");
        $view['nurse']          = (object) Mongo::table('users')->where("user_type","nurse")->get();

        $view                   = $this->filters($r,$view);
        $view['menu']       = 'home';
        // $view['tb_booking'] = (object) Mongo::table("tb_booking")->get();
        $view['period01']        = $view['booking']->period_service;
        $view['period02']        = $view['booking']->period_special;
        $view['period03']        = $view['booking']->period_overtime;
        $view['tablet']         = getCONFIG("tablet");
        // dd(1);

        return view('Endocapture.ipad.booking.index',$view);
    }

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

    }


    public function filters($r,$view){
        $nodata=true;
        // dd($r->all());
        if($r->event == 'filters'){
            $view['search']             = $r->search_hn;
            $view['search_physician']   = $r->select_book_physician;
            $view['search_procedure']   = $r->search_procedure;
            $view['search_name']        = $r->search_name;
            if(isset($r->search_hn)){
                $w[] = array('hn','like',"%$r->search_hn%");
            }
            if(isset($r->select_book_physician)){
                $physician_convert = $r->select_book_physician;
                $physician = intval($physician_convert);
                // dd($physician);
                $w[] = array('physician', $physician);
            }
            if(isset($r->search_name)){
            // dd($r->search_name);
                $w[] = array('patient_name','like',"%$r->search_name%");
            }
            // if(isset($r->search_procedure)){
            //     $w[] = array('procedure','like',"%$r->search_procedure%");
            // }
            if(isset($w)){
                // dd($w);
                $date = date('Y-m-d');
                $view['tb_booking'] =   Mongo::table('tb_booking')
                ->whereDate('date','>',$date)
                ->where($w)
                ->orderby('date','asc')
                ->get();
                // dd($view);
                $nodata=false;
            }
        }
        if($nodata){
            // dd(2);

            $date = date('Y-m-d');
            $view['date2'] = date('Y-m-d');
            $view['tb_booking'] =   Mongo::table('tb_booking')
                                    ->whereDate('date','>',$date)
                                    ->orderby('date','asc')
                                    ->get();
            $view['s_day'] = date('d');
        }
        return $view;


    }



    public function get_casephysician($r,$view,$physician){
        if(isset($r->date_select)){
            $date_select = $r->date_select;
        }else{
            $date_select = date("Y-m-d");
        }
        $tb_booksum_physician = Mongo::table("tb_booksum_physician")
                                ->where("date",$date_select)
                                ->where("physician",$physician."")
                                ->get();
        $tb_bookset_department = Mongo::table("tb_bookset_department")
                                ->where("department_code",uget("department"))
                                ->first();
        $djson  = jsonDecode($tb_bookset_department['department_json']);
        $view['time_am'] = $djson->time_am;
        $view['time_pm'] = $djson->time_pm;
        $view['time_ot'] = $djson->time_ot;
        $view['time_all']= $djson->time_am+$djson->time_pm+$djson->time_ot;
        $sum_case = 0;
        $sum_min  = 0;
        foreach($tb_booksum_physician as $data){
            $data = (object) $data;
            $view[$data->period]['procedure']    = $data->procedure;
            $view[$data->period]['min']          = $data->min;
            $sum_min    = $sum_min + $data->min;
            $sum_case   = $sum_case + $this->case_count($data->procedure);
        }

        $view['am']['min']  = $view['time_am']-$this->checkisset(@$view['am']['min']);
        $view['pm']['min']  = $view['time_pm']-$this->checkisset(@$view['pm']['min']);
        $view['ot']['min']  = $view['time_ot']-$this->checkisset(@$view['ot']['min']);
        $view['sum_min']    = $view['time_all']-$sum_min;
        $view['sum_case']   = $sum_case;
        return $view;
    }



    public function get_casedepartment($r,$view){
        if(isset($r->date_select)){
            $date_select = $r->date_select;
        }else{
            $date_select = date("Y-m-d");
        }
        $tb_booksum_department =    Mongo::table("tb_booksum_department")
                                    ->where("date",$date_select)
                                    ->get();

        $calendar   = Mongo::table("tb_bookset_calendar_department")
                    ->where("calendar_date",$date_select)
                    ->first();

        $tb_bookset_department = (object) Mongo::table("tb_bookset_department")
                                ->where("department_code",uget("department"))
                                ->first();
        //
        $view['time_am'] = $tb_bookset_department->time_am*$this->checkisset(@$calendar['calendar_roomam']);
        $view['time_pm'] = $tb_bookset_department->time_pm*$this->checkisset(@$calendar['calendar_roompm']);
        $view['time_ot'] = $tb_bookset_department->time_ot*$this->checkisset(@$calendar['calendar_roomot']);
        $view['time_all']= $view['time_am']+$view['time_pm']+$view['time_ot'];
        $sum_case = 0;
        $sum_min  = 0;
        foreach($tb_booksum_department as $data){
            $data = (object) $data;
            $view[$data->period]['procedure']    = $data->procedure;
            $view[$data->period]['min']          = $data->min;
            $sum_min    = $sum_min + $data->min;
            $sum_case   = $sum_case + $this->case_count($data->procedure);
        }

        $view['am']['min']  = $view['time_am']-$this->checkisset(@$view['am']['min']);
        $view['pm']['min']  = $view['time_pm']-$this->checkisset(@$view['pm']['min']);
        $view['ot']['min']  = $view['time_ot']-$this->checkisset(@$view['ot']['min']);
        $view['sum_min']    = $view['time_all']-$sum_min;
        $view['sum_case']   = $sum_case;
        return $view;
    }


    public function checkisset($val){
        $num = 0;
        if(isset($val)){
            $num = $val;
        }
        return $num;
    }


    public function case_count($procedure){
        $ex = array("anes01","anes02","anes03","equ01","equ02","equ03");
        $num = 0;
        foreach($procedure as $k=>$v){
            if(!in_array($k, $ex)){
                $num = $num+$v;
            }
        }
        return $num;
    }

    public function destroy($id,Request $r)
    {
        Mongo::table("tb_booking")->where("create_time",$r->id)->delete();
        return redirect()->back();
    }


    public function edit($idwithhn){

        $ex = explode("_", $idwithhn);
        $hn = $ex[0];
        $id = $ex[1];
       $tb_patient = (object) Server::table("tb_patient")->where("hn" , $hn)->first();
       $gender = "";
       if($tb_patient->gender == 1){$gender = "Male";}
       elseif($tb_patient->gender == 2){$gender = "Female";}
       else{$gender = "ไม่ระบุ";}

       $view['procedure']      = Department::procedure(uid());
       $view['doctor']         = Department::user('doctor', uid());
       $view['gender']  = $gender;
       $view['patient'] = $tb_patient;
       $view['booking'] = (object) Server::table("tb_booking")->where("_id", $id)->first();
       $view['treatment']      = Server::table("tb_treatmentcoverage")->get();


    //    dd($view);
    return view('Endocapture.ipad.booking.edit',$view);

        // return view("EndoBOOK.bookingviewer.editbooking" , $view);

        // echo $tb_booking;
    }



    public function edit_booking(Request $r){
        // dd(1);
        $val['date'] = $r->date;
        $val['period'] = $r->period;
        $val['urgent'] = $r->urgent;
        $val['description'] = $r->description;
        $val['treatment'] = $r->treatment_coverage;
        $val['patient_type'] = $r->type_of_patient_care;
        $val['physician'] = $r->physician;
        $val['procedure'] = $r->procedure;
        $val['anesthesia'] = $r->anesthesia;
        $val['special'] = $r->special;
        $user = (object) Server::table("users")
        ->where("id", intval($r->physician))
        ->where("user_type", "doctor")
        ->first();
        // dd($user);
        $val['physician_name'] = @$user->user_prefix . @$user->user_firstname . " " . @$user->user_lastname;
        Server::table("tb_booking")->where("_id", $r->id)->update($val);

    }






}
