<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use PDF;
use App\Models\Datacase;
use App\Models\Procedure;
use App\Models\Mongo;
use App\Models\casemedication;
use App\Models\Fileconfig;

class PDFController extends Controller
{

    public function change_pdf($r){
        // dd($r->all());
        $val['pdftype'] = $r->pdftype;
        Mongo::table("tb_case")->where("_id",$r->cid)->update($val);
    }


    public function show($id)
    {
        $val['casedata']            = Datacase::first($id);
        $val['tb_department']       = (object) Mongo::table('tb_department')->where('department_code' , uget('email'))->first();

        // dd($val);
        $apppoint                   = explode(" ", $val['casedata']->appointment);
        $val['folderdate']          = $apppoint[0];
        $val['hospital']            = getCONFIG("hospital");

        // dd($val);
        $val['patient']             = (object) Mongo::table("tb_patient")->where("hn", @$val['casedata']->case_hn)->first();
        $val['procedure']           = (object) Mongo::table("tb_procedure")->where("code", @$val['casedata']->case_procedurecode)->first();
        $val['pdfnameshow']         = Procedure::pdfnameshow($val['procedure']);
        $str                        = file_get_contents("D:/laragon/htdocs/config/project/admin.txt");
        $connection                 = jsonDecode($str);
        $val['img_first']           = 8;
        $val['tb_casemedication']   = CASEMEDICATION::checkdata($val['casedata'], '');
        if (isset($connection->pdf_img)) {
            $val['img_first'] = $connection->pdf_img;
        }
        $val['body_line']        = '8px';
        $val    = $this->photoselect($val);
        $val    = $this->pic_draw($val);

        $val    = $this->check_typepdf($val);
        $val    = $this->check_department($val);
        $camera_id          = @$val['casedata']->cysto_serial . '';
        $tb_scope           = (object) Mongo::table("tb_scope")->where("scope_id", intval($camera_id))->first();
        $val['casedata']->cameraname = isset($tb_scope->scope_name) ? $tb_scope->scope_name : '';


        // dd($val['pdftype']);
        if ($val['pdftype'] == "procedure") {
            $val = $this->procedure($val);
        }
        if ($val['pdftype'] == "onlypicture") {
            $val = $this->onlypicture($val);
        }
        if ($val['pdftype'] == "largepicture") {
            $val = $this->largepicture($val);
        }
        if ($val['pdftype'] == "prc_std7") {
            $val = $this->prc_std7($val);
        }
        if ($val['pdftype'] == "procedure_custom") {
            $val = $this->procedure_custom($val);
        }
        if ($val['pdftype'] == "no_writing") {
            $val = $this->no_writing($val);
        }
        if ($val['pdftype'] == "long_writing") {
            $val = $this->long_writing($val);
        }
        if ($val['pdftype'] == "long_image") {
            $val = $this->long_image($val);
        }
        if ($val['pdftype'] == "drawing") {
            $val = $this->drawing($val);
            // dd("msdfdfdfdf");
        }
        if ($val['pdftype'] == "pathology") {
            $val = $this->pathology($val);
        }
        if ($val['pdftype'] == "discharge") {
            $val = $this->discharge($val);
        }
        if ($val['pdftype'] == "accessory") {
            $val = $this->accessory($val);
        }
        $val    = $this->onepage($val);
        $val    = $this->html($val);
        $namefile = @$val['casedata']->case_hn . "_" . date('Ymd') . "_" . @$val['procedure']->name . ".pdf";
        $pdf            = PDF::loadHtml($val['html']);
        $pdf->add_info('Title', "$namefile");
        $pdf->getDomPDF()->set_option('isFontSubsettingEnabled', true);
        // $this->savepdf($val, $pdf);
        return $pdf->stream($namefile);
    }



