﻿/// <reference path="http://www-app.gdepb.gov.cn/ext-2.2/vswd-ext_2.1.js" />
Ext.namespace('EQPublish');

/**
* 自然保护区表格
*/
EQPublish.ProtectedRegionStore = function(config) {
	Ext.applyIf(config, {
		fields: [
			{ name: 'ProtectedRegionID', type: 'string', mapping: "ProtectedRegionID" },
			{ name:'ProtectedAreaCode', type:'string', mapping:'ProtectedAreaCode'},
			{ name: 'ProtectedAreaName', type: 'string', mapping: "ProtectedAreaName" },
			{ name: 'ProtectedSpecies', type: 'string', mapping: "ProtectedSpecies" },
			{ name: 'ControlLevel', type: 'string', mapping: "ControlLevel" },
			{ name: 'Longitude', type: 'float', mapping: "Longitude" },
			{ name: 'Latitude', type: 'float', mapping: "Latitude" }
		]
	});
	EQPublish.ProtectedRegionStore.superclass.constructor.call(this, config);
}
Ext.extend(EQPublish.ProtectedRegionStore, Ext.ux.AjaxStore);
var store = new EQPublish.ProtectedRegionStore({
	storeId: 'ProtectedRegionStore',
	url: 'handlers/ProtectedRegionHandler.ashx'
});
Ext.reg('ProtectedRegionStore', EQPublish.ProtectedRegionStore);

EQPublish.ProtectedRegionPanel = Ext.extend(Ext.grid.GridPanel, {
	ascxHandlerUrl: '',
	mapChartHandlerUrl: '',
	mapIconHandlerUrl: '',
	initComponent: function() {
		Ext.apply(this, {
			id: 'ProtectedRegionPanel',
			store: store,
			tbar: [
				'->',
				'-',
				'显示:',
				{ id: 'cb-1', xtype: 'checkbox', checked: true, handler: this.onCheck, scope: this },
				{ id: 'la-1', text: '国家级', xtype: 'label' },
				{ id: 'cb-2', xtype: 'checkbox', checked: true, handler: this.onCheck, scope: this },
				{ id: 'la-2', text: '省级', xtype: 'label' }
			],
			columns: [
				{ id: 'ProtectedRegionID', header: "地区ID", width: 30, sortable: true, dataIndex: 'ProtectedRegionID', hidden: true },
				{ header: "级别", width: 50, sortable: true, dataIndex: 'ControlLevel' },
				{ header: "名称", width: 180, sortable: true, dataIndex: 'ProtectedAreaName',id: 'ProtectedAreaName' }
			],
			autoExpandColumn: 'ProtectedAreaName',
			height: 478,
			width: 400
		});
		EQPublish.ProtectedRegionPanel.superclass.initComponent.apply(this, arguments);
		this.addEvents({ 'timechange': true });
		this.addEvents({ 'firstrowselect': true });
	},
	initEvents: function() {
		EQPublish.ProtectedRegionPanel.superclass.initEvents.call(this);
		this.on('cellclick', this.btnClick, this);
		this.getStore().on('beforeload', this._beforeLoad, this);
		this.getStore().on('load', this._afterLoad, this);
		this.getStore().on('loadexception', this._loadException, this);
	},
	_afterLoad: function(loader, node, response) {
		this.body.unmask();
		this.getSelectionModel().selectFirstRow();
		this.fireEvent('firstrowselect', this);
	},
	_beforeLoad: function(loader, node, callback) {
		this.body.mask('加载数据...', 'x-mask-loading');
	},
	_loadException: function(loader, node, response) {
		this.body.unmask();
		Ext.MessageBox.show({
			title: '提示：',
			msg: '加载数据出错',
			icon: Ext.MessageBox.ERROR,
			buttons: Ext.MessageBox.OK
		});
	},
	btnClick: function(thisGird, rowIndex, colIndex, evObject) {
	},
	onCheck: function(chk, evtobj) {
		var state = Ext.getCmp('cb-1').checked;
		var province = Ext.getCmp('cb-2').checked;
		if (state == false) {
			Ext.getCmp('cb-2').disabled = true;
		}
		if (province == false) {
			Ext.getCmp('cb-1').disabled = true;
		}
		if (state == true && province == true) {
			Ext.getCmp('cb-2').disabled = false;
			Ext.getCmp('cb-1').disabled = false;
		}
		this.store.load({ queryCondi: { state: state, province: province} });
		this.fireEvent('timechange', this, { state: state, province: province });
	},
	/**
	* 重写afterRender方法，可以在这里加载数据。
	*/
	afterRender: function() {
		EQPublish.ProtectedRegionPanel.superclass.afterRender.apply(this, arguments);
		var state = Ext.getCmp('cb-1').checked;
		var province = Ext.getCmp('cb-2').checked;
		this.store.load({ queryCondi: { state: state, province: province} });
		this.fireEvent('timechange', this, { state: state, province: province });
		//Ext.getCmp('ProtectedRegionPanel').getSelectionModel().selectFirstRow();
	},
	buttonRender: function(val) {
		if (!val) {
			return;
		}
		return '<a href="#">详细信息</a>';
	}
});
Ext.reg('ProtectedRegionPanel', EQPublish.ProtectedRegionPanel);
