<?php
	//include('./models/lm/.lm.header.php');
	$fileName = 'xxxx';
	$fileSize = 0;
	$fileUpload = 0;

	if (isset($_POST["import"])) {
		
		$fileName = $_FILES["file"]["tmp_name"];
		$fileSize = $_FILES["file"]["size"];
		$fileUpload = 1;
	}

	/* if (isset($_POST["upload"])) {
		
		$rows = $_SESSION['UPLOAD'];
		/* foreach ($rows as $row) {
			echo '<tr>';
			echo '	<td>'.$row['EmployeeNo'].'</td>';
			echo '	<td>'.$row['PrincipalCode'].'</td>';
			echo '	<td>'.$row['StoreLocationCode'].'</td>';
			echo '	<td>'.$row['WorkingDays'].'</td>';
			echo '	<td>'.$row['StartTime'].'</td>';
			echo '	<td>'.$row['EndTime'].'</td>';		
			echo '	<td>'.$row['isActive'].'</td>';
			echo '</tr>';		
		} */
		//echo '<span id="success">Data has been successfully uploaded.</span><br><br><a href="./csv_to_array.php" class="btn-submit">Upload another record</a>';
		//echo '<span id="success"></span>';
		//unset($_SESSION['UPLOAD']);
	//} */

	function csv_to_array($filename='', $delimiter=',')
	{
		if(!file_exists($filename) || !is_readable($filename))
			return FALSE;
		
		$header = NULL;
		$data = array();
		if (($handle = fopen($filename, 'r')) !== FALSE)
		{
			while (($row = fgetcsv($handle, 5000, $delimiter)) !== FALSE)
			{
				if(!$header)
					$header = $row;
				else
					$data[] = array_combine($header, $row);
			}
			fclose($handle);
		}
		return $data;
	}
	
?>

<script type="text/javascript">
	$(document).ready(function() {
		$("#frmCSVImport").on("submit", function () {

			$("#response").attr("class", "");
			$("#response").html("");
			var fileType = ".csv";
			var regex = new RegExp("([a-zA-Z0-9\s_\\.\-:])+(" + fileType + ")$");
			if (!regex.test($("#file").val().toLowerCase())) {
					$("#response").addClass("error");
					$("#response").addClass("display-block");
				$("#response").html("<span style='color:red;'>Invalid File format. Upload : <b>" + fileType + "</b> File only.</span>");
				$(":loading").loading("stop");
				return false;
			}
			return true;
		});
		
		//$("#frmUpload").on("submit", function () {
		//	$("#response").attr("class", "");
		//	$("#response").html("asaaasass");
		//});
		
		if( $('#err').length )         // use this if you are using id to check
		{
			$('#upload').hide();// it exists
		}
		if( $('#uploadStatus').length )         // use this if you are using id to check
		{
			$('#up-header').hide();// it exists
			$('#up-section').hide();
			//$("#header-section").html('<h1 class="title is-5">Data has been successfully uploaded.</h1><a href="./3" class="button is-link is-small"><b>Upload another record</b></a>');
		}
		
	});
</script>

