<?php
namespace App\Controllers;

use CodeIgniter\RESTful\ResourceController;

class API_Dashboard extends ResourceController {
	protected $format = 'json';
    
	public function index() {

		// Mengetahui Apakah User Login adalah Bali atau Surabaya
		$cityid = session()->get('usercityid');
		if ($cityid == 1) {
			$filter_query = " AND (tr.REQNUMBER LIKE 'B%' OR tr.REQNUMBER LIKE 'Z%')";
		} elseif ($cityid == 2) {
			$filter_query = " AND (tr.REQNUMBER LIKE 'S%' OR tr.REQNUMBER LIKE 'X%')";
		} else { $filter_query = " "; }

		$db = \Config\Database::connect();
		$date1 = $this->request->getPost('date1');
		$date2 = $this->request->getPost('date2');
		$sql = "SELECT 
				sr.COLLECTIONDATE, sr.SP_ACCESSNUMBER, sr.HOSTORDERNUMBER as REFFID, tr.REQNUMBER as HOSTORDERNUMBER, tr.REQSTATUS, p.PATNUMBER, concat(tp.FIRSTNAME, ' ', tp.lastname) as 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 REQUESTS r on r.ACCESSNUMBER=sr.SP_ACCESSNUMBER
					left join cmod.dbo.CM_TM_REQUESTS tr on tr.REFFID = sr.HOSTORDERNUMBER
					--left join cmod.dbo.CM_TM_PATIENTS tp on tr.PATID = tp.PATID
					left join PATIENTS p on sr.PATID = p.PATID
					left join cmod.dbo.CM_TM_PATIENTS tp on tp.PATNUMBER = LTRIM(p.patnumber, '0')
					where sr.COLLECTIONDATE between '$date1 00:00' and '$date2 23:59'" 
					.$filter_query.
					"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);
    }
}
