<?php
namespace App\Controllers;

use CodeIgniter\RESTful\ResourceController;

class API_HIS extends ResourceController {
	protected $format = 'json';
	
	public function requests() {
/*
		"reference_id": "4f52-8e25-48009b4f52-8e25-48009b4f52-8e25-48009b",
"created": "2024-10-19T00:47:06.424654Z",
"company_name": "company_name",
"branch": "PBMC Bali",
"total_patient_export_count": 2,
"total_test_export_count": 4,
"patient": {
	"rm_number": "1B0912243",
	"patient_first_name": "Jenifer",
	"patient_last_name": "Ngo",
	"patient_dob": "1999-11-12",
	"patient_sex": "F",
	"patient_phone": "62815456655885",
	"visit_number": "BV002304",
	"visit_description": null,
	"visit_date_time": "2024-10-19T00:47:06.424654Z",
	"agent_name": "Apollo Group",
	"agent": true/false,
	"treating_doctor": "Frida Susanti Sp.Ked",
	"visit_type": "MCU",
	"tests": [
		{
			"test_ref_id": "4f52-8e25-48009b04f52-8e25-4800900",
			"service_id": "SL00140"
		},
		{
			"test_ref_id": "4f52-8e25-48009b04f52-8e25-4800901",
			"service_id": "SL00142"
		}
	]
}

{
  "reference_id": "4f52-8e25-48009b4f52-8e25-48009b4f52-8e25-48009b",
  "created": "2024-10-19T00:47:06.424654Z",
  "company_name": "company_name",
  "branch": "PBMC Bali",
  "total_patient_export_count": 2,
  "total_test_export_count": 4,
  "patient": {
    "rm_number": "1B0912243",
    "patient_first_name": "Jenifer",
    "patient_last_name": "Ngo",
    "patient_dob": "1999-11-12",
    "patient_sex": "F",
    "patient_phone": "62815456655885",
    "visit_number": "BV002304",
    "visit_description": null,
    "visit_date_time": "2024-10-19T00:47:06.424654Z",
    "agent_name": "Apollo Group",
    "agent": true,
    "treating_doctor": "Frida Susanti Sp.Ked",
    "visit_type": "MCU",
    "tests": [
      {
        "test_ref_id": "4f52-8e25-48009b04f52-8e25-4800900",
        "service_id": "SL00140"
      },
      {
        "test_ref_id": "4f52-8e25-48009b04f52-8e25-4800901",
        "service_id": "SL00142"
      }
    ]
  }
}
*/
		$input = $this->request->getRawInput();        
        if (empty($input)) { return $this->fail('No JSON data received', 400); }
        
		var_dump($input);
		
        //return $this->respond($input, 200);
	}
    
	public function dictTests() {
		$db = \Config\Database::connect();
		$sql = "select top 5 HISCODE as ServiceCode, DESCS as ServiceName from cmod.dbo.CM_DICT_MAPPINGS";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data = $results;
        return $this->respond($data,200);
    }
	
	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;
		$data['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();
		$data['results'] = $results;
        return $this->respond($data,200);
    }
	
}
