<?php
namespace App\Http\Controllers\Lumina;
use App\Http\Controllers\Controller;
use App\Models\Datacase;
use App\Models\Datetime;
use App\Models\Department;
use Illuminate\Http\Request;

use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use Image;
use Exception;
use App\Models\Mongo;
use stdClass;

class PatientController extends Controller
{
    public function __construct() { login_lumina(); }

    public function index(){

    }


    public function show($id){
        $view['id']                 = @$id;
        $view['case']               = (object) Mongo::table('tb_case')->where('_id', @$id)->first();
        $view['doctor']             = Department::userActive('doctor');
        $view['month_all']          = Datetime::monthALL();
        $view['day_all']            = Datetime::dayALL();
        $view['year_all']           = Datetime::yearALL(120);
        $view['tb_procedure']       = (object) Mongo::table('tb_procedure')->get();

        return view('lumina.patient.show', $view);
    }



    public function edit($id){
        // dd($id);
        $view['case']               = (object) Mongo::table('tb_case')->where('_id', $id)->first();
        $view['patient']            = (object) Mongo::table('tb_patient')->where('hn', $view['case']->case_hn)->first();

        if($view['patient']->birthdate){
            $ex = explode("-",$view['patient']->birthdate);
            $view['byear'] = $ex[0]+543;
            $view['bmonth'] = $ex[1];
            $view['bdate'] = $ex[2];
        }else{
            $view['byear'] = date("Y");
            $view['bmonth'] = date("m");
            $view['bdate'] = date("d");
        }




        $view['doctor']             = Department::userActive('doctor');
        $view['month_all']          = Datetime::monthALL();
        $view['day_all']            = Datetime::dayALL();
        $view['year_all']           = Datetime::yearALL(120);
        $view['tb_procedure']       =  (object) Mongo::table('tb_procedure')->get();
        return view('lumina.patient.edit', $view);
    }

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


    public function move_instantcase($r){
        // create patient first if do not have
        $w[0] = array('hn', $r->hn);
        $tb_patient = Mongo::table('tb_patient')->where($w)->first();
        if(!isset($tb_patient)){
            $val['hn'] = $r->hn;
            $val['firstname'] = $r->firstname;
            $val['lastname'] = $r->lastname;
            $val['prefix'] = "" ;
            $val['gender'] = intval($r->gender);
            $val['date'] = (date('Y') - intval($r->age)). "-01-01";
            $this->create_patient($r->hn, '', $val);
        }

        // how to get accession no and visit no ? !!IMPORTANT!!
        $data = new stdClass();
        $data->move_hn = @$r->hn."";
        $data->case_id = @$r->case_id."";
        $data->folderdate = @$r->folderdate."";
        $data->procedure_code = @$r->case_procedurecode;
        $data->physician_id = @$r->case_physicians01;
        $newcid = move_case($data);
        // need test this if is send to server
        // $this->sendfile_server($r->hn);
        return redirect(url("lumina/storage/$newcid"));
    }

    function sendfile_server($hn){
        shell_exec("D:/allindex/recorder/other/ftp/client.py $hn");
    }

    public function create_patient($hn, $date, $val=null){
        $i['firstname'] = isset($val) ? @$val['firstname'].""  : 'test';
        $i['lastname']  = isset($val) ? @$val['lastname'].""   : 'test';
        $i['prefix']    = isset($val['prefix']) ? @$val['prefix']."" : 'นาย';
        $i['hn']        = isset($val) ? @$val['hn'].""         : $hn;
        $i['gender']    = isset($val) ? @$val['gender'].""     : 1;
        $i['birthdate'] = isset($val) ? @$val['date'].""       : $date;
        Mongo::table('tb_patient')->insert($i);
    }

    public function edit_patient(Request $r){



        if(isset($r->year)){
            $year   = $r->year??date("Y");
            $month  = $r->month??date("m");
            $date   = $r->date??date("d");
            $year = $year-543;
            $birthdate = "$year-$month-$date";
        }else{
            $birthdate = @$r->birthdate."";
        }



        try{
            // update patient
            $u['prefix']    = @$r->prefix."";
            $u['firstname'] = @$r->firstname."";
            $u['lastname']  = @$r->lastname."";
            $u['gender']    = intval($r->gender);
            $u['birthdate'] = $birthdate;
            $w[0] = array('hn', $r->hn);
            Mongo::table('tb_patient')->where($w)->update($u);
            $tb_patient = (object) Mongo::table('tb_patient')->where($w)->first();
        } catch(\Exception $e) {}

        try{
            // update case
            $u1['patientname']      = @$r->prefix."".@$r->firstname." ".@$r->lastname."";
            $u1['gender']           = @$r->gender."";
            $u1['age']              = @$r->age."";
            $u1['doctorname']       = @$r->case_physicianname."";
            $u1['procedurename']    = @$r->case_procedurename."";

            if(isset($r->case_physiciancode)){
                if($r->case_physiciancode!=""){
                    $u1['case_physicians01'] = intval($r->case_physiciancode);
                }
            }

            if(isset($r->case_procedurecode)){
                if($r->case_procedurecode!=""){
                    $u1['case_procedurecode'] = $r->case_procedurecode;
                }
            }

            $w[0] = array('_id', $r->ase_id);
            Mongo::table('tb_case')->where('_id', $r->case_id)->update($u1);

            // update tb_lumina
            Mongo::table('tb_lumina')->where('id', 1)->update($u1);
        } catch(\Exception $e){}

        return redirect(url('lumina/storage/'.$r->case_id));

    }
}
