<?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;
}

global $asset;

if (isset($_REQUEST['url'])) 
{
	$url = $_REQUEST['url'];
	
	$propertylist = "";
	$structure = $_SESSION['structure'];
	$json = json_decode($structure);
	$panel = $_REQUEST['panel'];
	$presentprops = array();
	foreach($json->panels[$panel]->points as $pnt)
	{
		if (!in_array($pnt->property,$presentprops))
		{
		
			if ($propertylist != "")
			{
				$propertylist .= ",";
			}
			$propertylist .= $pnt->property;
			$presentprops[] = $pnt->property;
			
		}
	}	
	//echo 'Working With URL...';
	
	?>
<menu class="menu show-menu">  
  <li class="menu-item">
  <?php if (($range != "") && ($range !== FALSE))
  {	  
	  $exurl = $url;
	  $px = strpos($exurl,"&range");	  
	  if ($px !== FALSE)
	  {
		  $exurl = substr($exurl,0,$px);
	  }
	  $exurl = $exurl."&properties=".$propertylist;	 
	  //echo $url;
	  ?>
	 <button type="button" class="menu-btn" onclick="GoTo('<?php echo $exurl;?>');"> <span class="menu-text">View Live</span> </button>
	 <?php 
  }
  else
  {
	  ?>
    <button type="button" class="menu-btn" onclick="GoTo('<?php echo $url;?>&properties=<?php echo $propertylist;?>&range=30min');"><span class="menu-text">View Time Series</span> </button><?php
  }?>
  </li>
  
  <li class="menu-item">
  <?php 
	  $nwurl = $url;
	  $prp = strpos($nwurl,"properties=");
	  if ($prp === FALSE)
	  {
		 $nwurl .= "&properties=".$propertylist;
	  }
	  else
	  {
		  $en = strpos($nwurl,"&",$prp);
		  if ($en === FALSE)		  
			  $nwurl = substr($nwurl,0,$prp).'properties='.$propertylist;		  
		  else
			  $nwurl = substr($nwurl,0,$prp).'properties='.$propertylist.substr($nwurl,$en);
	  }
	  
	  $prp = strpos($nwurl,"id=");
	  if ($prp === FALSE)
	  {
		 $nwurl .= "&id=".$_REQUEST['base'];
	  }
	  else
	  {
		  $en = strpos($nwurl,"&",$prp);
		  if ($en === FALSE)		  
			  $nwurl = substr($nwurl,0,$prp).'id='.$_REQUEST['base'];		  
		  else
			  $nwurl = substr($nwurl,0,$prp).'id='.$_REQUEST['base'].substr($nwurl,$en);
	  }
	  ?>
	 <button type="button" class="menu-btn" onclick="GoTo('<?php echo $nwurl;?>');"> <span class="menu-text">By Itself</span> </button>

  </li>
  
  
  <li class="menu-item submenu">
    <button type="button" class="menu-btn"> <span class="menu-text">Report Style...</span> </button>
	<menu class="menu">		
		<?php 
		
		$rangeextension = "";
		if ($range === FALSE) {
			$rangeextension = "range=1800&";
		}
		
			if (count($presentprops) > 1) { ?>
			<li class="menu-item">
			<button type="button" class="menu-btn" onclick="GoToReport('style=pcc&<?php echo $rangeextension;?>channels=[PANEL<?php echo $_REQUEST['panel'];?>]');">  <span class="menu-text">Parallel Coordinates
			</span> </button></li><?php }; ?>
			
			<li class="menu-item">
			<button type="button" class="menu-btn" onclick="GoToReport('style=heatmap&<?php echo $rangeextension;?>channels=[PANEL<?php echo $_REQUEST['panel'];?>]');">  <span class="menu-text">Heatmap
			</span> </button></li>
			<?php if ($range === FALSE) { ?>
			<li class="menu-item">
			<button type="button" class="menu-btn" onclick="GoToReport('style=treemap&channels=[PANEL<?php echo $_REQUEST['panel'];?>]');">  <span class="menu-text">Treemap
			</span> </button></li>
			<?php 
			}
			if (count($presentprops) == 2) { ?>
			<li class="menu-item">
			<button type="button" class="menu-btn" onclick="GoToReport('style=scatter&<?php echo $rangeextension;?>channels=[PANEL<?php echo $_REQUEST['panel'];?>]');">  <span class="menu-text">Scatter Plot
			</span> </button></li><?php }; ?>		
	</menu>
  </li>
  <?php
  //};
  
// echo 'Boo!';
$existing = array();
$pieces = explode('&',$url);
foreach($pieces as $p)
{
	$bits = explode('=',$p);
	if ($bits[0] == 'propertyid')
	{
		$existing = explode(',',$bits[1]);
	}
	if ($bits[0] == 'origin')
	{
		if (isset($_REQUEST['base']))
		{
			if ($_REQUEST['base'] != "")
			{
				//echo 'Base Specified!';
				$bits[1] = $_REQUEST['base'];
			}
		}
		
		$anyothers = false;
		
		//echo 'Checking For More!';
		
		$id = $bits[1];
		$asx = new Asset($id);
		$props = $asx->GetLocalProperties();
		foreach($props as $px)
		{
			$valid = false;
			switch($px->type)
			{
				case 'MEASUREMENT':
				case 'ENUM':
				case 'STATUS':
				case 'LOOKUP':
				case 'TEXT':
					$valid = true;
					break;
			}			
			if ($valid == true)
			{				
				if (!in_array($px->id,$existing))
				{
					//echo 'DDDDAAA Found Property: '.$px->name;
					
					$revisedurl = $url;
					$revisedurl = str_replace("propertyid=".implode(",",$existing),"propertyid=".implode(",",$existing).",".$px->id,$revisedurl);
					
					if ($anyothers == false)
					{?>
<li class="menu-item submenu also">
    <button type="button" class="menu-btn" onclick="$('#moremenu').fadeOut(250);"> <span class="menu-text">Also Show...</span> </button>
	<menu class="menu">				
				<?php
						$anyothers = true;
					}
					
					?>
		<li class="menu-item">
			<button type="button" class="menu-btn" onclick="GoTo('<?php echo $revisedurl;?>');"><span class="menu-text"><?php echo $px->name;?></span> </button>
		</li><?php
				}
			}
		}
		if ($anyothers == true)
		{
		?>		
	</menu>
  </li><?php
		}
  	}
}
  ?>
  <?php if ($range !== FALSE) {
	if (in_array('timeline',$addins->enabledaddons))
	{	
		$rng = explode(' to ',$range);?>
  <li class="menu-item">
    <button type="button" class="menu-btn" onclick="window.location='<?php echo $siteroot;?>/timeline/review?start=<?php echo $rng[0];?>&end=<?php echo $rng[1];?>';"> </i> <span class="menu-text">Describe Events</span> </button>
  </li>
  <?php
	}
  }?>
  <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">
<?php
	if (isset($_REQUEST['range']))
	{
		if (($_REQUEST['range'] != "") && ($_REQUEST['range'] != "live"))
		{ ?>
<li class="menu-item">
    <button type="button" class="menu-btn" onclick="GoTo('id=<?php echo $assetid;?>&range=<?php echo $_REQUEST['range'];?>');"> <span class="menu-text">Zoom In On Time</span></button>
  </li>
			<?php
		}
	}
?>
  <li class="menu-item">
    <button type="button" class="menu-btn" onclick="GoTo('id=<?php echo $assetid;?>');"> <span class="menu-text">View Asset</span></button>
  </li>
  <li class="menu-item">
    <button type="button" class="menu-btn" onclick="GoTo('id=<?php echo $assetid;?>&property=<?php echo $activeprop->id;?>');"> <span class="menu-text">By Itself</span></button>
  </li>
  <li class="menu-item">
  <?php if ($range !== FALSE)
  {?>
	 <button type="button" class="menu-btn" onclick="GoTo('id=<?php echo $assetid;?>&property=<?php echo $activeprop->id;?>');"> <span class="menu-text">View Live</span> </button><?php 
  }
  else
  {?>
    <button type="button" class="menu-btn" onclick="GoTo('id=<?php echo $assetid;?>&property=<?php echo $activeprop->id;?>&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">Show All...</span> </button>
	<menu class="menu">     
		<li class="menu-item submenu">
            <button type="button" class="menu-btn"> <span class="menu-text">Inside...</span> </button>
			<menu class="menu">
				<li class="menu-item">
					<button type="button" class="menu-btn" onclick="GoTo('propertyid=<?php echo $activeprop->id; ?>&related=2&origin=<?php echo $thisass->id;?>&depth=X<?php echo $rext;?>');"><span class="menu-text"><?php echo $nm;?></span> </button>
				</li><?php
		$query = $db->query('SELECT parentasset FROM assetrelclosures WHERE relationship=2 AND childasset='.$assetid.' ORDER BY depth DESC');
		
		//print('SELECT node,assetid,propertyid FROM datalinks WHERE profile='.$sampleprofile.' AND assetid IN ('.implode(',',$alist).') AND propertyid IN ('.implode(',',$proplist).')');

		while($row = $query->fetch(PDO::FETCH_ASSOC)) 
		{
			if ($row['parentasset'] == 2) continue;
			$asx = new Asset($row['parentasset']); ?>
			<li class="menu-item">
					<button type="button" class="menu-btn" onclick="GoTo('propertyid=<?php echo $activeprop->id; ?>&related=2&origin=<?php echo $row['parentasset'];?>&depth=X<?php echo $rext;?>');"> <span class="menu-text"><?php echo $asx->name;?></span> </button>
				</li><?php
		}
?>				
			</menu>
			
	  
	  <?php 
			global $asset;
			if (isset($asset[$assetid]['relationships']))
			{
			
			foreach($asset[$assetid]['relationships'] as $k => $v) { 
				if ($k == 3) continue;
				if ($k == 2) continue;
				$thisrel = FALSE;
				foreach($relset as $rx)
				{
					if ($rx->id == $k)
					{
						$thisrel = $rx;
					}
				}?>
	      <li class="menu-item submenu">
            <button type="button" class="menu-btn"> <span class="menu-text">By <?php echo $thisrel->name;?>...</span> </button>
			<menu class="menu">
				  <li class="menu-item">
					<button type="button" class="menu-btn" onclick="GoTo('propertyid=<?php echo $activeprop->id; ?>&related=<?php echo $k; ?>&origin=<?php echo $assetid;?>&depth=X<?php echo $rext;?>&all=true');"> <span class="menu-text">All</span> </button>
				  </li>          
				  <li class="menu-item">
					<button type="button" class="menu-btn" onclick="GoTo('propertyid=<?php echo $activeprop->id; ?>&related=<?php echo $k; ?>&origin=<?php echo $assetid;?>&depth=X<?php echo $rext;?>');"> <span class="menu-text">Downstream</span> </button>
				  </li>
				  <li class="menu-item submenu">
					<button type="button" class="menu-btn"> <span class="menu-text">Nearby</span> </button>
					<menu class="menu">
					<li class="menu-item">
						<button type="button" class="menu-btn" onclick="GoTo('propertyid=<?php echo $activeprop->id; ?>&related=<?php echo $k; ?>&origin=<?php echo $assetid;?>&depth=1<?php echo $rext;?>&all=true');"> <span class="menu-text">x1</span> </button>
					</li> 
					<li class="menu-item">
						<button type="button" class="menu-btn" onclick="GoTo('propertyid=<?php echo $activeprop->id; ?>&related=<?php echo $k; ?>&origin=<?php echo $assetid;?>&depth=2<?php echo $rext;?>&all=true');"> <span class="menu-text">x2</span> </button>
					</li> 
					<li class="menu-item">
						<button type="button" class="menu-btn" onclick="GoTo('propertyid=<?php echo $activeprop->id; ?>&related=<?php echo $k; ?>&origin=<?php echo $assetid;?>&depth=4<?php echo $rext;?>&all=true');"> <span class="menu-text">x4</span> </button>
					</li> 
					</menu>
				  </li>
				  
				  
			</menu>
          </li>   
			<?php }; }; ?>	  	  
		
				<?php
		$query = $db->query('SELECT parentasset FROM assetrelclosures WHERE relationship=3 AND childasset='.$assetid.' ORDER BY depth DESC');
		
		$first = true;
		$processed = [];
		while($row = $query->fetch(PDO::FETCH_ASSOC)) 
		{			
			if ($row['parentasset'] == 3) continue;
			if (in_array($row['parentasset'],$processed))
				$processed[] = $row['parentasset'];
			if ($first == true)
			{
				?>
		<li class="menu-item submenu">
            <button type="button" class="menu-btn"><span class="menu-text">Of Type...</span> </button>
			<menu class="menu">	<?php $first = false; };			
			$asx = new Asset($row['parentasset']); ?>
			<li class="menu-item">			
					<button type="button" class="menu-btn" onclick="GoTo('propertyid=<?php echo $activeprop->id; ?>&related=3&origin=<?php echo $row['parentasset'];?>&depth=X<?php echo $rext;?>');">  <span class="menu-text"><?php echo $asx->name;?></span> </button>
				</li><?php
		}
		if ($first == false)
		{
			echo '</menu></li>';
		}
?>				
	  <li class="menu-item">
		<button type="button" class="menu-btn" onclick="GoTo('propertyid=<?php echo $activeprop->id; ?>&related=2&origin=1&depth=X<?php echo $rext;?>');">  <span class="menu-text">Anywhere</span> </button>
	  </li>            
	</menu>
  </li>
  
  <?php if (($activeprop !== FALSE)) { ?>
  <li class="menu-item submenu">
    <button type="button" class="menu-btn"> <span class="menu-text">Compare Against...</span> </button>
	<menu class="menu">	
		<li class="menu-item">
				<button type="button" class="menu-btn" onclick="GoToReport('style=xbr&channels=<?php echo $assetid.':'.$activeprop->id;?><?php if ($range === FALSE) echo '&range=1800';?>');">  <span class="menu-text">Itself</span></button>
		</li>
		<?php
	$scanset = array();
	$scanset[] = $assetid;
	$touched = array();
	
	$isdiscrete = false;
	if (($activeprop->type == "STATUS") || ($activeprop->type == "ENUM"))
		$isdiscrete = true;
	
	while(count($scanset) > 0)
	{		
		$ast = array_pop($scanset);
		if (in_array($ast,$touched)) continue;		
		$ass = new Asset($ast);
		$touched[] = $ast;
		
		$dta = $ass->GetLocalProperties();
		foreach($dta as $px)
		{
			if (($ast == $assetid) && ($activeprop->id == $px->id))			
			{
				continue;	
			}
			
			if (($px->type == "MEASUREMENT") || ($px->type == "STATUS") || ($px->type == "ENUM"))
			{
				$compchannels = $assetid.':'.$activeprop->id.','.$ast.':'.$px->id;
				$alsodiscrete = false;
				if (($px->type == "STATUS") || ($px->type == "ENUM"))
					$alsodiscrete = true;
	
				$style = "scatter";
				if (($isdiscrete == true) && ($alsodiscrete == false))
					$style='breakout';
				if (($isdiscrete == false) && ($alsodiscrete == true))
				{
					$compchannels = $ast.':'.$px->id.','.$assetid.':'.$activeprop->id;
					$style='breakout';
				}
				if (strpos(strtolower($px->name),"target") !== FALSE) $style="xbar";
				if (strpos(strtolower($px->name),"setpoint") !== FALSE) $style="xbar";
				if (strpos(strtolower($px->name),"set point") !== FALSE) $style="xbar";
				?>
		<li class="menu-item">
			<button type="button" class="menu-btn" onclick="GoToReport('style=<?php echo $style; ?>&channels=<?php echo $compchannels;?><?php if ($range === FALSE) echo '&range=1800';?>');">  <span class="menu-text"><?php 
			if ($ast != $assetid) 
				echo $ass->name.' ';
			
					
			$nm = $px->name;
			$bits = explode(' - ',$px->name);
			if (count($bits) > 1)
				$nm = $bits[1].' '.$bits[0];
			echo $nm;?></span> </button>
		</li><?php
			}
		}
		
		foreach($asset[$ast]['relationships'][2] as $rx)
		{
			if ($rx['upstream'] == true)
			{
				$scanset[] = $rx['target'];
			}
		}
	}?>
	</menu>
  </li>
  <?php
  };
   
  if ($activeprop !== FALSE)
  {
	  
	$propcategory = "";
	$bits = explode(' - ',$activeprop->name);
	if (count($bits) > 1)
	{
		$propcategory = $bits[0];
		$matches = array();
  ?>
	<?php 
	//$propset = Properties::GetAllProperties();
		$props = $thisass->GetLocalProperties();
		
		//print_r($props);
		foreach($props as $px)
		{		
			//if ($px->id == $activeprop->id) continue;
			$propname = $px->name;
			$bits = explode(' - ',$px->name);
			if (count($bits) > 1)
			{				
				$propname = $bits[1].' '.$bits[0];
				if ($bits[0] == $propcategory)
				{
					$doit = false;
					switch($px->type)
					{
						case "MEASUREMENT":
						case "LOOKUP":
						case "ENUM":
						case "TEXT":
						case "STATUS":
						   $doit = true;
						   break;			   
					}
					if ($doit == false) continue;
					$matches[] = $px;?>			
			<?php
				}
			}
			
		}
		if (count($matches) > 0)
		{
			$matchstring = "";
			foreach($matches as $px)
			{
				if ($matchstring != "") $matchstring .= ",";
				$matchstring .= $px->id;
			}?>
			<li class="menu-item">
				<button type="button" class="menu-btn" onclick="GoTo('properties=<?php echo $matchstring; ?>&id=<?php echo $assetid;?><?php echo $rext;?>');">  <span class="menu-text">Focus On <?php echo $propcategory;?></span> </button>
			</li><?php
		}
	}
  }	?>
  <li class="menu-separator"></li>  
  
  <li class="menu-item">
    <button type="button" class="menu-btn" onclick="$('#moremenu').fadeOut(250);"> </i> <span class="menu-text">Cancel</span> </button>
  </li>
</menu>