<?php

Lib::loadClass('RouteToolBase');
Lib::loadClass('UI');
Lib::loadClass('Response');
Lib::loadClass('Theme');

// class name must have the same name as file
// index.php?_route=UrlAction_ExampleTool  - uruchamia defaultAction
class RouteTool_ExampleTool extends RouteToolBase {

	public function defaultAction() {
		UI::gora();
		Theme::top();
		echo '<h1>Example Tool</h1>';
		UI::inlineJS(__FILE__ . '.example.js', [
			'URL_TEST_AJAX_ACTION' => $this->getLink('testAjax'),
		]);
		UI::dol();
	}

	public function testAjaxAction() {
		Response::sendTryCatchJson(array($this, 'testAjax'), $_REQUEST); // args from request
		// Response::sendTryCatchJson(array($this, 'testAjax'), $args = 'JSON_FROM_REQUEST_BODY'); // args from json request
	}
	public function testAjax($args) { // args given by sendTryCatchJson
		$items = [
			[ 'ID' => 1, 'name' => 'x', 'desc' => 'a' ],
			[ 'ID' => 2, 'name' => 'y', 'desc' => 'b' ],
			[ 'ID' => 3, 'name' => 'z',  'desc' => 'c' ],
		];

		return [
			'type' => 'success',
			'msg' => 'OK',
			'body' => [
				'items' => $items,
			]
		];
	}

}
