<?php

namespace App\Http\Controllers\EndoBOOK;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Datetime;
use App\Models\Mongo;

class PatientController extends Controller
{
    public function __construct(Request $r)
    {
        checklogin();
    }

    public function create(Request $r)
    {
        if (isset($r->hn) && isset($r->action)) {
            $view['patient'] = DB::table('patient')->where('hn', $r->hn)->first();
            $view['action']  = $r->action;
        }
        $view['gender']             = DB::table('dd_gender')->get();
        $view['righttotreatment']   = DB::table('dd_righttotreatment')->get();
        $view['prefix']             = DB::table('dd_prefix')->get();
        $view['nationality']        = DB::table('dd_national')->get();
        $view['month_all']          = Datetime::monthALL();
        $view['day_all']            = Datetime::dayALL();
        $view['year_all']           = Datetime::yearALL(120);
        return view('endobook.patient.create', $view);
    }


    public function store(Request $r)
    {

        if (isset($r->event)) {
            if ($r->event == 'get_idcard') {
                return $this->get_idcard($r);
            }
        }
        $arr['allergic']            = @$r->allergic . "";
        $arr['congenital_disease']  = @$r->congenital_disease . "";
        $arr['emer_name']           = @$r->emer_name . "";
        $arr['emer_tel']            = @$r->emer_tel . "";
        $patient['patient_json']    = jsonEncode($arr);

        $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 = "";
        }
        $patient['firstname']          = @$r->firstname;
        $patient['middlename']         = @$r->middlename . '';
        $patient['lastname']           = @$r->lastname;
        $patient['phone']              = @$r->phone;
        $patient['an']                 = @$r->an;
        $patient['citizen']            = @$r->citizen;
        $patient['pic']                = @$pic;
        $patient['email']              = @$r->email;
        $patient['prefix']             = @$r->prefix;
        $patient['hn']                 = @$r->hn;
        $patient['gender']             = @$r->gender;
        $patient['birthdate']          = @$birth;
        $patient['nationality']        = @$r->nationality;
        $patient['regis_date']         = @$r->regis_date;
        $patient['regis_time']         = @$r->regis_time;


        if ($r->event == 'edit_patient') {
            Mongo::table('tb_patient')->where('hn', $patient['hn'])->update($patient);
        } else {
            Mongo::table('tb_patient')->insert($patient);
        }


        $dates['date']     = date("Y-m-d");
        $datas['hn']       = $r->hn;
        $cid = Mongo::table('tb_casenote')->insertGetId($datas);
        return redirect("book/registration/$cid?book");
    }

    public function get_idcard($r)
    {
        $path = "D:\\allindex\\idcard\\idcard.txt";
        $dec = '[""]';
        if (file_exists($path)) {
            $size = file_get_contents($path);
            if ($size == '[""]') {
                sleep(2);
                $size = file_get_contents($path);
            }

            if ($size != '[""]') {
                $dec = jsonDecode($size);
                if (isset($dec) && $dec != '') {
                    if (isset($dec->th_name)) {
                        $fullname = explode(' ', $dec->th_name);
                    } else if (!isset($dec->th_name) && isset($dec->th_name)) {
                        $fullname = explode(' ', $dec->eng_name);
                    } else {
                        return jsonEncode($dec);
                    }
                    $fullname = array_values(array_filter($fullname));
                    $firstname = isset($fullname) ? $fullname[1] : null;
                    $lastname = isset($fullname) ? end($fullname) : null;
                    $hn = '';
                    if (isset($firstname) && isset($lastname)) {
                        $w[] = array('firstname', $firstname);
                        $w[] = array('lastname', $lastname);


                        $patient = DB::table('patient')->where($w)->first();
                        $hn = isset($patient) ? $patient->hn : '';
                    }
                    $dec->hn = $hn;
                    $dec = jsonEncode($dec);
                }
            }
        }

        echo $dec;
    }
}
