<?php
		  if ($accounttype != '1'){
$AH = getAHItem($item['itemid'],$getstack);
    $dataPoints = array(
    );
	
    $gilPoints = array(
    );
if($AH == 'empty'){		
}
else{
	echo'<table width=100% style="min-height: 500px;"><tr><td valign=top width=50%><table class="table table-hover" width=100%>
	<tr class="table-success"><td colspan=5><b><center>Auction House History</a></td></tr>
	<tr class="table-warning"><td>Seller</td><td>Buyer</td><td>Quantity</td><td>Gil</td><td>Date</td></tr>';
	$i=0;
	foreach ($AH as $house) {
		$date=date('Y/m/d', $house['sell_date']);
		array_push($dataPoints,array("y" => $house['sale'], "label" => $date));
		array_push($gilPoints,$house['sale']);
		$char = getCharacterName($house['seller']);		
		$chars = getCharacterByName($house['buyer_name']);
		$i++;
		if($house['stack']==1){
			$q=$item['stackSize'];
		}
		else{
			$q=1;
		}
		if($char == 'empty' or $chars== 'empty'){	
		}
		elseif($i<=15){		
			echo '<tr><td><a href="?p=characters&id='.$house['seller'].'">'.ucwords(str_replace('_',' ',$char)).'</a></td><td><a href="?p=characters&id='.$chars['charid'].'">'.ucwords(str_replace('_',' ',$house['buyer_name'])).'</a></td><td>'.$q.'</td><td>'.$house['sale'].'</td><td>'.$date.'</td></tr>';
		}
  }
	
$average = array_sum($gilPoints) / count($gilPoints);

$today=date('Y/m/d', time());
$dataPoints = array_reverse($dataPoints);
$totaldays=strtotime($today)-strtotime($dataPoints[0]["label"]);
$totaldays=$totaldays/86400;
$averageperday=count($gilPoints)/$totaldays;
echo '</table></td><td width=5% valign=top>
<table class="table table-hover" >
<tr class="table-success"><td colspan=2><strong>Calculations</strong></td></tr>
<tr><td>Max</td><td>'.max($gilPoints).'</td></tr>
<tr><td>Min</td><td>'.min($gilPoints).'</td></tr>
<tr><td>Average</td><td>'.round($average,0).'</td></tr>
<tr><td>Last</td><td>'.$gilPoints[0].'</td></tr>
<tr><td>Per Day</td><td>'.round($averageperday,2).'</td></tr>
</table>

</td><td valign=top with=40%">';

     
    ?>
    <script>
    window.onload = function () {
     
    var chart = new CanvasJS.Chart("chartContainer", {
    	title: {
    		text: "Sale History"
    	},
    	axisY: {
    		title: "Gil"
    	},
    	data: [{
    		type: "line",
    		dataPoints: <?php echo json_encode($dataPoints, JSON_NUMERIC_CHECK); ?>
    	}]
    });
    chart.render();
     
    }
    </script>
<script src="https://cdn.canvasjs.com/canvasjs.min.js"></script>
<?php
echo '<div id="chartContainer"></div>
</td></tr></table>
';
}
		  }
?>
