<?php
class Api_Model extends Model{
	function __construct(){
		parent::__construct();
	}
	
	function loadNewDatabase($data){
	
		$cpanel = new CPanel_Api;
		
		$data = $this->url_get_contents(MAIN_URL.'databases/'.$data.'.sql');
		
		$dbName = $_POST['name'];
		
		if(SERVERTYPE != 'local'){
			
			/* ****** for live ****** */
			$database = str_replace(GENERATEDDATABASEUSER_PRE,"",$dbName);
			$cpanel->create_database($database, 'kabayan_user');
			$dbName = GENERATEDDATABASEUSER_PRE.$database;
		} else {
			
			/* ***** for local **** */
			$conn->query('Create database `'.$dbName.'`;');
			
		}
		
		$conn = new mysqli(DATABASEHOST, GENERATEDDATABASEUSER, GENERATEDDATABASEPASS, $dbName);
		
		if ($conn->connect_error) {
            die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);
        }
        
		if ($conn->multi_query($data) === TRUE) {
			$conn->close();
		} else {
			echo "Error Setting up Data Tables: " . $conn->error;
			$conn->close();
			exit;
		}
		
// 		sleep(10);
		$data = DAOFactory::getTblDatabaseListDAO()->queryByDatabaseName($database)[0];
		
		$data->status = 'available';
		$data->dateUsed = date('Y-m-d H:i:s');
		
		DAOFactory::getTblDatabaseListDAO()->update($data);
		Model::setLogs('admin transaction', 'load database:'.$database);
		
	}
	
	/* new record on tbl_database_list */
	function createNewDatabaseList($data){
		$datas = DAOFactory::getTblDatabaseListDAO()->queryAllOrderBy('
						CASE 
							WHEN ub = "'.$data.'" AND `status` = "available" 
								THEN 0 
							WHEN ub = "'.$data.'" AND `status` = "queue" 
								THEN 1
							WHEN ub = "'.$data.'" 
								THEN 2
							ELSE 3
						END, id LIMIT 1
						');
		$datas = $datas[0];
		
		$i = true;
		$count = 1;
		while($i == true){
			$datas->databaseName = ++$datas->databaseName;
			$d = DAOFactory::getTblDatabaseListDAO()->queryByDatabaseName($datas->databaseName);
			if(isset($d) && !empty($d)){
				$i = true;
			} else {
				$i = false;
			}
		}
		
		$datas->dateCreated = date('Y-m-d H:i:s');
		$datas->status = 'queue';
		// exit;
		$datas->id = DAOFactory::getTblDatabaseListDAO()->insert($datas);
		
		return $datas;
	}
	
	/* set record on tbl_database_list ( createNewDatabaseList(), loadNewDatabase() ) and returns the updated database name */
	function getSetAvailableDatabase($data){
		$datas = DAOFactory::getTblDatabaseListDAO()->queryAllOrderBy('
						CASE 
							WHEN ub = "'.$data.'" AND `status` = "available" 
								THEN 1 
							WHEN ub = "'.$data.'" AND `status` = "queue" 
								THEN 2
							WHEN ub = "'.$data.'" 
								THEN 3
							ELSE 4
						END, id  LIMIT 1
						');
		if(isset($datas) && !empty($datas)){
			$datas = $datas[0];
			if($datas->status == 'queue'){
				$_POST['name'] = $datas->databaseName;
				$this->loadNewDatabase($data);
			} elseif($datas->status == 'used'){
				// $data = $this->createNewDatabaseList();
				// $_POST['name'] = $data->databaseName;
				// $this->loadNewDatabase();
			}
			$datas->status = 'used';
			$datas->dateUsed = date('Y-m-d H:i:s');
			DAOFactory::getTblDatabaseListDAO()->update($datas);
		} else {
			// $data = $this->createNewDatabaseList();
			// $_POST['name'] = $data->databaseName;
			// $this->loadNewDatabase();
		}
		
		$d = $this->createNewDatabaseList($data);
		// print_r($d);
		return $datas;
	}
}