<?php

namespace App\Controllers;

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

class API_Results extends ResourceController {

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

		// $sql = "select HOSTORDERNUMBER as HISNO from SP_REQUESTS where SP_ACCESSNUMBER='$accessnumber'";
		// $query = $db->query($sql);
		// $results = $query->getResultArray();
		// $data['LISNO'] = $accessnumber;
		// $hisno = $results[0]['HISNO'];
		// $sql = "select TESTCODE, TESTORDER, RESTYPE, RESVALUE, RESFLAG, UNIT, REFRANGE, USERVAL, RESDATE
		// 	from cmod.dbo.CM_RESULTS r 
		// 	where ACCESSNUMBER='$accessnumber'
		// 	order by TESTORDER";
		// $query = $db->query($sql);
		// $results = $query->getResultArray();

		$sql = "SELECT sp.HOSTORDERNUMBER, hiso.LOC, hiso.PAYERNAME
				FROM dbo.SP_REQUESTS sp
				LEFT JOIN cmod.dbo.CM_HIS_ORDERS hiso ON hiso.VISITNUMBER = sp.HOSTORDERNUMBER
				WHERE SP_ACCESSNUMBER = '$accessnumber'";
		$query = $db->query($sql);
        $results = $query->getResultArray();

		$location = $results[0]['LOC'] == null ? 'PBMC Bali' : 'PBMC Surabaya';
		$payer_name = $results[0]['PAYERNAME'] == null ? '' : $results[0]['PAYERNAME'];

		$sql = "select 
				dc.CHAPID, 
				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.DEPTH as text_depth,
				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.* 
				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
				where r.ACCESSNUMBER='$accessnumber' 
				ORDER BY t.TESTORDER";
        $query = $db->query($sql);
        $test_results = $query->getResultArray();

		// $uuid24 = $this->generateUUID24();
		$response = [
			"headers" => [
				"Content-Type" => "application/json",
				"Authorization" => "Transmedic api key"
			],
			"data" => [
				"reference_id" => "",
				"created" => "2024-10-19T02:11:06.424654Z", 
				"company_name" => $payer_name,
				"branch" => $location,
				"test_results" => []
			]
		];

		// Digunakan Untuk Hasil test 
		foreach ($test_results as $result) {

			// var_dump($result);die();

			if ($result['text_depth'] == 0) {
				// $uuid32 = $this->generateUUID32();
				$uuid24 = $this->generateUUID24();

				$data = [
					"test_ref_id"=> $uuid24,
					"service_id"=> "",
					"service_name"=> $result['test_eng'],
					"test_medium"=> $result['serum_type'],
					"test_container"=> "",
					"chapter_type"=> $result['chap_eng'], 
					"test_summary"=> "", 				
					"status"=> "", 
				];
				
				array_push($response['data']['test_results'], $data);
			}

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

}