<?php


class TableJSDataTable {

	private $_tbl = '';
	private $_cnf = '';
	private $_htmlID = '';
	private $_dataSource = null;

	function __construct($tblName, $tblConf) {
		$this->_tbl = $tblName;
		$this->_cnf = $tblConf;
		$this->_htmlID = $tblName;//uniqid($tblName);
		Lib::loadClass('Data_Source');
		$this->_dataSource = new Data_Source();
		$this->_dataSource->set_table($this->_tbl);
	}

	function getHtmlID() {
		return $this->_htmlID;
	}

	function render() {
		$visible_cols = $this->_cnf->getVisibleFieldList();

		$colModel = array();
		foreach ($visible_cols as $name) {
			$colModel[] = (object)array('display'=>$name, 'name'=>$name, 'width'=>strlen($name) * 5, 'sortable'=>true, 'align'=>'left');
		}

		$out = '';

		//$out .= '<link rel="stylesheet" href="stuff/flexigrid/css/flexigrid.css" type="text/css" />' . "\n";
		$out .= '<script src="stuff/jquery.dataTables.min.js"></script>' . "\n";

		$out .= '<form id="' . $this->_htmlID . 'Frm" style="display:none">' . "\n";
			$out .= 'Value 1 : <input type="text" name="val1" value="" autocomplete="off" /><br />' . "\n";
			$out .= 'Value 2 : Is a hidden input with value 3<input type="hidden" name="val2" value="3" /><br />' . "\n";
			$out .= 'Value 3 : ' . "\n";
			$out .= '<select name="val3">' . "\n";
				$out .= '<option value="1">One</option>' . "\n";
					$out .= '<option value="2">Two</option>' . "\n";
					$out .= '<option value="3">Three</option>' . "\n";
					$out .= '<option value="4">Four</option>' . "\n";
					$out .= '<option value="5">Five</option>' . "\n";
			$out .= '</select><br />' . "\n";
			$out .= 'Value 4 : <input type="checkbox" name="val4" id="val4" value="4" /><label for="val4">This will pass a value 4 if checked</label>' . "\n";
			$out .= '</p>' . "\n";
			$out .= '<p><input type="submit" value="Submit" /></p>' . "\n";
		$out .= '</form>';

		$out .= '<table id="' . $this->_htmlID . '" cellpadding="0" cellspacing="0" border="1" class="tbl-view">' . "\n";
		$out .= '<thead>';
			$out .= '<tr>';
			foreach ($visible_cols as $name) {
				$out .= '<th>' . $name . '</th>';
			}
			$out .= '</tr>';
		$out .= '</thead>';
//		$out .= '<tfoot>';
//		$out .= '</tfoot>';
		$out .= '<tbody>';
		$out .= '</tbody>';
		$out .= '</table>' . "\n";
		$out .= '<script>' . "
$(document).ready(function() {
	var oTable = $('#{$this->_htmlID}').dataTable({
		'bProcessing': true,
		'sAjaxSource': 'index-ajax.php?_tbl={$this->_tbl}'
	} );
} );
		" . '</script>' . "\n";
		return $out;
	}

}