    public function exportpdf($id,$r)
    {
        $val['casedata']            = Datacase::first($id);
        $val['tb_department']       = (object) Mongo::table('tb_department')->where('department_name' , $r->department)->first();

        // dd($val);
        $apppoint                   = explode(" ", $val['casedata']->appointment);
        $val['folderdate']          = $apppoint[0];
        $val['hospital']            = getCONFIG("hospital");

        // dd($val);
        $val['patient']             = (object) Mongo::table("tb_patient")->where("hn", @$val['casedata']->case_hn)->first();
        $val['procedure']           = (object) Mongo::table("tb_procedure")->where("code", @$val['casedata']->case_procedurecode)->first();
        $val['pdfnameshow']         = Procedure::pdfnameshow($val['procedure']);
        $str                        = file_get_contents("D:/laragon/htdocs/config/project/admin.txt");
        $connection                 = jsonDecode($str);
        $val['img_first']           = 8;
        $val['tb_casemedication']   = CASEMEDICATION::checkdata($val['casedata'], '');
        if (isset($connection->pdf_img)) {
            $val['img_first'] = $connection->pdf_img;
        }
        $val['body_line']        = '8px';
        $val    = $this->photoselect($val);
        $val    = $this->pic_draw($val);

        $val    = $this->check_typepdf($val);
        $val['department'] = $r->department;
        // $val    = $this->check_department($val);
        $camera_id          = @$val['casedata']->cysto_serial . '';
        $tb_scope           = (object) Mongo::table("tb_scope")->where("scope_id", intval($camera_id))->first();
        $val['casedata']->cameraname = isset($tb_scope->scope_name) ? $tb_scope->scope_name : '';


        // dd($val['pdftype']);
        if ($val['pdftype'] == "procedure") {
            $val = $this->procedure($val);
        }
        if ($val['pdftype'] == "onlypicture") {
            $val = $this->onlypicture($val);
        }
        if ($val['pdftype'] == "largepicture") {
            $val = $this->largepicture($val);
        }
        if ($val['pdftype'] == "prc_std7") {
            $val = $this->prc_std7($val);
        }
        if ($val['pdftype'] == "procedure_custom") {
            $val = $this->procedure_custom($val);
        }
        if ($val['pdftype'] == "no_writing") {
            $val = $this->no_writing($val);
        }
        if ($val['pdftype'] == "long_writing") {
            $val = $this->long_writing($val);
        }
        if ($val['pdftype'] == "long_image") {
            $val = $this->long_image($val);
        }
        if ($val['pdftype'] == "drawing") {
            $val = $this->drawing($val);
            // dd("msdfdfdfdf");
        }
        if ($val['pdftype'] == "pathology") {
            $val = $this->pathology($val);
        }
        if ($val['pdftype'] == "discharge") {
            $val = $this->discharge($val);
        }
        if ($val['pdftype'] == "accessory") {
            $val = $this->accessory($val);
        }
        // dd($val);
        $val    = $this->onepage($val);
        $val    = $this->html($val);
        $namefile = @$val['casedata']->case_hn . "_" . date('Ymd') . "_" . @$val['procedure']->name . ".pdf";
        $pdf            = PDF::loadHtml($val['html']);
        $pdf->add_info('Title', "$namefile");
        $pdf->getDomPDF()->set_option('isFontSubsettingEnabled', true);



        $this->get_pdffile($val, $pdf);
        return $pdf->stream($namefile);
    }




    public function get_pdffile($val, $pdf)
    {
        $tb_case    = (object) $val['casedata'];
        $cid        = $tb_case->_id;
        $folderdate = $val['folderdate'];
        $hn         = isset($tb_case->case_hn) ? $tb_case->case_hn : $tb_case->hn;
        $content    = $pdf->output();
        $datetime   = date('YmdHis');
        $pdfversion = $hn . '_' . $datetime . '.pdf';
        $makedirfull = makedirfull("D:/pdfall/".$val['casedata']->appointment_date);

        file_put_contents("D:/pdfall/".$val['casedata']->appointment_date."/".$pdfversion, $content);
    }
















    public function savepdf($val, $pdf)
    {
        $tb_case    = (object) $val['casedata'];
        $cid        = $tb_case->_id;
        $folderdate = $val['folderdate'];
        $hn         = isset($tb_case->case_hn) ? $tb_case->case_hn : $tb_case->hn;
        $content    = $pdf->output();
        $datetime   = date('YmdHis');
        $pdfversion = $hn . '_' . $datetime . '.pdf';
        makedirfull(htdocs("store/$hn/$folderdate/pdf"));
        file_put_contents(htdocs("store/$hn/$folderdate/pdf/$pdfversion"), $content);
        makedirfull(htdocs("store/$hn/temp"));
        file_put_contents(htdocs("store/$hn/temp/temp.pdf"), $content, LOCK_EX);
        if (isset($tb_case->pdfcreate)) {
            if (!$tb_case->pdfcreate) {
                $log['case_from'] = $tb_case->caseuniq;
                $log['case_hn']   = $hn;
                $log['path'] = htdocs("store/$hn/$folderdate/pdf/$pdfversion");
                logdata('tb_logpdf', uid(), 'create pdf', $log);

                $temp['user']   = uget("name");
                $temp['when']   = date('Y-m-d H:i:s');
                $temp['pdf']    = $pdfversion;
                if(isset($tb_case->case_pdfversion)){
                    $array      = $tb_case->case_pdfversion;
                    $array[]    = (object) $temp;
                }else{
                    $array[]    = (object) $temp;
                }


                $arr['pdfcreate']       = true;
                $arr['case_pdfversion'] = $array;
                Mongo::table('tb_case')->where('_id', $cid)->update($arr);

            }



        }

        $mydir = "D:/laragon/htdocs/pdf/";
        if(file_exists($mydir)){
            file_put_contents($mydir.$hn."_".$tb_case->caseuniq.'.pdf', $content);
        }
    }


