<?php
require_once (PATH_CONTROLLERS 	. "Common.php") ;
require_once (PATH_MODELS . "dimension/AnalysisUsageClass.php") ;
require_once (PATH_MODELS . "dimension/AnalysisCodeClass.php") ;
require_once (PATH_MODELS . "dimension/AnalysisSettingClass.php") ;
require_once (PATH_MODELS . "ledger_entry/LedgerHeaderClass.php") ;
require_once (PATH_MODELS . "ledger_entry/LedgerAnalysisClass.php") ;
require_once (PATH_MODELS . "ledger_entry/LedgerDocumentClass.php") ;
require_once (PATH_MODELS . "ledger_setting/LedgerSettingClass.php") ;
require_once (PATH_MODELS . "account/AccountClass.php") ;
require_once (PATH_MODELS . "account_group/AccountGroupClass.php") ;
require_once (PATH_MODELS . "payment_terms/PaymentTermsClass.php") ;
require_once (PATH_MODELS . "sales_invoice/SalesInvoiceClass.php") ;
require_once (PATH_MODELS . "sales_invoice/SalesInvoiceItemsClass.php") ;
require_once (PATH_MODELS . "sales_invoice/SalesInvoiceItemsDetailClass.php") ;
require_once (PATH_MODELS . "sales_invoice/SalesInvoiceMaintenanceClass.php") ;
require_once (PATH_MODELS . "sales_invoice/SalesInvoiceMaintenanceDetailClass.php") ;
require_once (PATH_MODELS . "sales_invoice/SalesInvoiceAttachmentClass.php") ;
require_once (PATH_MODELS . "sales_quotation/SalesQuotationClass.php") ;
require_once (PATH_MODELS . "sales_quotation/SalesQuotationItemsClass.php") ;
require_once (PATH_MODELS . "sales_quotation/SalesQuotationItemsDetailClass.php") ;
require_once (PATH_MODELS . "sales_quotation/SalesQuotationMaintenanceClass.php") ;
require_once (PATH_MODELS . "sales_quotation/SalesQuotationMaintenanceDetailClass.php") ;

class AuditLog 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 = REQ_VIEW;
		
		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::AuditLogInquiryView:
					$this->processView($params) ;
					break ;
				case REQ_QUERY:
					$this->filterAuditLog($params) ;
					break ;
				case "POPULATE_LOG":
                    $this->populateAuditLog($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 processView($params) {
        ob_start() ;
		include (PATH_VIEWS . "admin/AuditLogInquiryView.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']  = "Audit Log Inquiry" ;
		$this->sendJsonResponse(Status::Ok,"Load Audit Log Inquiry Info",$dataReturn,$this->type);
    }
    
    private function filterAuditLog($params) {
		$cls = new AuditLogClass($this->db) ;

		$log_bu             = $params['log_bu'] ;
		$log_table          = $params['log_table'] ;
		$log_table_desc     = $params['log_table_desc'] ;
		$log_user          	= $params['log_user'] ;
		$log_activity       = $params['log_activity'] ;
		$log_date_from    	= $this->getParamDate($params,'log_date_from',"") ;
		$log_date_to      	= $this->getParamDate($params,'log_date_to',"") ;

		$datas = $cls->getAuditLogList($log_bu, $log_table, $log_table_desc, $log_user, $log_activity, $log_date_from, $log_date_to);
		
		$htmlContent = $this->convertDataToHtmlTable($datas);

		$datasReturn = array();
		$datasReturn['htmlContent'] = $htmlContent;
		$datasReturn['totalRecords'] = count($datas);

		$this->sendJsonResponse(Status::Ok,"",$datasReturn,$this->type);
		unset($list);
	}

	private function convertDataToHtmlTable($datas) {
		// log::write($datas);
		$itemLen = count($datas);
		$htmlStr = "";

		for($i = 0; $i < $itemLen; $i++) {
			$row = $datas[$i];

			$tableName = str_replace(" ", "", ucwords(strtolower($row['log_table']), " "));
			$className = $tableName . "Class";
			$cls = new $className($this->db); //based on table filtered
			
			$logStr = $cls->beautifyAuditLog($tableName, $row['log_data']);

			$htmlStr .= "<tr id=\"auditlog-row" . ($i+1) . "\">" .
							"<td style='display:none;'>" . ($i+1) . "</td>" .
							"<td></td>" .
							"<td style='padding-left:8px!important;padding-right:3px!important;padding-top:5px!important;'>" . $row['log_table'] . "</td>" .
							"<td style='padding-left:8px!important;padding-right:3px!important;padding-top:5px!important;'>" . $this->getAuditLogActivityTypeDesc($row['log_type']) . "</td>" . 		
							"<td style='padding-left:8px!important;padding-right:3px!important;padding-top:5px!important;'>" . $row['log_date'] . "</td>" .
							"<td style='padding-left:8px!important;padding-right:3px!important;padding-top:5px!important;'>" . $logStr . "</td>" .
							"<td style='padding-left:8px!important;padding-right:3px!important;padding-top:5px!important;'>" . $row['log_user'] . "</td>" . 
						"</tr>";
		}
		
		return $htmlStr;
	}

	private function populateAuditLog($params) {
		$logDatas = "ACCOUNT_CODE=16500~~~ACCOUNT_TYPE=1~~~ACCOUNT_DESCRIPTION=Vehicles~~~ACCOUNT_GROUP_ID=33~~~ACTIVE_STATUS=1~~~PAYMENT_TERMS_ID=~~~ADDRESS=~~~COMPANY_REG=~~~TAX_REG=~~~WORKSTATION_ID=47.147.125.160~~~MODIFY_BY=10~~~MODIFY_DATE=2022-09-20 10:13:55";

		$cls = new AccountClass($this->db); //will based on table filtered
		$logStr = $cls->beautifyAuditLog($logDatas);

		$this->sendJsonResponse(Status::Ok,"Audit Log",$logStr,$this->type);
	}
}
?>