var membersearchresultstore = new Ext.data.JsonStore({
url: 'data/getmembersearch.php',
root: 'result',
totalProperty: 'count',
fields: [{ name: 'member_id' },
		 { name: 'member_group' },
		 { name: 'member_name' },
		 { name: 'member_approvedfrom' },
		 { name: 'member_approvedto' },
		 { name: 'member_maxissuelimit' },
		 { name: 'getTotalNoofIssueBook'}]
});	

membersearchresultstore.on({'beforeload': {
	fn: function(membersearchresultstore, options){
		options.params || (options.params = {}); //assert params
		Ext.apply(options.params, {
			pageNo: this.pageNumber,
			memberno:Ext.getCmp('txtmemberno').getValue(),
			radiomemberno:Ext.getCmp('radiotype1').getValue(),
			membername:Ext.getCmp('txtmembername').getValue(),
			radiomembername:Ext.getCmp('radiotype2').getValue()
		});
	},
	scope: this
}});

var membersearchresultgrid = new Ext.grid.GridPanel({
store: membersearchresultstore,
columns: [
	{ 
		header: "Member Id",
		width: 20, 
		dataIndex: 'member_id',
		sortable: true
	},
	{   
		header: "Member Group",
		width: 30,
		dataIndex: 'member_group',
		sortable: true
	},
	{   
		header: "Member Name",
		width: 30,
		dataIndex: 'member_name',
		sortable: true
	},
	{   
		header: "Approved From",
		width: 25,
		dataIndex: 'member_approvedfrom',
		sortable: true
	},
	{   
		header: "Approved To",
		width: 25,
		dataIndex: 'member_approvedto',
		sortable: true
	},
	{   
		header: "Max Issue Limit",
		width: 25,
		dataIndex: 'member_maxissuelimit',
		sortable: true

	}
],
autoExpandColumn: 'title-col',
width: 565,
height: 230,
loadMask: true,
columnLines: true,
viewConfig: {
	forceFit: true
},
bbar: new Ext.PagingToolbar({
	pageSize: 10,
	store: membersearchresultstore,
	displayInfo: true,
	displayMsg: 'Displaying data {0} - {1} of {2}',
	emptyMsg: "No Data found"
})
});		

var membersearchitems = { 
xtype: 'fieldset',
labelWidth: 80,
border: false,

defaults: { width: 260 }, 
autoHeight: true,
width:584,
bodyStyle: Ext.isIE ? 'padding:0 0 5px 15px;' : 'padding:10px 15px;',
layout:'column',
items:[    
	{ 
		columnWidth:.26,
		layout:'form',
		items:[	
			{
				xtype: 'radio',
				hideLabel: true,
				name: 'radiotype',
				id: 'radiotype1',
				checked:true,
				inputValue: '1',
				boxLabel: 'Member No',
				handler:function(){
					if(this.checked) {
						Ext.getCmp('txtmemberno').enable();
						Ext.getCmp('txtmembername').reset();
						Ext.getCmp('txtmembername').disable();
					}												  	
				}												  
			}
		]
	},
	{ 
		columnWidth:.5,
		layout:'form',
		labelWidth:1,
		items:[	
			{
				xtype      : 'numberfield',
				name       : 'txtmemberno',
				id         : 'txtmemberno',
				allowBlank:false,
				autoCreate: {tag: 'input', type: 'text',  maxlength: '10'}
			}
		]
	},
	{ 
		columnWidth:.26,
		layout:'form',
		items:[	
			{
				xtype: 'radio',
				hideLabel: true,
				name: 'radiotype',
				id: 'radiotype2',
				inputValue: '2',												  
				boxLabel: 'Member Name',
				handler:function(){
					if(this.checked) {
						Ext.getCmp('txtmembername').enable();
						Ext.getCmp('txtmemberno').reset();
						Ext.getCmp('txtmemberno').disable();
					}	
				}												  
			}
		]
	 },
	 { 
		columnWidth:.5,
		layout:'form',
		labelWidth:1,
		items:[	
			{
				xtype      : 'textfield',
				name       : 'txtmembername',
				id         : 'txtmembername',
				disabled:true,
				allowBlank:false,
				autoCreate: {tag: 'input', type: 'text',  maxlength: '50'}
			}
		]
	},
	 { 
		columnWidth:0.99,
		layout:'form',
		items:[
			{buttons:[{ 
				text: 'Search',
				style:{margin:'0px 0px 0px 0px',padding:'0px 0px 0px 380px'},	 
				handler: function(){		
					if(Ext.getCmp('radiotype1').getGroupValue()==1){
						if(Ext.getCmp('txtmemberno').getValue()==""){
							Ext.MessageBox.show({
								title: 'Error',
								msg: 'Please fill Member No.',
								buttons: Ext.MessageBox.OK,
								icon:Ext.MessageBox.ERROR
							});	
						}
						else{
							load_membergrid(); 
						}
					}
					else if(Ext.getCmp('radiotype2').getGroupValue()==2){
						if(Ext.getCmp('txtmembername').getValue()==""){
							Ext.MessageBox.show({
								title: 'Error',
								msg: 'Please fill Member Name.',
								buttons: Ext.MessageBox.OK,
								icon:Ext.MessageBox.ERROR
							});	
						}
						else{
							load_membergrid(); 
						}
					}
				}
			},
			{
				text: 'Reset',
				id:'reset',
				handler: function () { 
					reset_memberform();
				}
			}]
		}]
	} 
]
}
  
