<?php
require_once (PATH_CONTROLLERS 	. "Common.php") ;
require_once (PATH_MODELS . "prospect/ProspectClass.php") ;
require_once (PATH_MODELS . "admin/SoftwareClass.php") ;

class AllAccountReport extends GLBase {
	private $type = "" ;
	function __construct() {
		$this->db 		= $_SESSION[SE_DB] ;
		$this->orgid 	= $_SESSION[SE_ORGID] ;
	}
	function __destruct() {
		unset($this->db) ;
	}
	public function processRequest($params) {
		$this->type = Command::AllAccountReportView ;

		try {
			$this->db->open() ;
			if (isset($params) && count($params) > 0) {
				if (isset($params['type']))
					$this->type = $params['type'] ;
			}
			if (!$this->checkSessionTimeout($this->type)) {
				$this->db->close();
				return false;
			}
			switch ($this->type) {
				case Command::AllAccountReportView:
					$this->processAllAccountReportInfo($params) ;
					break ;
				case Command::AllAccountReportFilter:
					$this->processAllAccountReportFilter($params) ;
					break ;
				default:
					$this->sendJsonResponse(Status::Error,"invalid request.","",$this->type) ;
					break ;
			}
			$this->db->close() ;
			return true ;
		} catch (Exception $e) {
			$this->db->close() ;
			die ($e->getMessage()) ;
		}
	}

	private function processAllAccountReportInfo($params) {
		ob_start() ;
		include (PATH_VIEWS . "report/AllAccountReportView.php") ;

		$action_menu = "";

		$var_screen = str_replace("[index]",$params['index'], Util::minifyHtml(ob_get_clean())) ;
		$dataReturn = [];
		$dataReturn['htmlContent'] = $var_screen;
		$dataReturn['action_menu'] = $action_menu;
		$dataReturn['headTitle']   = "All Account Report" ;
		$this->sendJsonResponse(Status::Ok,"Load All Account Report",$dataReturn,$this->type);
	}

	private function processAllAccountReportFilter($params) {
		$cls = new ProspectClass($this->db) ;

		try{
			$rows        = $cls->getAllAccountList();
			$htmlContent = $this->convertAllAccountReportToHtmlTable($rows);
			$this->sendJsonResponse(Status::Ok,"",$htmlContent,$this->type);
			unset($cls);
			return;
		}catch(Exception $e){
			Log::write("[AllAccountReport]" . $e->getMessage());
			$this->sendJsonResponse(Status::Error,Message::DB_ERR_QUERY,'',$this->type);
			unset($cls);
			return;
		}
	}

	private function convertAllAccountReportToHtmlTable($list) {
		// Define common padding style for table cells
		$cellStyle = "padding-left:8px!important;padding-right:3px!important;padding-top:5px!important;";

		//column indexes here must match the ones passed to sortAllAccountReport()
		$htmlStr = "<thead style='background-color: grey;color:white;'>" .
					"<tr>" .
						"<th style='min-width:60px;width:60px;'>S/N</th>" .
						"<th style='min-width:120px;cursor:pointer;' onclick='sortAllAccountReport(1);'>Type<i class='fa'></i></th>" .
						"<th style='min-width:300px;cursor:pointer;' onclick='sortAllAccountReport(2);'>Company Name<i class='fa fa-sort-up'></i></th>" .
						"<th style='min-width:200px;cursor:pointer;' onclick='sortAllAccountReport(3);'>Accounting Software<i class='fa'></i></th>" .
					"</tr>" .
				   "</thead> <tbody>";

		if (!is_null($list) && count($list) > 0) {
			$sn = 1;
			foreach($list as $row) {
				//PROSPECT_ACCOUNT_FLAG is nchar(1), so trim before mapping it to Supplier/Customer
				$type = $this->getAccountFlagsDesc(trim($row[ProspectTable::C_FLAG] ?? "")) ;

				$htmlStr .= "<tr id='allaccountreport-row" . $sn . "'>" .
								"<td style='" . $cellStyle . "'>" . $sn . "</td>" .
								"<td style='" . $cellStyle . "'>" . htmlspecialchars($type) . "</td>" .
								"<td style='" . $cellStyle . "'>" . htmlspecialchars($row[ProspectTable::C_NAME] ?? "") . "</td>" .
								"<td style='" . $cellStyle . "'>" . htmlspecialchars($row['SoftwareAcc'] ?? "") . "</td>" .
							"</tr>";
				$sn++;
			}
		} else {
			$htmlStr .= "<tr class='no-records'><td colspan='4' style='" . $cellStyle . "'>No records found.</td></tr>";
		}
		$htmlStr .= "</tbody>";

		return $htmlStr;
	}
}
?>
