<?php
require_once (PATH_CONTROLLERS 	. "Common.php") ;
require_once (PATH_LIB . "LicenseFile.php");
require_once (PATH_MODELS . "customer/CustomerClass.php") ;
require_once (PATH_MODELS . "admin/SoftwareClass.php") ;

class ConversationReport 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::ConversationReportView ;

		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::ConversationReportView:
					$this->processConversationReportInfo($params) ;
					break ;
				case Command::ConversationReportFilter:
					$this->processConversationReportFilter($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 processConversationReportInfo($params) {
		ob_start() ;
		include (PATH_VIEWS . "report/ConversationReportView.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']  = "Conversation Report" ;
		$this->sendJsonResponse(Status::Ok,"Load Customer Contact Listing Info",$dataReturn,$this->type);
    }

    private function processConversationReportFilter($params) {
		$cls 			= new CustomerClass($this->db) ;
		
		$company_name		= $params['company_name'] ;
		$contact_name 		= $params['contact_name'] ;
		$conv_date_from		= $params['conv_date_from'];
		$conv_date_to		= $params['conv_date_to'];
		$next_date_from		= $params['next_date_from'];
		$next_date_to		= $params['next_date_to'];
		$assign_to			= $params['assign_to'];

		try{
			$rows = $cls->getCoversationReport($company_name, $contact_name, $conv_date_from, $conv_date_to, $next_date_from, $next_date_to, $assign_to);
			$htmlContent = $this->convertConversationReportToHtmlTable($rows, $next_date_from, $next_date_to);
			$this->sendJsonResponse(Status::Ok,"",$htmlContent,$this->type);
			unset($list);
			return;
		}catch(Exception $e){
			$this->sendJsonResponse(Status::Error,Message::DB_ERR_QUERY,'',$this->type);
			unset($list);
			return;
		}

	}

	private function convertConversationReportToHtmlTable($list, $next_date_from, $next_date_to) {
        
		// Define common padding style for table cells
		$cellStyle = "padding-left:8px!important;padding-right:3px!important;padding-top:5px!important;";
		
		$htmlStr = "<thead style='background-color: grey;color:white;'>" .
					"<tr>" .
						'<th style="text-align: center;min-width:30px">' .
							'<i class="fa fa-filter" style="font-size:12px;cursor:pointer;" onclick="filterRow(\'conversationreport-row-filter\',this,true);"></i>' .
						'</th>' .
						"<th style='min-width:250px'>Customer Name</th>".
						"<th style='min-width:100px'>Customer Contact</th>".
						"<th style='min-width:100px'>Conversation Date</th>".
						"<th style='min-width:100px'>Conversation</th>".
						"<th style='min-width:100px'>Next Action</th>".
						"<th style='min-width:100px'>Next Action Date</th>".
						"<th style='min-width:100px'>Assign to</th>".
					"</tr>";
		$htmlStr .= '<tr style="left: 0px;" id="conversationreport-row-filter">
						<td style="min-width:30px" data-field="filter" id="col_filter">&nbsp;</td>
						<td style="min-width:50px" data-field="type" id="col_customer_name">
							<input type="text" class="form-control" name="conversationreport-input-filter" id="txtcolcustomername" onkeyup="filterData(this,1);" />
						</td>
						<td style="min-width:50px" data-field="number" id="col_contact_name">
							<input type="text" class="form-control" name="conversationreport-input-filter" id="txtcolcontactname" onkeyup="filterData(this,2);" />
						</td>
						<td style="min-width:50px" data-field="line" id="col_conversation_date">
							<input type="text" class="form-control" name="conversationreport-input-filter" id="txtcolconversationdate" onkeyup="filterData(this,3);" />
						</td>
						<td style="min-width:50px" data-field="period" id="col_conversation">
							<input type="text" class="form-control" name="conversationreport-input-filter" id="txtcolconversation" onkeyup="filterData(this,4);" />
						</td>
						<td style="min-width:100px" data-field="trans_date" id="col_next_action">
							<input type="text" class="form-control" name="conversationreport-input-filter" id="txtcolnextaction" onkeyup="filterData(this,5);" />
						</td>
						<td style="min-width:150px" data-field="trans_ref" id="col_next_action_date">
							<input type="text" class="form-control" name="conversationreport-input-filter" id="txtcolnextactiondate" onkeyup="filterData(this,6);" />
						</td>
						<td style="min-width:150px" data-field="assign_to" id="col_next_action_date">
							<input type="text" class="form-control" name="conversationreport-input-filter" id="txtcolnextassignto" onkeyup="filterData(this,7);" />
						</td>
					</tr>';
		$htmlStr .=	"</thead> <tbody>";
		$person_name  = "";
		foreach($list as $key=>$row) {
			if($person_name == ""){
				$person_name	  = $row[PersonContactTable::C_PREFER_NAME];
				$next_action 	  = $row[ProspectContactTable::C_NEXT_ACTION];
				$next_action_date = "";
				if($row[ProspectContactTable::C_ACTION_DATE] != NULL_DATE . ".000"){
					$next_action_date 	= date_create($row[ProspectContactTable::C_ACTION_DATE])->format('d/m/Y');
				}

			}else if($person_name != $row[PersonContactTable::C_PREFER_NAME]){
				$next_action 	  = $row[ProspectContactTable::C_NEXT_ACTION];
				$next_action_date = "";
				if($row[ProspectContactTable::C_ACTION_DATE] != NULL_DATE . ".000"){
					$next_action_date 	= date_create($row[ProspectContactTable::C_ACTION_DATE])->format('d/m/Y');
				}
			}else{
				$next_action 		= "";
				$next_action_date 	= "";
			}

			if(($next_date_from != '' || $next_date_to != '')){
				if($person_name != $row[PersonContactTable::C_PREFER_NAME] || $key == 0){
					$person_name = $row[PersonContactTable::C_PREFER_NAME];
					$htmlStr .= "<tr id='conversationreport-row" . $key . "'>" .
									"<td></td>" .
									"<td style='" . $cellStyle . "'>" . htmlspecialchars($row[ProspectTable::C_NAME] ?? "") . "</td>" .
									"<td style='" . $cellStyle . "'>" . htmlspecialchars($row[PersonContactTable::C_PREFER_NAME] ?? "") . "</td>" .
									"<td style='" . $cellStyle . "'>" . date_create($row[ProspectConversationTable::C_CREATE_DATE])->format('d/m/Y') . "</td>" .
									"<td style='" . $cellStyle . "'>" . htmlspecialchars($row[ProspectConversationTable::C_DESC] ?? "") . "</td>" .
									"<td style='" . $cellStyle . "'>" . htmlspecialchars($next_action ?? "") . "</td>" .
									"<td style='" . $cellStyle . "'>" . htmlspecialchars($next_action_date ?? "") . "</td>" .
									"<td style='" . $cellStyle . "'>" . htmlspecialchars($row[UserTable::C_FULL_NAME] ?? "") . "</td>" .
									"</tr>";
				}
			}else{
				if($person_name != $row[PersonContactTable::C_PREFER_NAME]){
					$person_name = $row[PersonContactTable::C_PREFER_NAME];
				}
				$htmlStr .= "<tr id='conversationreport-row" . $key . "'>" .
								"<td></td>" .
								"<td style='" . $cellStyle . "'>" . htmlspecialchars($row[ProspectTable::C_NAME] ?? "") . "</td>" .
								"<td style='" . $cellStyle . "'>" . htmlspecialchars($row[PersonContactTable::C_PREFER_NAME] ?? "") . "</td>" .
								"<td style='" . $cellStyle . "'>" . date_create($row[ProspectConversationTable::C_CREATE_DATE])->format('d/m/Y') . "</td>" .
								"<td style='" . $cellStyle . "'>" . htmlspecialchars($row[ProspectConversationTable::C_DESC] ?? "") . "</td>" .
								"<td style='" . $cellStyle . "'>" . htmlspecialchars($next_action ?? "") . "</td>" .
								"<td style='" . $cellStyle . "'>" . htmlspecialchars($next_action_date ?? "") . "</td>" .
								"<td style='" . $cellStyle . "'>" . htmlspecialchars($row[UserTable::C_FULL_NAME] ?? "") . "</td>" .
								"</tr>";
			}
		}
		$htmlStr .="</tbody>";

		return $htmlStr;
	}

	function getSoftwareOptions(){
		$clsSoftware = new SoftwareClass($this->db);
		$rows = $clsSoftware->getTable();


		$vls   = array() ;
		$vls[] = array ('code'=>"0",'desc'=>"-- Select a Contact --") ;

		if(!is_null($rows) && count($rows) > 0) {
			foreach ($rows as $row) {
				$vls[] = array ('code'=>$row[SoftwareTable::C_ID],'desc'=>$row[SoftwareTable::C_DESC]) ;
			}
		}
		return Util::createOptionValue($vls, "") ;
	}
}
?>