<?php
/*echo '<link rel="stylesheet" href="includes/media/css/demo_page.css" type="text/css" />
<link rel="stylesheet" href="includes/media/css/demo_table.css" type="text/css" />
<link rel="stylesheet" href="includes/media/css/TableTools_JUI.css" type="text/css" />
<script type="text/javascript" language="javascript" src="includes/media/js/jquery.js"></script>
<script type="text/javascript" language="javascript" src="includes/media/js/jquery.dataTables.js"></script>
<script type="text/javascript" language="javascript" src="includes/media/js/ZeroClipboard.js"></script>
<script type="text/javascript" language="javascript" src="includes/media/js/TableTools.js"></script>
';*/
	/*echo '<script type="text/javascript" charset="utf-8">
		$(document).ready(function() {
			oTable1 = $('#example2').dataTable({
					"sScrollY": "450px",
					"bPaginate": false,
					"bJQueryUI": true,
					"sPaginationType": "full_numbers",
					"sDom": '<"H"Tfr>t<"F"ip>',
					"oTableTools": {
							"sSwfPath" : "includes/media/swf/copy_csv_xls_pdf.swf",
							"aButtons": [
									"csv", "pdf"
							]
					}
			});
		} );
	</script>';*/

	//echo PEAR.'/PHPExcel.php';
	
	error_reporting(E_ALL);
	ini_set('display_errors', 0);
	
	$part_id = isset($_REQUEST['part_id']) ? $_REQUEST['part_id'] : "";
    $part_id = isset($_REQUEST['partcpnt_id']) ? $_REQUEST['partcpnt_id'] : "";
	
    $cupn_whr = "";
    $reg_whr = "";
    
    if($part_id != "")
    {
        $cupn_whr = " WHERE coupn_reg_id = '".$part_id."'";
        $reg_whr = " WHERE reg_id = '".$part_id."'";
		
        $cupn_whr = " WHERE coupn_reg_id = '".$part_id."'";
        $reg_whr = " WHERE reg_participant_id = '".$part_id."'";
        $reg_whr = " WHERE lab_smple_part_id = '".$part_id."'";
    }
    
    $qry = "SELECT coupn_count FROM custom_coupon_master $cupn_whr ORDER BY coupn_count DESC";
    $common->sql->query($qry, SQL_ALL, SQL_ASSOC);
    $rec = $common->sql->record;
    $cols = $rec[0]['coupn_count'];
    
    $qry = "SELECT * FROM custom_participant_registration
				INNER JOIN custom_gender_master ON (custom_gender_master.gen_code = custom_participant_registration.reg_sex)
				INNER JOIN custom_survey_master ON (custom_survey_master.sur_code = custom_participant_registration.reg_survey_id)
				INNER JOIN custom_coupon_master ON (custom_coupon_master.coupn_reg_id = custom_participant_registration.reg_participant_id)
			WHERE custom_coupon_master.coupn_issue_date <= '".$common->date_format($prd_to, DT_FMT, '')."'".$reg_whr." GROUP BY reg_participant_id";
	
	$qry = "SELECT * FROM custom_lab_samples
				INNER JOIN custom_lab_master ON (custom_lab_master.lab_id = custom_lab_samples.lab_smple_tst_id)
				INNER JOIN custom_participant_registration ON (custom_participant_registration.reg_participant_id = custom_lab_samples.lab_smple_part_id)
				INNER JOIN custom_survey_master ON (custom_survey_master.sur_code = custom_participant_registration.reg_survey_id)
				INNER JOIN custom_gender_master ON (custom_gender_master.gen_code = custom_participant_registration.reg_sex)
				INNER JOIN custom_lab_result_master ON (custom_lab_result_master.lab_result_code = custom_lab_samples.lab_smple_result_id)".$reg_whr;
    $common->sql->query($qry, SQL_ALL, SQL_ASSOC);
    $rec = $common->sql->record;
	
	if($attr == "excel")
	{
		require_once PEAR.'/PHPExcel.php';
		
		$styleThickBrownBorderOutline = array(
			'font'    => array(
				'bold'      => true
			)
		);
		
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->setActiveSheetIndex(0);
		
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(0, 1, "RDStat Report");
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18);
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel->getActiveSheet()->mergeCells('A1:H1');
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(0, 2, "Protocol");
		$objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(1, 2, $rec[0]['sur_title']);
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(4, 2, "Report Printed On");
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(5, 2, "=today()");
		$objPHPExcel->getActiveSheet()->getStyle('E2')->getFont()->setBold(true);
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(0, 3, "Report As On");
		$objPHPExcel->getActiveSheet()->getStyle('A3')->getFont()->setBold(true);
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(1, 3, $prd_to);
		
		$j = 0;
		$row = 5;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Seed ID");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Type");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Age");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Gender");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Netwrok Size");
		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
		$j++;
		
		for($i=0; $i<$cols; $i++)
		{
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, "Coupon ID ".($i+1));
			$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $row)->applyFromArray($styleThickBrownBorderOutline);
			$j++;
		}
		$hdr_col = $j;
		
		$row++;
		for($i=0; $i<count($rec); $i++)
		{
			$type = $rec[$i]['reg_participant_type'];
			if($type == "S")
				$reg_type = "Seed";
			elseif($type == "W")
				$reg_type = "Recruit";
			
			$qry = "SELECT * FROM custom_coupon_master WHERE coupn_reg_id = '".$rec[$i]['reg_participant_id']."'";
			$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
			$cupn_rec = $common->sql->record;
			
			$j = 0;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $rec[$i]['reg_participant_id']);
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $reg_type);
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $rec[$i]['reg_age']);
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $rec[$i]['gen_name']);
			$j++;
			$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $rec[$i]['reg_network_size']);
			$j++;
			for($k=0; $k<$cols; $k++)
			{
				$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $row, $cupn_rec[$k]['coupn_participant_id']);
				$j++;
			}
			$row++;
		}
		// Rename worksheet
		$objPHPExcel->getActiveSheet()->setTitle('RD STAT Report');
		$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
		//$objPHPExcel->getActiveSheet()->protectCells('A12:ZZ12', 'Excel5');
		//$objPHPExcel->getActiveSheet()->getStyle('B10')->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);

		// Set active sheet index to the first sheet, so Excel opens this as the first sheet
		$objPHPExcel->setActiveSheetIndex(0);


		// Redirect output to a client’s web browser (Excel5)
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="RD STAT Report.xls"');
		header('Cache-Control: max-age=0');

		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output');
	}
	elseif($attr == "print")
	{
		echo '<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; line-height: 40px;" align="center" class="display" id="example2">
			<thead>';
		echo '<tr>
				<td><strong>Test</strong></td>
				<td><strong>Result</strong></td>';
		echo '</tr>
			</thead>
			<tbody>';
		
		for($i=0; $i<count($rec); $i++)
		{
			echo '<tr>
					<td>'.$rec[$i]['lab_test_name'].'</td>
					<td>'.$rec[$i]['lab_result_name'].'</td>';
			echo '</tr>';
		}
		echo '
			</tbody>
			</table>';
	}
	else
	{
		$disp_header = '
			<div style="border: 1px solid #000000; margin: 10px auto; border-radius: 5px 5px 5px 5px;">
			<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; line-height: 10px;">
				<tr>
					<td width="20%"><font size="8"><strong> Patient ID/ VCT No </strong></font></td>
					<td width="2%"><font size="8"><strong> : </strong></font></td>
					<td width="20%"><font size="8"> '.$part_id.'</font></td>
					<td width="10%" align="right"><font size="8"><strong> Age </strong></font></td>
					<td width="2%"><font size="8"><strong> : </strong></font></td>
					<td width="8%"><font size="8"> '.$rec[0]['reg_age'].' Years</font></td>
					<td width="15%" align="right"><font size="8"><strong> Sex </strong></font></td>
					<td width="2%"><font size="8"><strong> : </strong></font></td>
					<td width="15%"><font size="8"> '.$rec[0]['gen_name'].' </font></td>
				</tr>
				<tr>
					<td><font size="8"><strong> Referred By </strong></font></td>
					<td><font size="8"><strong> : </strong></font></td>
					<td><font size="8"> IDU RDSII Study</font></td>
					<td align="right" colspan="4"><font size="8"><strong> Sample Accession Number </strong></font></td>
					<td><font size="8"><strong> : </strong></font></td>
					<td><font size="8"> '.$rec[0]['lab_smple_acc_no'].' </font></td>
				</tr>
				<tr>
					<td><font size="8"><strong> Collection Date </strong></font></td>
					<td><font size="8"><strong> : </strong></font></td>
					<td><font size="8"> '.$common->date_format($rec[0]['lab_smple_date'], DT_FMT, true).' </font></td>
					<td align="right" colspan="4"><font size="8"><strong> Reported On </strong></font></td>
					<td><font size="8"><strong> : </strong></font></td>
					<td><font size="8"> '.$common->date_format($rec[0]['lab_smple_date'], DT_FMT, true).' </font></td>
				</tr>
			</table>
			</div>
			<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; line-height: 10px;">
				<tr>
					<th align="center"><font size="13"><strong>SEROLOGY</strong></font></th>
				</tr>
				<tr>
					<th align="center"><font size="10"><strong><i>HIV Antibody Test: Specimen Plasma</i></strong></font></th>
				</tr>
			</table>
			<br><br>';
		$disp_table = '<table border="1" cellpadding="0" cellspacing="0" style="width: 60%; line-height: 10px; margin-left: 10px;">
			<thead>';
		$disp_table .= '<tr>
				<td><strong> Test</strong></td>
				<td align="center"><strong> Result</strong></td>';
		$disp_table .= '</tr>
			</thead>
			<tbody>';
		
		for($i=0; $i<count($rec); $i++)
		{
			$disp_table .= '<tr>
					<td nowrap> '.$rec[$i]['lab_test_name'].'</td>
					<td> '.$rec[$i]['lab_result_name'].'</td>';
			$disp_table .= '</tr>';
		}
		$disp_table .= '
			</tbody>
			</table>';
		$disp_table_btm = '
			<br/><br/><br/>
			<table border="0" cellpadding="0" cellspacing="0" style="width: 100%;">
				<!-- Commented by Vidhya - 01-11-2017
				<tr>
					<th align="justify"><font size="10">Note: HIV testing algorithm at YRG CARE includes following antibody rapid test kits; 1) Determine HIV-1/2, Alere Medical Co. Ltd., Japan (Immunochromatography) 2) First Response HIV 1-2.0 card test  (Immunochromatography), PMC, India. 3) Signal HIV 1/2, Span Diagnostics (Antibody trapping by immobilization), India. 4) HIV TRI-DOT, Biomed Industries, India.</font></th>
				</tr>
				-->
				<tr>
					<th align="center"><font size="10"><br/><br/><i>--- End of the Report ---</i></font></th>
				</tr>
			</table>
			<br/><br/><br/><br/><br/><br/><br/>
			<table border="0" cellpadding="0" cellspacing="0" style="width: 100%;">
				<tr>
					<th align="left"><font size="10">Lab Technician</font></th>
					<th align="right"><font size="10"><!--Site Lab In-charge--></font></th>
				</tr>
			</table>';
		
		$query = "SELECT * FROM custom_settings";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$stngs_rec = $common->sql->record[0];
		
		$stngs_org_name = $stngs_rec['stngs_org_name'];
		$stngs_org_addrs = $stngs_rec['stngs_org_addrs'];
		$stngs_org_phone = $stngs_rec['stngs_org_phone'];
		$stngs_org_mail = $stngs_rec['stngs_org_mail'];
		$stngs_org_website = $stngs_rec['stngs_org_website'];
		
		$disp_footer = "<div>".$stngs_org_name.",<br/>".$stngs_org_addrs."<br/><b>T : </b>".$stngs_org_phone."<br/><b>E : </b>".$stngs_org_mail."  <b>  Web : </b>".$stngs_org_website."</div>";
		// echo "<div>".$stngs_org_name.",<br/>".$stngs_org_addrs."<br/><b>T : </b>".$stngs_org_phone."<br/><b>E : </b>".$stngs_org_mail."  <b>  Web : </b>".$stngs_org_website."</div>";
		// exit;
		
		// Extend the TCPDF class to create custom Header and Footer
		class MYPDF extends TCPDF {
			
			//Page header
			// public function Header() {
				// Logo
				// $image_file = K_PATH_IMAGES.'logo_example.jpg';
				// $this->Image($image_file, 10, 10, 15, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false);
				// Set font
				// $this->SetFont('helvetica', 'B', 20);
				// Title
				// $this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M');
			// }

			// Page footer
			public function Footer() {
				/*
					// Position at 15 mm from bottom
					$this->SetY(-15);
					// Set font
					$this->SetFont('helvetica', 'I', 8);
					// Page number
					$this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
				*/
				if (empty($this->pagegroups)) {
					$pagenumtxt = $this->l['w_page'].' '.$this->getAliasNumPage().' / '.$this->getAliasNbPages();
				} else {
					$pagenumtxt = $this->l['w_page'].' '.$this->getPageNumGroupAlias().' / '.$this->getPageGroupAlias();
				}
				$disp_footer = "<font size=\"3\"><br/></font><strong>YR Gaitonde Centre for AIDS Research and Education</strong><br><font size=\"6\">VHS Campus, Rajiv Gandhi Road, Taramani, Chennai 600113 India.<br><b>T : </b>+91 44 39106600<br><b>E : </b>info@yrgcare.org  <b>  Web : </b>http://www.yrgcare.org</font>";
				$this->writeHTMLCell(0, 5, '', '', $this->getAliasRightShift().$pagenumtxt, 0, $ln=1, $fill=0, true, 'R', true);
				// $this->writeHTMLCell(0, 5, '', '', $this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, $ln=1, $fill=0, true, 'R', true);
				$this->writeHTMLCell(0, 0, $x='', $y='', $disp_footer, 'T', $ln=1, $fill=0, $reseth=true, 'C', $autopadding=true);
			}
		}
		
		$pdf = '';
		$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
		
		// set document information
		$pdf->SetCreator(PDF_CREATOR);
		$pdf->SetAuthor('YRG Care');
		$pdf->SetTitle('Lab Test Report');
		$pdf->SetSubject('TCPDF Tutorial');
		$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
		
		// set default header data
		$pdf->SetHeaderData(PDF_HEADER_LOGO, '20', $stngs_org_name, "<font size=\"6\">".$stngs_org_addrs."<br/><b>T : </b>".$stngs_org_phone."<br/><b>E : </b>".$stngs_org_mail."  <b>  Web : </b>".$stngs_org_website."</font>");
		// $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING);
		
		// set header and footer fonts
		/*$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', 20, 'L', true));
		$pdf->setHeaderStringFont(Array(PDF_FONT_NAME_MAIN, '', 10, 'L', true));
		$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));*/
		
		$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', 9, '', true));
		// $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
		$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', 9, '', true));
		
		// set default monospaced font
		$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
		
		//set margins
		$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
		$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
		// $pdf->SetHeaderMargin(PDF_MARGIN_FOOTER);
		$pdf->SetFooterMargin("20");
		
		//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);
		
		// ---------------------------------------------------------
		
		// set font
		$pdf->SetFont('dejavusans', '', 8);
		
		// add a page
		$pdf->AddPage('P');
		
		$dt_fmt = str_replace("mm", "m", DT_FMT);
		$dt_fmt = str_replace("dd", "d", $dt_fmt);
		$dt_fmt = str_replace("yyyy", "Y", $dt_fmt);
		$dt = date($dt_fmt);
		
		$title_rght = "<br/><br/><font size=\"6\"><b>Printed from :</b> ".APP_NAME."<br/>
		<b>Printed by : </b>".USRNME."<br/>
		<b>Report Printed on : </b>".$dt."</font>";
		
		// Print text using writeHTMLCell()
		$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $disp_header, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
		$pdf->writeHTMLCell($w=0, $h=0, $x=65, $y='', $disp_table, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
		$pdf->writeHTML($disp_table_btm, true, false, true, false, '');
		$pdf->MultiCell(0, 10, $title_rght, 0, 'R', 0, 1, '', 5, false, 0, true, true, 0, 'T', false);
		$pdf->lastPage();
		$pdf->Output("Lab Result ".$part_id.".pdf", "D");
	}
?>