<?php
include('db_connection.php');
$Mainclass=new DB_Controller();
//echo $_FILES["uploadFile"]['tmp_name'];
if ($_REQUEST["submit"] != "") { 
$row = 0;
//$err = "<div class='import-data'>";
$tempFileName = time().".csv";
if ( is_uploaded_file( $_FILES["uploadFile"]['tmp_name']) ) {
	$fileCopied = copy( $_FILES["uploadFile"]['tmp_name'], $tempFileName);
	
	if($fileCopied)  {
		
		//echo 1111111111;
	
		if (($handle = fopen($tempFileName, "r")) !== FALSE) {
			while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
				
				$num = count($data);
				for ($c=0; $c < $num; $c++)
							 {
								 $arr[$row][$c] = $data[$c];
								 //echo $data[$c];
							}
				$row++;
				}
					//echo $arr[1][0];
			for($i=1; $i<$row; $i++)
			{
				//echo $data[$i][0];
			

			    //$SqlCheck="SELECT * FROM  product_master Where Parent_Id='".trim($arr[$i][0])."' and Product_Id='".trim($arr[$i][1])."' and Short_Description=''";
				 $SqlCheck="SELECT * FROM    product_master Where Product_Code='".trim($arr[$i][0])."'";
				$Check=mysqli_query($Mainclass->dbh,$SqlCheck);
				if(mysqli_num_rows($Check)!=0)
				{
				 //$sql = "UPDATE product_master SET Short_Description='".$Mainclass->real_escapes(trim($arr[$i][2]))."' where Parent_Id='".trim($arr[$i][0])."' and Product_Id='".trim($arr[$i][1])."'";
				 
				 //$sql = "UPDATE product_master SET Stock_Type='".$Mainclass->real_escapes(trim($arr[$i][2]))."' where Parent_Id='".trim($arr[$i][0])."' and Product_Id='".trim($arr[$i][1])."'";
				// $sql = "UPDATE  product_details SET Product_Description='".mysqli_real_escape_string($Mainclass->dbh,trim($arr[$i][1]))."' where Product_Code='".trim($arr[$i][0])."'";
				 $sql = "UPDATE  product_details SET BinNumber='".mysqli_real_escape_string($Mainclass->dbh,trim($arr[$i][1]))."' where Product_Code='".trim($arr[$i][0])."'";
				
				//echo $sql;
				 $q=mysqli_query($Mainclass->dbh,$sql);
				//$r=mysqli_affected_rows($q);
				 $rslt=$rslt+$q;
				$err .= "<div class='import-data'>";
				 $err .= "<div class='column2' style='width:195px;'>".$i."</div><div class='column2' style='width:195px;'> <strong>".$arr[$i][0]."</strong></div><div class='column2' style='width:198px; border-right:none;'>  Updated Sucessfully</div>";
				$err .= "</div >";
				}
				else
				{
				$err .= "<div class='import-data'>";
				$err .= "<div class='column2' style='width:195px;'>".$i."</div><div class='column2' style='width:195px;'> <strong>".$arr[$i][0]."</strong></div><div class='column2' style='width:198px; border-right:none;'>  Not Present In Database</div>";
				$err .= "</div >";
				 //$err .= "".$i.". Product <strong>'".$arr[$i][0]."'</strong> Already Present In Database.<br>";
				}
			

			}
			fclose($handle);
			@unlink($tempFileName);
			$echo="File Upload Completed. See The Result.";
		}
	}
	

}

}

$msg= "Total ".$rslt." Product/Products Updated Sucessfully."; 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Chandrani | CMS</title>

<?php //include("../common/report-headerlink.php"); ?>

<script language="javascript">
function divHide()
{
//alert();
result.style.display='block'; 
}
</script>

<script>
function msg()
{
document.getElementById("msg").innerHTML='Uploading....Please Wait...';
}
</script>
</head>

<body onLoad="startTime()">
<div id="wrapper">
<!-----------------------------header------------------------->
<?php include("../common/report-header.php"); ?>
<!--------------------------------------body------------------------------------>
<div class="bodybg">
<div class="body-heading">Import - Bin</div>	
<form name="myform" method="post" enctype="multipart/form-data" action="">
<table width="400" class="border-box" border="0" style="margin-left:270px;">
  <tr>
    <td class="import-txt">Select a file to Update Bin</td>
  </tr>
  <tr>
    <td>
    
<input type="file" name="uploadFile" class="txtbox" style="height:26px; width:200px; margin-left:80px;" />

    
    
    </td>
  </tr>
  <tr>
    <td style="padding-top:10px;"><input name="submit" type="submit" value="UPDATE" class="btn-submit" style="margin-left:120px;" onclick="msg()" />
    							  <input name="submit" type="button" value="RESULT" class="btn-submit" style="margin-left:10px;" onclick="divHide()" />
    </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td align="center"><strong><span id="msg" style="font-size:15px; color:#f06693; "><?php echo $echo;?></span></strong></td>
  </tr>
 
</table>
</form>

<table width="400" border="0" cellspacing="0" cellpadding="0" style="margin-left:270px; margin-bottom:15px;">
  <tr>
    <td style="padding-bottom:25px;"><a href="import-sequence-example.csv" target="_blank"><div class="linkbtn">Download example file</div></a></td>
  </tr>
  </table>

<div id="result" style="display:none" >

<table width="800" border="0" cellspacing="0" cellpadding="0" style="margin-left:100px;">
  <tr>
    <td  class="caption">RESULT: <span style="font-size:15px; color:#f06693; "><?php if($rslt){ echo $msg ;}else{ echo "No Products Updated.";}?></span></td>
  </tr>
</table>
<table width="800" class="border-box1" border="0" cellspacing="0" cellpadding="0"  style="margin-left:100px;">
  <tr>
    <td>
     <div class="import-head">
     <div class="column1" style="width:195px;">Sln</div>
     <div class="column1" style="width:195px;">Product Code</div>
     <div class="column1" style="width:195px; border-right:none;">Status</div>
     </div>
     
     
     
    
     
      <div class="import-bg"> 
    
	<?php echo $err ?>
   <!-- <div class="column2" style="width:195px;">xxx</div>
    <div class="column2" style="width:195px;">xxxx</div>
    <div class="column2" style="width:195px; border-right:none;">cdefgvtrhyj</div>-->
    
    </div>
    
    
    
    </td>
  </tr>
</table>


</div>

<!--<div style="width:400px; margin-left:275px; color:#FF0000"><?php echo $err ?></div>-->
<!--<div><a href="example.xlsx" target="_blank">Download example file</a></div>-->
</div>
<!--------------------------------------footer------------------------------------>
<?php include("../common/report-footer.php"); ?>

</div>

<script>
		(function($){
			$(window).load(function(){
				$("#content_1").mCustomScrollbar({
				advanced:{
        			updateOnContentResize: true
    			},
					autoHideScrollbar:false,
					autoDraggerLength:true,
					theme:"dark-thin"
				});
			});
		})(jQuery);
</script>

</body>
</html>
