$.widget( "fitness.transferList", {
	options: {
		transfers: [],
		url_delete : '',
		url_pay : '',
		url_pay_all_visible: '',
		name_transfers: '',//name to use for transfer id in form
		id_form: '',
		columns: [
		          
		   { 'label' : 'Idő' },
		   { 'label' : 'Termék' },
		   { 'label' : 'Ár' },
		   { 'label' : 'Db' },
		   { 'label' : 'Összesen' },
		   { 'label' : '' },
		 ],
		 footers: [
		    { 
		    	'label' : 'Összesen',
		    	'total' : function(transfers){
		    		var totals;
		    		totals = 0;
		    		for ( var i = 0; i < transfers.length ; i++ ){
		    			totals += transfers[i].money;
		    		}
		    		return totals;
		    	},
		    	'count' : function(transfers){
		    		var totals;
		    		totals = 0;
		    		for ( var i = 0; i < transfers.length ; i++ ){
		    			totals += transfers[i].count;
		    		}
		    		return totals;
		    	},
            }
		 ]
	},
	_create: function() {
		var o;
		o = this.options;
		this._refresh();
	},
	_refresh: function(){
		var s;
		s = this._render('table',{ 'columns' : this.options.columns, 'transfers' : this.options.transfers,'footer': this.options.footers });
		s += this.makeForm();
		this.element.html(s);
	},
	 
	_render: function(template,params){
		var s;
		var columsn;
		var transfer;
		var footer;
		s = "";
			switch(template){
			case 'table':
				s += this._render('openTable', {});
				s += this._render('header', { 'columns'   : params.columns   });
				s += this._render('body'  , { 'transfers' : params.transfers });
				s += this._render('footer'  , { 'transfers' : params.transfers ,'footers': this.options.footers});
				s += this._render('closeTable', { });
				break;
			case 'openTable':
				s += '<table class="table table-bordered table-striped table-transfers">';
				break;
			case 'closeTable':
				s += '</table>';
				break;
			case 'header':
				s += '<thead>'
				columns = params.columns;
				for(var i = 0; i < columns.length; i++){
					s += this._render('headrow', { 'column' : columns[i] })
				}
				s += '</thead>';
				break;
			case 'headrow':
				s += '<th>';
				s += params.column.label;
				s += '</th>';
				break;
			case 'body':
				s += '<tbody>';
				for( var i = 0; i < params.transfers.length; i++){
					s += this._render('row', { 'transfer' : params.transfers[i] })
				}
				s += '</tbody>';
				break;
			case 'row':
				transfer = params.transfer;
				s += '<tr>';
				s += '<td class="product-time">';
				s += transfer.time;
				s += '</td>';
				s += '<td class="product-name">';
				s += transfer.product_name;
				s += '</td>';
				s += '<td class="sale-price">';
				s += transfer.item_price;
				s += '</td>';
				s += '<td class=\'product-count\' >';
				s += transfer.count;
				s += '</td>';
				s += '<td class=\'product-money\'  >';
				s += transfer.money;
				s += '</td>';
				s += '<td class=\'product-action\'  style="width: 110px;" >';
				s += '<form action="'+this.options.url_pay +"&id="+ transfer.id_transfer +'" method="POST" >';
				s +=  "<button type='submit' class='btn btn-success pull-left' style='margin-right: 3px;' title='Tranzakció kifizetve' ><span  style='font-size: 10px;' class= 'glyphicon glyphicon-eur\'></span></button>";
				s += '</form>';
				s += '<form action="'+this.options.url_delete +"&id="+ transfer.id_transfer +'" method="POST">';
				s +=  "<button type='submit' class='btn btn-danger pull-left' title='Tranzakció törlése' ><span style='font-size: 10px;' class= 'glyphicon glyphicon-trash\'></span></button>";
				s += '</form>';
				s += '</td>';
				s += '</tr>';
			break;
			case 'footer':
				s += "<tfoot>";
				for ( var i = 0; i < params.footers.length; i++ ){
					footer = params.footers[i];
					s += "<tr class='warning' >";
					s += "<th class='product-time'>";
					s += footer.label;
					s += "</th>";
					s += "<th class='product-name'>";
					s += "</th>";
					s += "<td class=\"sale-price\">";
					s += "</td>";
					s += "<td class=\'product-count\' >";
					s += footer.count( params.transfers );
					s += "</td>";
					s += "<td class=\'product-money\'  >";
					s += footer.total( params.transfers );
					s += "</td>";
					s += "<td  >";
					s += "";
					s += "</td>";
					s += "</tr>";
				}
				s += "</tfoot>";
				break;
			}
		
		return s;
	},
	_setOption: function (key, value) {
		var self = this,
			prev = this.options[key],
			fnMap = {
				'transfers': function () {
					self._refresh();
				},
			};	
			// base
			this._super(key, value);
		 
			if (key in fnMap) {
				fnMap[key]();

			// Fire event
//			this._triggerOptionChanged(key, prev, value);
		  }
	}, makeForm: function(){
		var s;
		s = "";
		s += "<form method='post' action='"+ this.options.url_pay_all_visible + "' id='"+this.options.id_form+"'>";
//		s += "<input type='hidden' name='"+this.options.name_number+"' value='"+this.options.value_number+"'>";
		for( var i = 0; i < this.options.transfers.length; i++){
			s += "<input type='hidden' name='"+this.options.name_transfers+"' value='"+this.options.transfers[i].id_transfer+"'>";
		}
		s += "</form>";
		return s;
	}
	
	
	
	 
});