<?php
$assetid = 0;
if (isset($_REQUEST['asset'])) $assetid = $_REQUEST['asset'];

$propid = 0;
if (isset($_REQUEST['property'])) $propid = $_REQUEST['property'];

require_once($installfolder.'/model/property.php');
require_once($installfolder.'/model/relationship.php');
$propset = Properties::GetAllProperties();
$relset = Relationships::GetAllRelationships();
$activeprop = FALSE;
foreach($propset as $px)
{
	if ($propid == $px->id)
	{
		$activeprop = $px;
		break;
	}
}

$assetid = filter_var($assetid,FILTER_VALIDATE_INT);
if ($assetid === FALSE) exit();

$range = FALSE;
if (isset($_REQUEST['range']))
{
	if ($_REQUEST['range'] != 'live')
	{
		$range = $_REQUEST['range'];
	}
}

//print_r($_REQUEST);

$rext = "";
if ($range !== FALSE)
{
	$rext = "&range=".$range;
}

$thisass = new Asset($assetid);
$nm = $thisass->ern;
if ($nm == "")
{
	$nm = $thisass->name;
}

$panel = FALSE;
if (isset($_REQUEST['panel']))
{
	$panel = $_REQUEST['panel'];
}

$json = json_decode($_SESSION['structure']);
$apanel = $json->panels[$panel];

global $asset;

if (isset($_REQUEST['url'])) {?>
<menu class="menu show-menu">  
  <li class="menu-item">
  <?php if (($range != "") && ($range !== FALSE))
  {
	  $url = $_REQUEST['url'];
	  $px = strpos($url,"&range");
	  //echo $url;
	  if ($px !== FALSE)
	  {
		  $url = substr($url,0,$px);
	  }
	  //echo $url;
	  ?>
	 <button type="button" class="menu-btn" onclick="GoTo('<?php echo $url;?>');"> <span class="menu-text">View Live</span> </button><?php 
  }
  else
  {?>
    <button type="button" class="menu-btn" onclick="GoTo('<?php echo $_REQUEST['url'];?>&range=1800');"> <span class="menu-text">View Time Series</span> </button><?php
  }?>
  </li>
  <li class="menu-item submenu">
    <button type="button" class="menu-btn">  <span class="menu-text">Scale...</span> </button>
	<menu class="menu">     
	  <li class="menu-item">
		<button type="button" class="menu-btn" onclick="ChangeScaling(<?php echo $_REQUEST['panel'];?>,'full');"> <span class="menu-text">Full Range</span> </button>
	  </li>	
	  <li class="menu-item">
		<button type="button" class="menu-btn" onclick="ChangeScaling(<?php echo $_REQUEST['panel'];?>,'auto');">  <span class="menu-text">Auto</span> </button>
	  </li>
	</menu>
  </li>   
  <li class="menu-item submenu">
    <button type="button" class="menu-btn">  <span class="menu-text">Show As...</span> </button>
	<menu class="menu">     
	  <li class="menu-item">
		<button type="button" class="menu-btn" onclick="ChangeStyle(<?php echo $_REQUEST['panel'];?>,'barchart');"> <span class="menu-text">Bar Chart</span> </button>
	  </li>	
	  <li class="menu-item">
		<button type="button" class="menu-btn" onclick="ChangeStyle(<?php echo $_REQUEST['panel'];?>,'discrete');"> <span class="menu-text">Discrete Chart</span> </button>
	  </li>
	  <li class="menu-item">
		<button type="button" class="menu-btn" onclick="ChangeStyle(<?php echo $_REQUEST['panel'];?>,'gauge');"> <span class="menu-text">Gauge</span> </button>
	  </li>
	  <li class="menu-item">
		<button type="button" class="menu-btn" onclick="ChangeStyle(<?php echo $_REQUEST['panel'];?>,'line');">  <span class="menu-text">Line Graph</span> </button>
	  </li>
	  <li class="menu-item">
		<button type="button" class="menu-btn" onclick="ChangeStyle(<?php echo $_REQUEST['panel'];?>,'text');"> <span class="menu-text">Text</span> </button>
	  </li>
	  <li class="menu-item">
		<button type="button" class="menu-btn" onclick="ChangeStyle(<?php echo $_REQUEST['panel'];?>,'radar');"> <span class="menu-text">Radar</span> </button>
	  </li>
	</menu>
  </li>   
  <li class="menu-item submenu">
    <button type="button" class="menu-btn">  <span class="menu-text">Merge With</span> </button>
	<menu class="menu">     
	  <?php
	  $structure = $_SESSION['structure'];
	  $json = json_decode($structure);
	  $counter = -1;
	  foreach($json->panels as $px)
	  {
		  $counter++;
		  if ($_REQUEST['panel'] == $counter) continue;
		  ?>
	  <li class="menu-item">
		<button type="button" class="menu-btn" onclick="CombinePanel(<?php echo $_REQUEST['panel'];?>,<?php echo $counter;?>);$('#moremenu').fadeOut(250);"> <span class="menu-text"><?php echo $px->name;?></span> </button>
	  </li>
		  <?php
	  }
	  ?>
	</menu>
  </li>  <?php if (count($apanel->points) > 1) { ?>
  <li class="menu-item">
    <button type="button" class="menu-btn" onclick="SplitPanel(<?php echo $_REQUEST['panel'];?>);$('#moremenu').fadeOut(250);"> <span class="menu-text">Split</span> </button>
  </li><?php }; ?> 
  <li class="menu-item">
    <button type="button" class="menu-btn"> <span class="menu-text" onclick="RenamePanel(<?php echo $_REQUEST['panel'];?>);$('#moremenu').fadeOut(250);">Rename</span> </button>
  </li>
  <li class="menu-item">
    <button type="button" class="menu-btn" onclick="DeleteChannel(<?php echo $_REQUEST['panel'];?>,-1,-1);"> <span class="menu-text">Remove</span> </button>
  </li>
  <li class="menu-item">
    <button type="button" class="menu-btn" onclick="$('#moremenu').fadeOut(250);">  <span class="menu-text">Cancel</span> </button>
  </li>
</menu>
<?php
	exit();
}
?>

