<!DOCTYPE html>
<html>
<body>
<?php
	require('xl_read.php');
?>
<form method="post" enctype="multipart/form-data">
    Select File to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload" name="submit">
</form>

<?php

$dbhost = 'localhost';
$dbuser = 'safehunter_admindb';
$dbpass = 'dcm-1269';
$dbname = 'safehunter_asfaleies';

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
	$target_dir = "uploads/";
	$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
	$uploadOk = 1;
	$FileType = pathinfo($target_file,PATHINFO_EXTENSION);
  	if (file_exists($target_file)) {
    	echo "Sorry, file already exists.";
    	if(!isset($_POST['use_local'])){
    		echo "<form method='POST'>
    			Do you want to use the local file anyway?<br />
    			<input type='hidden' value='".$target_file."' name='filename'>
    			<input type='submit' value='Yes' name='use_local'>
    			</form>";

    	$uploadOk = 0;
  		}
  	}
  	elseif($FileType != "xls" && $FileType != "xlsx" && $FileType != "XLS" && $FileType != "XLSX") {
    	echo "Sorry, only xls, xlsx files are allowed.";
    	$uploadOk = 0;
  	}
  	else{
    	if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        if(isset($_GET['type']) && $_GET['type']=='motor'){
          $XL = new Motor($target_file);
          $XL->to_db($dbhost,$dbuser,$dbpass,$dbname);
        } elseif(isset($_GET['type']) && $_GET['type']=='bluebyte'){
          $XL = new BlueByte($target_file);
          $XL->to_db($dbhost,$dbuser,$dbpass,$dbname);
        }
        	

    	} else {
        	echo "Sorry, there was an error uploading your file.";
    	}
  	}
}
elseif(isset($_POST['use_local'])){
    if(isset($_GET['type']) && $_GET['type']=='motor'){
      $XL = new Motor($_POST['filename']);
      $XL->to_db($dbhost,$dbuser,$dbpass,$dbname);
    } elseif(isset($_GET['type']) && $_GET['type']=='bluebyte'){
          $XL = new BlueByte($_POST['filename']);
          $XL->to_db($dbhost,$dbuser,$dbpass,$dbname);
    }
}


?>

</body>
</html>