<?php

	class Database extends Balloon {

		private $config;

		function __construct(){


			if($_SERVER['REQUEST_METHOD'] == 'POST'){
				$this->update();
			}else{
				global $databaseConfig;
				$this->config = $databaseConfig;
			}

		}

		public function getDatabaseConfig(){
			return $this->config;
		}

		private function update(){

			$hostname = $_POST['hostname'];
			$username = $_POST['username'];
			$password = $_POST['password'];
			$database = $_POST['database'];

			// Deletando configuração antiga
			@unlink(ROOT_PATH . 'config/database.php');

			$data  = '<?php $databaseConfig = array(';
			$data .= "'host' => '{$hostname}',";
			$data .= "'user' => '{$username}',";
			$data .= "'pass' => '{$password}',";
			$data .= "'name' => '{$database}');";

			// Salvando nova
			$save = file_put_contents(ROOT_PATH . 'config/database.php', $data);

			if($save){
				$this->newBalloon(true, 'Atualizado com sucesso!');
			}else{
				$this->newBalloon(false, 'Erro ao atualizar');
			}

		}

	}

	loadBaseView('database', new Database);