/*!
 * Ext JS Library 3.2.1
 * Copyright(c) 2006-2010 Ext JS, Inc.
 * licensing@extjs.com
 * http://www.extjs.com/license
 */
 


var store; 
var grid;

 

 
Ext.onReady(function Constroi(bReload){
  var dados = [];
  var Return = false;
	
  if (bReload == undefined) bReload = false;

if ((bPVez == true)||(bPVez == undefined)) return;
  var And = 'CPF='+$('#CPF').val();
  if ( (Trim($('#Ano').val())!='')&&(Trim($('#Num').val())!='') )
  {
     And = And + '&Ano='+$('#Ano').val()+'&Num='+$('#Num').val();
  }

  $.ajax({
   type: "POST",
   url: 'grid_meus_proc/grid_meus_proc.php',
   data: And,
   async: false,
   success: function(msg){

				if (Trim(msg) == '')
				{
					$('#grid').hide();
					alert("Não existe nenhum processo para o CPF/CNPJ informado");
					$('#CPF').focus();
					return;
				}
				
				$('#grid').show();
				dados=msg.split("||");
				depois(bReload)
		   }
   });

  
   
 function depois(bReload)
 {

	if (store == undefined) bReload = false;
	
	Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
	var array = [];
	i = 0;
	
	while (i < dados.length-1)
	{
		array.push(dados[i].split("##"));
		i = i + 1;
	}
	
	if (bReload == true) {store.loadData(array);return}	
	
        store = new Ext.data.ArrayStore({
        fields: [
           {name: 'NumProc', type:'int'},
           {name: 'Ano', type: 'int'},
           {name: 'DataEnt', type: 'string' },
           {name: 'Requerente', type: 'string'},
           //{name: 'Destinatario', type: 'string'},
           {name: 'Assunto', type: 'string'}
        ]
    });

    // manually load local data
    store.loadData(array);
	
	// Create grid view  


	
    // create the Grid
		grid = new Ext.grid.GridPanel({
        store: store,
		tbar: [{
            ref: '../viewBtn',
            iconCls: 'icon-ver',
            text: 'Ver ',
            disabled: true,
            handler: function(){
				//Chama a consulta de processos ja preenchendo o numero e ano de processo
				var s = grid.getSelectionModel().getSelections();
				$("#txtConsAno").val(s[0]['data']['Ano']);
				$("#txtConsNum").val(s[0]['data']['NumProc']);
				$("#frmConsProc").submit();
           }
        }],		
        columns: [
            {header: 'Proc.', sortable: false , width: 40,  dataIndex: 'NumProc'},
            {header: 'Ano', sortable: false, width: 40, dataIndex: 'Ano'},
            {header: 'Data', sortable: false, width: 70, dataIndex: 'DataEnt'},
            {header: 'Requerente', sortable: false, width: 140, dataIndex: 'Requerente'},
            //{header: 'Destinatario', sortable: false, width: 220, dataIndex: 'Destinatario'},
            {header: 'Assunto', sortable: false, width: 140, dataIndex: 'Assunto'}
        ],
        stripeRows: true,
        height: 300,
        width: 480,
        //title: 'Array Grid',
        // config options for stateful behavior
        stateful: true,
        stateId: 'grid'        
    });

    // render the grid to the specified div in the page
	grid.render('grid');
	grid.getSelectionModel().on('selectionchange', function(sm){
	    grid.viewBtn.setDisabled(sm.getCount() < 1);
		
						
    });
	
 	
  }	
  


});

