<?php
include_once 'config.php';

$action = $_REQUEST['action'];
$result = null;
$error = null;

switch ($action) {
	default:
	case 'help':
		$result = help();
		break;
	case 'add':
		$report_id = reportAdd($_REQUEST['title'], $_REQUEST['description'], $_REQUEST['status'], $_REQUEST['group'], $_REQUEST['priority']);
		if ($report_id === false) $error = 'Report add failed';
		$result = array('report_id' => $report_id);
		break;
	case 'update':
		$suc = reportUpdate($_REQUEST['report_id'], json_decode($_REQUEST['report_data'], true));
		if ($suc === false) $error = 'Update failed';
		$result = array('processed' => $suc);
		break;
	case 'delete':
		$suc = reportDelete($_REQUEST['report_id']);
		if ($suc === false) $error = 'Update failed';
		$result = array('processed' => $suc);
		break;
	case 'get':
		$result = reportGet($_REQUEST['report_id']);
		break;
	case 'getAll':
		$result = reportGetAll($_REQUEST['status']);
		break;
	case 'getAllGrouped':
		$result = reportGetAllGrouped($_REQUEST['status']);
		break;
	case 'updateOrdNum':
		$suc = reportUpdateOrdnum($_REQUEST['ordnums']);
		if ($suc === false) $error = 'Update Ordnum failed';
		$result = array('processed' => $suc);
		break;
	case 'updateStatus':
		$suc = reportUpdateStatus($_REQUEST['report_id'], $_REQUEST['status']);
		if ($suc === false) $error = 'Update Status failed';
		$result = array('processed' => $suc);
		break;
	case 'attachmentAdd':
		$suc = attachmentAdd($_REQUEST['report_id'], $_REQUEST['attachment_type'], $_REQUEST['attachment_content']);
		if ($suc === false) $error = 'Attachment add failed';
		$result = array('processed' => $suc);
		break;
	case 'attachmentUpdate':
		$suc = attachmentUpdate($_REQUEST['attachment_id'], $_REQUEST['attachment_content']);
		if ($suc === false) $error = 'Attachment update failed';
		$result = array('processed' => $suc);
		break;
	case 'attachmentGetAll':
		$result = attachmentGetAll($_REQUEST['report_id']);
		break;
	case 'attachmentDelete':
		$suc = attachmentDelete($_REQUEST['attachment_id']);
		if ($suc === false) $error = 'Attachment delete failed';
		$result = array('processed' => $suc);
		break;
}

header('Content-Type: application/json');
$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '*';
header('Access-Control-Allow-Origin: ' . $origin);
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept');
echo json_encode(
	is_null($error)
		? array('status' => 'OK', 'data' => $result)
		: array('status' => 'ERROR', 'msg' => $error),
);
exit;

function help()
{
	return [
		'actions' => [
			'help' => [
				'name' => 'help',
				'description' => 'Show this help',
				'params' => []
			],
			'add' => [
				'name' => 'add',
				'description' => 'Add report',
				'params' => [
					'title' => 'Report title',
					'description' => 'Report description',
					'status' => 'Report status',
					'group' => 'Report group',
					'priority' => 'Report priority',
				]
			],
			'update' => [
				'name' => 'update',
				'description' => 'Update report',
				'params' => [
					'report_id' => 'Report id',
					'report_data' => [
						'title' => 'Report title',
						'description' => 'Report description',
						'status' => 'Report status',
						'group' => 'Report group',
						'priority' => 'Report priority',
					]
				]
			],
			'delete' => [
				'name' => 'delete',
				'description' => 'Delete report',
				'params' => [
					'report_id' => 'Report id',
				]
			],
			'get' => [
				'name' => 'get',
				'description' => 'Get report',
				'params' => [
					'report_id' => 'Report id',
				]
			],
			'getAll' => [
				'name' => 'getAll',
				'description' => 'Get all reports',
				'params' => [
					'status' => '(ptional) Report status, default: 0,1,2,3',
				]
			],
			'getAllGrouped' => [
				'name' => 'getAllGrouped',
				'description' => 'Get all reports grouped by group',
				'params' => [
					'status' => '(ptional) Report status, default: 0,1,2,3',
				]
			],
			'updateOrdNum' => [
				'name' => 'updateordnum',
				'description' => 'Update report ordnum',
				'params' => [
					'ordnums' => 'Report ordnums in json format {report_id: ordnum, ...}',
				]
			],
			'updateStatus' => [
				'name' => 'updatestatus',
				'description' => 'Update report status',
				'params' => [
					'report_id' => 'Report id',
					'status' => 'Report status',
				]
			],
			'attachmentAdd' => [
				'name' => 'attachmentAdd',
				'description' => 'Add attachment to report',
				'params' => [
					'report_id' => 'Report id',
					'content_type' => 'Attachment content type',
					'content' => 'Attachment content',
				]
			],
			'attachmentUpdate' => [
				'name' => 'attachmentUpdate',
				'description' => 'Update attachment',
				'params' => [
					'attachment_id' => 'Attachment id',
					'content' => 'Attachment content; if empty, attachment will be deleted',
				]
			],
			'attachmentGetAll' => [
				'name' => 'attachmentGetAll',
				'description' => 'Get all attachments for report',
				'params' => [
					'report_id' => 'Report id',
				]
			],
			'attachmentGet' => [
				'name' => 'attachmentGet',
				'description' => 'Get attachment',
				'params' => [
					'attachment_id' => 'Attachment id',
				]
			]
		]
	];
}
