<?php 
$list="1";
include('header.php');
 ?>

<script type="text/javascript" src="<?=$website?>/js/jquery.sparkline.min.js"></script>
<script src="<?=$website?>/js/libraries/RGraph.common.core.js" ></script>
<script src="<?=$website?>/js/libraries/RGraph.common.dynamic.js" ></script>
<script src="<?=$website?>/js/libraries/RGraph.common.tooltips.js" ></script>
<script src="<?=$website?>/js/libraries/RGraph.common.effects.js" ></script>
<script src="<?=$website?>/js/libraries/RGraph.common.key.js" ></script>
<script src="<?=$website?>/js/libraries/RGraph.bar.js" ></script>
<script src="<?=$website?>/js/libraries/RGraph.line.js" ></script>
<!--[if lt IE 9]><script src="<?=$website?>/js/excanvas/excanvas.js"></script><![endif]-->
<div class="grid_full page_wrap">
<?php
	if(is_numeric($_GET['stats'])){
		$idedit=$_GET['stats'];
		$getnamefile=@mysql_result(mysql_query("SELECT `name` FROM `products` WHERE `id`='$idedit'"),0,0);
		$getlastdate=@mysql_result(mysql_query("SELECT `date` FROM `sales` WHERE `idp`='$idedit' ORDER BY `date` ASC LIMIT 1"),0,0);
			$sir=array();
			$sirtips=array();
			$numar=array();
			if($getlastdate){
				$datenow=date('d-m-Y',$getlastdate);
			}else{
				$datenow=date('d-m-Y');
			}
			$sir[]="'".$datenow."'";
			$numarquery=@mysql_num_rows(mysql_query("SELECT `id` FROM `sales` WHERE `datefix`='$datenow' AND `idp`='$idedit'"));
			$numar[]=$numarquery;
			$sirtips[]="'$numarquery new sales'";
				for ($i = 1; $i <= 10; $i++) {
					if($getlastdate){
						$nextdate=date('d-m-Y', strtotime('+'.$i.' day',$getlastdate));
					}else{
						$nextdate=date('d-m-Y', strtotime('+'.$i.' day'));
					}
					$sir[]="'".$nextdate."'";
					$numarquery=@mysql_num_rows(mysql_query("SELECT `id` FROM `sales` WHERE `datefix`='$nextdate' AND `idp`='$idedit'"));
					$numar[]=$numarquery;
					$sirtips[]="'$numarquery new sales'";
				}
			$sir=implode(",",$sir);
			$sirtips=implode(",",$sirtips);
			$numar=implode(",",$numar);
		echo "<script type=\"text/javascript\">
$(document).ready(function(){
	
		var line2 = new RGraph.Bar('mycvs', [$numar]);
            line2.Set('chart.filled', true);
            line2.Set('chart.fillstyle', ['transparent']);
            line2.Set('chart.colors', ['red']);
            line2.Set('chart.linewidth', 2);
            line2.Set('chart.hmargin', 5);
            line2.Set('chart.tickmarks', 'circle');
            line2.Set('chart.labels', [$sir]);
            line2.Set('chart.tooltips', [$sirtips]);
           // line2.Draw();
		   RGraph.Effects.Fade.In(line2);
});
</script>
<h3>Stats for $getnamefile </h3>	
	<canvas id=\"mycvs\" width=\"920\" height=\"400\">[No canvas support]</canvas>";
		
		
	}
	if(is_numeric($_GET['edit'])){
		$idedit= $_GET['edit'];
		$fetch=@mysql_fetch_array(mysql_query("SELECT * FROM `products` WHERE `id`='$idedit'"));
		if($fetch[idu] > 0){
			$username=@mysql_result(mysql_query("SELECT `username` FROM `members` WHERE `id`='$fetch[idu]'"),0,0);
		}else{
			$username="NO";
		}
		
		if($_POST['submit']){
			$name=$_POST['name'];
			$description=$_POST['description'];
			$price=$_POST['price'];
			@mysql_query("UPDATE `products` SET `name`='$name',`price`='$price',`description`='$description' WHERE `id`='$idedit'");
			echo "<div class=\"alert alert-success\">Update Succesfully</div>";
		}else{
?>
<legend><strong>Edit:</strong> <?=$fetch[name]?></legend>
<form action="" method="post" class="form-horizontal" style="width: 450px; margin: 0 auto;">
	<div class="control-group">
		<label class="control-label" for="inputUsername">Username:</label>
		<div class="controls">
			<input type="text" name="username" id="inputUsername" readonly value="<?=$username?>">
		</div>
	</div>
	<div class="control-group">
		<label class="control-label" for="inputPrice">Price: </label>
		<div class="controls">
			<input type="text" name="price" id="inputPrice" value="<?=$fetch[price]?>"/> <?=$fetch[price_value]?>
		</div>
	</div>
	<div class="control-group">
		<label class="control-label" for="inputProductName">Product name: </label>
		<div class="controls">
			<input type="text" name="name" id="inputProductName" value="<?=$fetch[name]?>"/>
		</div>
	</div>
	<div class="control-group">
		<label class="control-label" for="inputProductName">Description: </label>
		<div class="controls">
			<textarea name="description"><?=$fetch[description]?></textarea>
		</div>
	</div>
	<div style="text-align: center;"><input type="submit" name="submit" class="btn btn-primary btn-large" style="width: 300px;" /></div>
</form>



<hr />

<?php
		}
	}
	if($_GET['del']){
	$idde=$_GET['del'];
	@mysql_query("DELETE FROM `products` WHERE `id`='$idde'");
	echo "<div class=\"alert alert-error\">Delete Succesfully</div>";
	}
		$query=@mysql_query("SELECT * FROM `products` ORDER BY id DESC");
			if(mysql_num_rows($query) > 0){
?>
<table class="table table-bordered">
	<thead>
		<tr>
			<th width="10">Id:</th>
			<th>Product Name:</th>
			<th width="100">Price:</th>
			<th>Email:</th>
			<th width="280">Action:</th>
		</tr>
	</thead>
	<tbody>
	<?php
			while($row=@mysql_Fetch_array($query)){
				echo "<tr>
							<td>$row[id]</td>
							<td>$row[name]</td>
							<td>$row[price] $row[price_value]</td>
							<td>$row[contact_email]</td>
							<td><a href=\"?edit=$row[id]\" class=\"btn btn-info\"><i class=\"icon-pencil icon-white\"></i> Edit</a> <a href=\"?del=$row[id]\" class=\"btn btn-danger\"><i class=\"icon-remove icon-white\"></i> Delete</a> <a href=\"?stats=$row[id]\" class=\"btn btn-success\"><i class=\"icon-signal icon-white\"></i> Statistics</a></td>
						</tr>";
			
			
			}
			
	?>
			
		
	</tbody>
</table>
<?php
			}else{
?>
    <div class="alert alert-error">
    Not Found
    </div>
<?php
}
?>

</div>

<?php include('footer.php'); ?>