<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use App\Http\Controllers\SendMail;
use App\Logbook;
use App\User;
use App\Periksa; 
use App\Pasien;
use App\Riwayat;
use App\Rekaplogbook;
use App\PeriksaTest;
use Validator;
use Carbon\Carbon;
use Session;

class LogbookController extends Controller
{
    public function index() {
        if (Session::get('previlage') == ''){
			return redirect('/login');
		} else {
            $data               = [];
            $data['alldata']	= Logbook::where('kelompok', '0')->get();
            $data['allspv']	    = User::where('previlage', 'supervisor')->get();
            $data['masterid']   = Session('id');
            $data['masternama'] = Session('nama');
            $cekdokter1         = User::where('id', Session('id'))->first();
            $photo 	            = $cekdokter1->getPhoto->xfile ?? '/doctor.png';
            $data['masterphoto']= $photo;
            $data['masternama'] = $cekdokter1->nama ?? 'Deleted PPDS';
            return view('admin.logbook', $data);
        }
    }
    public function viewLogbookppds($id){
        if (Session::get('previlage') == ''){
			return redirect('/login');
		} else {
            $data               = [];
            $cekdokter1         = User::where('id', $id)->first();
            $photo 	            = $cekdokter1->getPhoto->xfile ?? '/doctor.png';
            $data['alldata']	= Logbook::where('kelompok', '0')->get();
            $data['allspv']	    = User::where('previlage', 'supervisor')->get();
            $data['masterid']   = $id;
            $data['masternama'] = $cekdokter1->nama ?? 'Deleted PPDS';
            $data['masterphoto']= $photo;
            return view('admin.logbook', $data);
        }
    }
    public function logbookControl() {
        if (Session::get('previlage') == ''){
			return redirect('/login');
		} else {
            $data               = [];
            $data['riwayat']	= Riwayat::where('keterangan', 'Logbook')->orderBy('id','DESC')->limit(100)->get();
            $data['allspv']	    = User::where('previlage', 'supervisor')->get();
            return view('admin.logbookcontrol', $data);
        }
    }
    public function logbookStatistik() {
        if (Session::get('previlage') == ''){
			return redirect('/login');
		} else {
            $data               = [];
            $data['allppds']	= User::where('previlage', 'ppds')->get();
            $data['allspv']	    = User::where('previlage', 'supervisor')->get();
            return view('admin.logbookstatistik', $data);
        }
    }
    public function jsonListlogbook(Request $request) {
		$userid 	= $request->input('val01');
		$jenis 	    = $request->input('val02');
		$kelompok 	= $request->input('val03');
        $arraylist  = [];
        $results	= Logbook::where('kelompok', '0')->get();
        if (!empty($results)){
            foreach ($results as $result) {
                $arraylist[] = array(
                    'id'            =>  $result->id,   
                    'kode'		    =>  $result->kode,
                    'kepanjangan'	=>  $result->kepanjangan,
                );   
            }
        }
	    echo json_encode($arraylist);
    }
    public function jsonViewlistlogbookppds(Request $request) {
		$kode 	        = $request->input('set01');
        $idppds 	    = $request->input('set02');
        $arrhakaksess   = [];
        $results	    = Logbook::where('kelompok', $kode)->orderBy('kepanjangan', 'ASC')->get();
        if (!empty($results)){
            foreach ($results as $hasil) {
                $id 		    = $hasil->id;
                $target 		= $hasil->target;
                $target2 		= $hasil->target2;
                $target3 		= $hasil->target3;
                $penyakit 		= $hasil->kepanjangan;
                
                if ($target != 0 AND $idppds != '0'){
                    $countuwes      = Periksa::where('ppdssenior', $idppds)->where('diagnosa', $id)->count();
                    $countuwesvers  = Periksa::where('ppdssenior', $idppds)->where('diagnosa', $id)->whereNotNull('verifikasi')->count();
                    if ($target <= $countuwesvers){
                        $countuwesvers = '<span class="label label-success">Complete</span>';
                    }
                } else {
                    $countuwes      = '';
                    $countuwesvers	= '';
                }
                if ($target2 != 0 AND $idppds != '0'){
                    $countuwes2      = Periksa::where('diagnosa', $id)->where('middleppds', $idppds)->orWhere('ppdsmiddle2', $idppds)->count();
                    $countuwesvers2  = Periksa::where('diagnosa', $id)->whereNotNull('verifikasi')->where('middleppds', $idppds)->orWhere('ppdsmiddle2', $idppds)->count();
                    if ($target2 <= $countuwesvers2){
                        $countuwesvers2 = '<span class="label label-success">Complete</span>';
                    }
                } else {
                    $countuwes2     = '';
                    $countuwesvers2	= '';
                }
                if ($target3 != 0 AND $idppds != '0'){
                    $countuwes3      = Periksa::where('diagnosa', $id)->where('ppdsjunior', $idppds)->orWhere('ppdsjunior2', $idppds)->count();
                    $countuwesvers3  = Periksa::where('diagnosa', $id)->whereNotNull('verifikasi')->where('ppdsjunior', $idppds)->orWhere('ppdsjunior2', $idppds)->count();
                    if ($target3 <= $countuwesvers3){
                        $countuwesvers3 = '<span class="label label-success">Complete</span>';
                    }
                } else {
                    $countuwes3     = '';
                    $countuwesvers3	= '';
                }
                $arrhakaksess[] = array(
                    'id2' 			=> $hasil->id,
                    'idppds' 		=> $idppds,
                    'tercapai' 		=> $countuwes,
                    'tervalidasi' 	=> $countuwesvers,
                    'tercapai2' 	=> $countuwes2,
                    'tervalidasi2' 	=> $countuwesvers2,
                    'tercapai3' 	=> $countuwes3,
                    'tervalidasi3' 	=> $countuwesvers3,
                    'parent' 		=> $hasil->kelompok,
                    'kuota' 		=> $hasil->kuota,
                    'target' 		=> $target,
                    'kuota2' 		=> $hasil->kuota2,
                    'target2' 		=> $target2,
                    'kuota3' 		=> $hasil->kuota3,
                    'target3' 		=> $target3,
                    'kode2' 		=> $kode,
                    'kepanjangan2' 	=> $penyakit,
                );
            }
        }
	    echo json_encode($arrhakaksess);
    }
    public function jsonViewlogbook(Request $request) {
		$idppds 	    = $request->input('val01');
        $idstase 	    = $request->input('val02');
        $arraysurat     = [];
        $kelompokstase  = '';
        $cekdokter 	    = User::where('id', $idppds)->first();
        if (isset($cekdokter->nama)){
            $namadokter = $cekdokter->nama;
            $kelompok   = $cekdokter->previlage;
        } else { $namadokter = 'Deleted Staf'; $kelompok = ''; }
        $kodestase      = '';
        $i              = 1;
        if ($idstase == 'junior'){
            $result	= Periksa::where('ppdsjunior', $idppds)->get();
            if (!empty($result)){
                foreach($result as $rmaster1){
                    $id 		    = $rmaster1->id;
                    $idspv 		    = $rmaster1->dokter_id;
                    $status 		= $rmaster1->status;
                    $idpasien		= $rmaster1->pasien_id;
                    $nofoto		    = $rmaster1->nofoto;
                    $poli_id		= $rmaster1->poli_id;
                    $idstase		= $rmaster1->diagnosa;
                    $diagnosa2		= $rmaster1->diagnosa2;
                    $verifikasi	    = $rmaster1->verifikasi;
                    $from 		    = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $rmaster1->daftar);
			
                    $getjenis 		= explode("-", $nofoto);
                    if (isset($getjenis[0])){
                        $jenis1     = $getjenis[0];
                    } else { $jenis1= ''; }
    
                    if (isset($getjenis[1])){
                        $jenis2     = $getjenis[1];
                    } else { $jenis2= ''; }
                    $jenis          = $jenis1.'-'.$jenis2;
                    
                    if ($status == ''){
                        $tlsstatus    	= '<font color="red">NEW</font>';
                        $katapertama    = '';
                    } else {
                        $cekstat 		= explode(" ", $status);
                        $katapertama	= $cekstat[0];
                        if ($katapertama == 'Telah'){
                            $tlsstatus    	= '<font color="blue">'.$status.'</font>';
                        } else if ($katapertama == 'Diperiksa'){
                            $tlsstatus    	= '<strong>'.$status.'</strong>';
                        } else if ($katapertama == 'Draft'){
                            $tlsstatus    	= '<font color="green">'.$status.'</font>';
                        } else if ($katapertama == 'Expertise'){
                            $tlsstatus    	= '<strong><font color="green">'.$status.'</font></strong>';
                        } else if ($katapertama == 'Decliend'){
                            $tlsstatus    	= '<span class="badge badge-danger">'.$status.'</span>';
                        }  else if ($katapertama == 'Selesai'){
                            $tlsstatus    	= '<span class="badge badge-success">'.$status.'</span>';
                        }   else if ($katapertama == 'Arsip'){
                            $tlsstatus    	= '<span class="badge badge-primary">'.$status.'</span>';
                        } else {
                            $tlsstatus    	= '<font color="grey">'.$status.'</font>';
                        }
                    }
                    $ceksek         = Rekaplogbook::where('idkasus', $id)->where('idppds', $idppds)->first();
                    if (isset($ceksek->id)){
                        if ($ceksek->marking == 'aktif'){
                            $arraysurat[] = array(
                                'id'            => $ceksek->id,
                                'idppds'        => $idppds,
                                'idkasus'       => $id,
                                'nomor'         => $i,
                                'sebagai'       => $ceksek->sebagai,
                                'namappds'      => $ceksek->namappds,
                                'kelompokstase' => $ceksek->kelompokstase,
                                'kodestase'     => $ceksek->kodestase,
                                'kepanjangan'   => $ceksek->kepanjangan,
                                'target'        => $ceksek->target,
                                'idstase'       => $ceksek->idstase,
                                'nofoto'        => $ceksek->nofoto,
                                'verifikasi'    => $ceksek->verifikasi,
                                'supervisor'    => $ceksek->supervisor,
                                'layanan'       => $ceksek->layanan,
                                'klinis'        => $ceksek->klinis,
                                'modality'      => $ceksek->modality,
                                'ruangan'       => $ceksek->ruangan,
                                'durasi'        => $ceksek->durasi
                            );
                            $i++;
                        }
                    } else {
                        if (is_null($idstase) OR $idstase == 0 OR $idstase == ''){
                            $kodestase = 'Deleted Stase'; $kepanjangan = 'Deleted Data'; $target = 0;
                        } else {
                            $ceknama 	    = Logbook::where('id', $idstase)->first();
                            if (isset($ceknama->kepanjangan)){
                                $kelompokstase  = $ceknama->kelompok;
                                $kodestase      = $ceknama->kode;
                                $kepanjangan    = $ceknama->kepanjangan;
                                $target         = $ceknama->target2;
                                if ($kodestase == ''){ $kodestase = $kelompokstase; }
                            } else { $kodestase = 'Deleted Stase'; $kepanjangan = 'Deleted Data'; $target = 0; }
                        }
                        $cekspv 	    = User::where('id', $idspv)->first();
                        if (isset($cekspv->nama)){
                            $supervisor = $cekspv->nama;
                        } else { $supervisor = 'Deleted SPV'; }
                        if (is_null($verifikasi)){ 
                            $verifikasi = '';
                            $to 		= '';
                            $durasi 	= 'On Progress';
                        } else {
                            $to 		= \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $verifikasi);
                            $durasi 	= $to->diffForHumans($from);
                            $to 		= $to->tostring();
                        }
                        $idrekap = Rekaplogbook::insertGetid([
                            'idppds'        => $idppds,
                            'idkasus'       => $id,
                            'namappds'      => $namadokter,
                            'sebagai'       => 'junior',
                            'kelompokstase' => $kelompokstase,
                            'kodestase'     => $diagnosa2,
                            'kepanjangan'   => $kepanjangan,
                            'target'        => $target,
                            'idstase'       => $idstase,
                            'nofoto'        => $nofoto,
                            'verifikasi'    => $verifikasi,
                            'supervisor'    => $supervisor,
                            'layanan'       => $rmaster1->reques,
                            'klinis'        => $rmaster1->klinis,
                            'modality'      => $rmaster1->modality,
                            'ruangan'       => $rmaster1->ruangan,
                            'durasi'        => $durasi,
                            'marking'       => 'aktif'
                        ]);
                        $arraysurat[] = array(
                            'id'            => $idrekap,
                            'idppds'        => $idppds,
                            'idkasus'       => $id,
                            'nomor'         => $i,
                            'sebagai'       => 'junior',
                            'namappds'      => $namadokter,
                            'kelompokstase' => $kelompokstase,
                            'kodestase'     => $diagnosa2,
                            'kepanjangan'   => $kepanjangan,
                            'target'        => $target,
                            'idstase'       => $idstase,
                            'nofoto'        => $nofoto,
                            'verifikasi'    => $verifikasi,
                            'supervisor'    => $supervisor,
                            'layanan'       => $rmaster1->reques,
                            'klinis'        => $rmaster1->klinis,
                            'modality'      => $rmaster1->modality,
                            'ruangan'       => $rmaster1->ruangan,
                            'durasi'        => $durasi
                        );
                        $i++;
                    }
                }
            }
            $result	= Periksa::where('ppdsjunior2', $idppds)->get();
            if (!empty($result)){
                foreach($result as $rmaster1){
                    $id 		    = $rmaster1->id;
                    $idspv 		    = $rmaster1->dokter_id;
                    $status 		= $rmaster1->status;
                    $idpasien		= $rmaster1->pasien_id;
                    $nofoto		    = $rmaster1->nofoto;
                    $poli_id		= $rmaster1->poli_id;
                    $idstase		= $rmaster1->diagnosa;
                    $diagnosa2		= $rmaster1->diagnosa2;
                    $verifikasi	    = $rmaster1->verifikasi;
                    $from 		    = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $rmaster1->daftar);
			
                    $getjenis 		= explode("-", $nofoto);
                    if (isset($getjenis[0])){
                        $jenis1     = $getjenis[0];
                    } else { $jenis1= ''; }
    
                    if (isset($getjenis[1])){
                        $jenis2     = $getjenis[1];
                    } else { $jenis2= ''; }
                    $jenis          = $jenis1.'-'.$jenis2;
                    
                    if ($status == ''){
                        $tlsstatus    	= '<font color="red">NEW</font>';
                        $katapertama    = '';
                    } else {
                        $cekstat 		= explode(" ", $status);
                        $katapertama	= $cekstat[0];
                        if ($katapertama == 'Telah'){
                            $tlsstatus    	= '<font color="blue">'.$status.'</font>';
                        } else if ($katapertama == 'Diperiksa'){
                            $tlsstatus    	= '<strong>'.$status.'</strong>';
                        } else if ($katapertama == 'Draft'){
                            $tlsstatus    	= '<font color="green">'.$status.'</font>';
                        } else if ($katapertama == 'Expertise'){
                            $tlsstatus    	= '<strong><font color="green">'.$status.'</font></strong>';
                        } else if ($katapertama == 'Decliend'){
                            $tlsstatus    	= '<span class="badge badge-danger">'.$status.'</span>';
                        }  else if ($katapertama == 'Selesai'){
                            $tlsstatus    	= '<span class="badge badge-success">'.$status.'</span>';
                        }   else if ($katapertama == 'Arsip'){
                            $tlsstatus    	= '<span class="badge badge-primary">'.$status.'</span>';
                        } else {
                            $tlsstatus    	= '<font color="grey">'.$status.'</font>';
                        }
                    }
                    $ceksek         = Rekaplogbook::where('idkasus', $id)->where('idppds', $idppds)->first();
                    if (isset($ceksek->id)){
                        if ($ceksek->marking == 'aktif'){
                            $arraysurat[] = array(
                                'id'            => $ceksek->id,
                                'idppds'        => $idppds,
                                'idkasus'       => $id,
                                'nomor'         => $i,
                                'sebagai'       => $ceksek->sebagai,
                                'namappds'      => $ceksek->namappds,
                                'kelompokstase' => $ceksek->kelompokstase,
                                'kodestase'     => $ceksek->kodestase,
                                'kepanjangan'   => $ceksek->kepanjangan,
                                'target'        => $ceksek->target,
                                'idstase'       => $ceksek->idstase,
                                'nofoto'        => $ceksek->nofoto,
                                'verifikasi'    => $ceksek->verifikasi,
                                'supervisor'    => $ceksek->supervisor,
                                'layanan'       => $ceksek->layanan,
                                'klinis'        => $ceksek->klinis,
                                'modality'      => $ceksek->modality,
                                'ruangan'       => $ceksek->ruangan,
                                'durasi'        => $ceksek->durasi
                            );
                            $i++;
                        }
                    } else {
                        if (is_null($idstase) OR $idstase == 0 OR $idstase == ''){
                            $kodestase = 'Deleted Stase'; $kepanjangan = 'Deleted Data'; $target = 0;
                        } else {
                            $ceknama 	    = Logbook::where('id', $idstase)->first();
                            if (isset($ceknama->kepanjangan)){
                                $kelompokstase  = $ceknama->kelompok;
                                $kodestase      = $ceknama->kode;
                                $kepanjangan    = $ceknama->kepanjangan;
                                $target         = $ceknama->target2;
                                if ($kodestase == ''){ $kodestase = $kelompokstase; }
                            } else { $kodestase = 'Deleted Stase'; $kepanjangan = 'Deleted Data'; $target = 0; }
                        }
                        $cekspv 	    = User::where('id', $idspv)->first();
                        if (isset($cekspv->nama)){
                            $supervisor = $cekspv->nama;
                        } else { $supervisor = 'Deleted SPV'; }
                        if (is_null($verifikasi)){ 
                            $verifikasi = '';
                            $to 		= '';
                            $durasi 	= 'On Progress';
                        } else {
                            $to 		= \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $verifikasi);
                            $durasi 	= $to->diffForHumans($from);
                            $to 		= $to->tostring();
                        }
                        $idrekap = Rekaplogbook::insertGetid([
                            'idppds'        => $idppds,
                            'idkasus'       => $id,
                            'namappds'      => $namadokter,
                            'sebagai'       => 'junior',
                            'kelompokstase' => $kelompokstase,
                            'kodestase'     => $diagnosa2,
                            'kepanjangan'   => $kepanjangan,
                            'target'        => $target,
                            'idstase'       => $idstase,
                            'nofoto'        => $nofoto,
                            'verifikasi'    => $verifikasi,
                            'supervisor'    => $supervisor,
                            'layanan'       => $rmaster1->reques,
                            'klinis'        => $rmaster1->klinis,
                            'modality'      => $rmaster1->modality,
                            'ruangan'       => $rmaster1->ruangan,
                            'durasi'        => $durasi,
                            'marking'       => 'aktif'
                        ]);
                        $arraysurat[] = array(
                            'id'            => $idrekap,
                            'idppds'        => $idppds,
                            'idkasus'       => $id,
                            'nomor'         => $i,
                            'sebagai'       => 'junior',
                            'namappds'      => $namadokter,
                            'kelompokstase' => $kelompokstase,
                            'kodestase'     => $diagnosa2,
                            'kepanjangan'   => $kepanjangan,
                            'target'        => $target,
                            'idstase'       => $idstase,
                            'nofoto'        => $nofoto,
                            'verifikasi'    => $verifikasi,
                            'supervisor'    => $supervisor,
                            'layanan'       => $rmaster1->reques,
                            'klinis'        => $rmaster1->klinis,
                            'modality'      => $rmaster1->modality,
                            'ruangan'       => $rmaster1->ruangan,
                            'durasi'        => $durasi
                        );
                        $i++;
                    }
                }
            }
        } else if ($idstase == 'middle'){
            $result	= Periksa::where('middleppds', $idppds)->get();
            if (!empty($result)){
                foreach($result as $rmaster1){
                    $id 		    = $rmaster1->id;
                    $idspv 		    = $rmaster1->dokter_id;
                    $status 		= $rmaster1->status;
                    $idpasien		= $rmaster1->pasien_id;
                    $nofoto		    = $rmaster1->nofoto;
                    $poli_id		= $rmaster1->poli_id;
                    $idstase		= $rmaster1->diagnosa;
                    $diagnosa2		= $rmaster1->diagnosa2;
                    $verifikasi	    = $rmaster1->verifikasi;
                    $from 		    = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $rmaster1->daftar);
			
                    $getjenis 		= explode("-", $nofoto);
                    if (isset($getjenis[0])){
                        $jenis1     = $getjenis[0];
                    } else { $jenis1= ''; }
    
                    if (isset($getjenis[1])){
                        $jenis2     = $getjenis[1];
                    } else { $jenis2= ''; }
                    $jenis          = $jenis1.'-'.$jenis2;
                    
                    if ($status == ''){
                        $tlsstatus    	= '<font color="red">NEW</font>';
                        $katapertama    = '';
                    } else {
                        $cekstat 		= explode(" ", $status);
                        $katapertama	= $cekstat[0];
                        if ($katapertama == 'Telah'){
                            $tlsstatus    	= '<font color="blue">'.$status.'</font>';
                        } else if ($katapertama == 'Diperiksa'){
                            $tlsstatus    	= '<strong>'.$status.'</strong>';
                        } else if ($katapertama == 'Draft'){
                            $tlsstatus    	= '<font color="green">'.$status.'</font>';
                        } else if ($katapertama == 'Expertise'){
                            $tlsstatus    	= '<strong><font color="green">'.$status.'</font></strong>';
                        } else if ($katapertama == 'Decliend'){
                            $tlsstatus    	= '<span class="badge badge-danger">'.$status.'</span>';
                        }  else if ($katapertama == 'Selesai'){
                            $tlsstatus    	= '<span class="badge badge-success">'.$status.'</span>';
                        }   else if ($katapertama == 'Arsip'){
                            $tlsstatus    	= '<span class="badge badge-primary">'.$status.'</span>';
                        } else {
                            $tlsstatus    	= '<font color="grey">'.$status.'</font>';
                        }
                    }
                    $ceksek         = Rekaplogbook::where('idkasus', $id)->where('idppds', $idppds)->first();
                    if (isset($ceksek->id)){
                        if ($ceksek->marking == 'aktif'){
                            $arraysurat[] = array(
                                'id'            => $ceksek->id,
                                'idppds'        => $idppds,
                                'idkasus'       => $id,
                                'nomor'         => $i,
                                'sebagai'       => $ceksek->sebagai,
                                'namappds'      => $ceksek->namappds,
                                'kelompokstase' => $ceksek->kelompokstase,
                                'kodestase'     => $ceksek->kodestase,
                                'kepanjangan'   => $ceksek->kepanjangan,
                                'target'        => $ceksek->target,
                                'idstase'       => $ceksek->idstase,
                                'nofoto'        => $ceksek->nofoto,
                                'verifikasi'    => $ceksek->verifikasi,
                                'supervisor'    => $ceksek->supervisor,
                                'layanan'       => $ceksek->layanan,
                                'klinis'        => $ceksek->klinis,
                                'modality'      => $ceksek->modality,
                                'ruangan'       => $ceksek->ruangan,
                                'durasi'        => $ceksek->durasi
                            );
                            $i++;
                        }
                    } else {
                        if (is_null($idstase) OR $idstase == 0 OR $idstase == ''){
                            $kodestase = 'Deleted Stase'; $kepanjangan = 'Deleted Data'; $target = 0;
                        } else {
                            $ceknama 	    = Logbook::where('id', $idstase)->first();
                            if (isset($ceknama->kepanjangan)){
                                $kelompokstase  = $ceknama->kelompok;
                                $kodestase      = $ceknama->kode;
                                $kepanjangan    = $ceknama->kepanjangan;
                                $target         = $ceknama->target2;
                                if ($kodestase == ''){ $kodestase = $kelompokstase; }
                            } else { $kodestase = 'Deleted Stase'; $kepanjangan = 'Deleted Data'; $target = 0; }
                        }
                        $cekspv 	    = User::where('id', $idspv)->first();
                        if (isset($cekspv->nama)){
                            $supervisor = $cekspv->nama;
                        } else { $supervisor = 'Deleted SPV'; }
                        if (is_null($verifikasi)){ 
                            $verifikasi = '';
                            $to 		= '';
                            $durasi 	= 'On Progress';
                        } else {
                            $to 		= \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $verifikasi);
                            $durasi 	= $to->diffForHumans($from);
                            $to 		= $to->tostring();
                        }
                        $idrekap = Rekaplogbook::insertGetid([
                            'idppds'        => $idppds,
                            'idkasus'       => $id,
                            'namappds'      => $namadokter,
                            'sebagai'       => 'middle',
                            'kelompokstase' => $kelompokstase,
                            'kodestase'     => $diagnosa2,
                            'kepanjangan'   => $kepanjangan,
                            'target'        => $target,
                            'idstase'       => $idstase,
                            'nofoto'        => $nofoto,
                            'verifikasi'    => $verifikasi,
                            'supervisor'    => $supervisor,
                            'layanan'       => $rmaster1->reques,
                            'klinis'        => $rmaster1->klinis,
                            'modality'      => $rmaster1->modality,
                            'ruangan'       => $rmaster1->ruangan,
                            'durasi'        => $durasi,
                            'marking'       => 'aktif'
                        ]);
                        $arraysurat[] = array(
                            'id'            => $idrekap,
                            'idppds'        => $idppds,
                            'idkasus'       => $id,
                            'nomor'         => $i,
                            'sebagai'       => 'middle',
                            'namappds'      => $namadokter,
                            'kelompokstase' => $kelompokstase,
                            'kodestase'     => $diagnosa2,
                            'kepanjangan'   => $kepanjangan,
                            'target'        => $target,
                            'idstase'       => $idstase,
                            'nofoto'        => $nofoto,
                            'verifikasi'    => $verifikasi,
                            'supervisor'    => $supervisor,
                            'layanan'       => $rmaster1->reques,
                            'klinis'        => $rmaster1->klinis,
                            'modality'      => $rmaster1->modality,
                            'ruangan'       => $rmaster1->ruangan,
                            'durasi'        => $durasi
                        );
                        $i++;
                    }
                }
            }
            $result	= Periksa::where('ppdsmiddle2', $idppds)->get();
            if (!empty($result)){
                foreach($result as $rmaster1){
                    $id 		    = $rmaster1->id;
                    $idspv 		    = $rmaster1->dokter_id;
                    $status 		= $rmaster1->status;
                    $idpasien		= $rmaster1->pasien_id;
                    $nofoto		    = $rmaster1->nofoto;
                    $poli_id		= $rmaster1->poli_id;
                    $idstase		= $rmaster1->diagnosa;
                    $diagnosa2		= $rmaster1->diagnosa2;
                    $verifikasi	    = $rmaster1->verifikasi;
                    $from 		    = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $rmaster1->daftar);
			
                    $getjenis 		= explode("-", $nofoto);
                    if (isset($getjenis[0])){
                        $jenis1     = $getjenis[0];
                    } else { $jenis1= ''; }
    
                    if (isset($getjenis[1])){
                        $jenis2     = $getjenis[1];
                    } else { $jenis2= ''; }
                    $jenis          = $jenis1.'-'.$jenis2;
                    
                    if ($status == ''){
                        $tlsstatus    	= '<font color="red">NEW</font>';
                        $katapertama    = '';
                    } else {
                        $cekstat 		= explode(" ", $status);
                        $katapertama	= $cekstat[0];
                        if ($katapertama == 'Telah'){
                            $tlsstatus    	= '<font color="blue">'.$status.'</font>';
                        } else if ($katapertama == 'Diperiksa'){
                            $tlsstatus    	= '<strong>'.$status.'</strong>';
                        } else if ($katapertama == 'Draft'){
                            $tlsstatus    	= '<font color="green">'.$status.'</font>';
                        } else if ($katapertama == 'Expertise'){
                            $tlsstatus    	= '<strong><font color="green">'.$status.'</font></strong>';
                        } else if ($katapertama == 'Decliend'){
                            $tlsstatus    	= '<span class="badge badge-danger">'.$status.'</span>';
                        }  else if ($katapertama == 'Selesai'){
                            $tlsstatus    	= '<span class="badge badge-success">'.$status.'</span>';
                        }   else if ($katapertama == 'Arsip'){
                            $tlsstatus    	= '<span class="badge badge-primary">'.$status.'</span>';
                        } else {
                            $tlsstatus    	= '<font color="grey">'.$status.'</font>';
                        }
                    }
                    $ceksek         = Rekaplogbook::where('idkasus', $id)->where('idppds', $idppds)->first();
                    if (isset($ceksek->id)){
                        if ($ceksek->marking == 'aktif'){
                            $arraysurat[] = array(
                                'id'            => $ceksek->id,
                                'idppds'        => $idppds,
                                'idkasus'       => $id,
                                'nomor'         => $i,
                                'sebagai'       => $ceksek->sebagai,
                                'namappds'      => $ceksek->namappds,
                                'kelompokstase' => $ceksek->kelompokstase,
                                'kodestase'     => $ceksek->kodestase,
                                'kepanjangan'   => $ceksek->kepanjangan,
                                'target'        => $ceksek->target,
                                'idstase'       => $ceksek->idstase,
                                'nofoto'        => $ceksek->nofoto,
                                'verifikasi'    => $ceksek->verifikasi,
                                'supervisor'    => $ceksek->supervisor,
                                'layanan'       => $ceksek->layanan,
                                'klinis'        => $ceksek->klinis,
                                'modality'      => $ceksek->modality,
                                'ruangan'       => $ceksek->ruangan,
                                'durasi'        => $ceksek->durasi
                            );
                            $i++;
                        }
                    } else {
                        if (is_null($idstase) OR $idstase == 0 OR $idstase == ''){
                            $kodestase = 'Deleted Stase'; $kepanjangan = 'Deleted Data'; $target = 0;
                        } else {
                            $ceknama 	    = Logbook::where('id', $idstase)->first();
                            if (isset($ceknama->kepanjangan)){
                                $kelompokstase  = $ceknama->kelompok;
                                $kodestase      = $ceknama->kode;
                                $kepanjangan    = $ceknama->kepanjangan;
                                $target         = $ceknama->target2;
                                if ($kodestase == ''){ $kodestase = $kelompokstase; }
                            } else { $kodestase = 'Deleted Stase'; $kepanjangan = 'Deleted Data'; $target = 0; }
                        }
                        $cekspv 	    = User::where('id', $idspv)->first();
                        if (isset($cekspv->nama)){
                            $supervisor = $cekspv->nama;
                        } else { $supervisor = 'Deleted SPV'; }
                        if (is_null($verifikasi)){ 
                            $verifikasi = '';
                            $to 		= '';
                            $durasi 	= 'On Progress';
                        } else {
                            $to 		= \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $verifikasi);
                            $durasi 	= $to->diffForHumans($from);
                            $to 		= $to->tostring();
                        }
                        $idrekap = Rekaplogbook::insertGetid([
                            'idppds'        => $idppds,
                            'idkasus'       => $id,
                            'namappds'      => $namadokter,
                            'sebagai'       => 'middle',
                            'kelompokstase' => $kelompokstase,
                            'kodestase'     => $diagnosa2,
                            'kepanjangan'   => $kepanjangan,
                            'target'        => $target,
                            'idstase'       => $idstase,
                            'nofoto'        => $nofoto,
                            'verifikasi'    => $verifikasi,
                            'supervisor'    => $supervisor,
                            'layanan'       => $rmaster1->reques,
                            'klinis'        => $rmaster1->klinis,
                            'modality'      => $rmaster1->modality,
                            'ruangan'       => $rmaster1->ruangan,
                            'durasi'        => $durasi,
                            'marking'       => 'aktif'
                        ]);
                        $arraysurat[] = array(
                            'id'            => $idrekap,
                            'idppds'        => $idppds,
                            'idkasus'       => $id,
                            'nomor'         => $i,
                            'sebagai'       => 'middle',
                            'namappds'      => $namadokter,
                            'kelompokstase' => $kelompokstase,
                            'kodestase'     => $diagnosa2,
                            'kepanjangan'   => $kepanjangan,
                            'target'        => $target,
                            'idstase'       => $idstase,
                            'nofoto'        => $nofoto,
                            'verifikasi'    => $verifikasi,
                            'supervisor'    => $supervisor,
                            'layanan'       => $rmaster1->reques,
                            'klinis'        => $rmaster1->klinis,
                            'modality'      => $rmaster1->modality,
                            'ruangan'       => $rmaster1->ruangan,
                            'durasi'        => $durasi
                        );
                        $i++;
                    }
                }
            }
        } else if ($idstase == 'senior'){
            $result	= Periksa::where('ppdssenior', $idppds)->get();
            if (!empty($result)){
                foreach($result as $rmaster1){
                    $id 		    = $rmaster1->id;
                    $idspv 		    = $rmaster1->dokter_id;
                    $status 		= $rmaster1->status;
                    $idpasien		= $rmaster1->pasien_id;
                    $nofoto		    = $rmaster1->nofoto;
                    $poli_id		= $rmaster1->poli_id;
                    $idstase		= $rmaster1->diagnosa;
                    $diagnosa2		= $rmaster1->diagnosa2;
                    $verifikasi	    = $rmaster1->verifikasi;
                    $from 		    = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $rmaster1->daftar);
			
                    $getjenis 		= explode("-", $nofoto);
                    if (isset($getjenis[0])){
                        $jenis1     = $getjenis[0];
                    } else { $jenis1= ''; }
    
                    if (isset($getjenis[1])){
                        $jenis2     = $getjenis[1];
                    } else { $jenis2= ''; }
                    $jenis          = $jenis1.'-'.$jenis2;
                    
                    if ($status == ''){
                        $tlsstatus    	= '<font color="red">NEW</font>';
                        $katapertama    = '';
                    } else {
                        $cekstat 		= explode(" ", $status);
                        $katapertama	= $cekstat[0];
                        if ($katapertama == 'Telah'){
                            $tlsstatus    	= '<font color="blue">'.$status.'</font>';
                        } else if ($katapertama == 'Diperiksa'){
                            $tlsstatus    	= '<strong>'.$status.'</strong>';
                        } else if ($katapertama == 'Draft'){
                            $tlsstatus    	= '<font color="green">'.$status.'</font>';
                        } else if ($katapertama == 'Expertise'){
                            $tlsstatus    	= '<strong><font color="green">'.$status.'</font></strong>';
                        } else if ($katapertama == 'Decliend'){
                            $tlsstatus    	= '<span class="badge badge-danger">'.$status.'</span>';
                        }  else if ($katapertama == 'Selesai'){
                            $tlsstatus    	= '<span class="badge badge-success">'.$status.'</span>';
                        }   else if ($katapertama == 'Arsip'){
                            $tlsstatus    	= '<span class="badge badge-primary">'.$status.'</span>';
                        } else {
                            $tlsstatus    	= '<font color="grey">'.$status.'</font>';
                        }
                    }
                    $ceksek         = Rekaplogbook::where('idkasus', $id)->where('idppds', $idppds)->first();
                    if (isset($ceksek->id)){
                        if ($ceksek->marking == 'aktif'){
                            $arraysurat[] = array(
                                'id'            => $ceksek->id,
                                'idppds'        => $idppds,
                                'idkasus'       => $id,
                                'nomor'         => $i,
                                'sebagai'       => $ceksek->sebagai,
                                'namappds'      => $ceksek->namappds,
                                'kelompokstase' => $ceksek->kelompokstase,
                                'kodestase'     => $ceksek->kodestase,
                                'kepanjangan'   => $ceksek->kepanjangan,
                                'target'        => $ceksek->target,
                                'idstase'       => $ceksek->idstase,
                                'nofoto'        => $ceksek->nofoto,
                                'verifikasi'    => $ceksek->verifikasi,
                                'supervisor'    => $ceksek->supervisor,
                                'layanan'       => $ceksek->layanan,
                                'klinis'        => $ceksek->klinis,
                                'modality'      => $ceksek->modality,
                                'ruangan'       => $ceksek->ruangan,
                                'durasi'        => $ceksek->durasi
                            );
                            $i++;
                        }
                    } else {
                        if (is_null($idstase) OR $idstase == 0 OR $idstase == ''){
                            $kodestase = 'Deleted Stase'; $kepanjangan = 'Deleted Data'; $target = 0;
                        } else {
                            $ceknama 	    = Logbook::where('id', $idstase)->first();
                            if (isset($ceknama->kepanjangan)){
                                $kelompokstase  = $ceknama->kelompok;
                                $kodestase      = $ceknama->kode;
                                $kepanjangan    = $ceknama->kepanjangan;
                                $target         = $ceknama->target2;
                                if ($kodestase == ''){ $kodestase = $kelompokstase; }
                            } else { $kodestase = 'Deleted Stase'; $kepanjangan = 'Deleted Data'; $target = 0; }
                        }
                        $cekspv 	    = User::where('id', $idspv)->first();
                        if (isset($cekspv->nama)){
                            $supervisor = $cekspv->nama;
                        } else { $supervisor = 'Deleted SPV'; }
                        if (is_null($verifikasi)){ 
                            $verifikasi = '';
                            $to 		= '';
                            $durasi 	= 'On Progress';
                        } else {
                            $to 		= \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $verifikasi);
                            $durasi 	= $to->diffForHumans($from);
                            $to 		= $to->tostring();
                        }
                        $idrekap = Rekaplogbook::insertGetid([
                            'idppds'        => $idppds,
                            'idkasus'       => $id,
                            'namappds'      => $namadokter,
                            'sebagai'       => 'senior',
                            'kelompokstase' => $kelompokstase,
                            'kodestase'     => $diagnosa2,
                            'kepanjangan'   => $kepanjangan,
                            'target'        => $target,
                            'idstase'       => $idstase,
                            'nofoto'        => $nofoto,
                            'verifikasi'    => $verifikasi,
                            'supervisor'    => $supervisor,
                            'layanan'       => $rmaster1->reques,
                            'klinis'        => $rmaster1->klinis,
                            'modality'      => $rmaster1->modality,
                            'ruangan'       => $rmaster1->ruangan,
                            'durasi'        => $durasi,
                            'marking'       => 'aktif'
                        ]);
                        $arraysurat[] = array(
                            'id'            => $idrekap,
                            'idppds'        => $idppds,
                            'idkasus'       => $id,
                            'nomor'         => $i,
                            'sebagai'       => 'senior',
                            'namappds'      => $namadokter,
                            'kelompokstase' => $kelompokstase,
                            'kodestase'     => $diagnosa2,
                            'kepanjangan'   => $kepanjangan,
                            'target'        => $target,
                            'idstase'       => $idstase,
                            'nofoto'        => $nofoto,
                            'verifikasi'    => $verifikasi,
                            'supervisor'    => $supervisor,
                            'layanan'       => $rmaster1->reques,
                            'klinis'        => $rmaster1->klinis,
                            'modality'      => $rmaster1->modality,
                            'ruangan'       => $rmaster1->ruangan,
                            'durasi'        => $durasi
                        );
                        $i++;
                    }
                }
            }
        } else if ($idstase == 'statistik'){
            $data   = Rekaplogbook::where('idppds', $idppds)->where('marking', 'aktif')->get();
            $getdata= $data->groupBy('diagnosa2');
            if (!empty($getdata)){
                foreach($getdata as $ceksek){
                    $sebagai    = $ceksek->sebagai;
                    $jumlah     = Rekaplogbook::where('idppds', $idppds)->where('marking', 'aktif')->where('sebagai', $sebagai)->count();
                    $arraysurat[] = array(
                        'total'         => $jumlah,
                        'sebagai'       => $sebagai,
                    );
                }
            }
        } else if ($idstase == 'terbanyakbylayanan'){
            $data       = Rekaplogbook::select(DB::raw('COUNT(id) as jumlah'), 'layanan')->where('idppds', $idppds)->where('marking', 'aktif')->orderBy('jumlah', 'DESC')->get();
            $getdata    = $data->groupBy('layanan');
            if (!empty($getdata)){
                foreach($getdata as $ceksek){
                    $arraysurat[] = array(
                        'total'      => $ceksek->jumlah,
                        'nama'       => $ceksek->layanan,
                    );
                }
            }
        } else if ($idstase == 'terbanyakbystase'){
            $data    = Rekaplogbook::select(DB::raw('COUNT(id) as jumlah'), 'kepanjangan')->where('idppds', $idppds)->where('marking', 'aktif')->where('kepanjangan', '!=', '')->orderBy('jumlah', 'DESC')->get();
            $getdata = $data->groupBy('kepanjangan');
            if (!empty($getdata)){
                foreach($getdata as $ceksek){
                    $arraysurat[] = array(
                        'total'      => $ceksek->jumlah,
                        'nama'       => $ceksek->kepanjangan,
                    );
                }
            }
        } else if ($idstase == 'terbanyakbyorgan'){
            $data   = Rekaplogbook::select(DB::raw('COUNT(id) as jumlah'), 'kodestase')->where('idppds', $idppds)->where('marking', 'aktif')->where('kodestase', '!=', '')->orderBy('jumlah', 'DESC')->get();
            $getdata= $data->groupBy('kodestase');
            if (!empty($getdata)){
                foreach($getdata as $ceksek){
                    $arraysurat[] = array(
                        'total'      => $ceksek->jumlah,
                        'nama'       => $ceksek->kodestase,
                    );
                }
            }
        } else if ($idstase == 'stase'){
            $tanggal    = $request->input('val03');
            $cektanggal = explode(" - ", $tanggal);
            if (isset($cektanggal[1])){
                $mulai  = $cektanggal[0];
                $akhir  = $cektanggal[1];
                $mulai  = \Carbon\Carbon::parse($mulai)->format('Y-m-d');
                $akhir  = \Carbon\Carbon::parse($akhir)->format('Y-m-d');
                $data   = Periksa::where(function ($query) use ($mulai, $akhir) {
                            $query->where(function ($q) use ($mulai, $akhir) {
                                $q->where('daftar', '>=', $mulai)
                                ->where('daftar', '<=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '<=', $mulai)
                                    ->where('daftar', '>=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '>=', $mulai)
                                    ->where('daftar', '<=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '>=', $mulai)
                                ->where('daftar', '<=', $akhir);
                            });
                        })->get();
                $sql = $data->groupBy('diagnosa2');
                if (!empty($sql)){
                    foreach ($sql as $rows){
                        $diagnosa   = $rows->diagnosa2;
                        $jumlah1    = Periksa::where(function ($query) use ($mulai, $akhir) {
                            $query->where(function ($q) use ($mulai, $akhir) {
                                $q->where('daftar', '>=', $mulai)
                                ->where('daftar', '<=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '<=', $mulai)
                                    ->where('daftar', '>=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '>=', $mulai)
                                    ->where('daftar', '<=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '>=', $mulai)
                                ->where('daftar', '<=', $akhir);
                            });
                        })->where('diagnosa2', $rows->diagnosa2)->where('ppdssenior', $idppds)->count();
                        $jumlah2    = Periksa::where(function ($query) use ($mulai, $akhir) {
                            $query->where(function ($q) use ($mulai, $akhir) {
                                $q->where('daftar', '>=', $mulai)
                                ->where('daftar', '<=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '<=', $mulai)
                                    ->where('daftar', '>=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '>=', $mulai)
                                    ->where('daftar', '<=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '>=', $mulai)
                                ->where('daftar', '<=', $akhir);
                            });
                        })->where('diagnosa2', $rows->diagnosa2)->where('middleppds', $idppds)->count();
                        $jumlah3    = Periksa::where(function ($query) use ($mulai, $akhir) {
                            $query->where(function ($q) use ($mulai, $akhir) {
                                $q->where('daftar', '>=', $mulai)
                                ->where('daftar', '<=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '<=', $mulai)
                                    ->where('daftar', '>=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '>=', $mulai)
                                    ->where('daftar', '<=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '>=', $mulai)
                                ->where('daftar', '<=', $akhir);
                            });
                        })->where('diagnosa2', $rows->diagnosa2)->where('ppdsjunior', $idppds)->count();
                        $jumlah4    = Periksa::where(function ($query) use ($mulai, $akhir) {
                            $query->where(function ($q) use ($mulai, $akhir) {
                                $q->where('daftar', '>=', $mulai)
                                ->where('daftar', '<=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '<=', $mulai)
                                    ->where('daftar', '>=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '>=', $mulai)
                                    ->where('daftar', '<=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '>=', $mulai)
                                ->where('daftar', '<=', $akhir);
                            });
                        })->where('diagnosa2', $rows->diagnosa2)->where('ppdsmiddle2', $idppds)->count();
                        $jumlah5    = Periksa::where(function ($query) use ($mulai, $akhir) {
                            $query->where(function ($q) use ($mulai, $akhir) {
                                $q->where('daftar', '>=', $mulai)
                                ->where('daftar', '<=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '<=', $mulai)
                                    ->where('daftar', '>=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '>=', $mulai)
                                    ->where('daftar', '<=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '>=', $mulai)
                                ->where('daftar', '<=', $akhir);
                            });
                        })->where('diagnosa2', $rows->diagnosa2)->where('ppdsjunior2', $idppds)->count();
                        $jumlah = $jumlah1 + $jumlah2 + $jumlah3 + $jumlah4 + $jumlah5;
                        $arraysurat[] = array(
                            'total'     => $jumlah,
                            'sebagai'   => $diagnosa,
                        );
                    }
                }
            }
        } else if ($idstase == 'vindicate'){
            $tanggal    = $request->input('val03');
            $cektanggal = explode(" - ", $tanggal);
            if (isset($cektanggal[1])){
                $mulai  = $cektanggal[0];
                $akhir  = $cektanggal[1];
                $mulai  = \Carbon\Carbon::parse($mulai)->format('Y-m-d');
                $akhir  = \Carbon\Carbon::parse($akhir)->format('Y-m-d');
                $sql    = Logbook::all();
                if (!empty($sql)){
                    foreach ($sql as $rows){
                        $iddiagnosa = $rows->id;
                        $diagnosa   = $rows->kepanjangan;
                        $jumlah1    = Periksa::where(function ($query) use ($mulai, $akhir) {
                            $query->where(function ($q) use ($mulai, $akhir) {
                                $q->where('daftar', '>=', $mulai)
                                ->where('daftar', '<=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '<=', $mulai)
                                    ->where('daftar', '>=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '>=', $mulai)
                                    ->where('daftar', '<=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '>=', $mulai)
                                ->where('daftar', '<=', $akhir);
                            });
                        })->where('diagnosa', $iddiagnosa)->where('ppdssenior', $idppds)->count();
                        $jumlah2    = Periksa::where(function ($query) use ($mulai, $akhir) {
                            $query->where(function ($q) use ($mulai, $akhir) {
                                $q->where('daftar', '>=', $mulai)
                                ->where('daftar', '<=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '<=', $mulai)
                                    ->where('daftar', '>=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '>=', $mulai)
                                    ->where('daftar', '<=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '>=', $mulai)
                                ->where('daftar', '<=', $akhir);
                            });
                        })->where('diagnosa', $iddiagnosa)->where('middleppds', $idppds)->count();
                        $jumlah3    = Periksa::where(function ($query) use ($mulai, $akhir) {
                            $query->where(function ($q) use ($mulai, $akhir) {
                                $q->where('daftar', '>=', $mulai)
                                ->where('daftar', '<=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '<=', $mulai)
                                    ->where('daftar', '>=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '>=', $mulai)
                                    ->where('daftar', '<=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '>=', $mulai)
                                ->where('daftar', '<=', $akhir);
                            });
                        })->where('diagnosa', $iddiagnosa)->where('ppdsjunior', $idppds)->count();
                        $jumlah4    = Periksa::where(function ($query) use ($mulai, $akhir) {
                            $query->where(function ($q) use ($mulai, $akhir) {
                                $q->where('daftar', '>=', $mulai)
                                ->where('daftar', '<=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '<=', $mulai)
                                    ->where('daftar', '>=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '>=', $mulai)
                                    ->where('daftar', '<=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '>=', $mulai)
                                ->where('daftar', '<=', $akhir);
                            });
                        })->where('diagnosa', $iddiagnosa)->where('ppdsmiddle2', $idppds)->count();
                        $jumlah5    = Periksa::where(function ($query) use ($mulai, $akhir) {
                            $query->where(function ($q) use ($mulai, $akhir) {
                                $q->where('daftar', '>=', $mulai)
                                ->where('daftar', '<=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '<=', $mulai)
                                    ->where('daftar', '>=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '>=', $mulai)
                                    ->where('daftar', '<=', $akhir);
                                })->orWhere(function ($q) use ($mulai, $akhir) {
                                    $q->where('daftar', '>=', $mulai)
                                ->where('daftar', '<=', $akhir);
                            });
                        })->where('diagnosa', $iddiagnosa)->where('ppdsjunior2', $idppds)->count();
                        $jumlah = $jumlah1 + $jumlah2 + $jumlah3 + $jumlah4 + $jumlah5;
                        $arraysurat[] = array(
                            'total'     => $jumlah,
                            'sebagai'   => $diagnosa,
                        );
                    }
                }
            }
        } else {
            $kelompokstase 	= $request->input('val02');
            $kepanjangan 	= $request->input('val03');
            if ($kepanjangan == '' OR is_null($kepanjangan)){
                $getdata    = Rekaplogbook::where('kelompokstase', $kelompokstase)->where('idppds', $idppds)->where('marking', 'aktif')->get();
            } else {
                $getdata    = Rekaplogbook::where('kelompokstase', $kelompokstase)->where('kepanjangan', $kepanjangan)->where('idppds', $idppds)->where('marking', 'aktif')->get();    
            }
            if (!empty($getdata)){
                foreach($getdata as $ceksek){
                    $arraysurat[] = array(
                        'id'            => $ceksek->id,
                        'idppds'        => $ceksek->idppds,
                        'idkasus'       => $ceksek->idkasus,
                        'nomor'         => $i,
                        'sebagai'       => $ceksek->sebagai,
                        'namappds'      => $ceksek->namappds,
                        'kelompokstase' => $ceksek->kelompokstase,
                        'kodestase'     => $ceksek->kodestase,
                        'kepanjangan'   => $ceksek->kepanjangan,
                        'target'        => $ceksek->target,
                        'idstase'       => $ceksek->idstase,
                        'nofoto'        => $ceksek->nofoto,
                        'verifikasi'    => $ceksek->verifikasi,
                        'supervisor'    => $ceksek->supervisor,
                        'layanan'       => $ceksek->layanan,
                        'klinis'        => $ceksek->klinis,
                        'modality'      => $ceksek->modality,
                        'ruangan'       => $ceksek->ruangan,
                        'durasi'        => $ceksek->durasi
                    );
                    $i++;
                }
            }
        }
	    echo json_encode($arraysurat);
    }
    public function jsonViewlistlogbook(Request $request) {
        $kelompokstase 	    = $request->input('set01');
        $arrhakaksess   = [];
        $results	    = Logbook::where('kelompok', $kelompokstase)->orderBy('kepanjangan', 'ASC')->get();
        if (!empty($results)){
            foreach ($results as $hasil) {
                $arrhakaksess[] = array(
                    'id2' 			=> $hasil->id,	
                    'parent' 		=> $hasil->kelompok,
                    'kuota' 		=> $hasil->kuota,
                    'target' 		=> $hasil->target,
                    'kode2' 		=> $hasil->kode,
                    'target2' 		=> $hasil->target2,
                    'kuota2' 		=> $hasil->kuota2,
                    'target3' 		=> $hasil->target3,
                    'kuota3' 		=> $hasil->kuota3,
                    'kepanjangan2' 	=> $hasil->kepanjangan,
                );
            }
        }
	    echo json_encode($arrhakaksess);
    }
    public function jsonPencarian(Request $request) {
        $idstase 	    = $request->input('set01');
        $ceknama 	    = Logbook::where('id', $idstase)->first();
        if (isset($ceknama->kepanjangan)){
            $kelompokstase  = $ceknama->kelompok;
            $kodestase      = $ceknama->kode;
            $kepanjangan    = $ceknama->kepanjangan;
            $target         = $ceknama->target;
            if ($kodestase == ''){ $kodestase = $kelompokstase; }
        } else { $kodestase = 'Deleted Stase'; $kepanjangan = 'Deleted Data'; $kelompokstase = ''; }
        
        $arrhakaksess   = [];
        $results	    = Logbook::where('kelompok', $kelompokstase)->orderBy('kepanjangan', 'ASC')->get();
        if (!empty($results)){
            foreach ($results as $hasil) {
                $arrhakaksess[] = array(
                    'id2' 			=> $hasil->id,	
                    'parent' 		=> $hasil->kelompok,
                    'kuota' 		=> $hasil->kuota,
                    'target' 		=> $hasil->target,
                    'kode2' 		=> $hasil->kode,
                    'target2' 		=> $hasil->target2,
                    'kuota2' 		=> $hasil->kuota2,
                    'target3' 		=> $hasil->target3,
                    'kuota3' 		=> $hasil->kuota3,
                    'kepanjangan2' 	=> $hasil->kepanjangan,
                );
            }
        }
	    echo json_encode($arrhakaksess);
    }
    public function jsonPencarianSPV(Request $request) {
		$arraylist  =   [];
		$idspv 		= 	$request->input('val01');
		$mulai 		= 	$request->input('val02');
        $akhir 		= 	$request->input('val03');
        $nomor      =   1;
		if ($mulai == ''){
            if ($akhir != ''){
                $mulai = date('Y-m-d H:i:s', strtotime($akhir . ' -1 day'));
            } else {
                $mulai = date('Y-m-d H:i:s');
                $akhir = date('Y-m-d H:i:s', strtotime($mulai . ' +1 day'));
            }
        } 
        if ($akhir == ''){
            if ($mulai != ''){
                $akhir  = date('Y-m-d H:i:s', strtotime($akhir . ' +1 day'));
            } else {
                $awal   = date('Y-m-d H:i:s');
                $mulai  = date('Y-m-d H:i:s', strtotime($awal . ' -1 day'));
                $akhir  = $awal;
            }
        }
        $cekbener1  = explode(' ', $mulai);
        if (!isset($cekbener1[1])){
            $mulai  = $mulai.' 00:00:00';
        }
        $cekbener2  = explode(' ', $akhir);
        if (!isset($cekbener2[1])){
            $akhir  = $akhir.' 00:00:00';
        }
        $lists 		= DB::table('periksa')->whereNotNull('verifikasi')->where('dokter_id', $idspv)->whereBetween('daftar', [$mulai, $akhir])->orderBy('noloket', 'ASC')->get();
		foreach ($lists as $list) {
            $tgl        =   $list->tgllahirpasien;
            $poli_id    =   $list->reques;
			$foto    	=   $list->foto;
			$baca    	=   $list->baca;
			$export    	=   $list->export;
			$nofoto    	=   $list->nofoto;
			$noregister =   $list->noregister;
			$usia    	=   $list->usia;
			$urgensi    =   $list->urgensi;
			$reques    	=   $list->reques;
			$ruangan    =   $list->ruangan;
			$daftar    	=   $list->daftar;
			$status    	=   $list->status;
			$asalpasien	=   $list->asalpasien;
			$dokter_id	=   $list->dokter_id;
			$ppdssenior	=   $list->ppdssenior;
			$middleppds	=   $list->middleppds;
			$ppdsjunior	=   $list->ppdsjunior;
			$radiografer=   $list->nmanalis;
			$excutor	=   $list->excutor;
			$pasiedin	= 	$list->pasien_id;
			$keterangan	= 	$list->keterangan;
			$kesimpulan	= 	$list->kesimpulan;
			$verifikasi	= 	$list->verifikasi;
			$nama    	=   $list->nmpasien;
			$jk    		=   $list->jkpasien;
            $telpon		=   $list->tlppasien;
            $from 		=   \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $list->daftar);
			
			if (is_null($verifikasi)){ 
				$verifikasi = ''; 
				$to 		= '';
				$durasi 	= 'On Progress';
			} else {
				$verifikasi	= $list->verifikasi;
				$to 		= \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $verifikasi);
				$durasi 	= $to->diffForHumans($from);
				$to 		= $to->tostring();
			}
            $ppds1		= 	$list->nmppdssenior;
            $ppds2		= 	$list->nmmiddleppds;
            $ppds3		= 	$list->nmppdsjunior;
            $dokter		= 	$list->nmdokter;
			
			

			if ($urgensi == 'CITO') { $tlsurgensi = '<span style="background-color: red;">C I T O</span>';}
			$arraylist[] = array(
				'daftar' 		=> $daftar,
				'nofoto' 		=> $nofoto,
				'asalpasien' 	=> $asalpasien,
				'id'            => $nomor,   
				'dokter'        => $dokter,
				'idruangan'     => $list->ruangan_id,
				'iddokter'     	=> $list->dokter_id,
				'ppds1'   	    => $ppds1,
				'ppds2'         => $ppds2,
				'ppds3'         => $ppds3,
				'reques'    	=> $reques,
				'telpon'        => $telpon,
				'nama'    	    => $nama,
				'jk'    		=> $jk,
				'noregister'   	=> $noregister,
				'klinis'    	=> $list->klinis,
				'klinisi'    	=> $list->klinisi,
				'filefoto'    	=> $list->filefoto,
				'dlp'    		=> $list->dlp,
				'nmanalis'      => $list->nmanalis,
				'excutor'    	=> $list->excutor,
                'modality'    	=> $list->dlp,
                'timestamp1'    => $from,
                'timestamp2'    => $to,
                'durasi'    	=> $durasi,
            );
            $nomor++;
        }
        echo json_encode($arraylist);
    }
    public function exMenuslogbook(Request $request) {
        $jenis 		= 	$request->input('set03');
        if ($jenis == 'delpenyakit'){
            if ($request->input('set01') == 'SURE'){
                $getalldata = Logbook::where('id', $request->input('set02'))->first();
                $kepanjangan= $getalldata->kepanjangan;
                
                $delete = Logbook::where('id', $request->input('set02'))->delete();
                if ($delete){
                    $kelakuan   = 'Delete Diease Name : '.$kepanjangan.' ID : '.$request->input('set02');
                    Riwayat::create([
                        'nofoto'	=> $request->input('set02'),
                        'jawaban'	=> $kelakuan, 
                        'inputor'	=> Session('nama'),
                        'keterangan'=> 'Logbook',
                    ]);
                    return response()->json(['icon' => 'success', 'warna' => '#5ba035', 'status' => 'Success', 'message' => 'Stase deleted']);
			        return back();
                
                } else {
                    return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Failed', 'message' => 'System Error, No ID is Define. Please Contact Your Web Administrator']);
			        return back();
                }
            } else {
                return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Failed', 'message' => 'To Make Sure your action, please write SURE in the form']);
			    return back();
            }
        } else if ($jenis == 'delstase'){
            if ($request->input('set01') == 'SURE'){
                $getalldata = Logbook::where('id', $request->input('set02'))->first();
                $kode       = $getalldata->kode;
                $kepanjangan= $getalldata->kepanjangan;
                if ($kode == ''){
                    return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Failed', 'message' => 'This ID not Stase Group']);
                    return back();
                } else {
                    $delete = Logbook::where('kelompok', $kode)->delete();
                    if ($delete){
                        $kelakuan   = 'Delete Diease Group Name : '.$kode.' ID : '.$request->input('set02');
                        Riwayat::create([
                            'nofoto'	=> $request->input('set02'),
                            'jawaban'	=> $kelakuan, 
                            'inputor'	=> Session('nama'),
                            'keterangan'=> 'Logbook',
                        ]);
                        
                        return response()->json(['icon' => 'success', 'warna' => '#5ba035', 'status' => 'Success', 'message' => 'Stase deleted']);
                        return back();
                    } else {
                        return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Failed', 'message' => 'System Error, No ID is Define. Please Contact Your Web Administrator']);
                        return back();
                    }
                }
                
            } else {
                return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Failed', 'message' => 'To Make Sure your action, please write SURE in the form']);
			    return back();
            }
        } else if ($jenis == 'delpenugasan'){
            if ($request->input('set01') == 'SURE'){
                $idpps      = $request->input('set03');
            
                $getalldata = Periksa::where('id', $request->input('set02'))->first();
                $ppdssenior = $getalldata->ppdssenior;
                $middleppds = $getalldata->middleppds;
                $ppdsjunior = $getalldata->ppdsjunior;
                $ppdsmiddle2= $getalldata->ppdsmiddle2;
                $ppdsjunior2= $getalldata->ppdsjunior2;
                
                if ($ppdssenior == $idpps){
                    $delete = Periksa::where('id', $request->input('set02'))->update([
                        'ppdssenior'    => '0'
                    ]);
                    if ($delete){
                        return response()->json(['icon' => 'success', 'warna' => '#5ba035', 'status' => 'Success', 'message' => 'Data For Senior PPDS on This Case Nulled']);
                        return back();
                    } else {
                        return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Failed', 'message' => 'System Error, No ID is Define. Please Contact Your Web Administrator']);
                        return back();
                    }
                } else if ($middleppds == $idpps){
                    $delete = Periksa::where('id', $request->input('set02'))->update([
                        'middleppds'    => '0'
                    ]);
                    if ($delete){
                        return response()->json(['icon' => 'success', 'warna' => '#5ba035', 'status' => 'Success', 'message' => 'Data For Middle PPDS on This Case Nulled']);
                        return back();
                    } else {
                        return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Failed', 'message' => 'System Error, No ID is Define. Please Contact Your Web Administrator']);
                        return back();
                    }
                } else if ($ppdsjunior == $idpps){
                    $delete = Periksa::where('id', $request->input('set02'))->update([
                        'ppdsjunior'    => '0'
                    ]);
                    if ($delete){
                        return response()->json(['icon' => 'success', 'warna' => '#5ba035', 'status' => 'Success', 'message' => 'Data For Junior PPDS on This Case Nulled']);
                        return back();
                    } else {
                        return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Failed', 'message' => 'System Error, No ID is Define. Please Contact Your Web Administrator']);
                        return back();
                    }
                } else if ($ppdsmiddle2 == $idpps){
                    $delete = Periksa::where('id', $request->input('set02'))->update([
                        'ppdsmiddle2'    => '0'
                    ]);
                    if ($delete){
                        return response()->json(['icon' => 'success', 'warna' => '#5ba035', 'status' => 'Success', 'message' => 'Data For Middle PPDS on This Case Nulled']);
                        return back();
                    } else {
                        return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Failed', 'message' => 'System Error, No ID is Define. Please Contact Your Web Administrator']);
                        return back();
                    }
                } else if ($ppdsjunior2 == $idpps){
                    $delete = Periksa::where('id', $request->input('set02'))->update([
                        'ppdsjunior2'    => '0'
                    ]);
                    if ($delete){
                        return response()->json(['icon' => 'success', 'warna' => '#5ba035', 'status' => 'Success', 'message' => 'Data For Junior PPDS on This Case Nulled']);
                        return back();
                    } else {
                        return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Failed', 'message' => 'System Error, No ID is Define. Please Contact Your Web Administrator']);
                        return back();
                    }
                } else {
                    return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Failed', 'message' => 'Cannot definve Senior/Middle/Junior PPDS. Please Contact Your Web Administrator']);
                    return back();
                }
                
            } else {
                return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Failed', 'message' => 'To Make Sure your action, please write SURE in the form']);
			    return back();
            }
        } else if ($jenis == 'stase'){
            $kelompok     = $request->input('set01');
            $kepanjangan  = $request->input('set02');
            if ($kelompok != '' AND $kepanjangan != ''){
                $ceksudah = Logbook::where('kode', $kelompok)->where('kelompok', '0')->count();
                if ($ceksudah == 0){
                    $input = Logbook::create([
                        'kelompok'      => '0',
                        'kode'          => $kelompok,
                        'kepanjangan'   => $kepanjangan,
                        'kuota'         => '0',
                        'target'        => '0',
                        'kuota2'        => '0',
                        'target2'       => '0',
                        'kuota3'        => '0',
                        'target3'       => '0'
                    ]);
                    if ($input){
                        return response()->json(['icon' => 'success', 'warna' => '#5ba035', 'status' => 'Success', 'message' => 'Stase Added']);
                        return back();
                    } else {
                        return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Failed', 'message' => 'System Error. Please Contact Your Web Administrator']);
                        return back();
                    }
                } else {
                    return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Failed', 'message' => 'This Stase Code already exist']);
                    return back();
                }
            } else {
                return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Failed', 'message' => 'Please fill all form data']);
			    return back();
            }
        } else if ($jenis == 'dellogbook'){
            $id 		= 	$request->input('set01');
            $markhapus = Rekaplogbook::where('id', $id)->update([
                'marking' => 'Dihapus Oleh '.Session('nama').' Pada '.date("Y-m-d H:i:s")
            ]);
            if ($markhapus){
                return response()->json(['icon' => 'success', 'warna' => '#5ba035', 'status' => 'Success', 'message' => 'Case Deleted']);
                return back();
            } else {
                return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Failed', 'message' => 'System Error. Please Contact Your Web Administrator']);
                return back();
            }
        } else {
            $idne           = $request->input('set01');
            $kuota          = $request->input('set02');
            $kepanjangan    = $request->input('set03');
            $target         = $request->input('set04');
            $penyakit       = $request->input('set05');
            $kuota2         = $request->input('set06');
            $target2        = $request->input('set07');
            $kuota3         = $request->input('set08');
            $target3        = $request->input('set09');
            if ($target == ''){ $target = 0; }
            if ($target2 == ''){ $target2 = 0; }
            if ($target3 == ''){ $target3 = 0; }
            if ($kuota == ''){ $kuota = 0; }
            if ($kuota2 == ''){ $kuota2 = 0; }
            if ($kuota3 == ''){ $kuota3 = 0; }
            $getkode = Logbook::where('kepanjangan', $kepanjangan)->where('kelompok','0')->first();
            if (isset($getkode->kode)){
                $kelompok = $getkode->kode;
            } else { $kelompok = ''; }
            if ($penyakit != '' AND $kelompok != ''){
                if ($idne == 'new'){
                    $ceksudah = Logbook::where('kelompok', $kelompok)->where('kepanjangan', $penyakit)->count();
                } else {
                    $ceksudah = Logbook::where('id', '!=', $idne)->where('kelompok', $kelompok)->where('kepanjangan', $penyakit)->count();
                }
                if ($ceksudah == 0){
                    if ($idne == 'new'){
                        $input = Logbook::create([
                            'kelompok'      => $kelompok,
                            'kode'          => '',
                            'kepanjangan'   => $penyakit,
                            'kuota'         => $kuota,
                            'target'        => $target,
                            'kuota2'        => $kuota2,
                            'target2'       => $target2,
                            'kuota3'        => $kuota3,
                            'target3'       => $target3
                        ]);
                        $kelakuan   = 'Add Diease Name : '.$penyakit.' Target/Kuota Senior : '.$target.'/'.$kuota.' Target/Kuota Junior : '.$target2.'/'.$kuota2.' Target/Kuota Midle : '.$target3.'/'.$kuota3;
                    } else {
                        $input = Logbook::where('id', $idne)->update([
                            'kelompok'      => $kelompok,
                            'kepanjangan'   => $penyakit,
                            'kuota'         => $kuota,
                            'target'        => $target,
                            'kuota2'        => $kuota2,
                            'target2'       => $target2,
                            'kuota3'        => $kuota3,
                            'target3'       => $target3
                        ]);
                        $kelakuan   = 'Update Diease Name : '.$penyakit.' Target/Kuota Senior : '.$target.'/'.$kuota.' Target/Kuota Junior : '.$target2.'/'.$kuota2.' Target/Kuota Midle : '.$target3.'/'.$kuota3;
                    }
                    if ($input){
                        Riwayat::create([
                            'nofoto'	=> $request->input('set02'),
                            'jawaban'	=> $kelakuan, 
                            'inputor'	=> Session('nama'),
                            'keterangan'=> 'Logbook',
                        ]);
                        return response()->json(['icon' => 'success', 'warna' => '#5ba035', 'status' => 'Success', 'message' => 'Diease Added']);
                        return back();
                    } else {
                        return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Failed', 'message' => 'System Error. Please Contact Your Web Administrator']);
                        return back();
                    }
                } else {
                    return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Failed', 'message' => 'This Diease on this stase already exist']);
                    return back();
                }
            } else {
                return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Failed', 'message' => 'Diease Name must filled']);
			    return back();
            }
        }
    }
}