<menu class="menu show-menu">
	<li class="menu-item submenu">
    <button type="button" class="menu-btn">  <span class="menu-text">Show As...</span> </button>
	<menu class="menu">     
	  <li class="menu-item">
		<button type="button" class="menu-btn" onclick="ChangeStyle(<?php echo $_REQUEST['panel'];?>,'barchart');"> <span class="menu-text">Bar Chart</span> </button>
	  </li>	
	  <li class="menu-item">
		<button type="button" class="menu-btn" onclick="ChangeStyle(<?php echo $_REQUEST['panel'];?>,'discrete');"> <span class="menu-text">Discrete Chart</span> </button>
	  </li>
	  <li class="menu-item">
		<button type="button" class="menu-btn" onclick="ChangeStyle(<?php echo $_REQUEST['panel'];?>,'gauge');"> <span class="menu-text">Gauge</span> </button>
	  </li>
	  <li class="menu-item">
		<button type="button" class="menu-btn" onclick="ChangeStyle(<?php echo $_REQUEST['panel'];?>,'line');">  <span class="menu-text">Line Graph</span> </button>
	  </li>
	  <li class="menu-item">
		<button type="button" class="menu-btn" onclick="ChangeStyle(<?php echo $_REQUEST['panel'];?>,'text');"> <span class="menu-text">Text</span> </button>
	  </li>
	</menu>
  </li>   
  <li class="menu-item submenu">
    <button type="button" class="menu-btn">  <span class="menu-text">Merge With</span> </button>
	<menu class="menu">     
	  <?php
	  $structure = $_SESSION['structure'];
	  $json = json_decode($structure);
	  $counter = -1;
	  foreach($json->panels as $px)
	  {
		  $counter++;
		  if ($_REQUEST['panel'] == $counter) continue;
		  ?>
	  <li class="menu-item">
		<button type="button" class="menu-btn" onclick="CombineChannel(<?php echo $assetid;?>,<?php echo $propid; ?>,<?php echo $_REQUEST['panel'];?>,<?php echo $counter;?>);$('#moremenu').fadeOut(250);"> <span class="menu-text"><?php echo $px->name;?></span> </button>
	  </li>
		  <?php
	  }
	  ?>
	</menu>
  </li>  
  <li class="menu-item">
    <button type="button" class="menu-btn" onclick="SplitChannel(<?php echo $assetid;?>,<?php echo $propid; ?>,<?php echo $_REQUEST['panel'];?>);$('#moremenu').fadeOut(250);"> <span class="menu-text">Split</span> </button>
  </li>  
  <!--<li class="menu-item">
    <button type="button" class="menu-btn"> <span class="menu-text">Rename</span> </button>
  </li>-->
  <li class="menu-item">
    <button type="button" class="menu-btn" onclick="DeleteChannel(<?php echo $_REQUEST['panel'];?>,<?php echo $assetid;?>,<?php echo $propid;?>);"> <span class="menu-text">Remove</span> </button>
  </li>
  <li class="menu-item">
    <button type="button" class="menu-btn" onclick="$('#moremenu').fadeOut(250);"> <span class="menu-text">Cancel</span> </button>
  </li>
</menu>