    public function pdfendosmart(Request $r)
    {
        $view   = "Recorder.pdf.index";
        $pdf    = PDF::loadView($view, $r);
        return @$pdf->stream();
    }




    public function html($val)
    {
        $val['html'] = '';
        $view = view($val['view'])->with($val);
        $val['html'] .= $view->render();
        $view = view($val['view2'])->with($val);
        $val['html'] .= $view->render();
        return $val;
    }



    public function pic_draw($val)
    {
        $procedure_pic      = $this->change_procedure_pic($val);
        $hn                 = @$val['casedata']->case_hn . "";
        $folderdate         = $val['folderdate'];
        $procedure_picori   = fileconfig("procedure/" . @$procedure_pic . ".jpg");
        $folder_hnpath      = htdocs("store/$hn/$folderdate/");
        $procedure_piccopy  = $folder_hnpath . @$val['casedata']->caseuniq . ".jpg";
        $val['pic_draw']    = @$val['casedata']->caseuniq . ".jpg";
        makedir($folder_hnpath);
        if (!file_exists($procedure_piccopy)) {
            copy($procedure_picori, $procedure_piccopy);
        }
        return $val;
    }

    public function change_procedure_pic($val)
    {
        $procedure_pic      = isset($val['casedata']->procedure_pic) ? $val['casedata']->procedure_pic : $val['casedata']->procedurename;
        $procedure_pic      = str_replace(' ', '_', $procedure_pic);
        return $procedure_pic;
    }

    public function photoselect($val)
    {
        $case_photo         = @$val['casedata']->photo;
        $val['photoselect'] = array();
        foreach (isset($case_photo) ? $case_photo : array() as $photo) {
            $photo = (object) $photo;
            if (isset($photo->ns)) {
                if ($photo->ns > 0 && $photo->st != 10) {
                    $val['photoselect'][$photo->ns]['nu'] = $photo->nu;
                    $val['photoselect'][$photo->ns]['ns'] = $photo->ns;
                    $val['photoselect'][$photo->ns]['na'] = $photo->na;
                    $val['photoselect'][$photo->ns]['sc'] = $photo->sc;
                    $val['photoselect'][$photo->ns]['st'] = $photo->st;
                    $val['photoselect'][$photo->ns]['tx'] = $photo->tx;
                }
            }
        }
        ksort($val['photoselect']);
        return $val;
    }
    public function accessory($val)
    {
        if (webconfig('pdf_procedure_pic') == "true") {
            $val['showprocedure']   = true;
            $val['num1']            = 7;    //8 จำนวนรูปที่แสดงในหน้าแรก
            $val['num2']            = 8;    //9 จำนวนรูปที่แสดงในหน
        } else {
            $val['showprocedure']   = false;
            $val['num1']            = 8;    //8 จำนวนรูปที่แสดงในหน้าแรก
            $val['num2']            = 9;    //9 จำนวนรูปที่แสดงในหน
        }
        $val['size_page']           = '50%';
        $val['right_page']          = '48%';
        $val['position']            = 1;
        $val['picperrow']           = 4;
        $val['right_page']          = '65%';
        $val['showprocedure']       = true;
        $val['num1']                = 1000;    //8 จำนวนรูปที่แสดงในหน้าแรก
        $val['num2']                = 1000;    //9 จำนวนรูปที่แสดงในหน
        $val['view']                = "endocapture.pdf.page_accessory";
        $val['view2']               = "endocapture.pdf.pdf_procedure_page2";
        $val['leftpagewidth']       =  "360px";
        $val['num3']                = 1000;
        $val['picperpage']          = 1000;
        return $val;
    }
    public function procedure($val)
    {
        $val['size_page']       = '50%';
        $val['right_page']      = '45%';
        $val['position']        = 1;
        // if (webconfig('pdf_procedure_pic') == "true") {
        //     $val['showprocedure']   = true;
        //     $val['num1']        = 7;    //8 จำนวนรูปที่แสดงในหน้าแรก
        //     $val['num2']        = 8;    //9 จำนวนรูปที่แสดงในหน
        // } else {
            $val['showprocedure']   = false;
            $val['num1']        = 8;    //8 จำนวนรูปที่แสดงในหน้าแรก
            $val['num2']        = 9;    //9 จำนวนรูปที่แสดงในหน
        // }
        $val['picperrow']       = configTYPE("pdf", "pagetwo");
        $val['view']            = "endocapture.pdf.page_start";
        $val['view2']           = "endocapture.pdf.pdf_procedure_page2";
        $val['leftpagewidth']   =  "300px";
        $val['num3']        = 10;
        $val['picperpage']  = 16;
        // dd($val);
        return $val;
    }



