<?php
class TableList{
	
	var $rows;
	var $headers;
	var $current_row;
	var $rowURL = false;
	var $openDiag = false;
	var $opt_menu = false;
	var $is_simple;
	
	function __construct($simple_list=false){
		$this->is_simple = $simple_list;
	}

	function addHeader(){
		foreach(func_get_args() as $k=>$v){
			$this->headers[$k]['name'] = $v[0];
			$this->headers[$k]['size'] = $v[1];
		}
		$this->n_cols = count($this->headers);
	}
	
	function addCol($value){
		$this->current_row[] = $value;
		if(count($this->current_row) == $this->n_cols){
			$this->rows[] = $this->current_row;
			unset($this->current_row);
		}
	}
	
	function output($pagination=false){
		
		$table  = '<table class="table table-list">';
		
		$table .= '<thead><tr>';
		
		foreach($this->headers as $v)
			$table .= '<th class="txt-overflow '.$v['size'].' bg-primary">'.$v['name'].'</th>';
		
		$table .= '</tr></thead><tbody>';		


		foreach($this->rows as $k=>$v){
			
			//row click
			if($this->rowURL[$k]){
				if(!$this->openDiag)
					$tr = '<tr onClick="window.location=\''.$this->rowURL[$k].'\'">';
				else
					$tr = '<tr onClick="dialog(\''.$this->rowURL[$k].'\')">';
			}/*else if($this->rowDiag[$k])
				$tr = '<tr data-open="%s" data-url="%s" data-small="%s">';*/

			$table .= $tr;
			
			foreach($v as $k=>$f)
				$table .= '<td class="txt-overflow '.$this->headers[$k]['size'].'">'.$f.'</th>';
				
			$table .= '</tr>';
		}


/*
	foreach($list['list'] as $id=>$field){
		
		//open dialog onclick
		if($field['diag'])
			$tr_opt = ' data-open="'.$field['diag']['data-open'].'" data-url="'.$field['diag']['data-url'].'" data-title="'.$field['diag']['data-title'].'" data-large="'.$field['diag']['data-large'].'"';
		else if($field['href'])
			$tr_opt = ' onClick="window.location=\''.$field['href'].'\'"';
		
		echo '<tr'.$tr_opt.'>';
		
		foreach($list['table-header'] as $record=>$v)
			echo '<td class="'.$v['w'].'">'. $field[$record] .'</td>';
		echo '</tr>';
	}
*/	
		$table .= '</tbody></table>';
		
		if($this->is_simple){
			echo $table;
		}else{
			echo '<div class="panel panel-default">'.$table.'</div>';
		}
		echo $pagination;
	}
	
	function setRowUrl($link,$is_diag=false){
		$this->rowURL[] = $link;
		$this->openDiag = $is_diag;
	}
		
	
}
?>