/**
 * Ext.mits.SpecGrid
 */
Ext.ns('Ext.mits');
Ext.mits.SpecGrid = Ext.extend(Ext.grid.GridPanel, {
 
	corchId: 0,
	data:[],
	countries:[],
	spec_types:[],
	frame:true,
	corchType:'h',
	hideToolbar:true,
	group_by:'hotel_name',
	hide_groups:false,
	remote_sort:false,
	title:'Спецпредложения',
		
	initComponent:function() {

		this.expTpl = new Ext.Template(
			'<div class="spec_body">{descr}'+((this.corchType != 'h')?'{path:this.showHotelLink}':'')+'</div>'
		);
		this.expTpl.showHotelLink = function(value) {
			return (value == null)?'':('<div class="tohotel"><a href="'+value+'">Посмотреть цены и описание</a></div>');
		};

		this.expander = new Ext.ux.grid.RowExpander({
			tpl: this.expTpl,
			expandOnDblClick:false,
			expandOnClick:true
		});
		
		this.cm = new Ext.grid.ColumnModel({
			columns:[
				this.expander,
				{
					header:"Название",
					renderer: {
						fn: function(value, metaData, record, rowIndex, colIndex, store) {
							return '<div class="clickable">'+(this.hide_groups?('<div class="spec_country">'+record.data.country_name+'</div>'):'')+'<b class="spec_name'+(record.data.spec_color?(' '+record.data.spec_color):'')+'">'+record.data.spec_name+'</b><br />'+((this.group_by == 'country_name' && record.data.hotel_name != null)?('<div class="spec_hotel">'+record.data.hotel_name+' <span class="stars">'+record.data.stars+'</span></div>'):'')+'<span class="spec_date">с <b>'+record.data.start_date+'</b> по <b>'+record.data.end_date+'</b></span></div>';
						},
						scope:this
					},
					id:"spec_name"
				},{
					header:"Отель",
					dataIndex:'hotel_name',
					id:"hotel_name",
					hidden:(this.group_by == 'hotel_name')?false:true,
					groupRenderer:function(v, unused, r) {
						if(r.data.hotel_name != null) {
							return (r.data.country_name?('<a href="'+r.data.country_path+'">'+r.data.country_name+'</a> - '):'')+(r.data.city_name?('<a href="'+r.data.path+'">'+r.data.city_name+'</a> - '):'')+'<a href="'+r.data.path+'">'+v+'</a> <span class="stars">'+r.data.stars+'</span>';
						} else {
							return 'Общие спецпредолжения по стране';
						}
					}
				},{
					header:"Страна",
					dataIndex:'country_name',
					renderer: {
						fn:function(value, metaData, record, rowIndex, colIndex, store) {
							if(record.data.country_name) {
								return '<a href="'+record.data.country_path+'">'+record.data.country_name+'</a>';
							} else {
								return '';
							}
						},
						scope:this
					},
					id:"country_name",
					hidden:(this.group_by == 'country_name' && !this.hide_groups)?false:true
				}
			],
			defaults: {
				sortable: true,
				menuDisabled: true
			}
		});
		
		var reader = new Ext.data.JsonReader({
			idProperty:'id',
			fields:[
				{name: 'id'},
				{name: 'spec_name'},
				{name: 'start_date'},
				{name: 'end_date'},
				{name: 'start_date_sort'},
				{name: 'end_date_sort'},
				{name: 'descr'},
				{name: 'spec_color'},
				{name: 'hotel_name', defaultValue:''},
				{name: 'stars', defaultValue:''},
				{name: 'path', defaultValue:''},
				{name: 'city_name', defaultValue:''},
				{name: 'city_path', defaultValue:''},
				{name: 'country_name', defaultValue:''},
				{name: 'country_path', defaultValue:''}
		]});
		
		this.store = new Ext.data.GroupingStore({
			proxy: new Ext.data.HttpProxy({
				disableCaching:false,
				url: '/data/get_corch_specs/'
			}),
			data:this.data,
			reader: reader,
			remoteSort:this.remote_sort,
			sortInfo:{field: 'end_date_sort', direction: "ASC"},
			groupField:this.hide_groups?null:this.group_by
		});
		
   		this.countryStore = new Ext.data.JsonStore({
	        fields: [
	        	{name: 'id', mapping:'id'},
	        	{name: 'name', mapping:'name'}
	        ],
	        idProperty:'id',
			data:this.countries
	    });
		
		this.countryCombo = new Ext.form.ComboBox({
			store:this.countryStore,
			displayField:'name',
			valueField:'id',
			value:0,
			width:200,
			triggerAction: 'all',
			mode: 'local',
			listeners: {
				select: {
					fn: function(field, newValue, oldValue) {
						this.store.load({
							params:{
								'corch_type': 'spec',
								'id': newValue.id,
								'type': -1
							},
							callback:function(r, options, success){
								this.specTypeCombo.setValue(-1);
							},
							scope:this
						});
					},
					scope:this
				}
			}
		});
		
		this.specTypeStore = new Ext.data.JsonStore({
	        fields: [
	        	{name: 'id', mapping:'id'},
	        	{name: 'name', mapping:'name'}
	        ],
	        idProperty:'id',
			data:this.spec_types
	    });
		
		this.specTypeCombo = new Ext.form.ComboBox({
			store:this.specTypeStore,
			displayField:'name',
			valueField:'id',
			value:-2,
			width:220,
			triggerAction: 'all',
			mode: 'local',
			listeners: {
				select: {
					fn: function(field, newValue, oldValue) {
						this.store.load({
							params:{
								'corch_type': 'spec',
								'id': this.countryCombo.getValue(),
								'type': newValue.id
							}
						});
					},
					scope:this
				}
			}
		});
		
		this.toolBar = new Ext.Toolbar({
			hidden:this.hideToolbar,
			items:['Страна:',
				this.countryCombo,
				'->','Тип:',
				this.specTypeCombo]
		});
		
		// {{{
		// hard coded (cannot be changed from outside)
		var config = {
			frame:this.frame,
			cm:this.cm,
			sm:new Ext.grid.RowSelectionModel({
				listeners:{
					beforerowselect:function() {
						return false;
					}
				}
			}),
			style:'z-index:1000;',
			store:this.store,
			hideHeaders:true,
			autoExpandColumn:'spec_name',
			autoHeight:true,
			loadMask:true,
			plugins:this.expander,
			title:this.title,
			id: 'spec-grid',
			tbar:this.toolBar
		};
		
		if(this.corchType != 'h') {
			config.view = new Ext.grid.GroupingView({
				showGroupName:false,
				hideGroupedColumn:true,
				groupTextTpl: '{group}'
			});
		}
 
		// apply config
		Ext.apply(this, config);
		Ext.apply(this.initialConfig, config);
		// }}}
		
		// call parent
		Ext.mits.SpecGrid.superclass.initComponent.apply(this, arguments);
	},

	onRender:function() {
		Ext.mits.SpecGrid.superclass.onRender.apply(this, arguments);
	}
 
}); // eo extend
 
// register xtype
Ext.reg('specgrid', Ext.mits.SpecGrid); 
 
// eof