<?php

namespace App\Http\Controllers\EndoBOOK;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Carbon\Carbon;
use App\Models\User;
use App\Models\Server;
use App\Models\Department;
use App\Models\BookingCAL;

class BookingController extends Controller
{

    public function index(Request $r)
    {

        $physician = intval($r->physician);
        $view['noteid']                 = Session::get('noteid');
        $view['uid']                    = uid();
        $view['utype']                  = uget("user_type");
        $view['patient_type']           = $r->patient_type;
        $view['form_time_type']         = $r->time_type;
        $view['form_procedure']         = array_filter($r->procedure);
        $view['form_period']            = $r->period;
        $view['physician']            = $r->physician;
        $view['physician_select']       = User::where("id",$physician)->first();

        // dd($view,$r);

        $view['physician']              = User::where("user_type","doctor")->get();
        $view['procedure']              = Department::procedure(uid());
        $view['arrwork']                = BookingCAL::calculate($r);


        // dd($view['arrwork'],$r);

        $view['usid']                   = uid();
        $view['alert']                  = 'none';
        $procedure                      = array_unique(array_filter($r->procedure));
        foreach($procedure as $data)
        {$val['procedure'][] = $data;}
        $view['procedure_select']       = $r->procedure;
        $view['procedure_ex_select']    = $r->procedure_ex;
        $procedure_select_name  = Server::table('tb_procedure')->whereIn("code",$r->procedure)->get();

        foreach ($procedure_select_name as $data) {
            $view['procedure_select_name'][] = $data['name'];
        }



        if(count($view['arrwork'])==0){
            if($view['usid']==$r->physician){
                $view['alert'] = 'redirect';
            }else{
                $view['alert'] = 'alert';
            }
        }
        $view['tb_treatmentcoverage'] = Server::table('tb_treatmentcoverage')->get();
        $view['show_calendar'] = false;
        if(isset($r->event) && @$r->event=='calculate'){
            $view['show_calendar'] = true;
        }
        // dd($view);
        if(isMobile()){
            return view('Endocapture.nurse_monitor.mockup.ipad.booking.index', $view);
        }

        // dd("mfdfd");
        return view("EndoBOOK.booking.show",$view);
    }

    public function store(Request $r){
        // dd($r->all());
        $view['uid']        = uid();
        $view['utype']      = uget("user_type");
        if(isset($r->event)){
            $event = $r->event;
            if($event=="calculate") {
                $view['physician']        = $r->physician;
                $view['patient_type']  = $r->patient_type;
                $view['form_time_type']     = $r->time_type;
                $view['form_period']        = $r->period;
                $view['form_procedure']     = array_filter($r->procedure);
                $view['physician']          = User::where("user_type","doctor")->get();
                $view['procedure']          = Department::procedure(uid());
                $view['arrwork']            = BookingCAL::calculate($r);
                $procedure                  = array_unique(array_filter($r->procedure));
                $procedure_ex               = array_unique(array_filter($r->procedure_ex));
                foreach($procedure      as $data){$val['procedure'][] = $data;}
                foreach($procedure_ex   as $data){$val['procedure'][] = $data;}
                $view['procedure_select']   = $procedure;
                $view['procedure_ex_select']= $procedure_ex;
                $view['usid']               = uid();
                $view['alert']              = 'none';
                if(count($view['arrwork'])==0){
                    if($view['usid']==$r->physician){
                        $view['alert'] = 'redirect';
                    }else{
                        $view['alert'] = 'alert';
                    }
                }

                return view("EndoBOOK.booking.show",$view);
            }
        }
    }





    public function show($id)
    {
        Session::put('noteid', $id);
        $view['uid']        = uid();
        $view['utype']      = uget("user_type");;
        $view['physician']  = User::where("user_type","doctor")->get();
        $view['procedure']  = Department::procedure(uid());
        $view['tb_bookset_calendar_department'] = Server::table('tb_bookset_calendar_department')
        ->where("calendar_date",">", Carbon::now()->addDay(15))
        ->orderBy("calendar_date")
        ->get();
        $view['show_calendar'] = false;

        $view['tb_treatmentcoverage'] = Server::table('tb_treatmentcoverage')->get();
        return view("EndoBOOK.booking.show",$view);
    }







}
