<?php 
include("../connect.php");
global $con;
include("../session.php");

$for = $_REQUEST['a'];
$id = isset($_REQUEST['id'])?$_REQUEST['id']:"";
$pricelist = null;
if ($for == "pricelist" || $for == "pricingplanlistcustomer" || $for == "pricingplanlistparts") {
	$pricelist = new PriceList($id);
}if ($for == "alta") {
	$alta = new Alta($id);
}

 ?>
 
 <!DOCTYPE html>
 <html lang="en">
   <head>
     <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <!--<meta name="viewport" content="width=device-width, initial-scale=1">-->
     <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
     <title><?= $config['CompanyName'] ?></title>
 
     <!-- Bootstrap -->
     <link href="../css/bootstrap.min.css" rel="stylesheet">
     <link rel="stylesheet" href="../css/bootstrap-combobox.css" />
 
     <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
     <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
     <!--[if lt IE 9]>
       <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
       <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
       <script src="../js/functions.js"></script>
       <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
           <!-- Include all compiled plugins (below), or include individual files as needed -->
           <script src="../js/bootstrap.min.js"></script>
           <script src="../js/fileinput.js"></script>
       	 <script src="../js/bootstrap-combobox.js"></script>
           <script type="text/javascript">
             $(document).ready(function(){
               
       //        $('.grpline').css("width", "100px");
             });
           </script>

     <![endif]-->
	<link rel="stylesheet" href="../css/styles.css"/>
	<link rel="stylesheet" href="../css/fileinput.min.css"/>
   </head>
   <body>
