<?php

namespace App\Http\Controllers\Endocapture;

use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\DB;

use Illuminate\Http\Request;
use App\Models\Mongo;

class AppointmentController extends Controller{


    public function index(){
        $appoint = DB::connection("hospital")->table("V_Medica_Procedure")->limit(50)->get();
        dd($appoint);
    }

    public function store(Request $r){
        if(isset($r->event)){
            $event = $r->event;
            if($event=="getappointment")    {$this->getappointment($r);}
            if($event=="createcase")        {$this->createcase($r); return redirect("home");}
        }
    }

    public function getappointment($r){

        try {
            $appoint = DB::connection("hospital")
            ->table("V_Medica_Procedure")
            // ->where("date_order",date("Y-m-d"))
            ->where("จุดให้บริการปัจจุบัน","LIKE","%ส่องกล้อง%")
            ->limit(20)
            ->get();
            //code...
        } catch (\Throwable $th) {
            //throw $th;
        }

        // dd($view,date("Y-m-d"),"2022-09-04");
        // +"type": "IPD"
        // +"hn": "43029945"
        // +"vnan": "650037436"
        // +"date_order": "2022-09-04"
        // +"จุดให้บริการปัจจุบัน": "ออร์โธปิดิกส์หญิงและเด็ก"
        // +"pname": "น.ส."
        // +"fname": "บัวไล"
        // +"lname": "สิงหาญ"
        // +"age": "54"
        // +"sex": "หญิง"
        // +"birthday": "1968-02-19"
        // +"oper_name": "การทำแผลเปิดขนาดใหญ่ (มากกว่า15 นาที)"
        // +"doctor_order": ""
        // +"nurse_order": "น.ส.ศิริพร  โฉมโสธร"
        // +"name": "Intraspinal abscess and granuloma"
        // +"ห้องที่ทำ": ""
        // Mongo::table("tb_appointment")->where("date_order","!=",date("Y-m-d"))->delete();


        // patientname	= "สดายุทองลอย"
        // accessionnumber	= "none"
        // patientid	= "1234"
        // modality	= "ES"
        // aetitle		= "*"
        // date		= "20230917"
        // proceduredescription = "EGD"
        // physicianname	= "สดายุ ทองลอย"
        // patient_nameTH	= "สดายุทองลอย"
        // status		= "register"
        // patient_status 	= "success"
        // import_status	= ""
        // visitno 	= ""
        // case_id 	= ""



        foreach(isset($appoint)?$appoint:[] as $data){
            $val["patientname"]         = "$data->fname $data->lname";
            $val["accessionnumber"]     = "";
            $val["patientid"]           = $data->hn;
            $val["modality"]            = "ES";
            $val["aetitle"]             = "*";
            $val["date"]                = str_replace("_","",$data->date_order);
            $val["proceduredescription"]= $data->oper_name;
            $val["physicianname"]       = $data->doctor_order;
            $val["patient_nameTH"]      = "$data->fname $data->lname";
            $val["status"]              = "register";
            $val["patient_status"]      = "success";
            $val["import_status"]       = "";
            $val["visitno"]             = "";
            $val["case_id"]             = "";

            $worklist = Mongo::table("tb_caseworklist")
            ->where("date"                  ,$val["date"])
            ->where("patientid"             ,$val["patientid"])
            ->where("proceduredescription"  ,$val["proceduredescription"])
            ->first();

            if(!isset($worklist["_id"])){
                Mongo::table("tb_caseworklist")->insert($val);
            }



            // $val["type"]        = $data->type;
            // $val["hn"]          = $data->hn;
            // $val["vnan"]        = $data->vnan;
            // $val["date_order"]  = $data->date_order;
            // $val["point"]       = $data->จุดให้บริการปัจจุบัน;
            // $val["pname"]       = $data->pname;
            // $val["fname"]       = $data->fname;
            // $val["lname"]       = $data->lname;
            // $val["age"]         = $data->age;
            // $val["sex"]         = $data->sex;
            // $val["birthday"]    = $data->birthday;
            // $val["oper_name"]   = $data->oper_name;
            // $val["doctor_order"]= $data->doctor_order;
            // $val["nurse_order"] = $data->nurse_order;
            // $val["name"]        = $data->name;
            // $val["room"]        = $data->ห้องที่ทำ;
            // $val["status"]      = "register";

            // $tb_appointment = Mongo::table('tb_appointment')
            // ->where('hn',$data->hn)
            // ->where('oper_name',$data->oper_name)
            // ->first();


            // if(!isset($tb_appointment['hn'])){
            //     Mongo::table("tb_appointment")->insert($val);
            // }
        }
        // $view['appoint'] = Mongo::table("tb_appointment")
        // ->where("status","registered")
        // ->limit(20)
        // ->get();

        // echo view("appointment.table_appointment" ,$view)->render();
    }




