<?php 


if(file_exists(__DIR__."/store-db-active.php")){
	include __DIR__."/store-db-active.php";
}
if(!empty($storedb)){
	db::add_map("¤",$storedb["prefix"]."__");
	db::add_mysql("mysql",$storedb["host"], $storedb["dbname"], $storedb["user"],  $storedb["pass"], empty($storedb["port"])?null:$storedb["port"]);
	db::db("mysql");
}else{
	db::sqlite(__DIR__."/default-db.sqlite");
}

api::usedb(function($data){
	if(file_exists(__DIR__."/store-db.php")){
		include __DIR__."/store-db.php";
	}else{
		app::apiRep("alert","Erreur : store-db.php introuvable!");
	}
	$str = "mysql:host=$storedb[host];";
	if(!empty($storedb["port"])){
		$str .= "port=$storedb[port];";
	}
	if(!empty($storedb["dbname"])){
		$str .= "dbname=$storedb[dbname];";
	}
	try {
		$DBH = new PDO($str, $storedb["user"], $storedb["pass"], array(PDO::ATTR_TIMEOUT => 1, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ) );
		file_put_contents(__DIR__."/store-db-active.php", "<?php \n\$storedb = ".var_export($storedb,true)."; \n?>");
		app::apiRep("alert","Success, Dès maintenant ".$_SERVER["HTTP_HOST"]." est connecté a db: '".$storedb["host"]."', vous pouvez supprimer les information affiché.");
	} catch (Exception $e) {
		app::apiRep("alert","Erreur de connecxion db");
	}
});

api::install(function($data){
	db::create("¤STOREDATA",function($table){
		$table->string("name")->pri();
		$table->text("data");
	})->install();
});
function storge($name){
	return db::val("SELECT data FROM ¤STOREDATA where name=:name",[":name"=>$name],"data");
}
api::storeDataApi(function($data){
	clog($data);
	switch (strtolower($data["name"])) {
		case 'db':
			switch ($data["action"]) {
				case 'save':
					unset($data["data"]["nodb"]);
					unset($data["data"]["ok"]);
					unset($data["data"]["cnx"]);
					unset($data["data"]["loaded"]);
					file_put_contents(__DIR__."/store-db.php", "<?php \n\$storedb = ".var_export($data["data"],true)."; \n?>");
				break;
				case 'load':
					if(file_exists(__DIR__."/store-db.php")){
						include __DIR__."/store-db.php";
					}
					if(empty($storedb)){
						$storedb = ["nodb"=>true];
					}else{
						$storedb["ok"] = true;
					}
					$storedb["loaded"] = true;
					return $storedb;
				break;
			}
		break;
		default:
			switch ($data["action"]) {
				case 'save':
					if (user::isAdmin()) {
						db::save("¤STOREDATA",["name"=>$data["name"],"data"=>$data["data"]]);
					}else{
						clog("user is not admin");
					}
				break;
				default:
					return db::val("SELECT data FROM ¤STOREDATA where name=:name",[":name"=>$data["name"]],"data");
				break;
			}
		break;
	}
});