    public function prc_std7($val)
    {
        $val['size_page']       = '50%';
        $val['right_page']      = '48%';
        $val['position']        = 1;
        $val['pdf_procedure_pic'] = true;
        // if (webconfig('pdf_procedure_pic') == "true") {
        //     $val['showprocedure']   = true;
        //     $val['num1']        = 7;    //8 จำนวนรูปที่แสดงในหน้าแรก
        //     $val['num2']        = 8;    //9 จำนวนรูปที่แสดงในหน
        // } else {
        //     $val['showprocedure']   = false;
        //     $val['num1']        = 8;    //8 จำนวนรูปที่แสดงในหน้าแรก
        //     $val['num2']        = 9;    //9 จำนวนรูปที่แสดงในหน
        // }
        $val['showprocedure']   = true;
        $val['num1']        = 7;    //8 จำนวนรูปที่แสดงในหน้าแรก
        $val['num2']        = 8;    //9 จำนวนรูปที่แสดงในหน
        $val['picperrow']       = configTYPE("pdf", "pagetwo");
        $val['view']            = "endocapture.pdf.page_start";
        $val['view2']           = "endocapture.pdf.pdf_procedure_page2";
        $val['leftpagewidth']   =  "360px";
        $val['num3']        = 10;
        $val['picperpage']  = 16;
        return $val;
    }





    public function no_writing($val)
    {
        if (webconfig('pdf_procedure_pic') == "true") {
            $val['showprocedure']   = true;
        } else {
            $val['showprocedure']   = false;
        }
        if ($val['showprocedure']) {
            $val['num1']            = $val['img_first'];    //8 จำนวนรูปที่แสดงในหน้าแรก
            $val['num2']            = $val['img_first'];    //9 จำนวนรูปที่แสดงในหน
        } else {
            $val['num1']            = $val['img_first'];    //8 จำนวนรูปที่แสดงในหน้าแรก
            $val['num2']            = $val['img_first'];    //9 จำนวนรูปที่แสดงในหน
        }
        $val['size_page']           = '50%';
        $val['right_page']          = '48%';
        $val['position']            = 1;
        $val['picperrow']           = configTYPE("pdf", "pagetwo");
        $val['right_page']          = '65%';
        $val['showprocedure']       = true;
        $val['num1']                = 1000;    //8 จำนวนรูปที่แสดงในหน้าแรก
        $val['num2']                = 1000;    //9 จำนวนรูปที่แสดงในหน
        $val['view']                = "endocapture.pdf.page_no";
        $val['view2']               = "endocapture.pdf.pdf_procedure_page2";
        $val['leftpagewidth']       = "360px";
        $val['num3']                = 1000;
        $val['picperpage']          = 1000;
        return $val;
    }




    public function long_writing($val)
    {
        $val['size_page']   = '10%';
        $val['right_page']  = '72%';
        $val['position']    = 1;
        $val['view']        = "endocapture.pdf.page_start";
        $val['view2']       = "endocapture.pdf.pdf_procedure_page2";
        $val['leftpagewidth']   =  "550px";

        if (webconfig('pdf_procedure_pic') == "true") {
            $val['showprocedure']   = true;
        } else {
            $val['showprocedure']   = false;
        }
        $val['picperrow']   = configTYPE("pdf", "pagetwo");
        if ($val['showprocedure']) {
            $val['num1']    = $val['img_first'] / 2;    //8 จำนวนรูปที่แสดงในหน้าแรก
            $val['num2']    = $val['img_first'] / 2;    //9 จำนวนรูปที่แสดงในหน
        } else {
            if ($val['img_first'] != 0) {
                $val['img_first'] = ($val['img_first'] / 2) + 1;
            }
            $val['num1']    = $val['img_first'];    //8 จำนวนรูปที่แสดงในหน้าแรก
            $val['num2']    = $val['img_first'];    //9 จำนวนรูปที่แสดงในหน
        }
        $val['num3']        = 1000; //
        $val['picperpage']  = 1000;
        return $val;
    }


