<?php
if($_COOKIE['sejahtera_role'] == 10)
{
	include_once ("includes/setup_pemohon.inc.php");
}
else
{
	include_once ("includes/setup.inc.php");
}
$myID = fetch_postget_val('id',$_POST, $_GET);
$dateTimeNow = dateTimeNow();

$indicator_Query = "SELECT A.*, C.desk_jenisaduan, D.desk_statusaduan, E.nama, F.desk_peranan, G.desk_dun, H.desk_daerah
				  FROM m_aduan A
				  LEFT JOIN i_jenisaduan C ON C.id_jenisaduan = A.id_jenisaduan
				  LEFT JOIN i_statusaduan D ON D.id_statusaduan = A.id_statusaduan
				  LEFT JOIN t_pengguna E ON E.id_pengguna = A.id_assign
				  LEFT JOIN t_peranan F ON F.id_peranan= E.id_peranan
				  LEFT JOIN i_dun G ON G.id_dun= A.id_dun
				  LEFT JOIN i_daerah H ON H.id_daerah= G.id_daerah
				  WHERE A.id_aduan = ?";
$PDO_stmt = $PDO->prepare($indicator_Query); 
$PDO_stmt->bindValue(1, $myID, PDO::PARAM_INT);
$PDO_stmt->execute();
$indicator_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

//***********************************************************************************************************************
// PRINT PDF
//***********************************************************************************************************************
require_once('classes/tcpdf/config/tcpdf_config.php');
require_once('classes/tcpdf/tcpdf.php');

//include_once ("inc/setup.inc.php");
error_reporting(0);

// Extend the TCPDF class to create custom Header and Footer
class MYPDF extends TCPDF {

	private $myHeaderTitle = array();
	public function setHeaderTable($headerArray)
	{
		$this->myHeaderTitle = array();
		for( $num = 0 ; $num < count($headerArray) ; $num++ )
		{
			$this->myHeaderTitle[] = $headerArray[$num];
		}
	}
	
	private $pageTitle = '';
	public function setHeaderTitle($title)
	{
		$this->pageTitle = $title;
	}
	//Page header
	public function Header() {
		
		//$this->setJPEGQuality(90);
		//$this->Image('images/banner.png', 0, 0, 210, 0, 'PNG', '');
				
		//$this->Ln(30);
		//$this->SetFont('helveticaB', '', 14); //CHANGE THIS FOR TITLE FONT
		//$this->MultiCell(0, 0, $this->pageTitle, 0, 'C', 0, 1, '', '', true);
		/*
		$HTML = '<table border="1" style="text-align:center;padding: 6px;">';
		$HTML .= '<tr style="font-weight: bold;">';
		$counter = 1;
		for( $num = 0 ; $num < count($this->myHeaderTitle) ; $num++ )
		{
			if($this->myHeaderTitle[$num][1] > 0)
			{
				$HTML .= '<th style="width:'.$this->myHeaderTitle[$num][1].'%">'.$this->myHeaderTitle[$num][0].'</th>';
			}
		}
		$HTML .= '</tr>';
		$HTML .= '</table>';
		$this->SetFont('helvetica', '', 10); //CHANGE THIS FOR HEADER TITLE FONT
		$this->writeHTMLCell(0, 0, '', 19, $HTML, 0, 0, 0, true, '', false);
		*/
	}

	// Page footer
	public function Footer() {
		
		// Position at 15 mm from bottom
		$this->SetFont('helvetica', '', 8);
		$this->MultiCell(0, 0, 'Muka Surat '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, 'R', 0, 1, '', '', true);
	}
}

// -----------------------------------------------------------------------------
// create new PDF document
$pdf = new MYPDF("P", PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); //P for portrait, L for landscape

$khairat = '';
if($indicator_List['m_pemohon']['id_statushidup'] == 2)
{
	$khairat = 'KHAIRAT KEMATIAN ';
}
$TITLE = 'MAKLUMAT ADUAN';
$FILE_NAME = 'MAKLUMAT ADUAN';
$output_filename = "borang_aduan.pdf";
$pdf->setHeaderTitle($TITLE);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('PSUKPP');
$pdf->SetTitle($TITLE);
$pdf->SetSubject($TITLE);
$pdf->SetKeywords('TCPDF, PDF, $TITLE');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

if($manual_TOP <= 0)
{
	$manual_TOP = PDF_MARGIN_TOP;
}
//set margins
//$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetMargins(5, 10, 5);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER - 10);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM - 10);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

// -----------------------------------------------------------------------------
$pdf->setCellPaddings(0, 0, 0, 0);
$pdf->AddPage();
// MultiCell($w, $h, $txt, $border=0, $align='J', $fill=0, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0)
$pdf->SetFont('helvetica', '', 10); //same font for the whole pdf

$HTML = '<table border="0" style="width:100%;padding:5px;">';
$HTML .= '<tr>';
$HTML .= '<td style="text-align:center" colspan="2"><h2>MAKLUMAT ADUAN</h2></td>';
$HTML .= '</tr>';
$HTML .= '</table>';
$pdf->writeHTMLCell(0, 0, '', '', $HTML, 0, 1, 0, true, 'L', false);
	
$HTML = '<table border="0" style="width:100%;padding:5px;border:1px solid black;">';
$HTML .= '<tr>';
$HTML .= '<td style="text-align:left;border:1px solid black" colspan="3"><h4>Maklumat Aduan</h4></td>';
$HTML .= '</tr>';
$HTML .= '<tr>';
	$HTML .= '<td style="width:30%">Nama Pengadu</td>';
	$HTML .= '<td style="width:2%">:</td>';
	$HTML .= '<td style="width:68%"><b>'.$indicator_List['nama_pengadu'].'</b></td>';
