<?php
use ProxyPay\Core\Db;
use ProxyPay\Core\Config;
use ProxyPay\Core\Factory;
use ProxyPay\Core\AjaxResponse;

session_start();
require_once __DIR__ . '/../engine/autoload.php';
require_once __DIR__ . '/../vendor/autoload.php';

$page = $_GET['controller'] ?? 'index';

$type = $_GET['type'] ?? 'html';

if ($type === 'json') {
	header("Content-Type: application/json; charset=utf-8");
} else {
	header('Content-Type:text/html; charset=utf-8');
}


$config = Config::get();
$db = Factory::getDb();


$contollerClass = 'ProxyPay\\Pages\\' . ucfirst($page);
if (!class_exists($contollerClass)) {
	header('Location: index.php');
	exit();
}
$teplatePath = __DIR__.'/../tmpl';
$controller = new $contollerClass($db, $teplatePath, $config);
$action     = $_REQUEST['task'] ?? 'index';

if ($type !== 'json') {
	$action = 'action' . ucfirst($action);
}


if (!method_exists($controller, $action)) {
	header('Location: index.php');
	exit();
}

if ($type === 'json') {
	$response = new AjaxResponse();
	try {
		$response = $controller->$action($response);
	} catch (\Exception $e) {
		$response->setError('Произошла ошибка, попробуйте повторить позднее');
	}
	echo json_encode($response);
	exit();
}

$content = $controller->$action();
if (is_array($content)) {
	echo json_encode($content);
	exit();
}
?><!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <link href="/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"/>
    <link href="/assets/css/propeller.min.css" rel="stylesheet" type="text/css"/>
    <link href="/assets/css/style.css" rel="stylesheet" type="text/css"/>
    <title><?php echo $controller->getTitle()?></title>
</head>
<body>
    <nav class="navbar navbar-default  pmd-navbar pmd-z-depth">
        <div class="container-fluid">
            <div class="navbar-header">
                <a href="/" class="navbar-brand">
                    <span>ProxyPay</span>
                </a>
            </div>
            <ul class="nav navbar-nav navbar-right">
                <li>
                </li>
                <li></li>
                <li><a href="https://proxypay.ru/docs/podeli_retailcrm.html" target="_blank">Документация</a></li>
                <li></li>
                <li></li>
            </ul>
        </div>
    </nav>
<div class="content">
	<?php if (!empty($_SESSION['error'])):?>
			<div class="alert alert-danger alert-dismissible fade show position-absolute start-50 translate-middle-x" role="alert" style="z-index: 4">
			  <?php echo $_SESSION['error']?>
			  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
			</div>
		<?php
		$_SESSION['error'] = null;
	endif;?>
	<?php if (!empty($_SESSION['notice'])):?>
			<div class="alert alert-success alert-dismissible fade show position-absolute start-50 translate-middle-x z-3" role="alert"  style="z-index: 4">
			  <?php echo $_SESSION['notice'];?>
			  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
			</div>
		<?php
		$_SESSION['notice'] = null;
	endif;?>
	<div class="container-fluid">
		<div class="content mb-3">
			<div class="card border-0">
			<div class="card-body">
			<?php echo $content;?>
			</div>
		</div>
		</div>
	</div>
		<footer class="d-flex justify-content-center align-items-center mt-auto py-3 flex-wrap">
			<div class="container-fluid">
	        <ul class="list-unstyled list-inline">
	            <li>
	                <h5>ProxyPay &copy;<?php echo date('Y')?></h5>
	            </li>
	            <li class="pull-right for-support">
	                <a href="mailto:retailcrm@proxypay.ru">
	                    <div>
	                        <span class="pmd-card-subtitle-text">Поддержка</span>
	                        <h3 class="pmd-card-title-text">retailcrm@proxypay.ru</h3>
	                    </div>
	                </a>
	            </li>
	        </ul>
	    </div>
		</footer>
		<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
		<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
		<script type="text/javascript" src="/assets/js/propeller.min.js"></script>
	</body>
</html>