<?php

namespace App\Controllers;

use CodeIgniter\RESTful\ResourceController;
use CodeIgniter\I18n\Time;
use Ramsey\Uuid\Uuid;

class API_Results extends ResourceController {

	public function results($accessnumber) {
		$db = \Config\Database::connect();

		$sql = "select r.HOSTORDERNUMBER, r.SP_HOSPNUMBER, cr.LOC, cr.COMPANY, cr.AGENT from SP_REQUESTS r
			left join cmod.dbo.CM_TM_REQUESTS cr on cr.REFFID=r.HOSTORDERNUMBER
			where r.SP_ACCESSNUMBER='$accessnumber'";
		$query = $db->query($sql);
		$results = $query->getResultArray();

		$visitnumber = $results[0]['SP_HOSPNUMBER'];
		$reffid = $results[0]['HOSTORDERNUMBER'];
		$location = $results[0]['LOC'];
		$payer_name = $results[0]['AGENT'];
		$company = $results[0]['COMPANY'];
		$createdt = gmdate('Y-m-d\TH:i:s.v\Z');

		$response = [
			"headers" => [
				"Content-Type" => "application/json",
				"Authorization" => "NasiGorengBambuRuncing",
				"AppCode" => "2"
			],
			"data" => [
				"reference_id" => "$reffid",
				"created" => "$createdt", 
				"company_name" => $company,
				"branch" => $location,
				"test_results" => []
			]
		];

		$sql = "select dc.CHAPID, t.DEPTH as depth_test, dc.FULLTEXT as chap_eng, dc.FULLTEXT as chap_ind, st.FULLTEXT as serum_type, cdt.TEXT1 as test_eng, cdt.TEXT2 as test_ind, cdt.UNIT as UNITTEXT, cdt.REFFTEXT, t.NOTPRINTABLE,
  t.TESTORDER, t.RESTYPE as code_type, t.VALIDATIONINITIALS  as validator, dt.SHORTTEXT,
  RESULT = case when t.RESVALUE is null then tx.FULLTEXT else t.RESVALUE end, cr.*, ct.*, tub.TUBENAME
from REQUESTS r 
left join TESTS t on t.REQUESTID = r.REQUESTID 
left join DICT_TESTS dt on dt.ENDVALIDDATE is null and t.TESTID=dt.TESTID
left join DICT_TEXTS tx on tx.TEXTID=t.CODEDRESULTID
left join DICT_CHAPTERS	dc on dc.CHAPID=dt.CHAPID and dc.ENDVALIDDATE is null
left join DICT_TEST_SAMPLES ts on ts.TESTID=t.TESTID and dt.TESTID=ts.TESTID
left join DICT_SAMPLES_TYPES st on st.SAMPTYPEID=ts.SAMPTYPEID
left join cmod.dbo.CM_DICT_TESTS cdt on dt.TESTCODE=cdt.TESTCODE
left join cmod.dbo.CM_RESULTS cr on cr.ACCESSNUMBER=r.ACCESSNUMBER and cr.TESTCODE=cdt.TESTCODE and cr.TESTCODE=dt.TESTCODE
left join cmod.dbo.CM_DICT_MAPPINGS m on m.LISCODE=cr.TESTCODE
left join cmod.dbo.CM_TM_TESTS ct on ct.HISCODE=m.HISCODE
left join cmod.dbo.CM_DICT_TUBES tub on tub.TUBEID=m.TUBEID
where r.ACCESSNUMBER='$accessnumber' 
ORDER BY dc.CHAPID, depth_test, t.TESTORDER";
        $query = $db->query($sql);
        $test_results = $query->getResultArray();		
		$d0 = 0;
		foreach ($test_results as $result) {
			$refftestid = trim($result['REFFTESTID']);
			$hiscode = $result['HISCODE'];
			$tubename = $result['TUBENAME'];
			$depth = $result['depth_test'];
			if ( $depth == 0) {
				$data = [
					"test_ref_id"=> "$refftestid",
					"service_id"=> "$hiscode",
					"service_name"=> $result['test_eng'],
					"test_medium"=> $result['serum_type'],
					"test_container"=> "$tubename",
					"chapter_type"=> $result['chap_eng'], 
					# "test_summary"=> "",		
					# "status"=> "", 
					"subtest_results" => [],
				];
				array_push($response['data']['test_results'], $data);
				$d0++;
			} else if ($depth == 1) {
				// $data = [
				// 	"subtest_ref_id"=> $uuid,
                //     "subtest_name"=> $result['SHORTTEXT'],
                //     "subtest_uom"=> $result['TESTCODE'],
                //     "subtest_lower_limit"=> "",
                //     "subtest_upper_limit"=> "",
                //     "subtest_critical_lower_limit"=> "",
                //     "subtest_critiacl_upper_limit"=> "",
                //     "subtest_result"=> "",
                //     "subtest_summary"=> "",
				// 	"sub_subtest_results" => [],
				// ];
				// array_push($response['data']['test_results']['subtest_results'], $data);
			}

		}

	
        return $this->respond($response,200);
    }

}