/**
 * Registos Online v2.0
 * Copyright(c) 2008, Rui Lourenço, INPI
 * rui.lourenco@inpi.pt
 *
 * http://www.inpi.pt
 */
Inpi.Pesquisas.DesignEntidade = Ext.extend(Ext.form.FormPanel, {

    frame: true,
    title: Inpi.lblEntidadeTitle1,
    width: 894,
    bodyStyle: 'padding-top: 5px; padding-left: 10px; padding-right: 10px;',
    
    constructor: function(config){
		
		var self = this;
		
        this.nif = new Inpi.Pesquisas.TextField({
            maxLength: 9,
            allowBlank: true,
            fieldLabel: Inpi.lblNIF,
            width: 100,
            name: 'nif'
        });
        
        this.codigo = new Inpi.Pesquisas.TextField({
            maxLength: 130,
            allowBlank: true,
            fieldLabel: Inpi.lblCodigoEntidade,
            width: 100,
            name: 'codigo_entidade'
        });

        this.nome = new Inpi.Pesquisas.TextField({
            maxLength: 130,
            allowBlank: true,
            fieldLabel: Inpi.lblNome,
            width: 260,
            name: 'nome_entidade'
        });

        this.morada = new Inpi.Pesquisas.TextField({
            maxLength: 130,
            allowBlank: true,
            fieldLabel: Inpi.lblMorada,
            width: 260,
            name: 'morada_entidade'
        });
		
		this.intervencaoData = [
        ['QUA', Inpi.lblQualquer],
        ['TIT', Inpi.lblTitular],
		['CON', Inpi.lblConcessionario]];

		this.intervencaoStore = new Ext.data.SimpleStore({
		    fields: ['codi', 'desi'],
		    data : this.intervencaoData
		});		

        this.intervencao = new Ext.form.ComboBox({
            store: this.intervencaoStore,
            fieldLabel: Inpi.lblTipoIntervencaoReq,
            displayField: 'desi',
            valueField: 'codi',
            typeAhead: true,
            mode: 'local',
            triggerAction: 'all',
            emptyText: Inpi.lblEscolhaIntervencao,
            selectOnFocus: true,
            forceSelection: true,
            width: 260,
            allowBlank: false,
			hiddenName: 'intervencao',
			value: 'TIT'
        });		
		
		this.pesquisar = new Ext.Button({
                text: Inpi.lblPesquisar,
                iconCls: 'registos-online-button-search-icn',
                handler: this.doPesquisar,
                scope: this
        });

		
		this.store = new Ext.ux.data.PagingStore({
		    proxy: new Ext.data.HttpProxy({
		        url: Inpi.Pesquisas.Config.serverController,
		        method: 'POST'
		    }),
			lastOptions: {params:{start: 0,limit: 16}},						
		    reader: new Ext.data.JsonReader({
				successProperty: 'success',
	            root: 'items',
	            totalProperty: 'total',				
	            fields: [{
	                name: 'moda'
	            }, {
	                name: 'nume_proc', type: 'float'
	            }, {
	                name: 'titular'
	            }, {
	                name: 'epigrafe'
	            }, {
	                name: 'nord'
	            }]
		    })			
		});
		
		
		this.pagbar = new Ext.PagingToolbar({
		    pageSize: 16,
		    store: this.store,
		    displayInfo: true,
		    displayMsg: Inpi.lblResultados,
		    emptyMsg: Inpi.lblNenhumProcesso
		});
		
        
        this.gridSm = new Ext.grid.RowSelectionModel({
            singleSelect: 'true'
        });
        
        this.gridId = Ext.id();
        this.gridCf = {
            xtype: 'grid',
            id: this.gridId,		
            store: this.store,
			bbar: this.pagbar, 
            width: 860,
            height: 418,
            frame: 'true',
            enableColumnMove: true,
            enableColumnResize: true,
			/*
            loadMask: {
                msg: Inpi.lblCarregar,
                msgCls: 'x-mask-loading'
            },
            */
            columns: [{
                id: 'moda',
                align: 'left',
                header: Inpi.lblModalidade,
                width: 160,
                dataIndex: 'moda',
				sortable: true
            }, {
                id: 'nume_proc',
                align: 'left',
                header: Inpi.lblNProcesso,
                width: 100,
                dataIndex: 'nume_proc',
				sortable: true,
                renderer: function change(value, metada, record, rowIndex, colIndex, store){
                    return '<div text-align="left"><a target=_self href="javascript:Ext.getCmp(\''+self.getId()+'\').viewDetail('+record.get('nord')+')">'+value+'</a></div>';
                }				
            }, {
                id: 'titular',
                align: 'left',
                header: Inpi.lblPrimeiroTitular,
                width: 220,
                dataIndex: 'titular',
				sortable: true
            }, {
                id: 'epigrafe',
                align: 'left',
                header: Inpi.lblTitulo,
                width: 140,
                dataIndex: 'epigrafe',
				sortable: true
            }],
            autoExpandColumn: 'epigrafe',
            sm: this.gridSm,
            stripeRows: true
        };
		
        config = Ext.apply({
			labelWidth: 200,
            url: Inpi.Pesquisas.Config.serverController,
            //defaultType: 'textfield',
            monitorValid: true,
            items: [this.nif,this.codigo, this.nome, this.morada, this.intervencao, this.pesquisar, {
				xtype: 'panel',
				height: 10
			}, this.gridCf],
            buttonAlign: 'left',
            buttons: [{
                text: Inpi.lblAnterior,
                iconCls: 'registos-online-back-icn',
                handler: this.previousForm,
                scope: this
            }]
        }, config);
        Inpi.Pesquisas.DesignEntidade.superclass.constructor.call(this, config);
		
    },
    
    doPesquisar: function(){
        if (this.validateForm()) {
			
			delete this.store.lastParams;
			//this.store.proxy.reset();
			this.store.baseParams = {
                cmd: 'PesquisaDesignEntidade',
				lang: language,
				codigo: this.codigo.getValue(),
				nome: this.nome.getValue(),
				morada: this.morada.getValue(),
				intervencao: this.intervencao.getValue(),
				nif: this.nif.getValue()
			};
			this.getEl().mask(Inpi.lblCarregar, 'x-mask-loading');
			
			this.store.load({
				params: {
					start: 0,
					limit: 16
				},
				callback: this.storeclb,
				scope: this				
			});
						
        }
    },
	
	storeclb: function(r, option, success){
		if (success) {
			// all ok			
		} else {
			Ext.getCmp(this.gridId).getEl().unmask();
			this.store.removeAll();
			Ext.Msg.alert(Inpi.lblAtencao, Inpi.lblPesquisaFail);
		}
		this.getEl().unmask();
		
	},
	
	viewDetail: function(nord){
		//Ext.Msg.alert('Detail', nord);
		Inpi.Pesquisas.Context.nord = nord;
		showEcran('design-detalhe', this.id);
	},
	
    showNext: function(){
		this.getForm().reset();
		this.store.removeAll();
		this.pagbar.doRefresh();
	},	
    
    resetForm: function(){
        this.getForm().reset();
    },
	
    previousForm: function(){
        showPreviousEcran(this.id);
    },	
    
    validateForm: function(){

        if (!this.getForm().isValid()) {
            Ext.Msg.alert(Inpi.lblAtencao, Inpi.lblAviso1);
            return false;
        }
		
        return true;
    }
    
});

Ext.reg('design-entidade', Inpi.Pesquisas.DesignEntidade);

