<?php
use Jcupitt\Vips;
include_once("config.php");
include_once("../../fly2/fly3.php");
//phpinfo();


/*
$real_location = "/mod/backend/dev/new/apiroot/pacs/data/dcm/I000001_1.dcm";



$image = new Imagick($real_location); 
$ident = $image->getImageProperties("dcm:*"); 

$patient_id = $ident["dcm:Patient'sID"];

echo $patient_id;

*/



$app->action('index', function (&$view) {
	$rows = query("select * from dicom  limit 100");
	print_r($rows);	
	$view = 'index_pacs';
	return array(
		pacs=>$rows
	);	
});


/*

$app->action('list-test', function (&$view) {
	$rows = query("select * from terminal limit 10");

	$view = 'list';
	return array(
		terminals=>$rows
	);	
});


*/

$app->action("dicom/fetch", function() {
	$a = $_REQUEST;
	if ($a[patientid])
		$patientid = " and patientsid=:patientid ";
	$rows = query("select * from dicom 
		where 
			1
			$patientid
		-- order by SeriesTime desc limit 100", $a);
	$result = array(
		title=>'Pacs Viewer',
		desc=>'MOD Pacs 뷰어',
		icon=>'https://www.radiustoday.com/wp-content/uploads/2017/11/icon-pacs-bl.png',
		'list'=>array(),
	);
	$i = 1;
	foreach($rows as &$r) {
		$cwd = getcwd();
		$data = $r;
		$pic = str_replace($cwd, "./", $r[pic_720p]);		
		
		$data[url] = "http://pacs.modtv.co.kr/$pic";
		$data[index] = $i++;
		$data[title] = $r[creationtime];
		$data[desc] = $r[PatientsName];
		
		array_push($result['list'], $data);
	}
	header("Access-Control-Allow-Origin: *");
	//print_r($result);
	reply_json($result);
});

$app->action("dicom/gc", function(){
        $year = date("Y");
        $time = time();
        echo "$year $month $time\n";
        //$app = new FallMonitor();
        for ($y = 2017; $y <= $year; $y++) {
                for ($m = 1; $m <= 12; $m++) {
			for($d = 1; $d <= 31; $d++) {
				$dt = strtotime("$m/$d/$y");
	                        $gap = $time - strtotime("$m/$d/$y");
        	                $gap /= 60*60*24;
                	        if ($gap > 14) {
					$dir = "data/IM38/jpg/".date("Ymd", $dt);;
					$cmd = "rm -rf $dir  \n";
					echo $cmd;
					echo shell_exec($cmd);
					//rm "data/IM22/$y$m
	                                //$app->cleanDir($y, $m);
        	                }
			}
                }
        }
});

//header("Access-Control-Allow-Origin: *");

?>