$HTML .= '</tr>';
$HTML .= '<tr>';
	$HTML .= '<td style="width:30%">No KP Pengadu</td>';
	$HTML .= '<td style="width:2%">:</td>';
	$HTML .= '<td style="width:68%"><b>'.$indicator_List['kpbaru_pengadu'].'</b></td>';
$HTML .= '</tr>';									
$HTML .= '<tr>';
	$HTML .= '<td style="width:30%">No KP Pemohon</td>';
	$HTML .= '<td style="width:2%">:</td>';
	$HTML .= '<td style="width:68%"><b>'.$indicator_List['kpbaru_pemohon'].'</b></td>';
$HTML .= '</tr>';
$HTML .= '<tr>';
	$HTML .= '<td style="width:30%">Email </td>';
	$HTML .= '<td style="width:2%">:</td>';
	$HTML .= '<td style="width:68%"><b>'.$indicator_List['emel_pengadu'].'</b></td>';
$HTML .= '</tr>';
$HTML .= '<tr>';
	$HTML .= '<td style="width:30%">No. Telefon</td>';
	$HTML .= '<td style="width:2%">:</td>';
	$HTML .= '<td style="width:68%"><b>'.$indicator_List['tel_pengadu'].'</b></td>';
$HTML .= '</tr>';
$HTML .= '<tr>';
	$HTML .= '<td style="width:30%">Dun</td>';
	$HTML .= '<td style="width:2%">:</td>';
	$HTML .= '<td style="width:68%"><b>'.$indicator_List['desk_dun'].'</b></td>';
$HTML .= '</tr>';
$HTML .= '<tr>';
	$HTML .= '<td style="width:30%">Daerah</td>';
	$HTML .= '<td style="width:2%">:</td>';
	$HTML .= '<td style="width:68%"><b>'.$indicator_List['desk_daerah'].'</b></td>';
$HTML .= '</tr>';
$HTML .= '<tr>';
	$HTML .= '<td style="width:30%">Jenis Aduan</td>';
	$HTML .= '<td style="width:2%">:</td>';
	$HTML .= '<td style="width:68%"><b>'.$indicator_List['desk_jenisaduan'].'</b></td>';
$HTML .= '</tr>';
$HTML .= '<tr>';
	$HTML .= '<td style="width:30%">Tajuk Aduan</td>';
	$HTML .= '<td style="width:2%">:</td>';
	$HTML .= '<td style="width:68%"><b>'.$indicator_List['tajuk'].'</b></td>';
$HTML .= '</tr>';
$HTML .= '<tr>';
	$HTML .= '<td style="width:30%">Keterangan</td>';
	$HTML .= '<td style="width:2%">:</td>';
	$HTML .= '<td style="width:68%"><b>'.$indicator_List['laporan'].'</b></td>';
$HTML .= '</tr>';
$HTML .= '</table>';
$HTML .= '<br><br>';
$pdf->writeHTMLCell(0, 0, '', '', $HTML, 0, 1, 0, true, 'L', false);

$aduanList = [];
$indicator_Query = "SELECT A.*, B.nama, C.desk_peranan
				  FROM m_aduanmbls A
				  LEFT JOIN t_pengguna B ON B.id_pengguna = A.id_keyin
				  LEFT JOIN t_peranan C ON C.id_peranan = B.id_peranan
				  WHERE A.id_aduan = ?
				  ORDER BY A.tkh_keyin ASC";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $myID, PDO::PARAM_INT);
$PDO_stmt->execute();
while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	$aduanList[] = $result;
}
$HTML = '<table border="0" style="width:100%;padding:5px;border:1px solid black;">';
$HTML .= '<tr>';
$HTML .= '<td style="text-align:left;border:1px solid black"><h4>Maklumbalas Aduan</h4></td>';
$HTML .= '</tr>';
	if(count($aduanList) > 0)
	{
		for($num = 0; $num < count($aduanList); $num++)
		{
			$nama = $aduanList[$num]['nama'];
			$peranan = $aduanList[$num]['desk_peranan'];
			if($aduanList[$num]['id_peranan'] == 10)
			{		
				$indicator_Query = "SELECT *
					  FROM m_aduan
					  WHERE id_aduan = ?";
				$PDO_stmt = $PDO->prepare($indicator_Query); 
				$PDO_stmt->bindValue(1, $myID, PDO::PARAM_INT);
				$PDO_stmt->execute();
				$aduan = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
				
				$nama = $aduan['nama_pengadu'];
				$peranan = 'PENGADU';				
			}
			
			$HTML .= '<tr>';
				$HTML .= '<td style="width:80%"><span style="font-size:12px"><b>'.$nama.' ('.$peranan.')</b></span><br>'.str_replace("\n", '<br>', $aduanList[$num]['maklumbalas']).'</td>';
				$HTML .= '<td style="width:20%;text-align:right;font-size:12px">'.formatDate($aduanList[$num]['tkh_keyin'], 'M d, Y H:i A').'</td>';
			$HTML .= '</tr>';
		}
	}
	else
	{
		$HTML .= '<tr>';
			$HTML .= '<td style="text-align:center">Tiada maklumbalas</td>';
		$HTML .= '</tr>';
	}
$HTML .= '</table>';
$pdf->writeHTMLCell(0, 0, '', '', $HTML, 0, 1, 0, true, 'L', false);

//Close and output PDF document
ob_end_clean();
$pdf->Output($FILE_NAME.'.pdf', 'I');

//***********************************************************************************************************************
// END PRINT PDF
//***********************************************************************************************************************

?>