/*
	Copyright (C) 2008 - Juan Ferrer Toribio

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this program; if not, write to the Free
	Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
	02111-1307 USA.
*/

var VnUsers = new Class
({
	Extends: VnModule,

	activate: function ()
	{
		var n;
		var tv;
		var vbox;
		var stmt;
		var rendText;
		var rendSpin;
		var rendButton;
		var entry;
		var model;
		var frame;
		var table;
		var tbody;
		var tr;
		var td;
		var op;
		var gui = this.gui;
		var db = this.db;

		vbox = new HtkVBox ();
		this.setChild (vbox);

		frame = new HtkFrame (TEXT_Users);
		frame.setSize (70, -1);
		vbox.add (frame, false);
		
		table = document.createElement ('table');
		table.cellSpacing = 8;
		frame.setChild (table);

		tbody = document.createElement ('tbody');
		table.appendChild (tbody);

		tr = document.createElement ('tr');
		tbody.appendChild (tr);
		
		op = new SqlGroup (SQL_OPERATION_AND);
	
		td = document.createElement ('td');
		td.style.textAlign = 'right';
		td.style.width = '80px';
		td.appendChild (document.createTextNode (TEXT_Id + ':'));
		tr.appendChild (td);

		td = document.createElement ('td');
		td.style.width = '70px';
		tr.appendChild (td);

		entry = new HtkText ();
		entry.setSize (-1, 50);
		td.appendChild (entry.getNode ());
		op.addEqual (entry, 'id');

		td = document.createElement ('td');
		td.style.textAlign = 'right';
		td.style.width = '150px';
		td.appendChild (document.createTextNode (TEXT_UserName + ':'));
		tr.appendChild (td);

		td = document.createElement ('td');
		tr.appendChild (td);

		entry = new HtkText ();
		entry.setEditable (true);
		td.appendChild (entry.getNode ());
		op.addLike (entry, 'name');
		
		stmt = new SqlString (
			'SELECT u.id, u.name, c.Cliente ' +
				'FROM user u INNER JOIN vn2008.Clientes c ON u.id = c.Id_Cliente ' + 
					'WHERE %s ORDER BY u.name'
		);
		stmt.addExpr (op);

		model = new DbModel (db);
		model.setStmt (stmt);
		
		rendSpin = new HtkCellRendererSpin ();
		rendText = new HtkCellRendererText ();
		rendButton = new HtkCellRendererImgButton ();
		rendButton.setData (this.rendererClicked, 'image/exit.png', TEXT_ChangeUser, gui);

		n = -1;
		tv = new HtkTreeView ();
		tv.appendColumn (++n, rendButton, "");
		tv.appendColumn (n, rendSpin, TEXT_Id);
		tv.appendColumn (++n, rendText, TEXT_User);
		tv.appendColumn (++n, rendText, TEXT_Name);
		tv.setModel (model);
		vbox.add (tv, true);
	},

	rendererClicked: function (renderer, value, gui)
	{
		var stmt = new SqlString ('UPDATE user_session_view SET user_id = %s');
		stmt.addValue (value);
		gui.db.stmt (stmt);

		gui.openModule (VnOrders, null);
	}
});