<?php 
	include("navbar.php");
	$page['inventory'] = array(
								"page_title" => "Inventory CSV Import",
								"page_body" => "In order to import the inventory data the CSV data must be in the following order: <br />
								<pre><samp>| GROUP | LINE | SKU | DESCRIPTION | NOTES | INTERCHANGE | QTY | PRICE(DEFAULT) | CORE | PRICE F | PRICE C | PRICE G |</samp></pre>");
	$page['customers'] = array(
								"page_title" => "Customers CSV Import",
								"page_body" => "In order to import the customers data the CSV data must be in the following order: <br />
								<pre><samp>| Name | City | AccountNumber | Email | Salesman | Phone |</samp></pre>");
	$page['salesmen'] = array(
								"page_title" => "Salesmen CSV Import",
								"page_body" => "In order to import the salesman data the CSV data must be in the following order: <br />
								<pre><samp>| ID | FullName | Username | Email |</samp></pre>");
	$page['pricelist'] = array(
								"page_title" => "Pricing Plan List CSV Import<br>
								Pricing Plan For: " . $pricelist->Name . "<br>
								Pricing Plan ID: <strong>$id</strong><br>",
								"page_body" => "In order to import the pricing plan list you must add the Pricing Plan ID column to the CSV file using Excel.<br /> The Pricing Plan ID is \"$id\" and the CSV data must be in the following order: <br />
								<pre><samp>| PricingPlanID | Group | Line | SKU | Price | UPC |</samp></pre>");
	$page['alta'] = array(
								"page_title" => "Alta CSV Import<br>
								Alta: " . $alta->Name . "<br>
								Alta ID: <strong>$id</strong><br>",
								"page_body" => "In order to import the alta you must add the Alta ID column to the CSV file using Excel.<br /> The Alta ID is \"$id\" and the CSV data must be in the following order: <br />
								<pre><samp>| AltaID | Group | Line | SKU |</samp></pre>");
	$page['invoice'] = array(
								"page_title" => "Aging CSV Import",
								"page_body" => "In order to import the aging data, the CSV data must be in the following order: <br />
								<pre><samp>| ST | InvoiceDate | CustomerNumber | InvoiceNumber | Reference | Subtotal | SalesTax | Terms |</samp></pre>");
	$page['credit'] = array(
								"page_title" => "Credits CSV Import",
								"page_body" => "In order to import the credits data, the CSV data must be in the following order: <br />
								<pre><samp>| ST | CreditDate | CustomerNumber | CreditNumber | Reference | Subtotal | SalesTax |</samp></pre>");
	$page['special'] = array(
								"page_title" => "Specials CSV Import",
								"page_body" => "In order to import the specials data, the CSV data must be in the following order: <br />
								<pre><samp>| Line | SKU | RegularPrice |</samp></pre>");
	$page['salesmanquota'] = array(
								"page_title" => "Salesman Quota CSV Import",
								"page_body" => "In order to import the Quota data, the CSV data must be in the following order: <br />
								<pre><samp>| Salesperson Number | SalespersonName | MTD | Transaction Count |</samp></pre>");
	$page['salesmanquotagoal'] = array(
								"page_title" => "Salesman Quota Goals CSV Import",
								"page_body" => "In order to import the Quota Goal data, the CSV data must be in the following order: <br />
								<pre><samp>| Salesperson Number | Month | Year | Goal |</samp></pre>");
	$page['pickingincentive'] = array(
								"page_title" => "Picking Incentive CSV Import",
								"page_body" => "In order to import the Sales data, the CSV data must be in the following order: <br />
								<pre><samp>| ST (location) | Date | PO# | InvoiceNumber | LineNumber | Description | NetSales | NetUnits |</samp></pre>");
	$page['pricingplanlistcustomer'] = array(
								"page_title" => "Pricing Plan List (Customer) CSV Import<br>
								Price Plan Name: " . $pricelist->Name . "<br>
								Price Plan ID: <strong>$id</strong><br>",
								"page_body" => "In order to import the Pricing Plan Customers data, the CSV data must be in the following order: <br />
								<pre><samp>| Pricing Plan ID | AccountNumber |</samp></pre>");
	$page['pricingplanlistparts'] = array(
								"page_title" => "Pricing Plan List (Parts) CSV Import<br>
								Price Plan Name: " . $pricelist->Name . "<br>
								Price Plan ID: <strong>$id</strong><br>",
								"page_body" => "In order to import the Pricing Plan Parts data, the CSV data must be in the following order: <br />
								<pre><samp>| Pricing Plan ID | Line | SKU | Price |</samp></pre>");
	
?>
        
   <!-- Stack the columns on mobile by making one full-width and the other half-width -->
     <div class="container " style="padding-top: 10px;">
     </div>    
		<div class="panel panel-default">
		  <div class="panel-heading">
		    <h3 class="panel-title"><?= $page[$for]['page_title']; ?></h3>
		  </div>
		  <div class="panel-body">
		    <?= $page[$for]['page_body']; ?>
		    The CSV file should NOT contain the column headers.	
		    <p class="bg-danger" id="uploadError"></p>
		    <div class="well">
		    	<input id="inputCSV" type="file" name="csv" class="file" data-show-preview="false">
		    	<select name="replace_append" class="form-control">
		    		<option value="append">Append/Update</option>
		    		<option value="replace">Replace [WARNING - THIS WILL DELETE THE ENTIRE TABLE]</option>
		    	</select>
		    </div>
		    
		    <div class="alert alert-info" style="display: none;" id="uploadSuccess" role="alert">
		    	File has been uploaded successfully! Proceeding to Insert the data into the table...</div>
		  </div>
		  <div class="alert alert-success" style="display: none;" id="importSuccess" role="alert">The inventory has been imported successfully!</div>
		  <div class="alert alert-danger" style="display: none;" id="importFail" role="alert">There has been an error while importing the data.</div>
		</div>
     <?php 
      ?>
     <script type="text/javascript">
     $('#inputCSV').on('filebatchuploadsuccess', function(event, data, previewId, index) {
         response = data.response;
         if(response.fileuploaded == true){
			$("#uploadSuccess").css('display', "");
			var a = $("[name=replace_append]").val();
			runImportCSV("<?= $for; ?>", a);
			
         }
         
     });
     $('#inputCSV').on('fileuploaded', function(event, data, previewId, index) {
         response = data.response;
         if(data.response.fileuploaded == true){
     		$("#uploadSuccess").css('display', "");
     		var a = $("[name=replace_append]").val();
     		runImportCSV("<?= $for; ?>", a);
         }
         
     });
     	var a = $("[name=replace_append]").val();
     	$("[name=replace_append]").on("change", function(event) {
     		a = $("[name=replace_append]").val();
     		$("#inputCSV").fileinput({
     			'showUpload':true, 
     			'uploadUrl':"upload_csv.php?for=<?= $for; ?>",
     			'fileTypeSettings':'text',
     			'elErrorContainer':'#uploadError',
     		});
     	});
     	$("#inputCSV").fileinput({
     		'showUpload':true, 
     		'uploadUrl':"upload_csv.php?for=<?= $for; ?>",
     		'fileTypeSettings':'text',
     		'elErrorContainer':'#uploadError',
     	});
     	function runImportCSV(destination, a) {
     		$.get("import_csv.php?for=" + destination + "&a=" + a, function(data) {


				if (data.result.result == "true") {
					var errorText = "";
					if (data.result.falseCount > 0) {
						for (var i = 0; i < data.result.errors.length; i++) {
							errorText = errorText + "Error " + (i+1) + ": " + data.result.errors[i] + "<br />";
						}
						errorText = "<br />Errors found: <br />" + errorText
					}
					$("#importSuccess").html("Import Success! <br /> Total Rows Processed: " + data.result.rowCount + "<br />" + "Rows Successfully Inserted: " + data.result.trueCount + "<br />Rows with errors: " + data.result.falseCount + errorText);
					
					$("#importSuccess").css('display', "");
				}else {
					var errorText = "";
					for (var i = 0; i < data.result.errors.length; i++) {
						errorText = errorText + "Error " + (i+1) + ": " + data.result.errors[i] + "<br />";
					}
					var text = $("#importFail").text();
					$("#importFail").html(text + "<br />Errors found: <br />" + errorText);
					$("#importFail").css('display', "");
				}
     			
     		
     		});
     	}     	
     
     </script>
   </body>
 </html>