<section class="section-">
	<div class="container">
		
		<h1 id="up-header" class="title is-5">
			How to upload schedule.
		</h1>
			
		<?=$uploadStatus;?>
		<div id="up-section">
			<?php
			if(!$fileUpload){	
			?>
				<p class="subtitle is-6" style="line-height: 1.8;">
					Download the template here. <a href="#">(Schedule Template)</a><br>
					<br>
					<u><b>About the columns:</b></u>
					<br>&nbsp;  - <b>EmployeeNo</b> - Provide a valid employee number. 
					<br>&nbsp;  - <b>EmployeeName</b> - Provide the employee name in the following format: Last Name, First Name. 
					<br>&nbsp;  - <b>PrincipalCode</b> - Provide only the principal code. 
					<br>&nbsp;  - <b>StoreLocationCode</b> - Provide the assigned Store Location Code. List of Store Location Code can be found here.
					<br>&nbsp;  - <b>WorkingDays</b> - Only one of the following working days are valid per entry: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday.
					<br>&nbsp;  - <b>StartTime</b> - Start time format must be in 24hours. Example: for 1:00pm equals to 13:00. <a href="http://www.lsoft.com/manuals/maestro/4.0/htmlhelp/interface%20user/TimeConversionTable.html" target="_blank"><u>To learn more about 24 Hour Clock Time Conversion Table (Click here)</u></a>
					<br>&nbsp;  - <b>EndTime</b> - End time format must be in 24hours. Example: for 1:00pm equals to 13:00.
					<br>&nbsp;  - <b>isActive</b> - Valid input: <b>1</b> or <b>0</b>. Input 1 for active schedule, 0 for inactive schedule.
				</p>
				<br>
				
				<p class="subtitle is-6">
					<u><b>Select the file to upload:</b></u>
				</p>
				<form class="form-horizontal" action="" method="post" name="frmCSVImport" id="frmCSVImport" enctype="multipart/form-data">
					<div class="--input-row">
						<label class="col-md-4 control-label">Choose CSV
							File</label> <input type="file" name="file"
							id="file" accept=".csv">
						<button type="submit" id="submit" name="import"
							class="button is-link is-small">View Import</button>
						<br />
					</div>
				</form>
			<?php
			}else{		
			?>
				<p class="subtitle is-6" style="line-height: 1.8;">
					<u><b>Review the records before you upload:</b></u>
				</p>
				<form class="form-horizontal" action="" method="post" id="frmUpload">
					<div class="input-row">
						<button type="submit" class="button is-link is-small" onClick="window.location.reload();">Back</button>
						<button type="submit" id="upload" name="upload" class="button is-success is-small" onclick="return confirm('Upload this record?')"><b>Upload Record</b></button>
						<br />
					</div>
				</form>
			<?php
			}
			?>
		</div>
		
		<div id="response"
			class="<?php if(!empty($type)) { echo $type . " display-block"; } ?>">
			<?php if(!empty($message)) { echo $message; } ?>
		</div>
		<div id="up-section3">
			<?php
				if($fileSize != 0){
				?>
				<table class="table is-striped is-narrow">
					<thead>
						<tr>
						<th width="100">#</th>
						<th width="300">Employee No.</th>
						<th width="300">Employee Name</th>
						<th width="300">Principal Code</th>
						<th width="300">Store Loc. Code</th>
						<th width="300">Working Days</th>
						<th width="300">Start Time</th>
						<th width="300">End Time</th>
						<th>Active</th>
						</tr>
					</thead>
			<?php					
					//$rows = csv_to_array('example.csv');
					
					$rows = csv_to_array($fileName);
					$_SESSION['UPLOAD'] = $rows;
					$error = 0;
					$ctr = 1;
					foreach ($rows as $row) {
						if(!isset($row['EmployeeNo'])){ $error = 1; }
						if(!isset($row['EmployeeName'])){ $error = 1; }
						if(!isset($row['PrincipalCode'])){ $error = 1; }
						if(!isset($row['StoreLocationCode'])){ $error = 1; }
						if(!isset($row['WorkingDays'])){ $error = 1; }
						if(!isset($row['StartTime'])){ $error = 1; }
						if(!isset($row['EndTime'])){ $error = 1; }
						if(!isset($row['isActive'])){ $error = 1; }
						
						if($error){
							echo '<br><span id="err" style="color:red;">Table columns does not match, please use the provided template.<br><br></span>';
							break;
						}else{
							echo '<tr>';
							echo '	<td>'.$ctr.'</td>';
							echo '	<td>'.$row['EmployeeNo'].'</td>';
							echo '	<td>'.$row['EmployeeName'].'</td>';
							echo '	<td>'.$row['PrincipalCode'].'</td>';
							echo '	<td>'.$row['StoreLocationCode'].'</td>';
							echo '	<td>'.$row['WorkingDays'].'</td>';
							echo '	<td>'.$row['StartTime'].'</td>';
							echo '	<td>'.$row['EndTime'].'</td>';		
							echo '	<td>'.$row['isActive'].'</td>';
							echo '</tr>';
							$ctr++;
						}								
					}					
				}
			?>
				</table>
		</div>
	</div>
</section>



