<?php
include 'connect/cms-config.php' ;
include 'requires/function.php' ;

$staff_idno   = escapeString($_GET['staff_idno']) ;

$mysqli_staff = $mysqli->query("SELECT * FROM staff
			 				    WHERE deleted_at IS NULL AND ( staff_date_resigned IS NULL || staff_date_resigned = '0000-00-00' || staff_date_resigned >= '".TODAYDATE."' ) AND staff_idno = '".$staff_idno."'  LIMIT 1") ;
if ( $mysqli_staff->num_rows > 0 ){
	header('Content-Type: text/x-vcard') ;
	header('Content-Disposition: inline; filename= "'.$staff_idno.'.vcf"');    

	// get staff details
	$row_staff    		    = $mysqli_staff->fetch_assoc() ;

	$staff_settings         = $row_staff['staff_settings'] ;
	if ( $staff_settings   != '' ){
		$staff_settings     = JsonEncodeDecode('decode', $staff_settings) ;
	}else{
		$staff_settings     = [] ;
	}

	// get all gender
	$gender                 = [] ;
	$get_gender             = $mysqli->query("SELECT * FROM master_gender
								    		WHERE deleted_at IS NULL") ;
	if ( $get_gender->num_rows > 0 ){
		while ( $row_gender = $get_gender->fetch_assoc() ){
			$gender[$row_gender['gender_id']] = $row_gender['gender_desc'] ;
		}
	}

	// get all department
	$department             = [] ;
	$get_department         = $mysqli->query("SELECT b.department_desc FROM staff_department a
									          LEFT JOIN setting_department_translation b ON ( a.department_id = b.department_id )
							                  WHERE a.deleted_at IS NULL AND b.lang = 'en' AND a.staff_id = '".$row_staff['staff_id']."'") ;

	if ( $get_department->num_rows > 0 ){
		while ( $row_department = $get_department->fetch_assoc() ){
			$department[] = $row_department['department_desc'] ;
		}
	}

	// get all position
	$position 	          = [] ;
	$get_position         = $mysqli->query("SELECT a.job_position_id, b.job_position_desc FROM setting_job_position a
					 						LEFT JOIN setting_job_position_translation b ON ( a.job_position_id = b.job_position_id )
								    		WHERE a.deleted_at IS NULL AND b.lang = 'en'") ;
	if ( $get_position->num_rows > 0 ){
		while ( $row_position = $get_position->fetch_assoc() ){
			$position[$row_position['job_position_id']] = $row_position['job_position_desc'] ;
		}
	}

	// get all section
	// $section 	  = [] ;
	// $get_section  = $mysqli->query("SELECT * FROM setting_job_section
	// 							    WHERE deleted_at IS NULL") ;
	// if ( $get_section->num_rows > 0 ){
	// 	while ( $row_section = $get_section->fetch_assoc() ){
	// 		$section[$row_section['job_section_id']] = $row_section['job_section_desc'] ;
	// 	}
	// }

	$get_department       = implode( ', ', $department ) ;
	$get_position         = $position[$row_staff['job_position_id']] ;
	$get_section          = $section[$row_staff['job_section_id']] ;
	$get_title            = $get_position . ( $get_section != '' && $get_section != 'NONE' ? ' ( '.$get_section.' )' : '' ) ;
	

	switch ( $staff_settings['vcard_mode'] ){
		case '1' :
			$get_title    = $get_position ;
		break ;
		case '2' :
			$get_title    = $get_department ;
		break ;
		default :
			$get_title    = $get_department . ' ' . $get_position ;
	}

	$data_joined          = ( $row_staff['staff_date_joined'] != '' && $row_staff['staff_date_joined'] != '0000-00-00' ? date( 'Ymd', strtotime($row_staff['staff_date_joined']) ) . "T195243Z" : '' ) ;

	$vcard 				  = "" ;
	$vcard               .= "BEGIN:VCARD" . "\r\n" ;
	$vcard               .= "VERSION:3.0" . "\r\n" ;
	$vcard               .= "N:".$row_staff['staff_shortname'] . "\r\n" ;
	$vcard               .= "ORG:".strtoupper( COMPANY ) . "\r\n" ;
	$vcard               .= "TITLE:".$get_title . "\r\n" ;
	$vcard               .= "GENDER:".substr( $gender[$row_staff['gender_id']], 0, 1 ) . "\r\n" ;
	$vcard               .= "TEL;TYPE=WORK,VOICE:+".str_replace( '+', '', $row_staff['staff_mobileno'] ) . "\r\n" ;
	$vcard               .= "TEL;TYPE=WORK,MSG:+".str_replace( '+', '', $row_staff['staff_mobileno'] ) . "\r\n" ;
	$vcard               .= "URL;TYPE=WORK:".WEBSITE . "\r\n" ;
	$vcard               .= "EMAIL;TYPE=INTERNET:".$row_staff['staff_email'] . "\r\n" ;
	$vcard               .= "REV:" . $data_joined . "\r\n" ;
	$vcard               .= "END:VCARD" ;

	echo $vcard ;
	exit ;

}else{
	header( "Location: hr-staff.php" ) ;
	exit ;
}

?>