
MantenedorPlanosInnergy = function(config)
{
	this.config = config;
};

MantenedorPlanosInnergy.prototype = {

	init : function(){

		function formatBoolean(value){
        	return value ? '<img src="images/delete.gif">' : ''; 
    	};

		this.cm = new Ext.grid.ColumnModel([
			{
				header: " ",
				dataIndex: 'Elm',
				width: 25,
				renderer: formatBoolean,
			   	editor: new Ext.grid.GridEditor(new Ext.form.Checkbox())
			},{
			   header: "Id",
			   dataIndex: 'PI_ID',
			   width: 30,
			   editor: null
			},{
			   header: "Descripción",
			   dataIndex: 'PI_DESC',
			   width: 300,
			   editor: new Ext.grid.GridEditor(new Ext.form.TextField({
						   allowBlank: false,
						   maxValue: 50
					   }))
			},{
			   header: "Ruta Plano",
			   dataIndex: 'PI_RUTA_ARCHIVO',
			   width: 250,
			   editor:  new Ext.grid.GridEditor(new Ext.form.TextField({
						   allowBlank: false,
						   maxValue: 355
					   }))
			},/*{
			   header: "Tipo Instalación",
			   dataIndex: 'PI_TIPO_INSTALACION_AFECTADA',
			   width: 150,
			   editor: new Ext.grid.GridEditor(new Ext.form.ComboBox({
						   typeAhead: true,
						   triggerAction: 'all',
						   transform:'tipoInstalacion',
						   lazyRender:true
						}))
			},*/{
			   header: "Instalación Afectada",
			   dataIndex: 'PI_INSTALACION_AFECTADA',
			   width: 300,
			   editor: new Ext.grid.GridEditor(new Ext.form.ComboBox({
						   typeAhead: true,
						   triggerAction: 'all',
						   transform:'instalacion',
						   lazyRender:true
						}))
			}]);
		
		this.cm.defaultSortable = true;
	
		this.Items = Ext.data.Record.create([
			   {name: 'Elm', 							type: 'string'},
			   {name: 'PI_ID', 							type: 'string'},
			   {name: 'PI_DESC',						type: 'string'},
			   {name: 'PI_RUTA_ARCHIVO', 				type: 'string'},
			  // {name: 'PI_TIPO_INSTALACION_AFECTADA', 	type: 'string'},
			   {name: 'PI_INSTALACION_AFECTADA', 		type: 'string'}
		  ]);
	
		this.ds = new Ext.data.Store({
			proxy		: new Ext.data.HttpProxy({url: 'modulos/planoInnergy/listar.asp'}),
			reader: new Ext.data.XmlReader({
				   record: 'planoInnergy'
			   }, this.Items)
		});
		
		this.grid = new Ext.grid.EditorGrid(this.config.gridEl, {
			ds: this.ds,
			cm: this.cm,
			enableColLock:false
		});
		
		var layout = Ext.BorderLayout.create({
			center: {
				margins:{left:3,top:3,right:3,bottom:3},
				panels: [new Ext.GridPanel(this.grid)]
			}
		}, this.config.conEl);
	
		this.grid.render();
	
		var gridHead = this.grid.getView().getHeaderPanel(true);
		this.tb = new Ext.Toolbar(gridHead, [{
					text: 'Guardar',
					handler : this.guardarCambios.createDelegate(this)
				},{
					text: 'Eliminar',
					handler : this.eliminarSeleccionados.createDelegate(this)
				},{
					text: 'Nuevo',
					handler : this.nuevoRegistro.createDelegate(this)
				}]);
	
		this.ds.load();	
	},

	nuevoRegistro  : function(){
		var p = new this.Items({Elm : '',
							PI_ID : '',
							PI_DESC : '',
							PI_RUTA_ARCHIVO : '',
							//PI_TIPO_INSTALACION_AFECTADA : '',
							PI_INSTALACION_AFECTADA : ''
							});
		this.grid.stopEditing();
		this.ds.insert(0, p);
		this.grid.startEditing(0, 0);
	},

	guardarCambios : function(){
		var lds = this.ds;
		// obtiene filas modificadas
		var eds = this.ds.getModifiedRecords();   
			
		for (i = 0; i < eds.length; i++){
			callback = {
				success: function(o){
						 var cadErrores = o.responseText.htmlDecode();
						 if (cadErrores != ""){
							 new mensaje('Guardar.', cadErrores ,{modal:true, width:380, height:400, shadow:true, closable:true}).show(this.id);
						 }else{
							lds.commitChanges();
							lds.load();
						 }
						},
				failure: function(o){
						new mensaje('Error.',o.responseText,{modal:true, width:380, height:400, shadow:true, closable:true}).show(this.id);
						}
			   };
			
			url = 'modulos/planoInnergy/insertar.asp?PI_ID=' + eds[i].data.PI_ID +  
										'&PI_DESC=' + eds[i].data.PI_DESC +  
										'&PI_RUTA_ARCHIVO=' + eds[i].data.PI_RUTA_ARCHIVO +  
										//'&PI_TIPO_INSTALACION_AFECTADA=' + eds[i].data.PI_TIPO_INSTALACION_AFECTADA +
										'&PI_INSTALACION_AFECTADA=' + eds[i].data.PI_INSTALACION_AFECTADA
										
			YAHOO.util.Connect.asyncRequest('GET', url, callback);
		}
		
		
		
		//this.ds.load();
		
	},
	
	eliminarSeleccionados: function(){
		var lds = this.ds;
		// obtiene filas modificadas
		var eds = this.ds.getModifiedRecords(); 
		
		eliminaSeleccionadosSi = function(){
				for (i = 0; i < eds.length; i++){
					callback = { 
						success: function(o){
							lds.commitChanges();
							lds.load();
						},
						failure: function(o){
								new mensaje('Error.',o.responseText,{modal:true, width:380, height:400, shadow:true, closable:true}).show(this.id);
								}
					   };
					if (eds[i].data.Elm){
						url = 'modulos/planoInnergy/eliminar.asp?PI_ID=' + eds[i].data.PI_ID;
						YAHOO.util.Connect.asyncRequest('GET', url, callback);
					}
				}
				//this.ds.load();
				
				dialogValida.hide();
		};
		
		eliminaSeleccionadosNo = function(){
			dialogValida.hide();
		};

		
		if (eds.length > 0){
			//confima si quiere eliminar
		 	var dialogValida = new mensaje('Eliminación.',
									'¿ Está seguro que desea eliminar los elementos seleccionados ?',
									{ modal:true,
									  width:300,
									  height:120,
									  shadow:true,
									  closable:true });
			
			dialogValida.addButton('Sí', eliminaSeleccionadosSi.createDelegate(this), this);		
			dialogValida.addButton('No', eliminaSeleccionadosNo.createDelegate(this), this);						  
			dialogValida.show(this.tb.items.map.item2.id);		
			
		}

	}
};