Route::is("__setting__",function(){
	view::reset();
?>
<!DOCTYPE html>
<html>
   <head>
		<title>Configuration</title>
		<meta charset="utf-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta content="width=device-width, initial-scale=1" name="viewport" />
		<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.css">
		<link href="//fonts.googleapis.com/css?family=Open+Sans:400,300,600,700&subset=all" rel="stylesheet" type="text/css" />
		<link href="//fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
		<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
		<style type="text/css">
		body,html{
			background: #e9ecf3;
		}
		hr{
			border-top: 1px solid #337ab7 !important;
		}
		*{
			/*color: #697882;*/
		}
		.form-control,.btn{
			border-radius: 0 !important;
			box-shadow: none !important;
		}
		.table td,input{
			font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;
		}	
		.table td,.table th{
			outline: none;
		}
		label:first-letter {
    		text-transform: uppercase;
		}
		.vakata-context{
			z-index: 15000;
		}
		.modal-content{
			background: #697882;
			border: 1px solid #697882;
		}
		.modal-header{
			color:#fff;
		}
		.modal-body{
			background: #fff;
		}
		.tablepagination table{
			background: #fff;
		}
		.pagecontent{
			min-height: 100vh;
			max-width: 1000px;
			margin: 0 auto; 
		}
		@media only screen and (max-width: 600px){
			body{
				zoom:0.8;
			}
			.h1, h1 {
		    	font-size: 29px;
			}
			.pagecontent{
				min-height: 125vh;
			}
		}
		</style>
		<? section("css-lib") ?>
	</head>
	<body ng-app="app">
		<div style="display: flex;flex-direction: column;" class="pagecontent">
			<store-data ng-model="db" name="db" widget="storeDB"></store-data>
			<store-data ng-model="config" name="config" widget="storeConfig"></store-data>
			<div style="flex:1;display: flex;flex-direction: column;">
			    <md-tabs md-dynamic-height md-border-bottom>

			    	<md-tab label="Configuration">
			    		<md-content class="md-padding">
			    			<div class="gridtable">
			    				<rowconfig-input ng-model="config.titre" store-widget="storeConfig">Titre</rowconfig-input>
			    				<rowconfig-input ng-model="config.logo" store-widget="storeConfig" type="image" titre="logo" dist="logo" max="1">
			    					<div>Logo</div>
			    					<small style="display: block;">Dimension : (50px X 180px)</small>
			    					<small style="display: block;">NB: Image sur couleur arrière plan menu</small>
			    				</rowconfig-input>
			    				<rowconfig-input ng-model="config.logoprint" store-widget="storeConfig" type="image" titre="logo" dist="logo" max="1">
			    					<div>Logo</div>
			    					<small>Dimension : (80px X 180px)</small>
			    					<small style="display: block;">NB: Image sur couleur arrière plan Blanche</small>
			    				</rowconfig-input>
			    				<rowconfig-input ng-model="config.bgAuth" store-widget="storeConfig" type="image" titre="logo" dist="logo" max="1">
			    					<div>Image arrière plan Auth</div>
			    					<small>Dimension : (1600px X 1024px)</small>
			    				</rowconfig-input>
			    				<rowconfig-input ng-model="config.bgApp" store-widget="storeConfig" type="image" titre="logo" dist="logo" max="1">
			    					<div>Image arrière plan App</div>
			    					<small>Dimension : (1600px X 1024px)</small>
			    				</rowconfig-input>
			    				<rowconfig-input ng-model="config.favicon" store-widget="storeConfig" type="image" titre="logo" dist="logo" max="1">
			    					<div>Favicon</div>
			    					<small>Dimension : (50px X 50px)</small>
			    				</rowconfig-input>
				    			<rowconfig-input ng-model="config.bg" store-widget="storeConfig" type="color">
				    				<div>Couleur arrière plan</div>
				    			</rowconfig-input>
				    			<rowconfig-input ng-model="config.bgmenu" store-widget="storeConfig" type="color">
				    				<div>Couleur arrière plan menu</div>
				    			</rowconfig-input>








								<rowconfig store-widget="storeConfig">
									<titre>
										<div style="display: flex;align-items: flex-start;">
											<span class="glyphicon glyphicon-menu-right" style="margin-top: 2px;font-weight: normal !important;color:#aaa;"></span>
											<div style="flex:1;margin-left: 10px;">
												Rôles
											</div>
										</div>
									</titre>
									<edit>
										<table class="table table-bordered" style="background:#efefef;width:100%">
											<tr>
												<th>Rôle</th>
												<th style="width:10px;"></th>
											</tr>
											<tr ng-repeat="elm in config.roles">
												<td style="padding:2px;"><input style="height:30px;" type="text" class="form-control" ng-model="elm.titre" placeholder="Titre"></td>
												<td style="padding:2px;"><button class="btn btn-sm" ng-click="config.roles.splice($index,1)"><span class="glyphicon glyphicon-trash"></span></button></td>
											</tr>
											<tr>
												<td colspan="2">
													<center>
														<button class="btn btn-xs btn-primary" ng-click="config.roles=config.roles||[];config.roles.push({titre:''})">Ajouter</button>
													</center>
												</td>
											</tr>
										</table>
									</edit>
									<show>
									 	<ul>
									 		<li ng-repeat="r in config.roles">{{r.titre}}</li>
									 	</ul>
									</show>
								</rowconfig>





			    				<rowconfig-input ng-model="config.menu" store-widget="storeConfig" type="menu">Menu</rowconfig-input>
			    				<rowconfig-input ng-model="config.urlbo" store-widget="storeConfig">URL Backoffice (ex : "erp-admin")</rowconfig-input>
			    				<rowconfig-input ng-model="config.defaulturl" store-widget="storeConfig">URL par default</rowconfig-input>

			    			</div>
						</md-content>
					</md-tab>

			    	<md-tab label="Modules">
			    		<md-content class="md-padding">
			    			<div class="gridtable">
			    				<? configModule::getAllHtml("config","storeConfig") ?>
			    			</div>
						</md-content>
					</md-tab>

			    	<md-tab label="Data Base">
			    		<md-content class="md-padding">
			    			<div class="gridtable">
			    				<rowconfig-input ng-model="db.host" store-widget="storeDB">Host</rowconfig-input>
			    				<rowconfig-input ng-model="db.port" store-widget="storeDB">Port</rowconfig-input>
			    				<rowconfig-input ng-model="db.dbname" store-widget="storeDB">Data base name</rowconfig-input>
			    				<rowconfig-input ng-model="db.user" store-widget="storeDB">User</rowconfig-input>
			    				<rowconfig-input ng-model="db.pass" store-widget="storeDB">Password</rowconfig-input>
			    				<rowconfig-input ng-model="db.prefix" store-widget="storeDB">Prefix Table name</rowconfig-input>
			    			</div>
			    			<div>
			    				<hr>
			    				<div style="display: flex;">
				    				<form method="post" action="usedb">
				    					<button class="btn btn-primary" type="submit" style="margin-right: 15px;">Appliquer</button>
				    				</form>
				    				<form method="post" action="install">
				    					<button class="btn btn-primary" type="submit" style="margin-right: 15px;">Install Tables</button>
				    				</form>
				    				<form method="post" action="updatesrc">
				    					<input type="hidden" name="update" value="src">
				    					<button class="btn btn-primary" type="submit">Update framework</button>
				    				</form>
			    				</div>
			    			</div>
						</md-content>
					</md-tab>
				</md-tabs>
			</div>
		</div>
		<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyA0bbh9FJxy1aQ4BF5M8BiyyLFbzvtZX0A"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" type="text/javascript"></script>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
        <script>(function(orig) {angular.modules = []; angular.module = function() {if (arguments.length > 1) {angular.modules.push(arguments[0]); } return orig.apply(null, arguments); } })(angular.module);</script>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-sanitize.min.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-animate.min.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-aria.min.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-messages.min.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.js"></script>
        <script src="/public/libs/ui-bootstrap.min.js"></script>
		<script type="text/javascript" src="/public/node_modules/ui-select/dist/select.min.js"></script>
		<link rel="stylesheet" type="text/css" href="/public/node_modules/ui-select/dist/select.min.css">
        <? section("js-lib-0") ?>
        <? section("js-lib") ?>
		<? section("js") ?>
	</body>
</html>








<?php
return false;
});