    public function procedure_custom($val)
    {
        // dd($val);
        $val['showprocedure']   =  false;
        $val['size_page']       = '50%';
        $val['right_page']      = '48%';
        $val['position']        = 1;
        $val['picperrow']       = configTYPE("pdf", "pagetwo");
        $val['right_page']      = '65%';
        $val['num1']            = 1000;    //8 จำนวนรูปที่แสดงในหน้าแรก
        $val['num2']            = 0;    //9 จำนวนรูปที่แสดงในหน
        $val['view']            = "endocapture.pdf.page_new";
        $val['view2']           = "endocapture.pdf.page_blank";
        $val['leftpagewidth']   = "360px";
        $val['num3']            = 1000;
        $val['picperpage']      = 1000;
        $val['pdfshow']         = false;
        $val['pdfshownew']      = false;
        return $val;
    }


    public function largepicture($val)
    {
        $val['size_page']       = '50%';
        $val['right_page']      = '30%';
        $val['position']        = 1;
        if (webconfig('pdf_procedure_pic') == "true") {
            $val['showprocedure']   = true;
            $val['num1']        = 6;    //8 จำนวนรูปที่แสดงในหน้าแรก
            $val['num2']        = 6;    //9 จำนวนรูปที่แสดงในหน
        } else {
            $val['showprocedure']   = false;
            $val['num1']        = 6;    //8 จำนวนรูปที่แสดงในหน้าแรก
            $val['num2']        = 6;    //9 จำนวนรูปที่แสดงในหน
        }
        $val['picperrow']       = configTYPE("pdf", "pagetwo");
        $val['view']            = "endocapture.pdf.page_start";
        $val['view2']           = "endocapture.pdf.pdf_procedure_page2";
        $val['leftpagewidth']   =  "150";
        $val['num3']        = 15;
        $val['picperpage']  = 16;
        return $val;
    }
    public function long_image($val)
    {
        $val['size_page']   = '50%';
        $val['right_page']  = '50%';
        $val['position']    = 2;
        $val['view']        = "endocapture.pdf.page_start";
        $val['view2']       = "endocapture.pdf.pdf_procedure_page2";
        $val['leftpagewidth']   =  "360px";
        if (webconfig('pdf_procedure_pic') == "true") {
            $val['showprocedure']   = true;
        } else {
            $val['showprocedure']   = false;
        }
        $val['picperrow']   = configTYPE("pdf", "pagetwo");
        $val['num1']        = 0; //7
        $val['num2']        = 0; //8
        $val['num3']        = 8; //
        $val['picperpage']  = 9;
        if ($val['img_first'] != 0) {
            $val['num1'] = ($val['img_first'] / 2) - 1;
            $val['num2'] = ($val['img_first'] / 2);
        }
        return $val;
    }

    public function drawing($val)
    {
        $val['size_page']   = '60%';
        $val['right_page']  = '53%';
        $val['position']    = 2;
        $val['view']        = "endocapture.pdf.page_start";
        $val['view2']       = "endocapture.pdf.pdf_procedure_page2";
        $val['leftpagewidth']   =  "360px";
        if (webconfig('pdf_procedure_pic') == "true") {
            $val['showprocedure']   = true;
        } else {
            $val['showprocedure']   = false;
        }
        $val['picperrow']   = configTYPE("pdf", "pagetwo");
        $val['num1']        = 0; //7
        $val['num2']        = 0; //8
        $val['num3']        = 8; //
        $val['picperpage']  = 9;
        if ($val['img_first'] != 0) {
            $val['num1'] = ($val['img_first'] / 2);
            $val['num2'] = ($val['img_first'] / 2) + 1;
        }
        return $val;
    }

