<?php

// default config setting
$boolean_ssl_lock      = true ;

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

$current_times         = date( "H:i:s", time() ) ;
$current_hours         = date( "H", time() ) ;
$current_minutes       = date( "m", time() ) ;

// $yesterday_day 		   = ( $_GET['from'] != '' ? $_GET['from'] : date( "Y-m-d", strtotime( "-1 days" ) ) ) ;
$today_day 			   = ( $_GET['from'] != '' ? $_GET['from'] : date( "Y-m-d", time() ) ) ;
$next_day 			   = ( $_GET['to'] != '' ? $_GET['to'] : date( "Y-m-d", time() ) ) ;

$search_query          = "" ;
$today_datetime 	   = 'manual-export' ;

$search_query         .= " AND a.created_at BETWEEN '" . $today_day . " 00:00:00' AND '" . $next_day . " 23:59:59'" ;


$mysqli_attendances   = $mysqli->query( "SELECT a.created_at, b.staff_idno FROM staff_attendance a
				 				         LEFT JOIN staff b ON ( a.staff_id = b.staff_id )
				 				         WHERE a.deleted_at IS NULL AND CHAR_LENGTH(b.staff_idno) > 6 " . $search_query . "
				 				         ORDER BY a.created_at" ) ;


$filename 			  = '../txt/' . $today_datetime . '.txt' ;
$file 			      = fopen( $filename, "wb" ) ;
fwrite($fp,$content);

if ( $mysqli_attendances->num_rows > 0 ){
	$count 			  = 0 ;
	$total 			  = $mysqli_attendances->num_rows ;

	while ( $row_attendances = $mysqli_attendances->fetch_assoc() ){
		$count++ ;

		$content      = $row_attendances['staff_idno'] . '|' . date( 'Y-m-d', strtotime($row_attendances['created_at']) ) . '|' . date( 'H:i:s', strtotime($row_attendances['created_at']) ) ;

		if ( $count  != $total ){
			$content .= "\n" ;
		}

		fwrite( $file, $content ) ;

	}
}

fclose( $file ) ;




// header("Content-Description: File Transfer"); 
// header("Content-Type: application/octet-stream"); 
// header("Content-Disposition: attachment; filename=\"". basename($filename) ."\"") ; 
// readfile ( $filename ) ;

echo '<a href="'.$filename.'" download>Download</a>' ;

exit ;

?>