var membersearchform = new Ext.FormPanel({
id: 'membersearchform',
frame: true,
labelAlign: 'left',
bodyStyle: 'padding:5px',

draggable: false,
layout:'column',
items:[membersearchitems,membersearchresultgrid]
});

function reset_memberform(){
	membersearchform.getForm().reset();
	membersearchresultstore.removeAll();	
}

function load_membergrid(){

	membersearchresultstore.removeAll();

	membersearchresultstore.load({params:{
		memberno: Ext.getCmp('txtmemberno').getValue(),
		membername: Ext.getCmp('txtmembername').getValue() ,start: 0,limit: 10
	}});	
}

function KeyCheck(e){
   var KeyID = (window.event) ? event.keyCode : e.keyCode;
	if(KeyID=='13'){
		load_membergrid();
	}
   if(KeyID=='46'){
		reset_memberform();
		 
	 }
}

var MemberSearchWindow = new Ext.Window({
	title: 'Member General Search' ,
	height:420,
	width:600,		
	layout: 'fit',
	border: false,
	closeAction:'close',
	modal:true,
	autoDestroy :true,
	collapsible:true,
	items: membersearchform
});


function OpenMemberSearch()
{
	MemberSearchWindow.show();	
	document.onkeyup = KeyCheck;
	membersearchresultgrid.getSelectionModel().on('rowselect', function(sm, rowIndex, record) {	
			Ext.getCmp('member_id').setValue(record.data.member_id);
			Ext.getCmp('txtgrno').setValue(record.data.member_id);
			Ext.getCmp('txtgrno').enable();
			
			Ext.getCmp('membername').setValue(record.data.member_name);
			Ext.getCmp('membername').enable();
			
			Ext.getCmp('txttotalIssueablebook').setValue(record.data.member_maxissuelimit);
			Ext.getCmp('txttotalIssueablebook').enable();
			Ext.getCmp('txttotalissuedbook').setValue(record.data.getTotalNoofIssueBook);
			Ext.getCmp('txttotalissuedbook').enable();
			
			Ext.getCmp('txtbookregno').enable();
			
			Ext.getCmp('txtbookname').enable();
			Ext.getCmp('book_returndate').enable();
			membersearchresultstore.reload();
			MemberSearchWindow.hide();
	});
}