<?php  

  session_start();
  include '../db.php';
 // error_reporting(0);
  if(isset($_SESSION['username']))
  {
	  
	  $user_id=$_GET['rest_id'];
	  
	  
	   require("phpexcel/php-excel-reader/excel_reader2.php");
	require('phpexcel/SpreadsheetReader.php');
 
   if(isset($_POST['upload_excel']))
   {
	   $filename=$_FILES['excelfile']['name'];
	   move_uploaded_file($_FILES['excelfile']['tmp_name'],'excel/'.$filename);
	   
	  // date_default_timezone_set('UTC');

	//$StartMem = memory_get_usage();


	try
	{
    	$Filepath="excel/".$filename;
		$Spreadsheet = new SpreadsheetReader($Filepath);
		//$BaseMem = memory_get_usage();

	//	$Sheets = $Spreadsheet -> Sheets();


	/*	foreach ($Sheets as $Index => $Name)
		{
			

			//$Time = microtime(true);

			$Spreadsheet -> ChangeSheet($Index);

			foreach ($Spreadsheet as $Key => $Row)
			{
				
				if ($Row)
				{
					
					$name=$Row[0];
					$description=$Row[1];
					$cat_id=$Row[2];
					$food_type=$Row[3];
					$price=$Row[4];
					$stock=$Row[5];
					//echo $cat_id.'-'.$name.'-'.$img.'-'.$amount.'-'.$activate;
					
					 $qry = "INSERT INTO `product` (`name`,`description`,`cat_id`,`food_type`,`price` ,`stock`,`date_time`) VALUES ( '$name','$description',$cat_id,'$food_type','$price','$stock',now())";
	                     $res=mysqli_query($conn,$qry);
	 
					echo mysqli_error($conn);
				}
				
			}
		
			
		}
		
		*/
		
	}
	catch (Exception $E)
	{
		die("Invalid Excel Format");
	}
	   
	   
	   
   }
	  
	  
	  
	  
	  
	  
?>






<?php require 'db.php'?>
<!DOCTYPE html>
<html lang="en">
<head>
    <!--
        ===
        This comment should NOT be removed.

        Charisma v2.0.0

        Copyright 2012-2014 Muhammad Usman
        Licensed under the Apache License v2.0
        http://www.apache.org/licenses/LICENSE-2.0

        http://usman.it
        http://twitter.com/halalit_usman
        ===
    -->
    <meta charset="utf-8">
    <title>admin</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="Charisma, a fully featured, responsive, HTML5, Bootstrap admin template.">
    <meta name="author" content="Muhammad Usman">

    <!-- The styles -->
    <link id="bs-css" href="css/bootstrap-cerulean.min.css" rel="stylesheet">

    <link href="css/charisma-app.css" rel="stylesheet">
    <link href='bower_components/fullcalendar/dist/fullcalendar.css' rel='stylesheet'>
    <link href='bower_components/fullcalendar/dist/fullcalendar.print.css' rel='stylesheet' media='print'>
    <link href='bower_components/chosen/chosen.min.css' rel='stylesheet'>
    <link href='bower_components/colorbox/example3/colorbox.css' rel='stylesheet'>
    <link href='bower_components/responsive-tables/responsive-tables.css' rel='stylesheet'>
    <link href='bower_components/bootstrap-tour/build/css/bootstrap-tour.min.css' rel='stylesheet'>
    <link href='css/jquery.noty.css' rel='stylesheet'>
    <link href='css/noty_theme_default.css' rel='stylesheet'>
    <link href='css/elfinder.min.css' rel='stylesheet'>
    <link href='css/elfinder.theme.css' rel='stylesheet'>
    <link href='css/jquery.iphone.toggle.css' rel='stylesheet'>
    <link href='css/uploadify.css' rel='stylesheet'>
    <link href='css/animate.min.css' rel='stylesheet'>

    <!-- jQuery -->
    <script src="bower_components/jquery/jquery.min.js"></script>

    <!-- The HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <!-- The fav icon -->
    <link rel="shortcut icon" href="img/favicon.ico">

</head>
<body>

<?php require 'header.php'?>
	<?php
 if (isset($_POST['add_product']))
 {
	 
	 $product_name = mysqli_real_escape_string($conn,$_POST['product_name']);
	/* $thumbnail =$_FILES['thumbnail']['name'];
	
	  $ext = pathinfo($thumbnail, PATHINFO_EXTENSION);
 $filename=mt_rand(100000, 999999).'_'.uniqid().'.'.$ext;
	  move_uploaded_file($_FILES["thumbnail"]["tmp_name"],"../images/".$filename);
	*/
	
	   $cat=$_POST['cat'];
	   
	   $food=$_POST['food'];
	    $description = mysqli_real_escape_string($conn,$_POST['description']);
		 $price = mysqli_real_escape_string($conn,$_POST['price']);
		 
	
	 $stock = mysqli_real_escape_string($conn,$_POST['stock']);
	$status="activate";
	
	
	 
	 $qry = "INSERT INTO `product` (`cat_id`,`food_type`,`name`,`description`,`price`,`stock`,`date_time`,`status`) VALUES ( $cat,'$food','$product_name','$description','$price','$stock',now(),'$status')";
	 $res=mysqli_query($conn,$qry);
	 echo mysqli_error($conn);
 }
 
 
 if (isset($_POST['del_product']))
 {
	 $del_id= $_POST['del_id'];
	 $qry = "delete from `product` where id = $del_id";
	 $res=mysqli_query($conn,$qry);
	 echo mysqli_error($conn);
  }
  
  
   if (isset($_POST['edit_product']))
   {
	   $cat_id= $_POST['pro_id'];
	   $food= $_POST['food'];
	   
	   
	   
	   $product_name = mysqli_real_escape_string($conn,$_POST['product_name']);
	/*
	    if($_FILES["thumbnail"]["name"]=="")
					   {
						   
						   $img=$_POST['thumbnail_h'];
					   }
					   else {
					  $ext = pathinfo($_FILES["thumbnail"]["name"], PATHINFO_EXTENSION);
 $filename=mt_rand(100000, 999999).'_'.uniqid().'.'.$ext;
  $img=$filename;
	  move_uploaded_file($_FILES["thumbnail"]["tmp_name"],"../images/".$filename);
					   
					   } */
					   $cat= $_POST['cat'];
					   
	   $description = mysqli_real_escape_string($conn,$_POST['description']);
	
 $price = mysqli_real_escape_string($conn,$_POST['price']);
	   
	   
	    $stock =  $_POST['stock'];
	
	  mysqli_query($conn,"Update `product` set `cat_id`=$cat,food_type='$food',name='$product_name' ,description='$description',price='$price',stock=$stock  where id=$cat_id ");
	  
	  echo mysqli_error($conn);
   }
   
  
  
  
?> 
<div class="ch-container">
   <div class="row">
  <!-- left menu starts -->
	<?php require 'navbar.php'?>
		 <div class="row">
			<div class="box col-md-8">
				<div class="box-inner">
					<div class="box-content">
						<ul class="breadcrumb">
								<li>
									<a href="index.php">Home</a>
								</li>
								<li>
									Product Details
								</li>
								
								
							</ul>
	<table class="table table-striped table-bordered bootstrap-datatable datatable responsive">
	<br>
						<br>
		<div class="container">
							 <!-- Trigger the modal with a button -->
							 <div class="row">
							 <div class="col-md-6" >
		<div>	<button type="button" class="btn btn-info btn-lg" data-toggle="modal"  data-target="#myModal">Add New</button> </div>
								<!-- Modal -->
								<div>
								<form action="" enctype="multipart/form-data" method="post">
					   <label>Upload Excel:</label>
					   
					   <input  required class="control-form" type="file" name="excelfile" />
					   <input  type="submit" name="upload_excel" value="Upload" class="btn btn-success">
					   </form>
					   </div>
					   </div>
					   </div>
		  <div class="modal fade" id="myModal" role="dialog">
									<div class="modal-dialog">
			<!-- Modal content-->
		  <div class="modal-content">
				<div class="modal-header">
						  <button type="button" class="close" data-dismiss="modal">&times;</button>
			  <h4 class="modal-title">Add New Product</h4>
			</div>
					<div class="modal-body">
						<form role="form" action="" enctype="multipart/form-data"  method ="POST">
														
								<div class="form-group">
					<label>Product Name</label>
					<input type="text" class="form-control" name="product_name" required >
					               </div>
								 
								 
								 
							
								   
								   								<div class="form-group">
					<label>Select Category:</label><br/>
					
					 <select name="cat" class="form-control" >
				        <?php $MQ=mysqli_query($conn,"select *from `category` where rest_id=$user_id"); 
						while($mcData=mysqli_fetch_assoc($MQ)) { ?>
				           
							
							<option value="<?php echo $mcData['id']; ?>"><?php echo $mcData['name']; ?></option>
							<?php }  ?>
							</select>
				
					               </div>
								   
								   
								   
								   <div class="form-group">
					<label>Select food type:</label><br/>
					
					 <select name="food" class="form-control" >
				       
				           <option value="">Select</option>
							
							<option value="veg">Veg</option>
								<option value="nonveg">NonVeg</option>
							</select>
				
					               </div>
								   
								   
								   
								   
								   	<div class="form-group">
					<label>Description</label>
					<textarea type="text" class="form-control" name="description" required ></textarea>
					               </div>
								 
								   <div class="form-group">
					<label>Price</label>
					<input type="number" class="form-control" name="price" required >
					               </div>
								
								   <div class="form-group">
					<label>Stock</label>
					<input type="number" class="form-control" name="stock" required >
					               </div>
															
															
				 <button type="submit" name="add_product" class="btn btn-primary">Submit</button>
							</form>
			</div>
			<div class="modal-footer">
		  <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
				</div>
			  </div>
		   </div>
					</div>
				</div>
						<br>
						<br>	
						<thead>
							<tr>
								<th>Sr Num</th>
								<th>Name</th>
								<th>Food Type</th>
								
								<th>Category</th>
								<th>Description</th>
 								<th>Price</th>
							
								<th>Stock</th>
								<th></th>
								<th></th>
							</tr>
						</thead>
						 <tbody>
								<?php
								 $ret=mysqli_query($conn,"select *from `product` where cat_id in (select id from category where rest_id=$user_id)");
								 $i=0;   while($row=mysqli_fetch_assoc($ret))
										{
											$i++;
									?>
							<tr>
								<td><?php  echo $i; ?></td>
								<td> <?php  echo $row['name']; ?></td>
								<td> <?php  echo $row['food_type']; ?></td>
							
							  
								<td><?php $cat_id=$row['cat_id'];  $catQ=mysqli_query($conn,"select *from category where id=$cat_id"); $catData=mysqli_fetch_assoc($catQ); echo $catData['name']; ?></td>

                                 <td> <?php  echo $row['description']; ?></td>

							  <td> <?php  echo $row['price']; ?></td>
							   
							   <td> <?php  echo $row['stock']; ?></td>



										<td>	
									
									<button type="button" class="btn btn-primary" data-toggle="modal"  data-target="#myModal<?php echo  $row['id']; ?>">Edit</button>
									
									  <div class="modal fade" id="myModal<?php echo  $row['id']; ?>" role="dialog">
									<div class="modal-dialog">
									<!-- Modal content-->
									  <div class="modal-content">
										<div class="modal-header">
											  <button type="button" class="close" data-dismiss="modal">&times;</button>
											  <h4 class="modal-title">Edit</h4>
											</div>
												<div class="modal-body">
							<form role="form" action="" enctype="multipart/form-data" method ="POST">
								<input type="hidden" name="pro_id" value="<?php echo $row['id']; ?>"/>							
								<div class="form-group">
					<label>Product Name</label>
					<input type="text" class="form-control" name="product_name" value="<?php echo $row['name']; ?>" required >
					               </div>
								   
								   
								
								
								
																<div class="form-group">
					<label> Category:</label><br/>
					
					 <select name="cat" class="form-control" >
				        <?php $MQ=mysqli_query($conn,"select *from `category` where rest_id=$user_id"); 
						while($mcData=mysqli_fetch_assoc($MQ)) { ?>
				           
							
							<option  value="<?php echo $mcData['id']; ?>"  <?php if($row['cat_id']==$mcData['id']) { echo "selected"; } ?>     ><?php echo $mcData['name']; ?></option>
							<?php }  ?>
							</select>
				
					               </div>  
								   
								   <div class="form-group">
					<label>Select food type:</label><br/>
					
					 <select name="food" class="form-control" >
				       
				           
							
							<option <?php  if($row['food_type']=="veg"){  ?> selected <?php } ?> value="veg">Veg</option>
								<option  <?php  if($row['food_type']=="nonveg"){  ?> selected <?php } ?> value="nonveg">NonVeg</option>
							</select>
				
					               </div>
								
								
								
								<div class="form-group">
					<label>Description</label>
					<input type="text" class="form-control" name="description" value="<?php echo $row['description']; ?>" required >
					               </div>
								   
								   
								
								
								<div class="form-group">
					<label>Price</label>
					<input type="text" class="form-control" name="price" value="<?php echo $row['price']; ?>" required >
					               </div>
								   
								   
								   
								 
								   
								   <div class="form-group">
					<label>Stock</label>
					<input type="text" class="form-control" name="stock" value="<?php echo $row['stock']; ?>" required >
					               </div>
								   
								   
								   
								   
															
														
														   <button type="submit" name="edit_product" class="btn btn-primary">Submit</button>
														</form>
												</div>
											<div class="modal-footer">
											  <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
											</div>
									  </div>
								   </div>
								</div>
							
									
								</td>
									
								 <td>
									<form action="" method="post" onsubmit="return confirm('Are You Sure?');">		
											 <input type="hidden" value="<?php echo $row['id']; ?> " name="del_id" />				
											<input type="submit" name="del_product" class="btn btn-danger" Value="Delete"/>
									</form>
								</td>
							</tr>
							<?php  mysqli_query($conn,"Update `product` set `new_to_read`=0");  ?>
	
							<?php  }  ?>
						</tbody>
					</table>
				</div>
			</div>
		</div><!--/span-->
	</div><!--/row-->
</div><!--/.fluid-container-->

<!-- external javascript -->

<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>

<!-- library for cookie management -->
<script src="js/jquery.cookie.js"></script>
<!-- calender plugin -->
<script src='bower_components/moment/min/moment.min.js'></script>
<script src='bower_components/fullcalendar/dist/fullcalendar.min.js'></script>
<!-- data table plugin -->
<script src='js/jquery.dataTables.min.js'></script>

<!-- select or dropdown enhancer -->
<script src="bower_components/chosen/chosen.jquery.min.js"></script>
<!-- plugin for gallery image view -->
<script src="bower_components/colorbox/jquery.colorbox-min.js"></script>
<!-- notification plugin -->
<script src="js/jquery.noty.js"></script>
<!-- library for making tables responsive -->
<script src="bower_components/responsive-tables/responsive-tables.js"></script>
<!-- tour plugin -->
<script src="bower_components/bootstrap-tour/build/js/bootstrap-tour.min.js"></script>
<!-- star rating plugin -->
<script src="js/jquery.raty.min.js"></script>
<!-- for iOS style toggle switch -->
<script src="js/jquery.iphone.toggle.js"></script>
<!-- autogrowing textarea plugin -->
<script src="js/jquery.autogrow-textarea.js"></script>
<!-- multiple file upload plugin -->
<script src="js/jquery.uploadify-3.1.min.js"></script>
<!-- history.js for cross-browser state change on ajax -->
<script src="js/jquery.history.js"></script>
<!-- application script for Charisma demo -->
<script src="js/charisma.js"></script>


</body>
</html>
 <?php  }  else header("Location:login.php");  ?>