 // vim: sw=4:ts=4:nu:nospell:fdc=4
 /*global Ext, EQP */
 /**
 * Componente de �rea de administra��o de grupos
 *
 * @author Ot�vio Augusto
 * @copyright (c) 2009, by Ot�vio Augusto
 * @company Net On - Solu��es Tecnol�gicas
 * @date 7 de Setembro 2009
 *
 */

//Define o namespace
Ext.ns('App.Admin');

App.Admin.Usuario = Ext.extend(App.Module,{
	
	title: 'Gerenciamento de Usu&aacute;rios',	
	id: 'admin-user',	
	bodyBorder: false,
	layout: 'fit',
	iconCls: 'app-user-16',
	moduleAction: 'usuario_action/',	
	
	/**
	 * Inicializa o componente
	 */
	initComponent: function(){				
		//Chama o initComponent da super classe
		App.Admin.Usuario.superclass.initComponent.apply(this, arguments);
		
		this.on('beforeinsert',this.fixCheck,this);
		this.on('beforeupdate',this.fixCheck,this);
		
	},//end of initComponent
		
	/**
	 * Cria o DataGrid de visualiza��o de Grupos
	 */
	createGrid : function(){
				
		/* Código comentado por Otávio em 02/02/10
                 * 
                 *Cria o DataStore,
		this.store = new Ext.data.JsonStore({
	    	proxy: new Ext.data.HttpProxy({ 
	    		url: App.sysURL + this.moduleAction + 'findAll/',
	    		method: 'POST'
	    	}),
		    root: 'results',
		    idProperty: 'id',  
		    totalProperty: 'total',
		    fields: ['nome','id','login','setor_id','tipo','admin','can_servico','can_produto','ativo','grupo','perfil','senha','setor','can_autorizar']
		});*/
            
                this.store = App.stores.Usuario;
		
		var grid = {
			xtype: 'editorgrid',
			store: this.store,
			id: this.id + '-grid',
			tbar: this.actionBar,
			 bbar: {
		        xtype: 'paging',
		        store: this.store,       // grid and PagingToolbar using same store
		        displayInfo: true,
		        pageSize: App.pageSize,
		        prependButtons: true
		    },			
			columns:[
				{id: 'nome',header:'Nome',dataIndex:'nome',width:300},
                                {id: 'email',header:'E-mail',dataIndex:'email',width:100},
				{id: 'login',header:'Login',dataIndex:'login',width:100},
				{id: 'setor',header:'Setor',dataIndex:'setor',width:100},
				{id: 'admin',header:'Administrador',dataIndex:'admin',width:100, renderer: this.renderSimNao},
				{id: 'autoriza',header:'Autoriza compras',dataIndex:'can_autorizar',width:100, renderer: this.renderSimNao},
				{id: 'servico',header:'Solicita servi&ccedil;o',dataIndex:'can_servico',width:100, renderer: this.renderSimNao},
				{id: 'produto',header:'Solicita produto',dataIndex:'can_produto',width:80, renderer: this.renderSimNao},
				{id: 'ativo',header:'Habilitado',dataIndex:'ativo',width:80, renderer: this.renderSimNao}
			],
			view : new Ext.grid.GridView({
				forceFit: true
			})
		};
		
		//Retorna o DataGrid criado
		return grid;
	},	
	
	/**
	 * Cria a janela e o formul�rio de edi��o de registro
	 */
	createForm : function(){
		/* Desativado em 02/02/10 por Otávio
                 *Cria o DataStore,
		this.setorStore = new Ext.data.JsonStore({
                    proxy: new Ext.data.HttpProxy({
                            url: App.sysURL + 'setor_action/findAll/',
                            method: 'POST'
                    }),
		    root: 'results',
		    idProperty: 'id',  
		    totalProperty: 'total',
		    fields: ['nome','id']
		});*/

                this.setorStore = App.stores.Setor
		
		this.window = new Ext.Window({
			title: 'Usu&aacute;rio',
			iconCls: 'app-user-16',
			width: 300,
			autoHeight: true,
			closeAction: 'hide',
			defaultButton: 0,
			items:[
				this.form = new Ext.form.FormPanel({
					baseCls: 'x-plain',
					labelAlign:'top',
					bodyStyle: 'padding:8px',
					items:[
						{
							xtype: 'hidden',
							name: 'id'
						},{
			                 name:'setor_id'
			                ,fieldLabel:'Setor'
			                ,xtype:'combo'
                                        ,allowBlank: false
			                ,triggerAction:'all'
			                ,mode:'local'
			                ,store: this.setorStore
			                ,displayField:'nome'
			                ,valueField:'id'
			                ,hiddenName:'setor_id'
			                ,anchor: '98%'
			            },
						{
							xtype: 'textfield',
							fieldLabel: 'Nome',
							allowBlank: false,
							name: 'nome',
							anchor: '98%'
						},
						{
							xtype: 'textfield',
							fieldLabel: 'E-mail',
							allowBlank: false,
							name: 'email',
							anchor: '98%',
                                                        vtype: 'email'
						},
						{
							xtype: 'textfield',
							fieldLabel: 'Login',
							allowBlank: false,
							name: 'login',
							anchor: '50%'
						},
						{
							xtype: 'textfield',
							fieldLabel: 'Senha',
							allowBlank: false,
							inputType: 'password',
							name: 'senha',
							anchor: '50%'
						},
						{
							xtype: 'checkbox',
							fieldLabel: '',
							hideLabel: true,
							inputValue: '1',
							boxLabel: 'Este usu&aacute;rio est&aacute; habilitado',
							allowBlank: false,
							name: 'ativo',
							anchor: '98%'
						},
						{
							xtype: 'checkbox',
							fieldLabel: '',
							hideLabel: true,
							inputValue: '1',
							boxLabel: 'Este usu&aacute;rio &eacute; administrador',
							allowBlank: false,
							name: 'admin',
							anchor: '98%'
						},
						{
							xtype: 'checkbox',
							fieldLabel: '',
							hideLabel: true,
							inputValue: '1',
							boxLabel: 'Este usu&aacute;rio pode autorizar compras',
							allowBlank: false,
							name: 'can_autorizar',
							anchor: '98%'
						},						
						{
							xtype: 'checkbox',
							fieldLabel: '',
							hideLabel: true,
							inputValue: '1',
							boxLabel: 'Este usu&aacute;rio solicita servi&ccedil;os',
							allowBlank: false,
							name: 'can_servico',
							anchor: '98%'
						},
						{
							xtype: 'checkbox',
							fieldLabel: '',
							hideLabel: true,
							inputValue: '1',
							boxLabel: 'Este usu&aacute;rio solicita produtos',
							allowBlank: false,
							name: 'can_produto',
							anchor: '98%'
						}						
						
						
					]
				})
			],
			buttons:[
				{
					text:'<b>Salvar</b>',
					id: this.id + '-save-btn',
					scope: this
				},
				{
					text: 'Cancelar',
					scope: this,
					handler: this.cancel
				}
			]
		});
		
		this.window.on('show',function(win){
			win.center();
		},this);
	},
	
	/**
	 * Renderiza a coluna Ativo
	 * @param {} value
	 */
	renderSimNao : function(value){
		if (value == '1')	
			return 'Sim';
		else
			return 'N&atilde;o';
	},
	
	/**
	 * Evento a ser executado antes da inser��o
	 * @param {} module
	 */
	fixCheck : function(module){
		//Se a propriedade ativo n�o estiver marcada, define seu valor como zero
		if (!module.params.ativo){
			module.params.ativo = 0;
		}
		//Se a propriedade ativo n�o estiver marcada, define seu valor como zero
		if (!module.params.admin){
			module.params.admin = 0;
		}
		//Se a propriedade ativo n�o estiver marcada, define seu valor como zero
		if (!module.params.can_servico){
			module.params.can_servico = 0;
		}

		//Se a propriedade ativo n�o estiver marcada, define seu valor como zero
		if (!module.params.can_produto){
			module.params.can_produto = 0;
		}

	}
});

//Registra o novo componente
Ext.reg('admin-user',App.Admin.Usuario);
