<?php

namespace App\Http\Controllers\EndoBOOK\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use App\Models\Mongo;


class BooksetController extends Controller
{

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

    public function hn_check($r){
        $patient = DB::table('patient')->where('hn',$r->value)->first();
        if($patient!=null){
            $dob = explode('-',$patient->birthdate);
            $data['prefix']             = @$patient->prefix.'';
            $data['firstname']          = @$patient->firstname.'';
            $data['middlename']         = @$patient->middlename.'';
            $data['lastname']           = @$patient->lastname.'';
            $data['birthdate_year']     = @$dob[0]+543;
            $data['birthdate_month']    = @$dob[1];
            $data['birthdate_day']      = @$dob[2];
            $data['phone']              = @$patient->phone.'';
            $data['citizen']            = @$patient->citizen.'';
            $data['gender']             = @$patient->gender.'';
            $json = json_encode($data);
            echo $json;
        }else{
            echo "false";
        }
    }

    public function birth_change($r){
        $day    = $r->day;
        $month  = $r->month;
        $year   = $r->year-543;
        $birth  = "$year-$month-$day";
        $age    = jsonEncode(array(Carbon::parse($birth)->age));
        echo $age;
    }



    public function doctorSETDATE($r){
        $w[0] = array('dc_date',$r->date);
        $w[1] = array('dc_doctor_id',$r->doctorID);
        $tb_doctor_calendar = Mongo::table('tb_doctor_calendar')->where($w)->first();

        $val['dc_date']         = $r->date;
        $val['dc_doctor_id']    = $r->doctorID;
        if($r->inout=="in")     {$val['dc_in']  = $r->value;}
        if($r->inout=="out")    {$val['dc_out'] = $r->value;}

        if($tb_doctor_calendar!=null){
            Mongo::table('tb_doctor_calendar')->where($w)->update($val);
        }else{
            Mongo::table('tb_doctor_calendar')->insert($val);
        }
    }




    public function departmentSETDATE($r){
        $inout = $r->inout;
        $w[] = array('calendar_date',$r->date);
        $tb_bookset_calendar_department = Mongo::table('tb_bookset_calendar_department')->where($w)->first();
        $val['calendar_date']           = $r->date;
        $val['calendar_weekday']        = $r->weekday;
        if($inout=="am")    {$val['calendar_roomam']    = intval($r->value);}
        if($inout=="pm")    {$val['calendar_roompm']    = intval($r->value);}
        if($inout=="ot")    {$val['calendar_roomot']    = intval($r->value);}
        // dd($tb_bookset_calendar_department,$val);
        if($tb_bookset_calendar_department!=null){
            Mongo::table('tb_bookset_calendar_department')->where($w)->update($val);
        }else{
            Mongo::table('tb_bookset_calendar_department')->insert($val);
        }
    }


    public function department_condition_room($r){
        $val[$r->key] = intval($r->value);
        Mongo::table("tb_bookset_department")
        ->where("department_code",$r->depID)
        ->update($val);
    }


    public function department_condition_procedure($r){
        $bookset = (object) Mongo::table("tb_bookset_department")
        ->where("department_code",$r->depID)
        ->first();

        if(isset($bookset->procedure)){
            foreach($bookset->procedure as $key1=>$key2){
                $key2 = (object) $key2;
                $arr['procedure'][$key1]['name']    = @$key2->name;
                $arr['procedure'][$key1]['min']     = intval(@$key2->min);
                $arr['procedure'][$key1]['room']    = intval(@$key2->room);
                $arr['procedure'][$key1]['rooms']   = intval(@$key2->rooms);
                $arr['procedure'][$key1]['times']   = intval(@$key2->times);
                $arr['procedure'][$key1]['cal_am']  = $this->caltime(@$bookset->time_am,@$key2->min,@$key2->rooms);
                $arr['procedure'][$key1]['cal_pm']  = $this->caltime(@$bookset->time_pm,@$key2->min,@$key2->rooms);
                $arr['procedure'][$key1]['cal_ot']  = $this->caltime(@$bookset->time_ot,@$key2->min,@$key2->rooms);
            }
        }
        $arr['procedure'][$r->procedure_code]['name']   = $r->procedure_name;
        $arr['procedure'][$r->procedure_code]['min']    = intval(($r->min!=null     ? $r->min   : 0));
        $arr['procedure'][$r->procedure_code]['room']   = intval(($r->room!=null    ? $r->room  : 0));
        $arr['procedure'][$r->procedure_code]['rooms']  = intval(($r->rooms!=null   ? $r->rooms : 0));
        $arr['procedure'][$r->procedure_code]['times']  = intval(($r->times!=null   ? $r->times : 0));
        $arr['procedure'][$r->procedure_code]['cal_am'] = $this->caltime(@$bookset->time_am,$r->min,$r->rooms);
        $arr['procedure'][$r->procedure_code]['cal_pm'] = $this->caltime(@$bookset->time_pm,$r->min,$r->rooms);
        $arr['procedure'][$r->procedure_code]['cal_ot'] = $this->caltime(@$bookset->time_ot,$r->min,$r->rooms);
        Mongo::table("tb_bookset_department")
        ->where("department_code",$r->depID)
        ->update($arr);
    }

    public function caltime($period,$min,$room){
        $val = 0;
        try {
            $val = ((int) $period)/((int) $min)*((int) $room);
        } catch (\Throwable $th) {
            //throw $th;
        }
        $val = intval($val);
        return $val;
    }


}
