<?php

include_once ("includes/setup.inc.php");

$script_base = 'pht_listjanji';
$myID = fetch_postget_val('appointment',$_POST, $_GET);

$indicator_Query = "SELECT nama_vatklinik, nama_pemilik, kpbaru_pemilik, nama_pet, desk_haiwan_peliharaan, appointment_date, appointment_time, remarks, check_result, m_vatklinik_appointment.rawatan_fee, total_resit, tkh_resit, resit_number
				  FROM m_vatklinik_appointment
				  LEFT JOIN i_vatklinik ON i_vatklinik.id_vatklinik = m_vatklinik_appointment.id_vatklinik
				  LEFT JOIN m_pemilik ON m_pemilik.id_pemilik = m_vatklinik_appointment.id_pemilik
				  LEFT JOIN m_pemilik_pet ON m_pemilik_pet.id_pemilik_pet = m_vatklinik_appointment.id_pemilik_pet
				  LEFT JOIN i_haiwan_peliharaan ON m_pemilik_pet.id_haiwan_peliharaan = i_haiwan_peliharaan.id_haiwan_peliharaan
				  WHERE id_vatklinik_appointment = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $myID, PDO::PARAM_INT);
$PDO_stmt->execute();
$appointment_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

$indicator_Query = "SELECT desk_ubat, quantity, ubat_remarks, price_ubat
				  FROM m_appointment_ubat
				  LEFT JOIN i_ubat ON m_appointment_ubat.id_ubat = i_ubat.id_ubat
				  WHERE id_vatklinik_appointment = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $myID, PDO::PARAM_INT);
$PDO_stmt->execute();
$ubat_List = [];
while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	$ubat_List[] = $result;
}

$myTitle = 'Resit';
$mySubHeaderTitle = $appointment_List['nama_vatklinik'];
$TITLE = $myTitle;
$AUTHOR = 'vat_system';
$manual_TOP = 20;
$manualY = 25;

