<?php


class TableFlexigrid {

	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/flexigrid/js/flexigrid.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 . '" style="display:none"></table>' . "\n";
		$out .= '<script>' . "
(function ($) {
	$.fn.flexGetOptions = function (p) {
		return this.each(function () {
			console.log('flexGetOptions..');
			if (this.grid) {
				console.log('this.grid');
				console.log(this.grid);
				console.log('this.p');
				console.log(this.p);
			}
		});
	};
})(jQuery);
		" . '</script>' . "\n";
		$out .= '<script>' . "
if (undefined === window.grids) {
	window.grids = [];
}
window.grid = jQuery('#{$this->_htmlID}').flexigrid({
	url: 'index-ajax.php?_tbl={$this->_tbl}',
	dataType: 'json',
//	colModel : [
//		{display: 'ISO', name : 'iso', width : 40, sortable : true, align: 'center'},
//		{display: 'Name', name : 'name', width : 180, sortable : true, align: 'left'},
//		{display: 'Printable Name', name : 'printable_name', width : 120, sortable : true, align: 'left'},
//		{display: 'ISO3', name : 'iso3', width : 130, sortable : true, align: 'left', hide: true},
//		{display: 'Number Code', name : 'numcode', width : 80, sortable : true, align: 'right'}
//	],
	colModel : " . json_encode($colModel) . ",
	buttons : [
		{name : 'Add', bclass : 'add', onpress : test },
		{name : 'Delete', bclass : 'delete', onpress : test },
		{separator : true}
	],
	searchitems : [
		{display: 'ISO', name : 'iso'},
		{display: 'Name', name : 'name', isdefault: true}
	],
	sortname: 'iso',
	sortorder: 'asc',
	usepager: true,
	title: '{$this->_tbl}',
	useRp: true,
	rp: 15,
	showTableToggleBtn: true,
	onSubmit: addFormData,
	width: 700,
	height: 200
});

function test(com, grid) {
	if (com == 'Delete') {
		confirm('Delete ' + $('.trSelected', grid).length + ' items?')
	} else if (com == 'Add') {
		alert('Add New Item');
	}
}

//This function adds paramaters to the post of flexigrid. You can add a verification as well by return to false if you don't want flexigrid to submit			
function addFormData(){
	//passing a form object to serializeArray will get the valid data from all the objects, but, if the you pass a non-form object, you have to specify the input elements that the data will come from
	var dt = $('#{$this->_htmlID}Frm').serializeArray();
	$('#{$this->_htmlID}').flexOptions({params: dt});
	return true;
}

$('#{$this->_htmlID}Frm').submit(function (){
	$('#{$this->_htmlID}').flexOptions({newp: 1}).flexReload();
	return false;
});
		" . '</script>' . "\n";
		return $out;
	}

}
