<?php

namespace App\Http\Controllers\EndoBOOK;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\casemedication;

use App\Models\Department;
use App\Models\Mongo;

use App\Models\Datacase;

class PatientHistoryController extends Controller
{

    public function store(Request $r)
    {

        if(isset($r->event)){
            if($r->event=="edit_patient")   {return $this->edit_patient($r);}
            if($r->event=="patient_create") {return $this->patient_create($r);}
            if($r->event=="patient_check")  {return $this->patient_check($r);}
        }
    }

    public function create(){
        $view['feature']    = getCONFIG("feature");
        $view['month_all']  = ['01','02','03','04','05','06','07','08','09','10','11','12'];
        $view['day_all']    = ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31'];
        $view['year_all']   = array();
        for($i=intval(date('Y')+543);$i>=intval(date('Y')+543)-120;$i--){array_push($view['year_all'],$i);}

        return view('endobook.patienthistory.create',$view);
    }





    public function show($id){
        $view['day']            = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'];
        
        return view('endobook.patienthistory.show',$view);
    }







    public function patient_create($r){
        $birth =  ($r->birthyear-543). "-" . $r->birthmonth . "-" . $r->birthday;
        if ($r['myHiddenField'] != null) {
            $data              = $r['myHiddenField'];
            list($type, $data) = explode(';', $data);
            list(, $data)      = explode(',', $data);
            $data              = base64_decode($data);
            file_put_contents('pic_patient/' . $r->hn . '.png', $data);
            $pic = $r->hn . '.png';
        } else {
            $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['pic']                 = $pic;
        $val['email']               = $r->email;
        $val['prefix']              = $r->prefix;
        $hn                         =  str_replace(".","",$r->hn);
        $val['hn']                  = $hn;
        $val['gender']              = $r->gender;
        $val['birthdate']           = $birth;
        $val['nationality']         = $r->nationality;
        $val['regis_date']          = $r->regis_date;
        $val['regis_time']          = $r->regis_time;
        $lastid = Mongo::table('tb_patient')->insertGetId($val);

        return redirect("book/patienthistory/$hn");

    }






}