    public function onlypicture($val){
        $val['size_page']       = '50%';
        $val['right_page']      = '48%';
        $val['position']        = 1;
        if(webconfig('pdf_procedure_pic')=="true"){
            $val['showprocedure']   = true;
            $val['num1']        = 0;    //8 จำนวนรูปที่แสดงในหน้าแรก
            $val['num2']        = 0;    //9 จำนวนรูปที่แสดงในหน
        }else{
            $val['showprocedure']   = false;
            $val['num1']        = 0;    //8 จำนวนรูปที่แสดงในหน้าแรก
            $val['num2']        = 0;    //9 จำนวนรูปที่แสดงในหน
        }
        $val['picperrow']       = configTYPE("pdf","pagetwo");
        $val['view']            = "endocapture.pdf.page_start_onlypicture";
        $val['view2']           = "endocapture.pdf.page_blank";
        $val['leftpagewidth']   =  "0";
        $val['num3']        = 15;
        $val['picperpage']  = 16;
        $val['width']       = 300;
        $val['height']       = 200;
        // dd($val);
        return $val;
    }

    public function pathology($val)
    {
        $val['size_page']   = '50%';
        $val['right_page']  = '50%';
        $val['position']    = 2;
        $val['leftpagewidth']   =  "360px";
        if (webconfig('pdf_procedure_pic') == "true") {
            $val['showprocedure']   = true;
        } else {
            $val['showprocedure']   = false;
        }
        $val['picperrow']     = configTYPE("pdf", "pagetwo");
        $val['num1']        = 8;
        $val['num2']        = 9;
        $val['num3']        = 8;
        $val['picperpage']  = 16;
        $val['view']        = "pdf.pdf_pathology";
        $val['view2']       = "pdf.pdf_pathology_page2";
        return $val;
    }

    public function discharge($val)
    {
        $val['size_page']   = '50%';
        $val['right_page']  = '50%';
        $val['position']    = 2;
        $val['leftpagewidth']   =  "width:10cm;";
        if (webconfig('pdf_procedure_pic') == "true") {
            $val['showprocedure']   = true;
        } else {
            $val['showprocedure']   = false;
        }
        $val['picperrow']   = configTYPE("pdf", "pagetwo");
        $val['view']        = "pdf.pdf_discharge";
        $val['view2']       = "pdf.pdf_discharge_page2";
        $val['num1']        = 4; //7
        $val['num2']        = 5; //8
        $val['num3']        = 8; //
        $val['picperpage']  = 16;
        return $val;
    }

    public function onepage($val)
    {
        // dd($val);
        $countrow = count($val['photoselect']);
        $val['num'] = ceil(($countrow - $val['num1']) / $val['picperpage']);
        $val['pagecurrent'] = 1;
        $val['pageall'] = $val['num'] + 1;
        if ($val['num'] == 0) {
            $val['onepage'] = true;
        } else {
            $val['onepage'] = false;
        }

        return $val;
    }

    public function check_typepdf($val)
    {
        if (!isset($val['casedata']->pdftype)) {
            $val['pdftype'] = "procedure";
        }else{
            $val['pdftype'] = $val['casedata']->pdftype;
        }
        return $val;
    }

    public function check_department($val)
    {
        $val['department'] = uget("department") ?? "GI";
        return $val;
    }


    public function cal_pdf_type($val)
    {
        $procedure_pic      = $this->change_procedure_pic($val);
        $val['tb_case']     = $val['casedata'];
        $count = count($val['photoselect']);
        if ($count < 5) {
            $val['pdftype'] = "long_writing";
        } else {
            $val['pdftype'] = "procedure";
        }
        $procedure_picori   = fileconfig("procedure/" . $procedure_pic . '.jpg');
        $folder_hnpath      = storePATH($val['tb_case']->case_hn . "/" . $val['folderdate'] . '/');
        $procedure_piccopy  = $folder_hnpath . $val['tb_case']->caseuniq . ".jpg";
        $val['pic_draw']    = $val['tb_case']->caseuniq . ".jpg";
        makedir($folder_hnpath);
        if (!file_exists($procedure_piccopy)) {
            copy($procedure_picori, $procedure_piccopy);
        }
        $ori    = Image::make($procedure_picori);
        $end    = Image::make($procedure_piccopy);
        if ($ori->filesize() != $end->filesize()) {
            $val['pdftype'] = "drawing";
        }
        //long_image
        foreach ($val['photoselect'] as $data => $photo) {
            if ($photo['ns'] > 0) {
                try {
                    $width  = Image::make(mePHOTO($val['tb_case']->case_hn, $photo['na'], $val['folderdate']))->width();
                    $height = Image::make(mePHOTO($val['tb_case']->case_hn, $photo['na'], $val['folderdate']))->height();
                    if ($width > ($height * 2)) {
                        $val['pdftype'] = "long_image";
                    }
                } catch (\Throwable $e) {
                }
            }
        }
        return $val;
    }
}
