<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Poli;
use App\Periksa;
use App\Dokter;
use App\Ruangan;
use App\Subjawaban;
use App\Jawaban;
use App\Pasien;
use App\User;
use App\Jadwalperiksa;
use Carbon\Carbon;
use DateTime;
use Session;
use Illuminate\Support\Facades\Log;
class ListController extends Controller
{
    public function index() {
		if (Session::get('previlage') == ''){
			return redirect('/login');
		} else {
			$polis              = Poli::all();
			$ruangans           = Ruangan::all();
			$dokters            = Dokter::all();
			$answers            = Jawaban::all();
			$details            = Subjawaban::all();
			$ruangpolis         = Ruangan::groupBy('poli')->select('poli')->get();
			$data       		= [];
			$i      			= 0;
			foreach ($ruangpolis as $ruangpoli) {
				$j  			= 0;
				$ruangpoli   	= $ruangpoli->poli;
				$rooms  		= Ruangan::where('poli', $ruangpoli)->get();
				foreach ($rooms as $room) {
					$data['ruangans'][$i][$j]['id']  =   $room->id;
					$data['ruangans'][$i][$j]['ruangan']  =   $room->ruangan;
					$j++;
				}
				$i++;
			}
	
			$x  =   0;
			foreach ($ruangpolis as $ruangpoli) {
				$data['ruangpolis'][$x]  =   $ruangpoli->poli;
				$x++;
			}
	
			$data['polis']      = $polis;
			$data['dokters']    = $dokters;
			$data['answers']   	= $answers;
			$data['details']    = $details;
	
			return view('admin.list', $data);
		}
    	
    }
    public function getList(Request $request) {
		$jenis 		    = 	$request->input('jenis');
		$mulai 		    = 	$request->input('mulai');
		$akhir 		    = 	$request->input('akhir');
		$lokasi		    = 	$request->input('poli');
		$valcari	    = 	$request->input('valcari');
		$master	        = 	$request->input('master');
		$lamanurlhasil 	= url('/').'/hasil/';
		if ($jenis === null){$jenis = '';}
		if ($master == 'logbook') {
			list($start, $end) = explode(' - ', $mulai);
			$startDate 	= Carbon::createFromFormat('m/d/Y', $start)->format('Y-m-d');
			$endDate 	= Carbon::createFromFormat('m/d/Y', $end)->format('Y-m-d');
			$arraylist 	= Periksa::select('id', 'mulai', 'akhir', 'noloket', 'nofoto', 'noregister', 'asalpasien', 'nmrs', 'pasien_id', 'nmpasien', 'jkpasien', 'tgllahirpasien', 'tlppasien', 'alamatpasien', 'reques', 'usia', 'berat', 'ruangan_id', 'ruangan', 'dokter_id', 'ppdssenior', 'middleppds', 'ppdsjunior', 'ppdsmiddle2', 'ppdsjunior2', 'analis', 'excutor', 'nmdokter', 'nmppdssenior', 'nmmiddleppds', 'nmppdsjunior', 'nmppdsmiddle2', 'nmppdsjunior2', 'nmanalis', 'nmexcutor', 'klinisi', 'klinis', 'keterangan', 'kesimpulan', 'poli_id', 'asuransi', 'urgensi', 'diagnosa', 'diagnosa2', 'modality',  'dlp', 'daftar', 'foto', 'tgldraft', 'nmdrafter', 'baca', 'nmpembaca', 'verifikasi', 'export', 'tgladendum', 'nmadendum', 'filefoto', 'status',  'created_at', 'updated_at', 
							DB::raw("CASE
									WHEN ppdssenior = $valcari THEN 'SENIOR'
									WHEN middleppds = $valcari THEN 'MIDDLE'
									WHEN ppdsjunior = $valcari THEN 'JUNIOR'
									WHEN ppdsmiddle2 = $valcari THEN 'MIDDLE'
									ELSE 'JUNIOR'
								END AS role"));
			$arraylist 	= $arraylist->whereBetween('daftar', [$startDate, $endDate]);
			$arraylist 	= $arraylist->where(function($query) use ($valcari) {
				$query->where('ppdssenior', $valcari)
					  ->orWhere('middleppds', $valcari)
					  ->orWhere('ppdsjunior', $valcari)
					  ->orWhere('ppdsmiddle2', $valcari)
					  ->orWhere('ppdsjunior2', $valcari);
			})->get();
        } else if ($master == 'pengambilan') {
			$arraylist = Periksa::select(
				DB::raw("CONCAT('<span class=\"badge badge-success\">', nofoto, '</span>') as tlsnofoto"),
				DB::raw("CONCAT('<span class=\"badge badge-success\">', noregister, '</span>') as tlsnoregister"),
				DB::raw("CONCAT('<span class=\"badge badge-success\">', nmpasien, '</span>') as tlsnama"),
				DB::raw("CONCAT('<span class=\"badge badge-success\">', urgensi, '</span>') as tlsurgensi"),
				DB::raw("CONCAT('<span class=\"badge badge-success\">', reques, '</span>') as tlsreques"),
				DB::raw("CONCAT('<span class=\"badge badge-success\">', ruangan, '</span>') as tlsruangan"),
				DB::raw("CONCAT('<span class=\"badge badge-success\">', daftar, '</span>') as tlsdaftar"),
				DB::raw("CONCAT('<span class=\"badge badge-success\">', status, '</span>') as tlsstatus"),
				DB::raw("CONCAT('<span class=\"badge badge-success\">', asalpasien, '</span>') as asalpasien"),
				'tlppasien as telpon', 'ruangan_id as idruangan', 'dokter_id as iddokter', 'id', 'daftar', 'nofoto', 'noloket', 'nmdokter as dokter')->whereNotNull('verifikasi')->where('status', '!=', 'Arsip');
			if ($valcari != ''){
				if ($jenis == 'norm'){
					$arraylist = $arraylist->where('noregister', $valcari);
				} else {
					$arraylist = $arraylist->where('nofoto', $valcari);
				}
			} 
			if ($akhir != '' AND $mulai != $akhir){
				$arraylist = $arraylist->whereBetween('daftar', [$mulai, $akhir]);
			} else {
				if ($jenis == 'ThisDay' OR $jenis == ''){
					if ($mulai == '' OR is_null($mulai)){ $mulai = date('Y-m-d'); }
					$arraylist = $arraylist->whereDate('daftar', $mulai);	
				} else if ($jenis == 'mundur2'){
					$arraylist = $arraylist->whereDate('daftar',  '>=', Carbon::today()->subDays(2));
				} else if ($jenis == 'mundur3'){
					$arraylist = $arraylist->whereDate('daftar',  '>=', Carbon::today()->subDays(2));
				} else {
					
				}
			}
			$arraylist = $arraylist->get();
        } else if ($master == 'verificationppds') {
			$arraylist = Periksa::select(
							'id', 'mulai', 'akhir', 'orderid', 'noloket', 'nofoto', 'noregister', 'asalpasien', 'nmrs', 'pasien_id', 'nmpasien',
							'jkpasien', 'tgllahirpasien', 'tlppasien', 'alamatpasien', 'reques', 'usia', 'berat', 'ktp', 'bpjs', 'ruangan_id',
							'ruangan', 'dokter_id', 'ppdssenior', 'middleppds', 'ppdsjunior', 'ppdsmiddle2', 'ppdsjunior2', 'analis', 'excutor',
							'nmdokter', 'nmppdssenior', 'nmmiddleppds', 'nmppdsjunior', 'nmppdsmiddle2', 'nmppdsjunior2', 'nmanalis', 'nmexcutor',
							'klinisi', 'klinis', 'keterangan', 'kesimpulan', 'poli_id', 'asuransi', 'urgensi', 'diagnosa', 'diagnosa2', 'modality',
							'kd_spesimen', 'nm_spesimen', 'dlp', 'daftar', 'foto', 'tgldraft', 'nmdrafter', 'baca', 'nmpembaca', 'verifikasi',
							'export', 'tgladendum', 'nmadendum', 'filefoto', 'pendaftar', 'status', 'created_at', 'updated_at',
							'bhp_media', 'bhp_potsputum', 'bhp_poturine', 'bhp_oshe', 'bhp_obyekglass', 'bhp_botolbd', 'bhp_parafilm',
							'bhp_tips', 'bhp_cottonswab', 'bhp_antibiotiktambahan', 'id_esbl', 'id_mrsa', 'rating', 'nmpendaftar',

							DB::raw("DATE(daftar) as daftartgl"),
							DB::raw("TO_CHAR(daftar, 'HH24:MI:SS') as daftarjam"),
							DB::raw("TO_CHAR(export, 'HH24:MI:SS') as cetakjam"),
							DB::raw("TO_CHAR(verifikasi, 'HH24:MI:SS') as verifikasijam"),
							DB::raw("TO_CHAR(baca, 'HH24:MI:SS') as bacajam"),
							DB::raw("TO_CHAR(foto, 'HH24:MI:SS') as fotojam"),

							DB::raw("CASE WHEN jkpasien = 'L' THEN '1' ELSE '' END AS l"),
							DB::raw("CASE WHEN jkpasien = 'P' THEN '1' ELSE '' END AS p"),
							DB::raw("CONCAT('https://10.10.123.218/hasil/', nofoto::text) as urlhasil"),

							DB::raw("CASE WHEN asuransi IN ('JKN', 'BPJS') THEN '1' ELSE '' END AS jkn"),
							DB::raw("CASE WHEN asuransi = 'Billing' THEN '1' ELSE '' END AS billing"),
							DB::raw("CASE WHEN asuransi = 'GCU' THEN '1' ELSE '' END AS gcu"),
							DB::raw("CASE WHEN asuransi IN ('MANDIRI', 'Umum') THEN '1' ELSE '' END AS umum"),
							DB::raw("CASE WHEN asuransi = 'SWASTA' THEN '1' ELSE '' END AS swasta"),
							DB::raw("CASE WHEN asuransi = 'TAG' THEN '1' ELSE '' END AS tag"),

							DB::raw("CASE WHEN urgensi != 'CITO' THEN '1' ELSE '' END AS elective"),
							DB::raw("CASE WHEN urgensi = 'CITO' THEN '1' ELSE '' END AS cito"),

							DB::raw("CASE
								WHEN status IS NULL THEN '<span class=\"badge badge-danger\">NEW</span>'
								WHEN status LIKE '%Dibatalkan%' THEN '<font color=grey>Batal</font>'
								WHEN status LIKE '%Pemeriksaan Sampel%' THEN '<font color=green>Pemeriksaan Sampel</font>'
								WHEN status LIKE '%Proses Analisis Sampel%' THEN '<strong>Diperiksa</strong>'
								WHEN status LIKE '%Draft%' THEN '<font color=green>Draft</font>'
								WHEN status LIKE '%Expertise%' THEN '<strong><font color=green>Expertise</font></strong>'
								WHEN status LIKE '%Decliend%' THEN '<strong><font color=red>Decliend</font></strong>'
								WHEN status LIKE '%Selesai%' THEN '<span class=\"badge badge-success\">Selesai</span>'
								WHEN status LIKE '%Arsip%' THEN '<span class=\"badge badge-primary\">Arsip</span>'
								ELSE '<font color=red>Unkown</font>'
							END AS tlsstatus"),

							DB::raw("CASE
								WHEN status IS NULL THEN CONCAT('<span class=\"badge badge-danger\">', nofoto::text, '</span>')
								WHEN status LIKE '%Dibatalkan%' THEN CONCAT('<font color=grey>', nofoto::text, '</font>')
								WHEN status LIKE '%Pemeriksaan Sampel%' THEN CONCAT('<font color=green>', nofoto::text, '</font>')
								WHEN status LIKE '%Proses Analisis Sampel%' THEN CONCAT('<strong>', nofoto::text, '</strong>')
								WHEN status LIKE '%Draft%' THEN CONCAT('<font color=green>', nofoto::text, '</font>')
								WHEN status LIKE '%Expertise%' THEN CONCAT('<strong><font color=green>', nofoto::text, '</font></strong>')
								WHEN status LIKE '%Decliend%' THEN CONCAT('<strong><font color=red>', nofoto::text, '</font></strong>')
								WHEN status LIKE '%Selesai%' THEN CONCAT('<span class=\"badge badge-success\">', nofoto::text, '</span>')
								WHEN status LIKE '%Arsip%' THEN CONCAT('<span class=\"badge badge-primary\">', nofoto::text, '</span>')
								ELSE CONCAT('<font color=red>', nofoto::text, '</font>')
							END AS tlsnofoto"),

							DB::raw("CASE
								WHEN status IS NULL THEN CONCAT('<span class=\"badge badge-danger\">', noregister::text, '</span>')
								WHEN status LIKE '%Dibatalkan%' THEN CONCAT('<font color=grey>', noregister::text, '</font>')
								WHEN status LIKE '%Pemeriksaan Sampel%' THEN CONCAT('<font color=green>', noregister::text, '</font>')
								WHEN status LIKE '%Proses Analisis Sampel%' THEN CONCAT('<strong>', noregister::text, '</strong>')
								WHEN status LIKE '%Draft%' THEN CONCAT('<font color=green>', noregister::text, '</font>')
								WHEN status LIKE '%Expertise%' THEN CONCAT('<strong><font color=green>', noregister::text, '</font></strong>')
								WHEN status LIKE '%Decliend%' THEN CONCAT('<strong><font color=red>', noregister::text, '</font></strong>')
								WHEN status LIKE '%Selesai%' THEN CONCAT('<span class=\"badge badge-success\">', noregister::text, '</span>')
								WHEN status LIKE '%Arsip%' THEN CONCAT('<span class=\"badge badge-primary\">', noregister::text, '</span>')
								ELSE CONCAT('<font color=red>', noregister::text, '</font>')
							END AS tlsnoregister"),

							DB::raw("CASE
								WHEN status IS NULL THEN CONCAT('<span class=\"badge badge-danger\">', nmpasien::text, '</span>')
								WHEN status LIKE '%Dibatalkan%' THEN CONCAT('<font color=grey>', nmpasien::text, '</font>')
								WHEN status LIKE '%Pemeriksaan Sampel%' THEN CONCAT('<font color=green>', nmpasien::text, '</font>')
								WHEN status LIKE '%Proses Analisis Sampel%' THEN CONCAT('<strong>', nmpasien::text, '</strong>')
								WHEN status LIKE '%Draft%' THEN CONCAT('<font color=green>', nmpasien::text, '</font>')
								WHEN status LIKE '%Expertise%' THEN CONCAT('<strong><font color=green>', nmpasien::text, '</font></strong>')
								WHEN status LIKE '%Decliend%' THEN CONCAT('<strong><font color=red>', nmpasien::text, '</font></strong>')
								WHEN status LIKE '%Selesai%' THEN CONCAT('<span class=\"badge badge-success\">', nmpasien::text, '</span>')
								WHEN status LIKE '%Arsip%' THEN CONCAT('<span class=\"badge badge-primary\">', nmpasien::text, '</span>')
								ELSE CONCAT('<font color=red>', nmpasien::text, '</font>')
							END AS tlsnama"),

							DB::raw("CASE
								WHEN status IS NULL THEN CONCAT('<span class=\"badge badge-danger\">', reques::text, '</span>')
								WHEN status LIKE '%Dibatalkan%' THEN CONCAT('<font color=grey>', reques::text, '</font>')
								WHEN status LIKE '%Pemeriksaan Sampel%' THEN CONCAT('<font color=green>', reques::text, '</font>')
								WHEN status LIKE '%Proses Analisis Sampel%' THEN CONCAT('<strong>', reques::text, '</strong>')
								WHEN status LIKE '%Draft%' THEN CONCAT('<font color=green>', reques::text, '</font>')
								WHEN status LIKE '%Expertise%' THEN CONCAT('<strong><font color=green>', reques::text, '</font></strong>')
								WHEN status LIKE '%Decliend%' THEN CONCAT('<strong><font color=red>', reques::text, '</font></strong>')
								WHEN status LIKE '%Selesai%' THEN CONCAT('<span class=\"badge badge-success\">', reques::text, '</span>')
								WHEN status LIKE '%Arsip%' THEN CONCAT('<span class=\"badge badge-primary\">', reques::text, '</span>')
								ELSE CONCAT('<font color=red>', reques::text, '</font>')
							END AS tlsreques")
						);

			
			if ($lokasi == 'allspv'){
				$arraylist = $arraylist->where('status', 'LIKE', 'Expertise Saved (Un Verified)')->whereNull('periksa.verifikasi');
			} else {
				$arraylist = $arraylist->where('status', 'LIKE', 'Expertise Saved (Un Verified)')->whereNull('periksa.verifikasi')->where('periksa.dokter_id', Session('id'));
			}
			$arraylist = $arraylist->get();
        } else {
			$arraylist = Periksa::select(
							'id', 'mulai', 'akhir', 'orderid', 'noloket', 'nofoto', 'noregister', 'asalpasien', 'nmrs', 'pasien_id', 'nmpasien',
							'jkpasien', 'tgllahirpasien', 'tlppasien', 'alamatpasien', 'reques', 'usia', 'berat', 'ktp', 'bpjs', 'ruangan_id',
							'ruangan', 'dokter_id', 'ppdssenior', 'middleppds', 'ppdsjunior', 'ppdsmiddle2', 'ppdsjunior2', 'analis', 'excutor',
							'nmdokter', 'nmppdssenior', 'nmmiddleppds', 'nmppdsjunior', 'nmppdsmiddle2', 'nmppdsjunior2', 'nmanalis', 'nmexcutor',
							'klinisi', 'klinis', 'keterangan', 'kesimpulan', 'poli_id', 'asuransi', 'urgensi', 'diagnosa', 'diagnosa2', 'modality',
							'kd_spesimen', 'nm_spesimen', 'dlp', 'daftar', 'foto', 'tgldraft', 'nmdrafter', 'baca', 'nmpembaca', 'verifikasi',
							'export', 'tgladendum', 'nmadendum', 'filefoto', 'pendaftar', 'status', 'created_at', 'updated_at',
							'bhp_media', 'bhp_potsputum', 'bhp_poturine', 'bhp_oshe', 'bhp_obyekglass', 'bhp_botolbd', 'bhp_parafilm',
							'bhp_tips', 'bhp_cottonswab', 'bhp_antibiotiktambahan', 'id_esbl', 'id_mrsa', 'rating', 'nmpendaftar',

							DB::raw("DATE(daftar) as daftartgl"),
							DB::raw("TO_CHAR(daftar, 'HH24:MI:SS') as daftarjam"),
							DB::raw("TO_CHAR(export, 'HH24:MI:SS') as cetakjam"),
							DB::raw("TO_CHAR(verifikasi, 'HH24:MI:SS') as verifikasijam"),
							DB::raw("TO_CHAR(baca, 'HH24:MI:SS') as bacajam"),
							DB::raw("TO_CHAR(foto, 'HH24:MI:SS') as fotojam"),

							DB::raw("CASE WHEN jkpasien = 'L' THEN '1' ELSE '' END AS l"),
							DB::raw("CASE WHEN jkpasien = 'P' THEN '1' ELSE '' END AS p"),
							DB::raw("CONCAT('https://10.10.123.218/hasil/', nofoto::text) as urlhasil"),

							DB::raw("CASE WHEN asuransi IN ('JKN', 'BPJS') THEN '1' ELSE '' END AS jkn"),
							DB::raw("CASE WHEN asuransi = 'Billing' THEN '1' ELSE '' END AS billing"),
							DB::raw("CASE WHEN asuransi = 'GCU' THEN '1' ELSE '' END AS gcu"),
							DB::raw("CASE WHEN asuransi IN ('MANDIRI', 'Umum') THEN '1' ELSE '' END AS umum"),
							DB::raw("CASE WHEN asuransi = 'SWASTA' THEN '1' ELSE '' END AS swasta"),
							DB::raw("CASE WHEN asuransi = 'TAG' THEN '1' ELSE '' END AS tag"),

							DB::raw("CASE WHEN urgensi != 'CITO' THEN '1' ELSE '' END AS elective"),
							DB::raw("CASE WHEN urgensi = 'CITO' THEN '1' ELSE '' END AS cito"),

							DB::raw("CASE
								WHEN status IS NULL THEN '<span class=\"badge badge-danger\">NEW</span>'
								WHEN status LIKE '%Dibatalkan%' THEN '<font color=grey>Batal</font>'
								WHEN status LIKE '%Pemeriksaan Sampel%' THEN '<font color=green>Pemeriksaan Sampel</font>'
								WHEN status LIKE '%Proses Analisis Sampel%' THEN '<strong>Diperiksa</strong>'
								WHEN status LIKE '%Draft%' THEN '<font color=green>Draft</font>'
								WHEN status LIKE '%Expertise%' THEN '<strong><font color=green>Expertise</font></strong>'
								WHEN status LIKE '%Decliend%' THEN '<strong><font color=red>Decliend</font></strong>'
								WHEN status LIKE '%Selesai%' THEN '<span class=\"badge badge-success\">Selesai</span>'
								WHEN status LIKE '%Arsip%' THEN '<span class=\"badge badge-primary\">Arsip</span>'
								WHEN status LIKE '%Data Vitek di Terima%' THEN '<span class=\"badge badge-warning\">Data Vitek di Terima</span>'
								WHEN status LIKE '%Data BD di Terima%' THEN '<span class=\"badge badge-info\">Data BD di Terima</span>'
								ELSE CONCAT('<span class=\"badge badge-secondary\">', status::text, '</span>')
							END AS tlsstatus"),

							DB::raw("CASE
								WHEN status IS NULL THEN CONCAT('<span class=\"badge badge-danger\">', nofoto::text, '</span>')
								WHEN status LIKE '%Dibatalkan%' THEN CONCAT('<font color=grey>', nofoto::text, '</font>')
								WHEN status LIKE '%Pemeriksaan Sampel%' THEN CONCAT('<font color=green>', nofoto::text, '</font>')
								WHEN status LIKE '%Proses Analisis Sampel%' THEN CONCAT('<strong>', nofoto::text, '</strong>')
								WHEN status LIKE '%Draft%' THEN CONCAT('<font color=green>', nofoto::text, '</font>')
								WHEN status LIKE '%Expertise%' THEN CONCAT('<strong><font color=green>', nofoto::text, '</font></strong>')
								WHEN status LIKE '%Decliend%' THEN CONCAT('<strong><font color=red>', nofoto::text, '</font></strong>')
								WHEN status LIKE '%Selesai%' THEN CONCAT('<span class=\"badge badge-success\">', nofoto::text, '</span>')
								WHEN status LIKE '%Arsip%' THEN CONCAT('<span class=\"badge badge-primary\">', nofoto::text, '</span>')
								ELSE CONCAT('<font color=red>', nofoto::text, '</font>')
							END AS tlsnofoto"),

							DB::raw("CASE
								WHEN status IS NULL THEN CONCAT('<span class=\"badge badge-danger\">', noregister::text, '</span>')
								WHEN status LIKE '%Dibatalkan%' THEN CONCAT('<font color=grey>', noregister::text, '</font>')
								WHEN status LIKE '%Pemeriksaan Sampel%' THEN CONCAT('<font color=green>', noregister::text, '</font>')
								WHEN status LIKE '%Proses Analisis Sampel%' THEN CONCAT('<strong>', noregister::text, '</strong>')
								WHEN status LIKE '%Draft%' THEN CONCAT('<font color=green>', noregister::text, '</font>')
								WHEN status LIKE '%Expertise%' THEN CONCAT('<strong><font color=green>', noregister::text, '</font></strong>')
								WHEN status LIKE '%Decliend%' THEN CONCAT('<strong><font color=red>', noregister::text, '</font></strong>')
								WHEN status LIKE '%Selesai%' THEN CONCAT('<span class=\"badge badge-success\">', noregister::text, '</span>')
								WHEN status LIKE '%Arsip%' THEN CONCAT('<span class=\"badge badge-primary\">', noregister::text, '</span>')
								ELSE CONCAT('<font color=red>', noregister::text, '</font>')
							END AS tlsnoregister"),

							DB::raw("CASE
								WHEN status IS NULL THEN CONCAT('<span class=\"badge badge-danger\">', nmpasien::text, '</span>')
								WHEN status LIKE '%Dibatalkan%' THEN CONCAT('<font color=grey>', nmpasien::text, '</font>')
								WHEN status LIKE '%Pemeriksaan Sampel%' THEN CONCAT('<font color=green>', nmpasien::text, '</font>')
								WHEN status LIKE '%Proses Analisis Sampel%' THEN CONCAT('<strong>', nmpasien::text, '</strong>')
								WHEN status LIKE '%Draft%' THEN CONCAT('<font color=green>', nmpasien::text, '</font>')
								WHEN status LIKE '%Expertise%' THEN CONCAT('<strong><font color=green>', nmpasien::text, '</font></strong>')
								WHEN status LIKE '%Decliend%' THEN CONCAT('<strong><font color=red>', nmpasien::text, '</font></strong>')
								WHEN status LIKE '%Selesai%' THEN CONCAT('<span class=\"badge badge-success\">', nmpasien::text, '</span>')
								WHEN status LIKE '%Arsip%' THEN CONCAT('<span class=\"badge badge-primary\">', nmpasien::text, '</span>')
								ELSE CONCAT('<font color=red>', nmpasien::text, '</font>')
							END AS tlsnama"),

							DB::raw("CASE
								WHEN status IS NULL THEN CONCAT('<span class=\"badge badge-danger\">', reques::text, '</span>')
								WHEN status LIKE '%Dibatalkan%' THEN CONCAT('<font color=grey>', reques::text, '</font>')
								WHEN status LIKE '%Pemeriksaan Sampel%' THEN CONCAT('<font color=green>', reques::text, '</font>')
								WHEN status LIKE '%Proses Analisis Sampel%' THEN CONCAT('<strong>', reques::text, '</strong>')
								WHEN status LIKE '%Draft%' THEN CONCAT('<font color=green>', reques::text, '</font>')
								WHEN status LIKE '%Expertise%' THEN CONCAT('<strong><font color=green>', reques::text, '</font></strong>')
								WHEN status LIKE '%Decliend%' THEN CONCAT('<strong><font color=red>', reques::text, '</font></strong>')
								WHEN status LIKE '%Selesai%' THEN CONCAT('<span class=\"badge badge-success\">', reques::text, '</span>')
								WHEN status LIKE '%Arsip%' THEN CONCAT('<span class=\"badge badge-primary\">', reques::text, '</span>')
								ELSE CONCAT('<font color=red>', reques::text, '</font>')
							END AS tlsreques")
						);

			
			
			if ($jenis == 'ThisDay') {
				$arraylist = $arraylist->where(function($q) {
								$q->where(function($sub) {
									$sub->whereNotIn('status', ['Selesai', 'Arsip'])
										->where('status', 'not like', 'Dibatalkan%');
								})->orWhereNull('status');
							});
			} else if ($jenis == 'ThisDayPendaftaran') {
				$arraylist = $arraylist->whereDate('daftar', Carbon::today());
			} else if ($jenis == 'mundur2') {
				$arraylist = $arraylist->whereDate('daftar', '>=', Carbon::today()->subDays(2));
			} else if ($jenis == 'mundur3') {
				$arraylist = $arraylist->whereDate('daftar', '>=', Carbon::today()->subDays(3));
			} else if ($jenis === 'searchbydate') {
				$mulai = $mulai ?: date("Y-m-d");
				$akhir = $akhir ?: date("Y-m-d");
				if ($mulai >= $akhir) {
					$akhir = date('Y-m-d', strtotime($akhir . ' +1 day'));
				}
				$arraylist = $arraylist->whereBetween('daftar', [$mulai, $akhir]);
			} else if ($jenis === 'searchbydatemanual') {
				$mulai = $mulai ?: date("Y-m-d");
				$akhir = $akhir ?: date("Y-m-d");
				if ($mulai >= $akhir) {
					$akhir = date('Y-m-d', strtotime($akhir . ' +1 day'));
				}
				$arraylist = $arraylist->whereBetween('daftar', [$mulai, $akhir])->whereNotIn('pendaftar', ['supervisor', 'admin', 'analis', 'ppds', 'developer']);
			} else if ($jenis === 'norm'){
				$arraylist = $arraylist->where('noregister', 'LIKE', '%'.$valcari.'%');
			} else if ($jenis === 'nofoto'){
				$arraylist = $arraylist->where('nofoto', 'LIKE', '%'.$valcari.'%');
			} else if ($jenis === 'rekues'){
				$arraylist = $arraylist->where('reques', 'LIKE', '%'.$valcari.'%');
			} else if ($jenis === 'keterangan'){
				$arraylist = $arraylist->where('keterangan', 'LIKE', '%'.$valcari.'%');
			} else if ($jenis === 'kesimpulan'){
				$arraylist = $arraylist->where('kesimpulan', 'LIKE', '%'.$valcari.'%');
			} else {
				$arraylist = $arraylist->where('nmpasien', 'LIKE', '%'.$valcari.'%');
			}
			$arraylist = $arraylist->orderBy('id', 'DESC')->get();
		}
		echo json_encode($arraylist);
    }
    public function listDetail(Request $request) {
    	$id 		= 	$request->input('id');
    	$periksa 	= 	DB::table('periksa')->where('id', $id)->first();
        echo json_encode($periksa);
    }
    public function delete(Request $request) {
    	$id 		=	$request->input('val01');
    	$alasan 	= 	$request->input('val02');
		$tabel 		= 	$request->input('val03');
		if ($id == 'buatulang'){
			$getdata= 	Periksa::where('id', $request->input('val02'))->first();
			if (isset($getdata->nofoto)){
				$ceksudah 	= Periksa::where('nofoto', 'LIKE', $getdata->nofoto.'_%')->count();
				$nomorbaru 	= $ceksudah++;
				$periksaid 			= Periksa::insertGetId([
					'mulai'        	=>  $getdata->mulai,
					'akhir'        	=>  $getdata->akhir,
					'noloket'       =>  $getdata->noloket,
					'nofoto'        =>  $getdata->nofoto.'_'.$nomorbaru,
					'noregister'    =>  $getdata->noregister,
					'asalpasien'    =>  $getdata->asalpasien,
					'nmrs'    		=>  $getdata->nmrs,
					'pasien_id'     =>  $getdata->pasien_id,
					'nmpasien'		=> 	$getdata->nmpasien,
					'jkpasien'		=> 	$getdata->jkpasien,
					'ktp'			=> 	$getdata->ktp,
					'bpjs'			=> 	$getdata->bpjs,
					'tgllahirpasien'=> 	$getdata->tgllahirpasien,
					'tlppasien'		=> 	$getdata->tlppasien,
					'alamatpasien'	=> 	$getdata->alamatpasien,
					'reques'     	=>  $getdata->reques,
					'usia'          =>  $getdata->usia,
					'berat'         =>  $getdata->berat,
					'ruangan_id'    =>  $getdata->ruangan_id,
					'ruangan'    	=>  $getdata->ruangan,
					'klinisi'       =>  $getdata->klinisi,
					'klinis'		=>  $getdata->klinis,
					'poli_id'       =>  $getdata->poli_id,
					'kd_spesimen'	=>  $getdata->kd_spesimen,
					'nm_spesimen'	=>  $getdata->nm_spesimen,
					'keterangan'    =>  '',
					'kesimpulan'    =>  '',
					'asuransi'      =>  $getdata->asuransi,
					'urgensi'       =>  $getdata->urgensi,
					'daftar'        =>  $getdata->daftar,
					'pendaftar'   	=>  Session('id'),
					'nmpendaftar'	=>  Session('nama'),
					'orderid'		=> 	$getdata->orderid,
				]);
				try {
					if ($getdata->kd_spesimen != '' AND $getdata->nm_spesimen != ''){
						PendaftaranOnListiner::updateOrCreate(
							[
								'rnoreg'	=> $getdata->nofoto,
							],
							[
								'rtglast'		=> date('Y-m-d'),
								'norm'			=> $getdata->noregister,
								'nama'			=> $getdata->nmpasien,
								'alamat'		=> $getdata->alamatpasien,
								'telp'			=> $getdata->tlppasien,
								'hp'			=> $getdata->tlppasien,
								'tgllahir'		=> $getdata->tgllahirpasien,
								'umur'			=> $getdata->usia,
								'rjenis'		=> $getdata->jkpasien,
								'kodedok'		=> Session('id'),
								'namadok'		=> Session('nama'),
								'ruangan'		=> $getdata->ruangan,
								'tes'			=> '',
								'alat'			=> 'All',
								'kd_spesimen'	=> $getdata->kd_spesimen,
								'nm_spesimen'	=> $getdata->nm_spesimen,
							]
						);
					}
					return response()->json(['status' => 'Sukses', 'message' => 'Data Order Berhasil di Gandakan'], 201);
				} catch (Exception $e) {
					return response()->json(['status' => 'Sukses', 'message' => 'Data Order Berhasil di Gandakan'], 201);
				}
			} else {
				return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Gagal', 'message' => 'Data Tidak Valid']);
				return back();
			}
		} else {
			if ($alasan == ''){
				return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Gagal', 'message' => 'Mohon isi semua form']);
				return back();
			} else {
				if ($tabel == 'aktif'){
					$alasan		= 'Dibatalkan dengan alasan '.$alasan;
					$input 		= Periksa::where('id', $id)->update([
						'noloket'	=> null,
						'status'	=> 	$alasan
					]);
					$pesan 		= 'Order Expertise Canceled';
				} else if ($tabel == 'arsip'){
					$total = 0;
					$input = Periksa::whereIn('id', $id)->update([
						'status'=> 	'Arsip'
					]);
					if ($input){
						$total++;
					} 
					$pesan 		= 'Expertise Archieved '.$total;
				} else {
					$alasan		= 'Dibatalkan dengan alasan '.$alasan;
					$input 		= Jadwalperiksa::where('id', $id)->update([
						'noloket'	=> null,
						'status'	=> 	$alasan
					]);
					$pesan 		= 'Schedulling Canceled';
				}
				if ($input){
					return response()->json(['icon' => 'success', 'warna' => '#5ba035', 'status' => 'Sukses', 'message' => $pesan]);
					return back();
				}else {
					return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Gagal', 'message' => 'System Down, please try again in a few years...']);
					return back();
				}
			}
		}
		
    }
}
