<?php

namespace App\Http\Controllers\capture;

use App\Http\Controllers\Controller;
use App\Models\Mongo;
use App\Models\Department;
use MongoDB\BSON\Regex;
use App\Models\Fileconfig;

use Illuminate\Http\Request;

class CasehistoryController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index(Request $r)
    {
        $view['procedure']          = Department::procedure(uid());
        $view['doctor'] = Department::user('doctor', uid());
        unset($r['_token']);
        $view['tb_case'] = $this->search_history($r);
        // dd($r->all());
        $view['project'] = configTYPE("admin", "project");
        $view['search_hn'] = $r->search_hn ?? null;
        $view['search_physician'] = $r->sel_physician ?? null;
        $view['search_procedure'] = $r->sel_procedure ?? null;
        $view['search_keyword'] = $r->search_keyword ?? null;
        $view['date_start'] = $r->date_start ?? null;
        $view['date_end'] = $r->date_end ?? null;;
        $config             = Fileconfig::first("admin");

        $store_url          = !empty($config->endosmart_url) ? $config->endosmart_url : domainname('store');
        $view['store_url']  = $store_url;

        // $config             = Fileconfig::first("admin");
        // $store_url          = !empty($config->endosmart_url) ? $config->endosmart_url : domainname('store').'/';
        // $view['store_url']  = $store_url;
        return view('capture.casehistory.index', $view);
    }

    public function show($id, Request $r)
    {
        if (isset($r->event)) {
            $event  = $r->event;
            return $this->$event($r);
        }
    }



    public function download_video($r)
    {
        // dd($r->all());
        $tb_case = Mongo::table('tb_case')->find($r->cid);
        $hn = $tb_case['case_hn'];
        $date = $tb_case['appointment_date'];
        $store = domainname('store');
        $file_path = $store .'/'. $hn . '/' . $date . '/';
        $vdo_name = $file_path .'/'. 'vdo' .'/'. $r->video_name;
        $arr = array();
        $arr['url'] = $vdo_name;
        $arr['status'] = true;
        // dd($arr);
        printJSON($arr);
    }




    public function download_file($r)
    {
        // dd($r->all());
        $tb_case = Mongo::table('tb_case')->find($r->cid);
      $photo   = $tb_case['photo'];
      $hn = $tb_case['case_hn'];
      $date = $tb_case['appointment_date'];
      $storeurl = domainname('store');
      $store = htdocs("store");
      $pdflastversion = $tb_case['case_pdfversion'];
      $pdf_end = end($pdflastversion);
      $pdf     = $pdf_end['pdf'];
    //   dd($pdf);
      $file_path = $store .'/'. $hn . '/' . $date . '/';
      $photoname = array();
      foreach ($photo as $key => $value) {
          $photoname[] = $file_path . $value['na'];

        }
        $photoname[] = $file_path .'pdf'.'/'. $pdf;
        // dd($photoname);

        $zip = new \ZipArchive();
        $zipFilePath = $file_path.'/' ."temp/photo.zip";
        $file_url = $storeurl .'/'. $hn . '/' . $date . '/'."temp/photo.zip";

        if ($zip->open($zipFilePath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE) === TRUE) {
            foreach ($photoname as $file) {
                $fileContent = file_get_contents($file);

                // dd($file);

                $zip->addFromString(basename($file), $fileContent);
            }
            $zip->close();


        }
        // return response()->download($zipFilePath);


        // return response()->download($zipFilePath)->deleteFileAfterSend(true);
        $arr = array();
        $arr['url'] = $file_url;
        $arr['status'] = true;
        printJSON($arr);
      }


    public function photo($r)
    {
        $view['cid'] = $r->cid;
        $view['tb_case'] = Mongo::table('tb_case')->find($r->cid);
        // dd($view,$r->all());
        $view['date'] = $view['tb_case']['appointment_date'];
        $view['hn'] = $view['tb_case']['case_hn'];
        $view['store'] = domainname('store');
        return view("capture.casehistory.photo", $view);
    }

    public function video($r)
    {
        // dd($r->all());
        $view['cid'] = $r->cid;
        $view['tb_case'] = Mongo::table('tb_case')->find($r->cid);
        $view['date'] = $view['tb_case']['appointment_date'];
        $view['hn'] = $view['tb_case']['case_hn'];
        $view['store'] = domainname('store');
        return view("capture.casehistory.video", $view);
    }









    public function download($r)
    {
        // dd($r->all());
        $view['cid'] = $r->cid;
        $view['tb_case'] = Mongo::table('tb_case')->find($r->cid);
        $vdo_file  = $view['tb_case']['video'];
        foreach ($vdo_file as $value) {
            $vdo_name[] = $value;
        }
        $view['vdo_name'] = $vdo_name;
        $view['date'] = $view['tb_case']['appointment_date'];
        $view['hn'] = $view['tb_case']['case_hn'];
        $view['store_htdocs'] = htdocs('store');
        return view("capture.casehistory.download", $view);
    }


    public function search_history(Request $r)
    {
        // dd($r->all());
        $department = uget("department");
        $tb_case = Mongo::table("tb_case")->where('department', $department);

        if (checkNULL(@$r->search_keyword)) {
            $tb_case = $tb_case->where('case_status', '!=', 90);
            $tb_case->where(function ($query) use ($r) {
                $query->where('case_hn', "like", "%" . $r->search_keyword . "%")
                    ->orWhere('patientname', "like", "%" . $r->search_keyword . "%")
                    ->orWhere('doctorname', "like", "%" . $r->search_keyword . "%")
                    ->orWhere("department", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("statusjob", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("procedurename", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("treatment_coverage", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("medication_unit", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("medi_other", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("indication", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("indication_other", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("anesthesia", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("anesthesiaother", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("finding", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("overall_finding", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("diagnostic_text", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("overall_diagnosis", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("procedure_subtext", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("bowel_other", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("bowelpreparation", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("gastriccontent", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("specimen1", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("specimen2", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("specimen3", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("specimen4", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("box_rapid_pending", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("rapid_other", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("complication", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("technique_other", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("case_comment", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("assistant", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("urgent", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("overall_procedure", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("case_comment", "like", "%" . $r->search_keyword . "%")
                    ->orWhere("urgent", "like", "%" . $r->search_keyword . "%");
            });
        }
        $tb_case->where(function ($query) use ($r) {
            if (isset($r->sel_procedure)) {
                $query->where('case_procedurecode', $r->sel_procedure ?? "");
            }

            if (isset($r->sel_physician)) {
                $query->where('case_physicians01', intval($r->sel_physician ?? ""));

            }

            if (isset($r->date_start)) {
                $query->where('appointment_date', '>=', $r->date_start);
            }

            if (isset($r->date_end)) {
                $query->where('appointment_date', '<=', $r->date_end);
            }
            if (isset($r->search_hn)) {
                $query->where('case_hn', "like", "%" . $r->search_hn ?? "");
            }
        });

        $tb_case = $tb_case->paginate(50);


        return $tb_case;
    }
}
