<?PHP
ob_start();
session_start();
include 'connect.php';
if(isset($_SESSION['adminemail']))
{
$user=$_SESSION['adminemail'];

$gtp=mysqli_query($conn,"select * from admin where username='$user'");
$gtp1=mysqli_fetch_assoc($gtp);

include 'top.php';
?>


<div id="page-wrapper" >
          <div id="page-inner">
			<div class="main-content">
				<div class="main-content-inner">
					<div class="breadcrumbs ace-save-state" id="breadcrumbs">
						<ul class="breadcrumb">
							<li>
								<i class="ace-icon fa fa-home home-icon"></i>
								<a href="#">Home</a>
							</li>
							<li class="active">Product</li>
						</ul><!-- /.breadcrumb -->
					</div>

					<div class="page-content">
			

						<div class="page-header">
                        
<?php

$aca=mysqli_query($conn,"select * from financial where user='$user' order by ID desc");
$aca1=mysqli_fetch_assoc($aca);

$financial=$aca1['financial'];

?>                        
                        
							<table style="width:100%;">
                        
                        <tr><td style="width:50%;">
                        
							<h1>
								Product
							</h1>
                            
                            </td>
                            
                            <td style="width:50%; text-align:right; padding-right:15px; font-size:15px;">
                            
                            <i class="fa fa-calendar"></i>&nbsp; <?php echo $financial; ?>
                            
                            </td></tr></table>
                            
						</div><!-- /.page-header -->

						<div class="row">
							<div class="col-xs-12">
                            
								<!-- PAGE CONTENT BEGINS -->

<script src="assets/js/jquery-2.1.4.min.js"></script>

<script>

function acrate()
{
	
	var rate=$('.rate').val()*1;
	var discount=$('.discount').val()*1;

	var ac=rate-discount;

	$('.actualrate').val(ac);

}

$(document).ready(function(){

$('.rate').blur(acrate);
$('.discount').blur(acrate);

});

</script>

<script>
	
jQuery(function($){
		   
	// simple jQuery validation script
	$('#course').submit(function(){
		
		var valid = true;
		var errormsg = 'This field is required!';
		var errorcn = 'error';
		
		$('.' + errorcn, this).remove();			
		
		$('#required', this).each(function(){
			var parent = $(this).parent();
			if( $(this).val() == '' ){
				var msg = $(this).attr('title');
				msg = (msg != '') ? msg : errormsg;
				$('<span style="font-size:13px;color:#FF0000; padding-left:3px;" class="'+ errorcn +'">'+ msg +'</span>')
					.appendTo(parent)
					.fadeIn('fast')
					.click(function(){ $(this).remove(); });
				valid = false;
			};
		});
		
		return valid;
		
		$('input:submit').prop('disabled', true);
		
	});
	
});	

</script>



<?php  



if(isset($_POST['submit']))

{

$id=$_POST['id'];

$category=$_POST['category'];

$product=$_POST['product'];

$rate=$_POST['rate'];

$discount=$_POST['discount'];

$actualrate=$_POST['actualrate'];

$unit=$_POST['unit'];


$up=mysqli_query($conn,"update product set product='$product',category='$category',unit='$unit',rate='$rate',discount='$discount',actualrate='$actualrate' where ID='$id'");


if($up)

{

header("location:viewproduct.php?s=1");

}



}      



elseif($_GET['id'])

{

$id=$_GET['id'];



$c=mysqli_query($conn,"select * from product where ID='$id'");
$c1=mysqli_fetch_assoc($c);



echo"<center><form action='viewproduct.php' method='post' id='course'>

<input type='hidden' name='id' value='".$id."'>";

		echo'<table width="60%" style="line-height:35px;">

		<tr><td id="page">Product <span class="red"></span> </td><td></td><td> <input  class="form-control" style="width: 30ch; margin-bottom:10px" type="text" name="product" id="required" title="*" style="width:220px;" value="'.$c1['product'].'" autocomplete="off" /></td></tr>
		
		
		<tr><td id="page">Unit </td><td></td><td> <input  class="form-control" style="width: 30ch; margin-bottom:10px" type="text" name="unit" style="width:220px;" autocomplete="off" class="unit" value="'.$c1['unit'].'" /></td></tr>
		
		
		<tr><td id="page">Rate </td><td></td><td> <input  class="form-control" style="width: 30ch; margin-bottom:10px" type="text" name="rate" style="width:220px;" autocomplete="off" class="rate" value="'.$c1['rate'].'" /></td></tr>
		
		
		<tr><td id="page">Discount </td><td></td><td> <input  class="form-control" style="width: 30ch; margin-bottom:10px" type="text" name="discount" style="width:220px;" autocomplete="off" class="discount" value="'.$c1['discount'].'" /></td></tr>
		
		
		<tr><td id="page">Actual Rate </td><td></td><td> <input  class="form-control" style="width: 30ch; margin-bottom:10px" type="text" name="actualrate" style="width:220px;" autocomplete="off" class="actualrate" value="'.$c1['actualrate'].'" /></td></tr>

		<tr><td id="page">Category <span class="red"></span> </td><td></td><td><select  class="form-control" style="width: 30ch; margin-bottom:10px" name="category" id="required" title="*" style="width:220px;">
		
		<option value="">Select Category</option>';
		
		$cte=mysqli_query($conn,"select * from product_category order by category asc");
		while($cte1=mysqli_fetch_assoc($cte))
		{
		
			$cate="<option value='".$cte1['ID']."'>".$cte1['category']."</option>";
			$cate=str_replace("<option value='".$c1['category']."'","<option value='".$c1['category']."' selected",$cate);
			echo $cate;
			
		}
	
		echo'</select></td></tr>

		<tr><td></td><td></td><td style="padding-top:7px;"><input class="btn btn btn-primary" type="submit" name="submit" value="Save" /></td></tr></table>

		</form></center>';

}

else

	{
	
if (isset($_GET['s'])) 	
	{
	 $status=$_GET['s'];
		
		 if($status=='1')
		 {
			 echo'<div class="alert alert-block alert-success">
									<button type="button" class="close" data-dismiss="alert">
										<i class="ace-icon fa fa-times"></i>
									</button>
						Values Edited Successfully.
				  </div>';				  
		 }
		 if($status=='2')
		 {
			 echo'<div class="alert alert-block alert-success">
									<button type="button" class="close" data-dismiss="alert">
										<i class="ace-icon fa fa-times"></i>
									</button>
						Product Created Successfully.
				  </div>';				  
		 }
		 if($status=='7')
		 {
			 echo'<div class="alert alert-block alert-success">
									<button type="button" class="close" data-dismiss="alert">
										<i class="ace-icon fa fa-times"></i>
									</button>
						Values Deleted Successfully.
				  </div>';				  
		 }
	}
	
	echo'<table style="width:99%; margin:11px auto 10px auto;">
                                        
                                        <tr><td style="width:20%;">
                                        <a href="product.php"><button class="btn btn-minier btn-info">+ Add Product</button></a>
                                        </td>
                                        
                                        <td style="width:50%;"></td>
                                        
                                        <td style="width:30%;">
                                        
                                        </td>
                                        
                                        </tr>
                                        
                                        </table>';
									
echo'<table id="dynamic-table" class="table table-striped table-bordered table-hover">';

echo"<thead><th height='25' width='5%'>S.No</th><th width='25%'>Product</th><th width='25%'>Category</th><th width='10%' style='text-align:center;'>Actions</th></thead>
<tbody>";

$select="select * from product order by ID ASC";
$select1=mysqli_query($conn,$select);
$sno=0;
while($value=mysqli_fetch_assoc($select1))
{
$sno=$sno+1;

$cte=mysqli_query($conn,"select * from product_category where ID='$value[category]'");
$cte1=mysqli_fetch_assoc($cte);

echo'<tr><td>'.$sno.'</td><td>'.$value['product'].'</td><td>'.$cte1['category'].'</td><td>';

													   if($gtp1['type']!='staff')
													   {
													   echo'<div class="hidden-sm hidden-xs btn-group">
													   
															<a href="viewproduct.php?id='.$value['ID'].'" title="Edit">
																<i class="ace-icon fa fa-pencil bigger-100 green"></i></a> &nbsp; 
																
															<a href="del.php?id='.$value['ID'].'&desc=product" title="Delete">
																<i class="ace-icon fa fa-trash-o bigger-110 dark grey"></i></a>
															
														</div>';
													   }

echo'</td></tr>';
}

echo"</tbody></table>";

}

?>
                                        
                                            
                                            							

										<!-- PAGE CONTENT ENDS -->
									</div><!-- /.col -->
								</div><!-- /.row -->
							</div><!-- /.page-content -->
						</div>
					</div><!-- /.main-content -->
					</div><!-- /.main-content -->
					</div><!-- /.main-content -->


<?php
include 'bottom.php';
}
else
{

 header("location:login.php");
}
ob_flush();
?>