<?php
class Report {
	private $hasQuotes, $PRIVATE_KEY, $TABLE_NAME;
	public function __construct($ID){

		global $con;

		//
		//	Private Key
		//
		$this->PRIVATE_KEY = "ReportID";
		//
		// Table name
		//
		$this->TABLE_NAME = "REPORT_HEAD";
		//
		// Get Table Column and add to object.
		//
		$query = "SELECT * FROM `{$this->TABLE_NAME}` WHERE `{$this->PRIVATE_KEY}` = $ID";
		$result = mysqli_query($con, $query);
		$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
		foreach($row as $key => $value){
				$this->$key = $value;
		}
		//
		// Get Columns Names and either if they need quotes in a query or not.
		//
		$this->hasQuotes = [];
		$query = "SHOW COLUMNS FROM `{$this->TABLE_NAME}`";
		$result = mysqli_query($con,$query);
		while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
			if(stripos($row['Type'], "int") === 0 || stripos($row['Type'], "double") === 0 || stripos($row['Type'], "tinyint") === 0 || stripos($row['Type'], "numeric") === 0 || stripos($row['Type'], "double") === 0 || stripos($row['Type'], "decimal") === 0 || stripos($row['Type'], "float") === 0 || stripos($row['Type'], "bigint") === 0 || stripos($row['Type'], "real") === 0 || stripos($row['Type'], "bool") === 0){
				$this->hasQuotes[$row['Field']] = false;
			}else{
				$this->hasQuotes[$row['Field']] = true;
			}
		}

		// $this->Columns = array();
		// $this->columnIndex = array();
		// $query = "SELECT id, ColumnName FROM REPORT_DETAIL WHERE ReportID = " . $this->ReportID;
		// $result = mysqli_query($con, $query);
		// while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
		// 	$this->columnIndex[$row['id']] = $row['ColumnName'];
		// 	//$column = new ReportColumn($row['id']);
		// 	//array_push($this->Columns, $column);
		// }


	}

	// public function getColumnByName($ColumnName){
	// 	global $con;
	// 	$query = "SELECT id FROM REPORT_DETAIL WHERE ReportID = $this->ReportID AND ColumnName LIKE '$ColumnName'";
	// 	$result = mysqli_query($con, $query);
	// 	echo mysqli_error($con)l
	// 	if(mysqli_num_rows($result)){
	// 			$row = mysqli_fetch_array($result);
	// 			return new ReportColumn($row['id']);
	// 	}else{
	// 		return false;
	// 	}
	//
	// }
	//
	// Function to create a "column = value" string usable by mysql query;
	//
	private function saveString($Column, $Value){

		if(is_null($Value)){
			$returnValue = "NULL";
		}else{
			$returnValue = ($this->hasQuotes[$Column])?"'$Value'":"$Value";
		}
		return " `$Column` = $returnValue ";
	}
	//
	// function to save all the values in the object to the table.
	//
	public function save(){
		global $con;

		$query = "UPDATE `{$this->TABLE_NAME}` SET ";
		$saveQuery = [];
		foreach($this->hasQuotes as $fieldName => $quotes){
			if($fieldName != $this->PRIVATE_KEY){
				$saveQuery[] = $this->saveString($fieldName, $this->$fieldName);
			}
		}
		$query .= implode(", ", $saveQuery) . " WHERE `{$this->PRIVATE_KEY}` = " . $this->{$this->PRIVATE_KEY};
		return mysqli_query($con, $query);
	}
}

class ReportColumn {
	private $hasQuotes, $PRIVATE_KEY, $TABLE_NAME;
	public function __construct($ID){
		if($ID == false){
			$this->Exists = false;
			return;
		}else{
			$this->Exists = true;
		}
		global $con;

		//
		//	Private Key
		//
		$this->PRIVATE_KEY = "id";
		//
		// Table name
		//
		$this->TABLE_NAME = "REPORT_DETAIL";
		//
		// Get Table Column and add to object.
		//
		$query = "SELECT * FROM `{$this->TABLE_NAME}` WHERE `{$this->PRIVATE_KEY}` = $ID";

		$result = mysqli_query($con, $query);
		//echo $query;
		$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
		//var_dump(mysqli_error($con));
		foreach($row as $key => $value){
				//var_dump($value);
				$this->$key = $value;
		}
		//
		// Get Columns Names and either if they need quotes in a query or not.
		//
		$this->hasQuotes = [];
		$query = "SHOW COLUMNS FROM `{$this->TABLE_NAME}`";
		$result = mysqli_query($con,$query);
		while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
			if(stripos($row['Type'], "int") === 0 || stripos($row['Type'], "double") === 0 || stripos($row['Type'], "tinyint") === 0 || stripos($row['Type'], "numeric") === 0 || stripos($row['Type'], "double") === 0 || stripos($row['Type'], "decimal") === 0 || stripos($row['Type'], "float") === 0 || stripos($row['Type'], "bigint") === 0 || stripos($row['Type'], "real") === 0 || stripos($row['Type'], "bool") === 0){
				$this->hasQuotes[$row['Field']] = false;
			}else{
				$this->hasQuotes[$row['Field']] = true;
			}
		}
	}
	//
	// Function to create a "column = value" string usable by mysql query;
	//
	private function saveString($Column, $Value){

		if(is_null($Value)){
			$returnValue = "NULL";
		}else{
			$returnValue = ($this->hasQuotes[$Column])?"'$Value'":"$Value";
		}
		return " `$Column` = $returnValue ";
	}
	//
	// function to save all the values in the object to the table.
	//
	public function save(){
		global $con;

		$query = "UPDATE `{$this->TABLE_NAME}` SET ";
		$saveQuery = [];
		foreach($this->hasQuotes as $fieldName => $quotes){
			if($fieldName != $this->PRIVATE_KEY){
				$saveQuery[] = $this->saveString($fieldName, $this->$fieldName);
			}
		}
		$query .= implode(", ", $saveQuery) . " WHERE `{$this->PRIVATE_KEY}` = " . $this->{$this->PRIVATE_KEY};
		return mysqli_query($con, $query);
	}
}
?>