    public function createcase($r){
        // "type" => "OPD"
        // "hn" => "65019965"
        // "vnan" => "650905084212"
        // "date_order" => "2022-09-05"
        // "point" => "จุดซักประวัติศูนย์ส่องกล้อง"
        // "pname" => "นาย"
        // "fname" => "ผ่อง"
        // "lname" => "ธูปคำ"
        // "age" => "62"
        // "sex" => "ชาย"
        // "birthday" => "1960-04-12"
        // "oper_name" => "Colonoscopy"
        // "doctor_order" => "พญ.วรมน  เจียมศรีพงษ์"
        // "nurse_order" => "พญ.วรมน  เจียมศรีพงษ์"
        // "name" => "Polyp of colon"
        // "room" => "จุดซักประวัติศูนย์ส่องกล้อง"
        // "status" => "registered"

        // dd($r);
        // "event" => "createcase"
        // "date" => "2023-09-05"
        // "appid" => "64f6bb299a2be19ec006f56b"
        // "formtype" => null
        // "righttotreatment" => null
        // "_token" => "fAi4a4W4w8kDYfrWyvuBkXPTDkYlEXvVuR0tERhX"
        // "patient_surgeon" => "34"
        // "patient_operation" => array:1 [▼
        //   0 => "EGD"
        // ]
        $tb_appointment = Mongo::table('tb_appointment')->where('_id',$r->appid)->first();



        // dd($tb_appointment);
        if(isset($tb_appointment['hn'])){
            $tb_patient = Mongo::table('tb_patient')->where("hn",$tb_appointment['hn'])->first();
            if(!isset($tb_patient['hn'])){
                $val['hn']                  = $tb_appointment['hn'];
                $val['prefix']              = $tb_appointment['pname'];
                $val['firstname']           = $tb_appointment['fname'];
                $val['middlename']          = @$tb_appointment['mname']."";
                $val['lastname']            = $tb_appointment['lname'];
                $val['birthdate']           = $tb_appointment['birthday'];
                $val['gender']              = genderCODE($tb_appointment['sex']);
                $val['allergic']            = "";
                $val['congenital_disease']  = "";
                $val['emer_name']           = "";
                $val['emer_tel']            = "";
                $val['phone']               = "";
                $val['an']                  = "";
                $val['citizen']             = "";
                $val['pic']                 = "";
                $val['email']               = "";
                $val['nationality']         = "";
                $val['regis_date']          = date("Y-m-d");
                $val['regis_time']          = date("H:i:s");
                Mongo::table('tb_patient')->insertGetId($val);
            }
        }

        $value                      = array();
        $case                       = array();
        $case['hn']                 = $tb_appointment['hn'];
        $case['case_physicians01']  = $r->patient_surgeon;
        $case['case_procedurecode'] = $r->patient_operation;
        $case['meet_date']          = date("Y-m-d");
        $case['meet_hour']          = "08";
        $case['room']               = "";
        $case = (object) $case;
        insertCASE($value, $case);

        $new['status'] = "booking";
        Mongo::table('tb_appointment')->where('_id', $tb_appointment['_id'])->update($new);


    }





}
