<?php
namespace App\Controllers;

use CodeIgniter\RESTful\ResourceController;

class API_HISPatients extends ResourceController {
	protected $format = 'json';
    
	public function index() {
		$db = \Config\Database::connect();
		$date1 = $this->request->getPost('date1');
		$date2 = $this->request->getPost('date2');
		$sql = "select o.VISITNUMBER, o.VISITDATE, o.PAYERNAME, o.TREATDOC, p.PATNUMBER, p.SEX, p.PATNAME,
			TESTS=stuff(( select ', '+t.HISCODE from
			( select t.HISCODE from cmod.dbo.CM_HIS_TESTS t 
				where t.ORDERID=o.ORDERID
			) as T
			for xml path('')),1,1,'')
			from cmod.dbo.CM_HIS_ORDERS o 
			left join cmod.dbo.CM_HIS_PATIENTS p on p.PATID=o.PATID";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['data'] = $results;
        return $this->respond($data,200);
    }
	
	public function patnumberSearch($patnumber) {
		$db = \Config\Database::connect();
		$sql = "select * from cmod.dbo.CM_HIS_PATIENTS where PATNUMBER like '%$patnumber%'";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['patients'] = $results;
        return $this->respond($data,200);
	}
	
	public function search() {
		$db = \Config\Database::connect();
		$patnumber = $this->request->getPost('patnumber');
		$patname = strtolower( $this->request->getPost('patname') );
		$sql = "select * from cmod.dbo.CM_HIS_PATIENTS";
		// Initialize a WHERE clause
		$where= '';
		if (!empty($patnumber) || !empty($patname)) {
			$where= ' WHERE ';
			if (!empty($patnumber)) { $where .= "PATNUMBER like '%$patnumber%'"; }
			if (!empty($shorttext)) {
				if (!empty($testcode)) { $where .= ' OR '; }
				$where .= "LOWER(PATNAME) LIKE '%$patname%'";
			}
		}
		$sql .= $where;
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['patients'] = $results;
		
		return $this->respond($data, 200);
	}
	
	public function detail($patid) {
		$db = \Config\Database::connect();
		$sql = "select * from cmod.dbo.CM_HIS_PATIENTS where PATID='$patid'";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['patient'] = $results[0];
        return $this->respond($data,200);
	}
	
	public function save() {
		$db = \Config\Database::connect();
		$patid = $this->request->getPost('patid');
		$patnumber = $this->request->getPost('patnumber');
		$patname = $this->request->getPost('patname');
		$sex = $this->request->getPost('sex');
		$birthdate = $this->request->getPost('birthdate');
		$address = $this->request->getPost('address');
		$phone = $this->request->getPost('phone');
		if($patid == 0) {
			$sql = "INSERT INTO cmod.dbo.CM_HIS_PATIENTS (PATNUMBER, PATNAME, SEX, BIRTHDATE, ADDRESS, PHONE) 
				VALUES ('$patnumber', '$patname', '$sex', '$birthdate', '$address','$phone')";
		} else {
			$sql = "update cmod.dbo.CM_HIS_PATIENTS set PATNUMBER='$patnumber', PATNAME='$patname', 
				SEX='$sex', BIRTHDATE='$birthdate', ADDRESS='$address', PHONE='$phone' Where PATID='$patid'";
		}
		$query = $db->query($sql);
		if($patid == 0 ) { $patid = $db->insertID(); }
		$data['patid'] = $patid;
		$data['patnumber'] = $patnumber;
		$data['sql'] = $sql;
		return $this->respond($data , 200);	
	}
}
