<?php

namespace App\Http\Controllers\EndoBOOK;
use App\Http\Controllers\Controller;
use App\Models\Mongo;
use App\Models\User;
use Illuminate\Http\Request;

use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use App\Models\Note;

use Illuminate\Support\Facades\Session;



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

    public function show($id,Request $r){
        $feature    = getCONFIG("feature");
        $view['hospital']            = get_hospital();
        $tb_booking     = Mongo::table("tb_booking")->where("_id" ,$id)->first();
        // dd($tb_booking);
        $nid            = Note::hndate($tb_booking['hn'] , $tb_booking['date']);

        // $view['casenote'] = $nid;

        $view['nid']   = $nid->_id;
        $view["bookid"] = $id;
        $view['prepare']    = Mongo::table("tb_booking")->where("_id" , $id)->first();
        // dd($view['prepare']);
        if(isset($r->template)){
            $template["template"] = $r->template;
            Mongo::table("tb_casenote")->where("_id",$id)->update($template);
        }
        $view['nurse'] = User::group("nurse");



        $tb_casenote            = Mongo::table('tb_casenote')
                                    ->where('_id',$id)
                                    ->first();
                                    unset($tb_casenote['_id']);

        $tb_booking             = Mongo::table('tb_booking')
                                    ->where('_id',$id)
                                    ->first();
        // dd($tb_booking['treatment']);
        $view['tb_booking']     = $tb_booking;

        $tb_treatmentcoverage   = (isset($tb_booking['treatment'])) ? (object) Mongo::table('tb_treatmentcoverage')->where('code', $tb_booking['treatment'])->first() : null;
        // dd($tb_treatmentcoverage);
        $view['treatment']      = (isset($tb_treatmentcoverage))    ? $tb_treatmentcoverage->name : '';

        unset($tb_booking['_id']);
        //cloudreceive
        $url                    = "http://medicaendo.com/api/cloudreceive";
        $post['noteid']         = $id;
        $post['event']          = "prepare_receive";
        $post['hospital_code']  = "10672";

        $post['json']           = jsonEncode(array_merge($tb_casenote??[],$tb_booking??[]));
        connectwebPOST($url,$post);

        // if(isset($r->template)){
            // $view['template'] = (object) Mongo::table("tb_preparetemplate")->where("templatename",$r->template)->first();
            // dd($view['template']);
        // }else{
            // $view['template'] = (object) Mongo::table("tb_preparetemplate")->first();
            // dd("bbbbb");


        $view['template_all'] = Mongo::table("tb_preparetemplate")->get();
        // dd($view);
           $view['patient']        = (object) Mongo::table('tb_patient')
        ->where("hn",$tb_booking['hn'])
        ->first();

        // dd($view);
        return view('endobook.prepare.show',$view);
    }

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

    public function delete_template($r){
        $w[] = array('_id', $r->temp_id);
        Mongo::table('tb_preparetemplate')->where($w)->delete();
        return redirect(url('').'/book/prepare/'.$r->noteid);
    }


    public function edit($id,Request $r){
    // dd($id);
        $view['hospital']   = get_hospital();
        // $view["nid"]        = $id;

        $view["bookid"]     = $id;

        $tb_booking             = Mongo::table('tb_booking')
                                    ->where('_id',$id)
                                    ->first();
        $view['tb_booking']     = $tb_booking;
        $tb_treatmentcoverage   = (isset($tb_booking['treatment'])) ? (object) Mongo::table('tb_treatmentcoverage')->where('code', $tb_booking['treatment'])->first() : null;
        $view['treatment']      = (isset($tb_treatmentcoverage))    ? $tb_treatmentcoverage->name : '';
        $view['patient']        = (object) Mongo::table('tb_patient')
                                    ->where("hn",$tb_booking['hn'])
                                    ->first();

        $view['prepare']        = $view['tb_booking']['prepare'] ?? [];
        $view['nurse'] = User::group("nurse");


        $view['casenote'] = (array) Note::hndate($tb_booking['hn'] , $tb_booking['date']);
        $view['nid'] = $view['casenote']['_id'];
        $hospital_name = $view['hospital']->hospital_name;


        $files = fileconfig("views\\pdfhead\\$hospital_name\\prepare");
        $files2 = [];
        if (is_dir($files)) {
            $files2 = array_diff(scandir($files), array('.', '..'));
        }

        // dd($files2);

        $type = "niflec_am";
        if(isset($r->template)){$type=$r->template;}

        $body = "pdfhead/";
        $body.= configTYPE('pdf', 'pdf_folder_head');
        $body.= "/prepare/";


        $view['header_path'] = $body."header";
        // $view['footer']      = $body."footer";
        $body.= "$type";


        return view($body,$view);
    }

}
