<?php
	include ('includes/bootstrap.inc.php');
	
	if(empty(Data::getSession('uid'))){
		header('Location: login.php?op=e');
		die();
	}
	
	$vid= Data::getGet ( 'vid' );

	$visit = new Visit( $vid );
	if( $visit->exists( $vid ) == false ){
		$msg = new Message( _("Cannot find visit to print" ) );
		header('Location: visit.history.php');
		die();
	}
	
	
	$pet = new Pet( $visit->pid );
	// d($visit);
	// d($pet);

	$pdf = new PDF();
	$pdf->SetMargins( 0, 0 );

	$pdf->setLogo( _URL_IMAGES . '/' . _APPLICATION_LOGO );
	$pdf->setHeading1( _CLINIC_NAME );
	$pdf->heading_font_size = 15;
	$pdf->setHeading2( _CLINIC_ADDRESS );
	$pdf->setHeading3( _( "ph: " ) . _CLINIC_PHONE . " - " . _( "email: ") . _CLINIC_EMAIL );


	$pdf->setFooter( _CLINIC_NAME . " - " . _CLINIC_ADDRESS . " - " . _( "ph: " ) . _CLINIC_PHONE . " - " . _( "email: ") . _CLINIC_EMAIL );

	$pdf->AddPage();



	// $pdf->SetFont('Arial','B',12);
	$pdf->Cell( 40, 10, _( 'Visit nr. ' ) . $visit->vid, 1 );
	$pdf->Cell( 100, 10, _( 'About: ' ) . $visit->teaser, 1 );
	$pdf->Cell( 170, 10, _( 'Status: ' ) . $visit->status, 1 );
	$pdf->Ln();
	$pdf->Cell( 105, 10, _( 'Reason: ' ) . $visit->reason, 1 );
	$pdf->Cell( 105, 10, _( 'Diagnosis: ' ) . $visit->diagnosis, 1 );
	$pdf->Ln();
	$pdf->Cell( 105, 10, _( 'Created: ' ) . $visit->created_date, 0 );
	$pdf->Cell( 105, 10, _( 'Modified: ' ) . $visit->modified_date, 0 );
	$pdf->Ln();
	$pdf->Cell( 105, 10, "", 0 );

	// create a cell to show pet detail
	$pet_detail = _("Pet detail") . "\n" .
					_( "Name: " ) . $pet->name . " " . _( "Gender: " ) . $pet->gender . "\n" .
					_( "Description: " ) . $pet->description . "\n" .
					_( "Date of Birth: " ) . $pet->date_of_birth_date . "\n";

	if( empty( $pet->date_of_green_meadows_timestamp ) == false ){
		$pet_detail .= _( "Date of green meadows: " ) . $pet->date_of_green_meadows_date . "\n";
	}
	if( empty( $pet->microchip ) == false ){
		$pet_detail .= _( "Microchip: " ) . $pet->microchip;
	}
	if( empty( $pet->tatuatge ) == false ){
		$pet_detail .= " " . _( "Tatuatge: " ) . $pet->tatuatge;
	}

	$pdf->MultiCell( 105, 5, $pet_detail, 'B' );
	$pdf->Ln();

	$pdf->SetFontSize( 8 );
	if( is_array( $visit->procedures ) == true && empty( $visit->procedures ) == false ){
		$pdf->Cell( 105, 5, _( "Adopted procedures" ), 0 );
		$pdf->Ln();
		$pdf->setFontStyle( '' );

		foreach ( $visit->procedures as $procedure ){
			$pdf->Cell( 105, 8, $procedure['term_name'], 0 );
			$pdf->Ln();
		}
	}
	$pdf->Ln();

	if( is_array( $visit->notes ) == true && empty( $visit->notes ) == false ){
		$pdf->setFontStyle( 'B' );
		$pdf->Cell( 105, 5, _( "NOTES:" ), 0 );
		$pdf->Ln();
		$pdf->setFontStyle( '' );

		foreach ( $visit->notes as $note ){
			$pdf->setFontStyle( 'B' );
			$pdf->Cell( 210, 5, $note['title'] . " - " . Data::UnixTimestampToDate( $note['created'], _DEFAULT_DATE_FULL_FORMAT ), 0 );
			$pdf->setFontStyle( '' );
			$pdf->Ln();
			$pdf->Cell( 210, 5, $note['text'], 0 );
			$pdf->Ln();
		}
	}
	$pdf->Ln();

	if( is_array( $visit->drugs ) == true && empty( $visit->drugs ) == false ){
		$pdf->setFontStyle( 'B' );
		$pdf->Cell( 105, 5, _( "Drugs (prescriptions)" ), 0 );
		$pdf->Ln();
		$pdf->setFontStyle( '' );

		foreach ( $visit->drugs as $drug ){
			$pdf->Cell( 185, 5, $drug['name'], 0 );
			$pdf->Cell( 25, 5, $drug['qty'], 0 );
			$pdf->Ln();
			$pdf->Cell( 210, 5, $drug['company'], 0 );
			$pdf->Ln();
		}
	}
	$pdf->Ln();


	if( is_array( $visit->diagnostic_tests ) == true && empty( $visit->diagnostic_tests ) == false ){
		$pdf->setFontStyle( 'B' );
		$pdf->Cell( 105, 5, _( "Diagnostic Test" ), 0 );
		$pdf->Ln();
		$pdf->setFontStyle( '' );

		foreach ( $visit->diagnostic_tests as $diagnostic_test ){
			$pdf->Cell( 185, 5, $diagnostic_test['term_name'], 0 );
			$pdf->Cell( 25, 5, $diagnostic_test['created'], 0 );
			$pdf->Ln();
		}
	}
	$pdf->Ln();


	$pdf->Output();