<?php

namespace App\Controllers;

class API_Tubes extends BaseController {
    
	public function collect($access, $sample) {
		$userid = session()->userid;
		$db = \Config\Database::connect();
		$sql = "if (not exists (select * from cmod.dbo.CM_TUBES where ACCESSNUMBER='$access' )) BEGIN
				INSERT INTO cmod.dbo.CM_TUBES(ACCESSNUMBER, SAMPLETYPE, CREATEDATE) select SP_ACCESSNUMBER, SAMPLETYPE, GETDATE() from SP_TUBES where SP_ACCESSNUMBER='$access'
			END";
		$query = $db->query($sql);
		$sql = "if (not exists (select * from cmod.dbo.CM_TUBES where ACCESSNUMBER='$access' AND SAMPLETYPE='$sample')) BEGIN
				INSERT INTO cmod.dbo.CM_TUBES(ACCESSNUMBER, SAMPLETYPE, COLLECTIONDATE, COLL_USERID, COLLSTATUS, CREATEDATE) VALUES ('$access','$sample', GETDATE(), '$userid', 1, GETDATE())
			END ELSE BEGIN
				UPDATE cmod.dbo.CM_TUBES set COLLECTIONDATE=GETDATE(), COLL_USERID='$userid', COLLSTATUS='1' where ACCESSNUMBER='$access' and SAMPLETYPE='$sample'
			END";
		$query = $db->query($sql);
    }
	
	public function uncollect($access, $sample) {
		$db = \Config\Database::connect();
		$sql = "UPDATE cmod.dbo.CM_TUBES set COLLSTATUS=0 where ACCESSNUMBER='$access' and SAMPLETYPE='$sample'";
		$query = $db->query($sql);
    }
	
	public function unreceive($access, $sample) {
		$db = \Config\Database::connect();
		$sql = "UPDATE SP_TUBES set TUBESTATUS=0 where SP_ACCESSNUMBER='$access' and SAMPLETYPE='$sample'";
		$query = $db->query($sql);
    }
	
	public function collectAll($access) {
		$userid = session()->userid;
		$db = \Config\Database::connect();
		$sql = "select SAMPLETYPE from SP_TUBES where SP_ACCESSNUMBER='$access'";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		foreach($results as $data) {
			$sample = $data['SAMPLETYPE'];
			$sql = "if (not exists (select * from cmod.dbo.CM_TUBES where ACCESSNUMBER='$access' AND SAMPLETYPE='$sample')) BEGIN
					INSERT INTO cmod.dbo.CM_TUBES(ACCESSNUMBER, SAMPLETYPE, COLLECTIONDATE, COLL_USERID, COLLSTATUS, CREATEDATE) VALUES
						('$access','$sample', GETDATE(), '$userid', 1, GETDATE())
				END ELSE BEGIN
					UPDATE cmod.dbo.CM_TUBES set COLLECTIONDATE=GETDATE(), COLL_USERID='$userid', COLLSTATUS='1' where ACCESSNUMBER='$access'
				END";
			//echo "<pre>$sql</pre>";
			$query = $db->query($sql);
		}
    }
	
	public function uncollectAll($access) {
		$db = \Config\Database::connect();
		$sql = "UPDATE cmod.dbo.CM_TUBES set COLLSTATUS=0 where ACCESSNUMBER='$access'";
		$query = $db->query($sql);
    }
	
	public function unreceiveAll($access) {
		$db = \Config\Database::connect();
		$sql = "UPDATE SP_TUBES set TUBESTATUS=0 where SP_ACCESSNUMBER='$access'";
		$query = $db->query($sql);
    }
	
	public function comment($access, $sample) {
		$userid = session()->userid;
		$request = service('request');
		$jsonData = $request->getJSON();
		$db = \Config\Database::connect();
        $comment = $jsonData->comment;
		$sql = "if (not exists (select * from cmod.dbo.CM_TUBES where ACCESSNUMBER='$access' AND SAMPLETYPE='$sample')) BEGIN
					INSERT INTO cmod.dbo.CM_TUBES(ACCESSNUMBER, SAMPLETYPE, COLL_USERID, TUBECOMMENT, CREATEDATE) VALUES
						('$access','$sample', '$userid', '$comment', GETDATE())
				END ELSE BEGIN
					UPDATE cmod.dbo.CM_TUBES set TUBECOMMENT='$comment' where ACCESSNUMBER='$access' and SAMPLETYPE='$sample'
				END";
		echo "$sql";
		$query = $db->query($sql);
    }
}
