<?php
namespace App\Controllers;

use CodeIgniter\RESTful\ResourceController;

class Dashboard extends ResourceController {
	protected $format = 'json';
    
	public function index() {
		$db = \Config\Database::connect();
		$sql = "select sr.COLLECTIONDATE, sr.SP_ACCESSNUMBER, sr.HOSTORDERNUMBER, p.PATNUMBER, p.NAME,
	TESTS=stuff(( select ', '+'('+T.SP_TESTCODE+')' from
			( select T.SP_TESTCODE from SP_TESTS T 
				where T.SP_ACCESSNUMBER=sr.SP_ACCESSNUMBER
				and T.DEPTH=0 AND T.SP_TESTCODE <> 'Q'
			) as T
			for xml path('')),1,1,''),
case 
when exists (select 1 from AUDIT_TRAIL at where at.ATR_ACCESSNUMBER=sr.SP_ACCESSNUMBER and at.STEPTYPE=2 and at.LIS_SESSION='RFC' ) then 
	case
		when exists (select 1 from SP_TUBES st where st.SP_ACCESSNUMBER=sr.SP_ACCESSNUMBER and st.TUBESTATUS=0 ) then 'PartRecv'
		when exists (select 1 from TESTS T where T.REQUESTID=r.REQUESTID and T.RESTYPE IN (null,'0') ) then 'Inc'
		--when exists (select 1 from TESTS T where T.REQUESTID=r.REQUESTID and T.RESTYPE IN (null,'0') and T.TESTID='1805' ) then 'Inc'
		else 'Comp'
	end
-- inc 
when exists (select 1 from TESTS T where T.RESTYPE not in (0,4) and T.REQUESTID=r.REQUESTID ) then 
	case
		when exists ( select 1 from cmod.dbo.CM_TUBES T where T.ACCESSNUMBER=sr.SP_ACCESSNUMBER and T.COLLSTATUS=0 ) then 'PartColl'
		when exists (select 1 from SP_TUBES st where st.SP_ACCESSNUMBER=sr.SP_ACCESSNUMBER and st.TUBESTATUS=0 ) then 'PartRecv'
		else 'Inc'
	end
--rcv
when not exists (select 1 from SP_TUBES st where st.SP_ACCESSNUMBER=sr.SP_ACCESSNUMBER and st.TUBESTATUS=0 ) then 
	case
		when exists (select 1 from cmod.dbo.CM_TUBES T where T.ACCESSNUMBER=sr.SP_ACCESSNUMBER and T.COLLSTATUS=0 ) then 'PartColl'
		else 'Recv'
	end
--coll
when exists (select 1 FROM cmod.dbo.CM_TUBES T where T.ACCESSNUMBER=sr.SP_ACCESSNUMBER ) then 
	case
		when exists (select 1 from SP_TUBES st where st.SP_ACCESSNUMBER=sr.SP_ACCESSNUMBER and st.TUBESTATUS=4 ) then 'PartRecv'
		when not exists (select 1 from cmod.dbo.CM_TUBES T where T.ACCESSNUMBER=sr.SP_ACCESSNUMBER and T.COLLSTATUS=0 ) then 'Coll'
		when exists (select 1 from cmod.dbo.CM_TUBES T where T.ACCESSNUMBER=sr.SP_ACCESSNUMBER and T.COLLSTATUS=1 ) then 'PartColl'
		else 'Pend'
	end
else 'Pend'
end STATS
	from SP_REQUESTS sr
	left join PATIENTS p on p.PATID=sr.PATID
	left join REQUESTS r on r.ACCESSNUMBER=sr.SP_ACCESSNUMBER
	where sr.COLLECTIONDATE between '2024-11-13 00:00' and '2024-11-21 23:59'
	order by sr.COLLECTIONDATE desc";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['data'] = $results;
		$data['count'] = array_count_values(array_column($results, 'STATS'));
        return $this->respond($data,200);
    }
	
	public function viewAccess($accessnumber): string {
		$db = \Config\Database::connect();
		$sql = "select p.PATNUMBER, p.NAME, sr.HOSTORDERNUMBER, tu.SAMPLETYPE, ds.SHORTTEXT, tu.TUBESTATUS, ct.COLLSTATUS, ct.TUBECOMMENT from SP_TUBES tu
			left join SP_REQUESTS sr on tu.SP_ACCESSNUMBER=sr.SP_ACCESSNUMBER
			left join PATIENTS p on p.PATID=sr.PATID
			left join DICT_SAMPLES_TYPES ds on ds.SAMPCODE= tu.SAMPLETYPE
			left join cmod.dbo.CM_TUBES ct on ct.SAMPLETYPE=tu.SAMPLETYPE and ct.ACCESSNUMBER=tu.SP_ACCESSNUMBER
			where tu.SP_ACCESSNUMBER='$accessnumber'";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['data'] = $results;
		$data['accessnumber'] = $accessnumber;
        return view('dashboard_viewAccess', $data);
    }
}
