<?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);
$id_program = fetch_postget_val('program',$_POST, $_GET);
$dateTimeNow = dateTimeNow();

$indicator_Query = "SELECT A.*, i_program.desk_program
				  FROM m_pemohon A
				  LEFT JOIN i_program ON A.id_program = i_program.id_program
				  WHERE A.id_pemohon = ? AND A.id_program = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $myID, PDO::PARAM_INT);
$PDO_stmt->bindValue(2, $id_program, 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 ("classes/phpqrcode/qrlib.php");

$errorCorrectionLevel ='L';
$matrixPointSize = 1;
$url = $indicator_List['kpbaru_pemohon'];
$filename = 'qrcodes/'.$indicator_List['kpbaru_pemohon'].'.png';
QRcode::png($url, $filename, $errorCorrectionLevel, $matrixPointSize, 1);	
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
$TITLE = 'SENARAI SEMAK PEMOHONAN PROGRAM KEBAJIKAN';
$FILE_NAME = 'SENARAI SEMAK';
$output_filename = "slip_pemohonan.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 + 25;
}
//set margins
//$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetMargins(15, $manual_TOP, 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

$indicator_Query = "SELECT A.*,B.desk_dokumen
				  FROM i_dokumenprogram A
				  LEFT JOIN i_dokumen B ON B.id_dokumen = A.id_dokumen
				  WHERE A.id_program = ?
				  ORDER BY A.flag_wajib DESC, A.id_dokumen";
$PDO_stmt = $PDO->prepare($indicator_Query);

$PDO_stmt->bindValue(1, $id_program, PDO::PARAM_STR);
$PDO_stmt->execute();
$num = 0; 

$HTML = '<table border="0" style="width:100%;padding:5px;">';
$HTML .= '<tr>';
$HTML .= '<td>';
$HTML .= '<img src="'.$filename.'" style="width:90px;height:90px">';	
$HTML .= '</td>';
$HTML .= '<td style="text-align: right;">
			No. Siri : <b>'.$indicator_List['no_siri'].'</b><br><b>'.$indicator_List['nama_pemohon'].'</b><br><b>'.$indicator_List['kpbaru_pemohon'].'</b>';
$HTML .= '</td>';
$HTML .= '</tr>';
$HTML .= '</table><br>';
$pdf->writeHTMLCell(0, 0, '', '', $HTML, 0, 1, 0, true, 'L', false);

//basic info for pemohon
$HTML = '<table style="width:100%;padding:2px" border="0">';
$HTML .= '<tr><td style="width:20%">Nama Pemohon</td><td style="width:2%">:</td><td style="width:70%"><b>'.$indicator_List['nama_pemohon'].'</b></td></tr>';
$HTML .= '<tr><td>No KP</td><td>:</td><td><b>'.$indicator_List['kpbaru_pemohon'].'</b></td></tr>';
$HTML .= '<tr><td>Program</td><td>:</td><td><b>'.$indicator_List['desk_program'].'</b></td></tr>';
$HTML .= '<tr><td>&nbsp;</td></tr>';
$HTML .= '<tr><td>&nbsp;</td></tr>';
$HTML .= '</table>';

//dokumen
$HTML .= '<table border="0" style="width:100%">';
while($result2 = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{		
	$wajib = '';
	if($result2['flag_wajib'] == 1)
	{
		$wajib = ' <b>(WAJIB)</b>';							
	}
	$HTML .= '<tr>';
		$HTML .= '<td style="width:10px"></td>';
		$HTML .= '<td style="width:25px;height:25px;border:1px solid black"></td>';
		$HTML .= '<td style="width:10px"></td>';
		$HTML .= '<td style="width:80%">';
			$HTML .= '<div style="font-size:3pt">&nbsp;</div>'; //hack to make vertical align
			$HTML .= $result2['desk_dokumen'].$wajib;
		$HTML .= '</td>';
	$HTML .= '</tr>';
	$HTML .= '<tr><td>&nbsp;</td></tr>';
	$num += 1;
}
$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
//***********************************************************************************************************************

?>