<?php
namespace App\Http\Controllers\Lumina;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

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

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

    public function index(){
        $view['mmm'] = "mmmm";
        return view('lumina.pdf.index',$view);
        // $id = '65b8beeeca4f75a2ff040e1b';
        // $data['hospital']            = get_hospital();
        // $data['id'] = Mongo::table('record_pdf')->where('rid',$id)->first();
        // $data['i']  = 0;
        // $data['case'] = Mongo::table('tb_case')->where('_id',$id)->first();

        // if(isset($data['case']['procedure'])){
        //     $data['procedure'] = Mongo::table('tb_procedure')->where('code',$data['case']['procedure'])->first();
        // }
        // return view('lumina.print.portrait.2x1',$data);
        $val['hospital']            = get_hospital();


    }


    public function show($id){
        // dd(1);
        $data['hospital']            = get_hospital();

        $data['id'] = Mongo::table('record_pdf')->where('rid',$id)->first();
        $data['i']  = 0;
        $data['case'] = Mongo::table('tb_case')->where('_id',$id)->first();

        // dd($data);
        if(isset($data['case']['procedure'])){
            $data['procedure'] = Mongo::table('tb_procedure')->where('code',$data['case']['procedure'])->first();
        }

        if(isset($data['case']['case_hn']) || isset($data['case']['hn'])){
            $hn = isset($data['case']['case_hn']) ? $data['case']['case_hn'] : $data['case']['hn'];
            $data['patient'] = (object) Mongo::table('tb_patient')->where('hn', strval($hn))->first();
        }
        // dd($data);

        $recorder = (object) Mongo::table('tb_lumina')->where('id', 1)->first();
        $recorder->picrow = isset($recorder->picrow) ? $recorder->picrow : '1x2|Portrait';

        $view   = "recorder.print.show";
        // $recorder = getCONFIG("lumina");
        // $recorder->picrow == '1x2';
        $ex = explode('|',$recorder->picrow);
        // dd($ex);
        $picrow = $ex[0];
        $orientation = $ex[1];

        // dd($orientation == "Landscape");
        if($orientation == "Landscape"){
            if($picrow == "1x1"){$view   = "lumina.print.landscape.1x1";}
            if($picrow == "1x2"){$view   = "lumina.print.landscape.1x2";}
            if($picrow == "2x2"){$view   = "lumina.print.landscape.2x2";}
            if($picrow == "2x3"){$view   = "lumina.print.landscape.2x3";}
            $pdf    = PDF::loadView($view,$data);
            $pdf->setPaper('A4','landscape');
        }else{
            if($picrow == "1x2"){$view   = "lumina.print.portrait.1x2";}
            if($picrow == "2x2"){$view   = "lumina.print.portrait.2x2";}
            if($picrow == "2x3"){$view   = "lumina.print.portrait.2x3";}
            if($picrow == "3x3"){$view   = "lumina.print.portrait.3x3";}
            $pdf    = PDF::loadView($view,$data);
            $pdf->setPaper('A4');
        }

        $case = (object) $data['case'];
        $status = get_config_scope();
        if($status){
            $hn         = isset($case->case_hn) ? $case->case_hn : $case->hn;
            $folderdate = isset($case->appointment) ? explode(' ', $case->appointment)[0] : '';
            $pdfversion = "$id.pdf";
            $folderdate = isset($case->appointment) ? explode(' ', $case->appointment)[0] : $case->appointment;
            if (!file_exists(htdocs("store/$hn/$folderdate/pdf"))) {
                mkdir(htdocs("store/$hn/$folderdate/pdf"), 0777, true);
            }
            $content    = $pdf->Output();
            $pdfpath = htdocs("store/$hn/$folderdate/pdf/$pdfversion");
            file_put_contents($pdfpath, $content);
        }
        // dd($data);
        // return $pdfpath;
        return @$pdf->stream();

    }

    public function store(Request $r){
        $val['hospital']            = get_hospital();
        if($r->event == 'get_image_pdf'){
            $check = Mongo::table('record_pdf')->where('rid',$r->id)->count();
            $data['img']    = jsonDecode($r->images);
            if($check===0){
                $data['rid']    = $r->id;
                Mongo::table('record_pdf')->insert($data);
            }else{
                Mongo::table('record_pdf')->where('rid',$r->id)->update($data);
            }
            $status = get_config_scope();
            if($status){
                $this->show($r->id);
            }
        }
    }


    public function edit($id){
        $view['cid'] = $id;
        $case = Mongo::table("tb_case")->where("_id",$id)->first();
        $view['hn']= $case['case_hn'];
        $ex = explode(" ",$case['appointment']);
        $view['date'] = $ex[0];
        sleep(2);
        return view("Lumina.print.edit",$view);
    }


}
