 // vim: sw=4:ts=4:nu:nospell:fdc=4
 /*global Ext, App */
 /**
 * Componente de �rea de administra��o de itens de solicita��o
 *
 * @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.Item = Ext.extend(App.Module,{
	
	title: 'Gerenciamento de Produtos e Servi&ccedil;os',	
	id: 'pedido-item',	
	bodyBorder: false,
	layout: 'fit',
	iconCls: 'app-item-16',
	moduleAction: 'item_action/',	
	
	/**
	 * Inicializa o componente
	 */
	initComponent: function(){				
		//Chama o initComponent da super classe
		App.Pedido.Item.superclass.initComponent.apply(this, arguments);
		
		this.on('beforeinsert',this.fixTipo,this);
		this.on('beforeupdate',this.fixTipo,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: ['descricao','tipo','classificacao_id','id','classificacao','unidade']
		});*/
                this.store = App.stores.Item;
		
		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: 'descricao',header:'Produto / Servi&ccedil;o',dataIndex:'descricao',width:200},
				{id: 'tipo',header:'Tipo',dataIndex:'tipo',width:100, renderer: this.renderTipo},
                                {id: 'unidade',header:'Unidade',dataIndex:'unidade',width:20},
                                {id: 'classificacao',header:'Centro de Custo',dataIndex:'classificacao',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(){
		/* Desativado em 02/02/10 por Otávio
		this.custoStore = new Ext.data.JsonStore({
	    	proxy: new Ext.data.HttpProxy({ 
	    		url: App.sysURL + 'classificacao_action/findAll/',
	    		method: 'POST'
	    	}),
		    root: 'results',
		    idProperty: 'id',  
		    totalProperty: 'total',
		    fields: ['descricao','id']
		});*/
                this.custoStore = App.stores.Custo;
				
		this.window = new Ext.Window({
			title: 'Produto / Servi&ccedil;o',
			iconCls: 'app-item-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
						},{
			                 name:'classificacao_id'
			                ,fieldLabel:'Centro de custo'
			                ,xtype:'combo'
			                ,triggerAction:'all'
			                ,mode:'local'
			                ,store: this.custoStore
			                ,displayField:'descricao'
			                ,valueField:'id'
			                ,hiddenName:'classificacao_id'
			                ,anchor: '98%'
			            },
						{
							xtype: 'textfield',
							fieldLabel: 'Nome do produto ou servi&ccedil;o',
							allowBlank: false,
							name: 'descricao',
							anchor: '98%'
						},
						{
							xtype: 'textfield',
							fieldLabel: 'Unidade',
							name: 'unidade',
                                                        maxLength: 5,
							anchor: '40%'
						},
						{
							xtype: 'radio',
							fieldLabel: '',
							hideLabel: true,
							inputValue: 'P',
							boxLabel: 'Produto',
							allowBlank: false,
							name: 'tipo',
							anchor: '98%'
						},
						{
							xtype: 'radio',
							fieldLabel: '',
							hideLabel: true,
							inputValue: 'S',
							boxLabel: 'Servi&ccedil;o',
							allowBlank: false,
							name: 'tipo',
							anchor: '98%'
						},
						{
							xtype: 'radio',
							fieldLabel: '',
							hideLabel: true,
							inputValue: 'T',
							boxLabel: 'Taxa',
							allowBlank: false,
							name: 'tipo',
							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){
                    //Carrega o data store de fornecedores
                    this.custoStore.load();

                    win.center();
		},this);
	},
	
	/**
	 * Renderiza a coluna Ativo
	 * @param {} value
	 */
	renderTipo : function(value){
		if (value == 'S')	
                    return 'Servi&ccedil;o';
		else if (value == 'P')
                    return 'Produto';
                else
                    return 'Taxa';
	},
	
	/**
	 * Evento a ser executado antes da inser��o
	 * @param {} module
	 */
	fixTipo : function(module){
		//Se a propriedade ativo n�o estiver marcada, define seu valor como zero
		if (!module.params.tipo){
			module.params.tipo = 'S';
		}
	}
});

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