<?php

namespace App\Http\Controllers\EndoINDEX;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use App\Models\Booking;
use App\Models\patient;
use App\Models\Mongo;
use App\Models\Department;
use App\Models\Datacase;


class ScreeningController extends Controller{




    public function show($id){
        // // ทองผาภูมิ :11283
        // http://medicaendo.com/sc/screening/11283
        // // บ่อพลอย:  11280
        // http://medicaendo.com/sc/screening/11280

        // พหลพล : 10731
        // http://medicaendo.com/sc/screening/10731
        // มะการัก : 10732
        // http://medicaendo.com/sc/screening/10732
        // สมเด็จ 19 : 11282
        // http://medicaendo.com/sc/screening/11282


        $view['id'] = $id;
        $view['hospitalcode'] = $id;
        $view['doctor']  = Mongo::table("users")
        ->orderBy('user_firstname')
        ->where('user_type',"doctor")
        ->get();
        return view("screening.show",$view);
    }






    public function store(Request $r){
        switch($r->event){
            case "create_booking"   :   $this->create_booking($r);      break;
            case "patient_import"   :   $this->patient_import($r);      break;
            case "inputcase"        :   $this->inputcase($r);    return redirect(url("screening/$r->hospitalcode"));       break;
        }
    }


    public function inputcase($r){
        // $this->dateformat($r->appointment);
        $this->patientCHECK($r);
        $cid = $this->createcase($r);

    }


    public function patientCHECK($r){
        // dd($r);
        $val['pic']                 = "";
        $val['allergic']            = @$r->allergic."";
        $val['congenital_disease']  = @$r->congenital_disease."";
        $val['emer_name']           = @$r->emer_name."";
        $val['emer_tel']            = @$r->emer_tel."";
        $val['firstname']           = @$r->firstname;
        $val['middlename']          = @$r->middlename.'';
        $val['lastname']            = @$r->lastname;
        $val['phone']               = @$r->phone;
        $val['an']                  = @$r->an;
        $val['citizen']             = @$r->citizen;
        $val['email']               = @$r->email;
        $val['prefix']              = @$r->prefix;
        $val['hn']                  = @$r->hn;
        $val['gender']              = @$r->gender;
        $val['birthdate']           = $this->birthdateMOCK($r->age);
        $val['nationality']         = @$r->nationality;
        $val['regis_date']          = @$r->regis_date;
        $val['regis_time']          = @$r->regis_time;

        $patient = Mongo::table("tb_patient")->where("hn",$r->hn)->first();
        if(!isset($patient['hn'])){
            $lastid = Mongo::table('tb_patient')->insertGetId($val);
        }

    }


    public function createcase($r){

        $tb_patient = Mongo::table('tb_patient')->where("hn",$r->hn)->first();
        if(!isset($tb_patient['hn'])){
            $val['hn']                  = $tb_patient['hn'];
            $val['prefix']              = $tb_patient['pname'];
            $val['firstname']           = $tb_patient['fname'];
            $val['middlename']          = @$tb_patient['mname']."";
            $val['lastname']            = $tb_patient['lname'];
            $val['birthdate']           = $tb_patient['birthday'];
            $val['gender']              = $r->gender;
            $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']                 = $r->hn;
        $case['case_physicians01']  = $r->patient_surgeon;
        $case['case_procedurecode'] = array("gi002");
        $case['meet_date']          = date("Y-m-d");
        $case['meet_hour']          = "08";
        $case['room']               = "";
        $case['diagnostic']         = @$r->diagnostic;        //
        $case['diagnostic_text']    = @$r->diagnostic_text;   //
        $case['procedure_sub']      = @$r->procedure_sub;     //
        $case['procedure_subtext']  = @$r->procedure_subtext; //
        $case['bowel']              = @$r->bowel;             //
        $case = (object) $case;
        insertCASE($value, $case);
    }


    public function birthdateMOCK($age){
        $year = 2023-$age;
        $birth = $year.'-01-01';
        return $birth;
        // dd($age);
    }

    public function dateformat($appointment){
        dd($appointment);
    }




    public function create_booking($r){


        $file = $r->file('fileuser');
        if(isset($file)){
            $temppath = $file->getPathName();
            $user_data = Excel::toArray([], $temppath);
            foreach ($user_data[0] as $index=>$data) {
                if($index != 0){
                    Booking::add_book_excel($data);
                }
            }
        }

    }
    public function index(){
        $this->patient_import("");
    }

    public function patient_import($r){

        // $file = $r->file('fileuser');
        // if(isset($file)){
            // $temppath = $file->getPathName();
            $temppath = "D:\\temp\\20240405\\Colonoscopy2023v2.xlsx";
            $user_data = Excel::toArray([], $temppath);
            foreach ($user_data[0] as $index=>$data) {
                if($index != 0){
                    patient::add_patient_excel($data);
                }
            }
        // }

    }

}
