<?php

use GPBMetadata\Google\Protobuf\Wrappers;

require_once (PATH_CONTROLLERS 	. "Common.php") ;
require_once (PATH_LIB . "LicenseFile.php");
require_once (PATH_MODELS . "corporate_gift/CorporateGiftClass.php") ;
require_once (PATH_MODELS . "corporate_gift/CorporateGiftHistoryClass.php") ;
require_once (PATH_MODELS . "customer/CustomerContactClass.php") ;

class CorporateGiftHistoryReport 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::CorporateGiftHistoryView ;

		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::CorporateGiftHistoryView:
					$this->processCorporateGiftHistoryReportInfo($params) ;
					break ;
				case Command::CorporateGiftHistoryFilter:
					$this->processCorporateGiftHistoryReportFilter($params) ;
					break ;
				case "C_GET_CUSTOMER_CONTACTS":
					$this->getCustomerContact($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 processCorporateGiftHistoryReportInfo($params) {
		ob_start() ;
		include (PATH_VIEWS . "report/CorporateGiftHistoryReportView.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']  = "Corporate Gift History" ;
		$this->sendJsonResponse(Status::Ok,"Load Corporate Gift History Info",$dataReturn,$this->type);
    }

    private function processCorporateGiftHistoryReportFilter($params) {
		$cls 			= new CorporateGiftHistoryClass($this->db) ;

		$customer_id	= $params['customer_id'] ;
		$contact_id		= $params['contact_id'] ;
		$delivery_from  = $params['delivery_from'] ;
		$delivery_to    = $params['delivery_to'] ;
		$gift    		= $params['gift'] ;

		$rows = $cls->getHistoryReport($customer_id, $contact_id, $delivery_from, $delivery_to, $gift);
		$htmlContent = $this->convertCorporateGiftHistoryToHtmlTable($rows);
		$this->sendJsonResponse(Status::Ok,"",$htmlContent,$this->type);
		unset($list);
	}

	private function convertCorporateGiftHistoryToHtmlTable($list) {
		// Define common padding style for table cells
		$cellStyle = "padding:5px!important;";
		
		$htmlStr = "<thead style='background-color: grey;color:white;'>" .
					"<tr>" .
						"<th style='min-width:100px;'>Company Name</th>".
						"<th style='min-width:200px;'>Contact Name</th>".
						"<th style='min-width:50px;'>Delivery Date</th>".
						"<th style='min-width:50px;'>Chosen Gift</th>".
						"<th style='min-width:50px;'>Gift Amount</th>".
					"</tr>";
		$htmlStr .=	"</thead> <tbody>";

		$total_gifts = [];
		foreach($list as $row) {
			$htmlStr .= '<tr>' .
							'<td style="' . $cellStyle . '">' . $row[CustomerTable::C_NAME] . '</td>' .
							'<td style="' . $cellStyle . '">' . $row[CustomerContactTable::C_PREFER_NAME] . '</td>' .
							'<td style="' . $cellStyle . '">' . date_create($row[CorporateGiftHistoryTable::C_DELIVERY_DATE])->format("d/m/Y") . '</td>' .
							'<td style="' . $cellStyle . '">' . $row[CorporateGiftDetailTable::C_ITEM_NAME] . '</td>' .
							'<td style="' . $cellStyle . '">' . $row[CorporateGiftHistoryTable::C_GIFT_AMOUNT] . '</td>' .
						'</tr>';
		}
		$htmlStr .='</tbody>';

		return $htmlStr;
	}

    private function getCustomerContact($params){
        $cls  = new CustomerContactClass($this->db);
        $rows = $cls->getActiveCustomerContact($params['id']);

        $vls[] = array ('code'=>"",'desc'=>"-- Select Contact --") ;
        foreach($rows as $row){
            $vls[] = array ('code'=>$row[CustomerContactTable::C_CONTACT_ID],'desc'=>$row[CustomerContactTable::C_PREFER_NAME]) ;
        }

        $option =  Util::createOptionValue($vls);
        $this->sendJsonResponse(Status::Ok,"Corporate Gift Entry Info",$option,$this->type);
    }
}
?>