Ext.ns('seteci');

seteci.MatrizTalentoHumano = {
	init: function(){	
		
		var store = new Ext.data.JsonStore({
			 autoSave: false
         , baseParams: {
            filterField: 'fondo'
         }
         , idProperty: 'id',
			fields: [
			'id',
			'fondo',
			'cooperante',
			'tematica',
			'detalle',
			'apoyo',
			'instituciones',
			'recursos',
			'aspectos',
			'link',
			'contacto',
			'idioma'
			],
			root: 'data',
			url: 'server/matriz.php'
		});
		store.load();
		
		var checkHandler = function(item, checked){
         if(checked){
            store.baseParams.filterField = item.key;
            searchFieldBtn.setText(item.text);
         }
      };
		
		var searchFieldBtn = new Ext.Button({
         menu: new Ext.menu.Menu({
            items: [
               { checked: true, checkHandler: checkHandler, group: 'filterField', key: 'fondo', scope: this, text: 'Fondo' }
               , { checked: false, checkHandler: checkHandler, group: 'filterField', key: 'cooperante', scope: this, text: 'Cooperante' }
               , { checked: false, checkHandler: checkHandler, group: 'filterField', key: 'apoyo', scope: this, text: 'Tipo de Apoyo' }
               , { checked: false, checkHandler: checkHandler, group: 'filterField', key: 'idioma', scope: this, text: 'Idioma' }
            ]
         })
         , text: 'Fondo'
      });

		var grid = new Ext.grid.GridPanel({
			store: store,
			columns: [
				{header:'Nombre de fondo', dataIndex:'fondo',sortable: true, width: 205},
				{header:'Cooperante', dataIndex:'cooperante',sortable: true, width: 120},
				{header:'Tipo de Apoyo', dataIndex:'apoyo',sortable: true, width: 120},
				{header:'Idioma', dataIndex:'idioma',sortable: true, width: 120}
			],
			height: 455,
			border: false,
			stripeRows: true,
			sm: new Ext.grid.RowSelectionModel({
				singleSelect: true,
				listeners: {
					rowselect: function(sm, row, rec) {
						Ext.getCmp("matriz-form").getForm().loadRecord(rec);
					}
				}
			}),
			listeners: {
				viewready: function(g) {
					g.getSelectionModel().selectRow(0);
				} // Allow rows to be rendered.
			}
		});

		var gridForm = new Ext.FormPanel({
			id: 'matriz-form',
			frame: true,
			labelAlign: 'right',
			xtype: 'fieldset',
			labelWidth: 100,
			title:'Detalles',
			defaults: {width: 250, border:false},
			defaultType: 'textfield',
			height: 455,
			border: false,
			style: {"margin-left": "10px"},
			items: [
				{fieldLabel: 'Fondo', name: 'fondo', xtype: 'textarea', height: 40},
				{fieldLabel: 'Cooperante',	name: 'cooperante'},
				{fieldLabel: 'Temática', name: 'tematica', xtype: 'textarea', height: 40},
				{fieldLabel: 'Detalle', name: 'detalle', xtype: 'textarea', height: 40},
				{fieldLabel: 'Tipo de Apoyo', name: 'apoyo'},
				{fieldLabel: 'Instituciones Elegibles', name: 'instituciones', xtype: 'textarea', height: 40},
				{fieldLabel: 'Recursos Disponibles', name: 'recursos'},
				{fieldLabel: 'Aspectos Adicionales', name: 'aspectos', xtype: 'textarea', height: 40},
				{fieldLabel: 'Link', name: 'link'},
				{fieldLabel: 'Contacto', name: 'contacto', xtype: 'textarea', height: 40},
				{fieldLabel: 'Idioma', name: 'idioma'}
			]
		});
		
		var win = new Ext.Window({
			title: 'Matriz de talento humano',
			layout: 'column',
			width: 1000,
			height:520,
			closeAction: 'hide',
			items: [{
				columnWidth: 0.60,
				layout: 'fit',
				items: grid
			},{				
				columnWidth: 0.4,
				layout: 'fit',
				items:gridForm
			}],
			tbar: [
				{
					text:'Excel',
					iconCls:'excel-icon',
					handler: function(){
						window.location.href = 'resources/fondos_talento_humano.xls';
					}
				},
				'-',
				{
					text:'PDF',
					iconCls:'pdf-icon',
					handler: function(){
						var id = grid.getSelectionModel().getSelected().data.id;
						window.open('server/pdf.php?id='+id,'Matriz','width=800,height=600,left=50,top=50,location=no, menubar=no,status=no,toolbar=no,scrollbars=no,resizable=no,directories=no');
					}
				},
				'->', {
               text: 'Buscar por:'
               , xtype: 'tbtext'
            }
            , ' '
            , searchFieldBtn
            , ' ', ' '
            , new SearchField({
               paramName: 'filterText'
               , store: store
            })
			]
		});

		win.show();
		
		Ext.get('ext-gen11').on('click',function(){
			window.opener=self;
			window.close();
		});
	}
}

Ext.onReady(seteci.MatrizTalentoHumano.init,seteci.MatrizTalentoHumano);