<?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 CustomerContactListing 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::CustomerContactListingView ;

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

    private function processCustomerContactListingFilter($params) {
		$cls 			= new CustomerClass($this->db) ;

		$company_code		= $params['company_code'] ;
		$company_name		= $params['company_name'] ;
		$software   		= $params['software'] ;
		$customer_status	= $params['customer_status'] ;
		$contact    		= $params['contact_name'] ;
		$contact_status    	= $params['contact_status'] ;

		$rows = $cls->getCustomerContactListing($company_code, $company_name, $software, $customer_status, $contact, $contact_status);

		$htmlContent = $this->convertCustomerContactListingToHtmlTable($rows);
		$this->sendJsonResponse(Status::Ok,"",$htmlContent,$this->type);
		unset($list);
	}

	private function convertCustomerContactListingToHtmlTable($list) {
        
		// 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(\'customercontactlisting-row-filter\',this,true);"></i>' .
						'</th>' .
						"<th style='min-width:50px'>Customer Code</th>".
						"<th style='min-width:250px'>Customer Name</th>".
						"<th style='min-width:100px'>Software</th>".
						"<th style='min-width:50px'>Customer Status</th>".
						"<th style='min-width:200px'>Address</th>".
						"<th style='min-width:200px'>Contact name</th>".
						"<th style='min-width:100px'>Department</th>".
						"<th style='min-width:100px'>Designation</th>".
						"<th style='min-width:150px'>Phone</th>".
						"<th style='min-width:100px'>Mobile</th>".
						"<th style='min-width:150px'>Email</th>".
						"<th style='min-width:50px'>Contact Status</th>".
					"</tr>";
		$htmlStr .= '<tr style="left: 0px;" id="customercontactlisting-row-filter">
						<td style="min-width:30px" data-field="filter" id="col_filter">&nbsp;</td>
						<td style="min-width:50px" data-field="type" id="txtcolcustomercode">
							<input type="text" class="form-control" name="customercontactlisting-input-filter" id="txtcolcustomercode" onkeyup="filterData(this,1);" />
						</td>
						<td style="min-width:50px" data-field="number" id="col_number">
							<input type="text" class="form-control" name="customercontactlisting-input-filter" id="txtcolcustomername" onkeyup="filterData(this,2);" />
						</td>
						<td style="min-width:50px" data-field="line" id="col_line">
							<input type="text" class="form-control" name="customercontactlisting-input-filter" id="txtcolsoftware" onkeyup="filterData(this,3);" />
						</td>
						<td style="min-width:50px" data-field="period" id="col_period">
							<input type="text" class="form-control" name="customercontactlisting-input-filter" id="txtcolcustomerstatus" onkeyup="filterData(this,4);" />
						</td>
						<td style="min-width:100px" data-field="trans_date" id="col_trans_date">
							<input type="text" class="form-control" name="customercontactlisting-input-filter" id="txtcoladdress" onkeyup="filterData(this,5);" />
						</td>
						<td style="min-width:150px" data-field="trans_ref" id="col_trans_ref">
							<input type="text" class="form-control" name="customercontactlisting-input-filter" id="txtcoldepartment" onkeyup="filterData(this,6);" />
						</td>
						<td style="min-width:100px" data-field="allocation" id="col_allocation">
							<input type="text" class="form-control" name="customercontactlisting-input-filter" id="txtcolalcontactname" onkeyup="filterData(this,7);" />
						</td>
						<td style="min-width:200px" data-field="desc" id="col_desc">
							<input type="text" class="form-control" name="customercontactlisting-input-filter" id="txtcoldesignation" onkeyup="filterData(this,8);" />
						</td>
						<td style="min-width:200px" data-field="desc" id="col_desc">
							<input type="text" class="form-control" name="customercontactlisting-input-filter" id="txtcolphone" onkeyup="filterData(this,9);" />
						</td>
						<td style="min-width:200px" data-field="desc" id="col_desc">
							<input type="text" class="form-control" name="customercontactlisting-input-filter" id="txtcolmobile" onkeyup="filterData(this,10);" />
						</td>
						<td style="min-width:200px" data-field="desc" id="col_desc">
							<input type="text" class="form-control" name="customercontactlisting-input-filter" id="txtcolemail" onkeyup="filterData(this,11);" />
						</td>
						<td style="min-width:200px" data-field="desc" id="col_desc">
							<input type="text" class="form-control" name="customercontactlisting-input-filter" id="txtcolcontactstatus" onkeyup="filterData(this,14);" />
						</td>
					</tr>';
		$htmlStr .=	"</thead> <tbody>";

		foreach($list as $key=>$row) {
			$customerStatus = ($row[CustomerTable::C_STATUS] == 0) ? 'Inactive ' : 'Active';
			$contactStatus  = ($row[CustomerContactTable::C_STATUS] == 0) ? 'Inactive' : 'Active';
			
			$htmlStr .= "<tr id='customercontactlisting-row" . $key . "'>" .
							"<td style='display:none;'>" . $row[CustomerTable::C_ID] . "_" . $row[CustomerContactTable::C_ID] . "</td>" .
							"<td></td>" .
							"<td style='" . $cellStyle . "'>" . htmlspecialchars($row[CustomerTable::C_CODE] ?? "") . "</td>" .
							"<td style='" . $cellStyle . "'>" . htmlspecialchars($row[CustomerTable::C_NAME] ?? "") . "</td>" .
							"<td style='" . $cellStyle . "'>" . htmlspecialchars($row[SoftwareTable::C_DESC] ?? "") . "</td>" .
							"<td style='" . $cellStyle . "'>" . $customerStatus . "</td>" .
							"<td style='" . $cellStyle . "white-space: pre-line;'>" . htmlspecialchars($row[CustomerTable::C_ADDRESS] ?? "") . "</td>" .
							"<td style='" . $cellStyle . "'>" . htmlspecialchars($row[CustomerContactTable::C_NAME] ?? "") . "</td>" .
							"<td style='" . $cellStyle . "'>" . htmlspecialchars($row[CustomerContactTable::C_DEPT] ?? "") . "</td>" .
							"<td style='" . $cellStyle . "'>" . htmlspecialchars($row[CustomerContactTable::C_DESIGNATION] ?? "") . "</td>" .
							"<td style='" . $cellStyle . "'>" . htmlspecialchars($row[CustomerContactTable::C_TELEPHONE] ?? "") . "</td>" .
							"<td style='" . $cellStyle . "'>" . htmlspecialchars($row[CustomerContactTable::C_MOBILE] ?? "") . "</td>" .
							"<td style='" . $cellStyle . "'>" . htmlspecialchars($row[CustomerContactTable::C_EMAIL] ?? "") . "</td>" .
							"<td style='" . $cellStyle . "'>" . $contactStatus . "</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, "") ;
	}
}
?>