<?php
include_once("dbConnect.php");
$content = "";

$header = '
<html><head>
		<meta charset="utf-8">
		<link href="style.css" rel="stylesheet" type="text/css">
</head><body>
';

$footer = '
	</body>
</html>
';

$todo = "";
if(isset($_GET['todo'])) {
	$todo = $_GET['todo'];
} 

switch($todo) {
	case "install_db":
		$content .= install_db_page();
	break;
	default;
		$content .= first_page();
}

function first_page() {
	$returndata = "";
	
	$returndata .= "<div style='width:70%;margin:auto;'>";
		$returndata .= "<h1>Install basic site database</h1>";
	$returndata .= "</div>";
	$returndata .= "<div style='width:70%;margin:auto;'>";
		$returndata .= "<div style='margin-auto;margin-top:15px;'>Make sure you have correct settings for your mysql connection (host, user, password, database). If you see no errors, connection was probably established without problems.</div>";
		$returndata .= "<div style='margin-auto;margin-top:15px;'><button type='button' onClick='window.location.href=\"install.php?todo=install_db\"'>Setup db tables</button></div>";
	$returndata .= "</div>";
	
	return $returndata;
}

function install_db_page() {
	$returndata = "";

	if(setup_tables()) {
		$returndata .= "<div style='width:70%;margin:auto;'>";
			$returndata .= "<h2>Install successfull!</h2>";
		$returndata .= "</div>";
		$returndata .= "<div style='width:70%;margin:auto;'>";
			$returndata .= "Please delete install.php.";
		$returndata .= "</div>";
		$returndata .= "<div style='width:70%;margin:auto;'>";
			$returndata .= "<a href='index.html'>To site</a>";
		$returndata .= "</div>";
	} else {
		$returndata .= first_page();
	}
	
	return $returndata;
}

function setup_tables() {
	global $con;
	
	$query = "CREATE TABLE IF NOT EXISTS `accounts` (
	`id` int(11) NOT NULL AUTO_INCREMENT,
  	`login_name` varchar(50) NOT NULL,
  	`user_name` varchar(50) NOT NULL,
  	`password` varchar(255) NOT NULL,
  	`email` varchar(100) NOT NULL,
  	`admin` TINYINT NOT NULL DEFAULT '0',
  	`lang` CHAR(3) NOT NULL DEFAULT 'eng',
  	`disabled` INT NOT NULL DEFAULT '0',
  	`last_login` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
    PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
";
	
	$result = $con->query($query);
	
	if(!$result) {
		print "Could not create table!";
		return false;
	}
	
	$query = "ALTER TABLE `accounts` ADD UNIQUE(`login_name`)";

	$result = $con->query($query);
	
	if(!$result) {
		print "Could not create table!";
		return false;
	}
	
	$query = "CREATE TABLE IF NOT EXISTS user_permission (id INT NOT NULL AUTO_INCREMENT, function VARCHAR(50), permission_lvl INT NOT NULL DEFAULT '0', PRIMARY KEY (`id`, function))";

	$result = $con->query($query);
	
	if(!$result) {
		print "Could not create user!<br>$query<br>".$con->error;
	}

	// Smormagasinet queries
	include("includes/lunch/databases.php");
	
	if(is_array($run_queries)) {
		foreach($run_queries as $this_query) {
			$result = $con->query($this_query);
			
			if(!$result) {
				print "Query failed!<br>$this_query<br>".$con->error;
			}
		}
	}

	$pwd = '$2y$10$SfhYIDtn.iOuCW7zfoFLuuZHX6lja4lF4XA4JqNmpiH/.P3zB8JCa'; // test
	$query = 'INSERT INTO `accounts` (`id`, `login_name`, user_name, `password`, `email`, admin, lang) VALUES (1, \'admin\', \'My name\', \''.$pwd.'\', \'test@test.com\', 1, \'eng\', \'0000-00-00 00:00:00\')';

	$result = $con->query($query);
	
	if(!$result) {
		print "Could not create user!<br>$query<br>".$con->error;
		return false;
	}
	

	return true;
}

print $header.$content.$footer;

?>