<?php

use GPBMetadata\Google\Protobuf\Wrappers;

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

class CorporateGiftSummaryReport 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::CorporateGiftSummaryView ;

		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::CorporateGiftSummaryView:
					$this->processCorporateGiftSummaryReportInfo($params) ;
					break ;
				case Command::CorporateGiftSummaryFilter:
					$this->processCorporateGiftSummaryReportFilter($params) ;
					break ;
				case "C_GET_GIFT_ITEM":
					$this->getGiftItem($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 processCorporateGiftSummaryReportInfo($params) {
		ob_start() ;
		include (PATH_VIEWS . "report/CorporateGiftSummaryReportView.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 Summary" ;
		$this->sendJsonResponse(Status::Ok,"Load Corporate Gift Summary Info",$dataReturn,$this->type);
    }

    private function processCorporateGiftSummaryReportFilter($params) {
		$cls 			= new CustomerScheduleClass($this->db) ;

		$delivery_from	= $params['delivery_from'] ;
		$delivery_to	= $params['delivery_to'] ;
		$gift    		= $params['gift'] ;
		$gift_type    	= $params['gift_type'] ;

		$rows = $cls->getCorporateGiftSummary($delivery_from, $delivery_to, $gift, $gift_type);
		$htmlContent = $this->convertCorporateGiftSummaryToHtmlTable($rows);
		$this->sendJsonResponse(Status::Ok,"",$htmlContent,$this->type);
		unset($list);
	}

	private function convertCorporateGiftSummaryToHtmlTable($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='min-width:100px;'>Delivery Date</th>".
						"<th style='min-width:200px;'>Gift Type</th>".
						"<th style='min-width:50px;'>Total Gift</th>".
					"</tr>";
		$htmlStr .=	"</thead> <tbody>";

		$total_gifts = [];
		foreach($list as $row) {
			if(!isset($total_gifts[$row[CorporateGiftDetailTable::C_ITEM_NAME]])){
				$total_gifts[$row[CorporateGiftDetailTable::C_ITEM_NAME]] = $row[CustomerScheduleTable::C_GIFT_AMOUNT];
			}else{
				$total_gifts[$row[CorporateGiftDetailTable::C_ITEM_NAME]] += $row[CustomerScheduleTable::C_GIFT_AMOUNT];
			}
			$htmlStr .= '<tr>' .
							'<td style="' . $cellStyle . '">' . date_create($row[CustomerScheduleTable::C_DELIVERY_DATE])->format("d/m/Y") . '</td>' .
							'<td style="' . $cellStyle . '">' . $row[CorporateGiftDetailTable::C_ITEM_NAME] . '</td>' .
							'<td style="' . $cellStyle . '">' . $row[CustomerScheduleTable::C_GIFT_AMOUNT] . '</td>' .
						'</tr>';
		}
		foreach($total_gifts as $key=>$total_gift){
			$htmlStr .= '<tr>' .
							'<td colspan="2" style="' . $cellStyle . ' text-align:right !important; font-weight: bold;">' . $key . '</td>'.
							'<td style="' . $cellStyle . '">' . $total_gift . '</td>'.
						'</tr>';
		}

		$htmlStr .='</tbody>';

		return $htmlStr;
	}

	private function getGiftItem($params){
		$cls  = new CorporateGiftDetailClass($this->db);
		$rows = $cls->getCorporateGiftDetailByGiftID($params['id']);

		$vls[] = array('code'=>'', 'desc'=>'All');
		foreach($rows as $row){
			$vls[] 		= array ('code'=>$row[CorporateGiftDetailTable::C_DETAIL_ID],'desc'=>$row[CorporateGiftDetailTable::C_ITEM_NAME]) ;
		}

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