 // 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.Pedido');

App.Pedido.Empresa = Ext.extend(App.Module,{
	
	title: 'Gerenciamento de Empresas',	
	id: 'pedido-empresa',	
	bodyBorder: false,
	layout: 'fit',
	iconCls: 'app-empresa-16',
	moduleAction: 'empresa_action/',
	
	/**
	 * Inicializa o componente
	 */
	initComponent: function(){				
		//Chama o initComponent da super classe
		App.Pedido.Empresa.superclass.initComponent.apply(this, arguments);
		
		this.on('beforeinsert',this.fixAtivo,this);
		this.on('beforeupdate',this.fixAtivo,this);
		
	},//end of initComponent
		
	/**
	 * Cria o DataGrid de visualiza��o de Grupos
	 */
	createGrid : function(){
				
		/* Desativado em 02/02/10 por Otávio
		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: ['razaosocial','cnpj','id','telefone','contato']
		});*/

                this.store = App.stores.Empresa;
		
		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: 'razaosocial',header:'Raz&atilde;o Social',dataIndex:'razaosocial',width:100},
				{id: 'cnpj',header:'CNPJ',dataIndex:'cnpj',width:100},
				{id: 'telefone',header:'Telefone',dataIndex:'telefone',width:100},
				{id: 'contato',header:'Nome do Contato',dataIndex:'contato',width:100}
			],
			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(){
		this.window = new Ext.Window({
			title: 'Empresa',
			iconCls: 'app-empresa-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',
							value: '0'
						},
						{
							xtype: 'textfield',
							fieldLabel: 'Raz&atilde;o Social',
							allowBlank: false,
							name: 'razaosocial',
							anchor: '98%'
						},
						{
							xtype: 'textfield',
							fieldLabel: 'CNPJ',
							allowBlank: false,
							name: 'cnpj',
							anchor: '98%'
						},
						{
							xtype: 'textfield',
							fieldLabel: 'Telefone',
							allowBlank: false,
							name: 'telefone',
							anchor: '98%'
						},
						{
							xtype: 'textfield',
							fieldLabel: 'Nome do contato',
							allowBlank: false,
							name: 'contato',
							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
	 */
	renderAtivo : function(value){
		if (value == '1')	
			return 'Sim';
		else
			return 'N&atilde;o';
	},
	
	/**
	 * Evento a ser executado antes da inser��o
	 * @param {} module
	 */
	fixAtivo : function(module){
		//Se a propriedade ativo n�o estiver marcada, define seu valor como zero
		if (!module.params.ativo){
			module.params.ativo = 0;
		}
	}
});

//Registra o novo componente
Ext.reg('pedido-empresa',App.Pedido.Empresa);