//***********************************************************************************************************************
// 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;
		}
		private $subHeaderTitle = '';
		public function setSubHeaderTitle($title)
		{
			$this->subHeaderTitle = $title;
		}
		//Page header
		public function Header() {
			
			$this->SetFont('helveticaB', '', 14); //CHANGE THIS FOR TITLE FONT
			$this->MultiCell(0, 0, $this->pageTitle, 0, 'C', 0, 1, '', '', true);
			if($this->subHeaderTitle != '')
			{
				$this->SetFont('helvetica', '', 10); //CHANGE THIS FOR TITLE FONT
				$this->MultiCell(0, 0, $this->subHeaderTitle, 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);
			
			$HTML = '<table border="0" style="padding: 3px;">';
			$HTML .= '<tr>';
				$HTML .= '<td style="width:50%">Cetakan : '.dateTimeNow().'</td>';
				$HTML .= '<td style="width:50%;text-align:right">Muka Surat '.$this->getAliasNumPage().'/'.$this->getAliasNbPages().'</td>';
			$HTML .= '</tr>';
			$HTML .= '</table>';
			$this->SetFont('helvetica', '', 10); //CHANGE THIS FOR HEADER TITLE FONT
			$this->writeHTMLCell(0, 0, '', '', $HTML, 0, 0, 0, true, '', false);
			
			//$this->MultiCell(0, 0, 'Dicetak pada '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, 'L', 0, 1, '', '', true);
			//$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
	$pdf->setHeaderTitle($myTitle);
	$pdf->setSubHeaderTitle($mySubHeaderTitle);
	//$pdf->setHeaderTable($headerTitle);
	
	// set document information
	$pdf->SetCreator(PDF_CREATOR);
	$pdf->SetAuthor($AUTHOR);
	$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, $manual_TOP, 5);
	$pdf->SetHeaderMargin(10);
	$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

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

	//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 = '';
	
	$HTML .= '<table style="padding:2px">';
		$HTML .= '<tr>';
			$HTML .= '<td style="width:20%">Nama</td>';
			$HTML .= '<td style="width:2%">:</td>';
			$HTML .= '<td style="width:36%">'.$appointment_List['nama_pemilik'].'</td>';
			$HTML .= '<td style="width:4%"></td>';
			$HTML .= '<td style="width:10%">No Resit</td>';
			$HTML .= '<td style="width:2%">:</td>';
			$HTML .= '<td style="width:26%">'.$appointment_List['resit_number'].'</td>';
		$HTML .= '</tr>';
		$HTML .= '<tr>';
			$HTML .= '<td>Haiwan Peliharaan</td>';
			$HTML .= '<td>:</td>';
			$HTML .= '<td>'.$appointment_List['nama_pet'].'</td>';
			$HTML .= '<td></td>';
			$HTML .= '<td>Tarikh</td>';
			$HTML .= '<td>:</td>';
			$HTML .= '<td>'.$appointment_List['tkh_resit'].'</td>';
		$HTML .= '</tr>';
		$HTML .= '<tr><td>&nbsp;</td></tr>';
	$HTML .= '</table>';
	$HTML .= '<table class="table table-sm thead-dark table-hover" style="padding:2px">';
		$HTML .= '<tr style="background-color:lightgray">';
			$HTML .= '<th style="width:10%;text-align:center">#</th>';
			$HTML .= '<th style="width:30%">Nama Ubat</th>';
			$HTML .= '<th style="width:30%">Remarks</th>';
			$HTML .= '<th style="width:10%;text-align:center">Quantity</th>';
			$HTML .= '<th style="width:10%;text-align:right">Price</th>';
			$HTML .= '<th style="width:10%;text-align:right">Total</th>';
		$HTML .= '</tr>';
			if(count($ubat_List) > 0)
			{
				for($num = 0; $num < count($ubat_List); $num++)
				{
					$HTML .= '<tr>';
						$HTML .= '<td style="text-align:center">'.($num+1).'</td>';
						$HTML .= '<td>'.$ubat_List[$num]['desk_ubat'].'</td>';
						$HTML .= '<td>'.$ubat_List[$num]['ubat_remarks'].'</td>';
						$HTML .= '<td style="text-align:center">'.$ubat_List[$num]['quantity'].'</td>';
						$HTML .= '<td style="text-align:right">'.number_format($ubat_List[$num]['price_ubat'], 2).'</td>';
						$HTML .= '<td style="text-align:right">'.number_format($ubat_List[$num]['quantity'] * $ubat_List[$num]['price_ubat'], 2).'</td>';
					$HTML .= '</tr>';
				}
			}
			else
			{
				$HTML .= '<tr>';
					$HTML .= '<td colspan=4 style="text-align:center">Tiada Ubat Diperlukan</td>';
				$HTML .= '</tr>';
			}
		$HTML .= '<tr>';
			$HTML .= '<td></td>';
			$HTML .= '<td>Charge Rawatan</td>';
			$HTML .= '<td></td>';
			$HTML .= '<td></td>';
			$HTML .= '<td></td>';
			$HTML .= '<td style="text-align:right">'.number_format($appointment_List['rawatan_fee'], 2).'</td>';
		$HTML .= '</tr>';
		$HTML .= '<tr>';
			$HTML .= '<td colspan="5" style="text-align:right;border-top:1px solid black">Jumlah</td>';
			$HTML .= '<td style="text-align:right;border-top:1px solid black">'.number_format($appointment_List['total_resit'], 2).'</td>';
		$HTML .= '</tr>';
	$HTML .= '</table>';
	/*
	$counter = 1; //FOR BIL
	$headerCounter = 0;
	$HTML = '<table border="1" style="text-align:center;padding: 6px;">';
	for( $num = 0 ; $num < count($dataArray) ; $num++ )
	{
		$HTML .= '<tr>';
		if($num == 0)
		{
			$HTML .= '<td style="width:'.$headerTitle[$headerCounter++][1].'%">'.($counter++).'</td>';
		}
		else
		{
			$HTML .= '<td>'.($counter++).'</td>';
		}
		
		if(preg_match('/\_pemohon$/', $_POST['baseName']) || preg_match('/se\_meninggal/', $_POST['baseName']) || preg_match('/general\-maklumat$/', $_POST['baseName']))
		{
			for($colNum = 0 ; $colNum < count($columns) ; $colNum++)
			{
				if(isset($columns[$colNum]['skip_print']))
				{
					//do nothing
				}
				else if (isset($columns[$colNum]['print'])) //filter based on columns array
				{
					if($num == 0 && $colNum != 0)
					{
						$HTML .= '<td style="width:'.$headerTitle[$headerCounter++][1].'%">'.$dataArray[$num][$colNum].'</td>';
					}
					else
					{
						$HTML .= '<td>'.$dataArray[$num][$colNum].'</td>';
					}
				}
			}
		}
		else
		{
			for($num2 = 0 ; $num2 < count($headerTitle) ; $num2++)
			{
				if (isset($columns[$num2]['table']) || isset($columns[$num2]['print'])) //filter based on columns array
				{
					if($num == 0 && $num2 != 0)
					{
						$HTML .= '<td style="width:'.$headerTitle[$headerCounter++][1].'%">'.$dataArray[$num][$num2].'</td>';
					}
					else
					{
						$HTML .= '<td>'.$dataArray[$num][$num2].'</td>';
					}
				}
			}
		}
		$HTML .= '</tr>';
	}
	$HTML .= '</table>';
	*/
	
	// writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=0, $reseth=true, $align='', $autopadding=true)
	if($manualY)
	{
		$pdf->writeHTMLCell(0, 0, '', $manualY, $HTML, 0, 0, 0, true, '', false);
	}
	else
	{
		$pdf->writeHTMLCell(0, 0, '', '', $HTML, 0, 0, 0, true, '', false);
	}

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

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

?>