<?php
namespace App\Http\Controllers\EndoBOOK;
use App\Http\Controllers\Controller;

use App\Models\Department;
use App\Models\User;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use Illuminate\Support\Facades\Session;

use App\Http\Controllers\Endobook\BookingController;

use App\Models\Datetime;
use App\Models\Mongo;
use App\Models\BookingCAL;

class BookrecordController extends Controller
{

    public function index()
    {
        $view['month_all']  = Datetime::monthALL();
        $view['day_all']    = Datetime::dayALL();
        $view['year_all']   = Datetime::yearALL(120);
        return view('EndoBOOK.bookrecord.index',$view);
    }

    public function store(Request $r){
        if(isset($r->event)){
            $event = $r->event;
            if($event=="period_select") {
                $view = $this->period_select($r);
                return view('EndoBOOK.bookrecord.index',$view);
            }

            if($event=="addto_tb_booking"){
                // $r->procedure   = $r->procedure;
                $r->period      = $r->form_period;
                $r->urgent      = $r->urgent;
                $r->physician   = $r->physician;
                $this->addto_tb_booking($r);
                $nid = Session::get('noteid');

                BookingCAL::calculate($r);
                // dd($nid);
                return redirect(url("book/prepare/$nid"));}
        }
    }

    public function period_select($r){
        $view['create_time']        = date("ymdhis");
        $view['physician']        = $r->physician;
        $view['patient_type']       = $r->patient_type;
        $view['form_time_type']     = $r->form_time_type;
        $view['urgent']          = $r->urgent;
        $view['date']            = $r->date;
        $view['form_procedure']     = $r->procedure;
        $view['period']             = @$r->form_period.'';
        $view['form_special']       = @$r->form_special.'';
        $view['form_anesthesia']    = @$r->form_anesthesia.'';
        $view['month_all']          = Datetime::monthALL();
        $view['day_all']            = Datetime::dayALL();
        $view['year_all']           = Datetime::yearALL(120);
        return $view;
    }




    public function addto_tb_booking($r){

        Mongo::table("tb_casenote")->where("_id",Session::get('noteid'))->update(['appointment'=>$r->date]);
        Mongo::table("tb_casenote")->where("_id",Session::get('noteid'))->update(['treatment'=>$r->param]);

        $val["create_time"]         = date("Ymdhis");
        $val["patient_type"]   = $r->patient_type;
        $val["period"]              = $r->form_period;
        $val["noteid"]              = Session::get('noteid');

        $tb_casenote                = (object) Mongo::table("tb_casenote")->where("_id",Session::get('noteid'))->first();
        $patient                    = (object) Mongo::table("tb_patient")->where("hn",$tb_casenote->hn)->first();
        $val["hn"]                  = $tb_casenote->hn;
        $val["prefixname"]          = $patient->prefix;
        $val["firstname"]           = $patient->firstname;
        $val["middlename"]          = $patient->middlename;
        $val["lastname"]            = $patient->lastname;
        $val["age"]                 = age($patient->birthdate);
        $val["treatment"]           = $r->param;

        $procedure                  = array_unique(array_filter($r->procedure));
        foreach($procedure as $data){$val['procedure'][] = $data;}

        $val['booker']   = uid();
        $val["physician"] = $r->physician;
        $val['physician_name']   = User::fullname($r->physician);
        $val['hn']       = $tb_casenote->hn;
        $val['date']     = $r->date;
        $val['period']     = $r->form_period;
        $val['urgent']   = $r->urgent;
        $val['status']   = "booking";
        Mongo::table('tb_booking')->insert($val);
        $tb_bookset_department  = (object) Mongo::table('tb_bookset_department')->where('department_code',"GI")->first();

        foreach($procedure as $data){
            $val['department']      = uget("department");
            $val['urgent']          = $r->urgent;
            $val['date']            = date("Y-m-d");
            $val['create_time']     = date("ymdhis");
            $val['create_user']     = uid();
            $val['period']          = $r->period;
            $val['physician']       = $r->physician;
            $val['procedure_code']  = $data;
            $val['min']             = $tb_bookset_department->procedure[$data]['min'];
            $val['hn']              = "temp";
            Mongo::table('tb_booking_source')->insert($val);
        }

    }









    public function show($id)
    {


        if($id=="emergency"){
            $view['physician']  = User::where("user_type","doctor")->get();
            $view['procedure']  = DB::table('tb_procedure')->where("procedure_code","like","gi%")->get();
            $view['month_all']  = Datetime::monthALL();
            $view['day_all']    = Datetime::dayALL();
            $view['year_all']   = Datetime::yearALL(120);
            return view('Endobook.bookrecord.emergency',$view);

        }
    }








}
