Ext.QuickTips.init();

var moviesDataStore = new Ext.data.JsonStore({
    url: 'data/inquiry-combo-data.php',
    baseParams: {cmd:'movies' },
    root: 'movies',
    totalProperty: 'count',
    id: 'inquiry_id',
    fields: [
		{ name: 'inquiry_id', mapping: 'inquiry_id' },
		{ name: 'inquierorg_name', mapping: 'inquierorg_name' },
		{ name: 'contactperson', mapping: 'contactperson' },
		{ name: 'inquier_phoneno', mapping: 'inquier_phoneno' },
		{ name: 'inquier_mobileno', mapping: 'inquier_mobileno' },
		{ name: 'inquier_faxno', mapping: 'inquier_faxno' },
		{ name: 'company_id', mapping: 'company_id' },
		{ name: 'inquier_address', mapping: 'inquier_address' },
        { name: 'appointmentdate', mapping: 'appointmentdate' },
        { name: 'appointmenttime', mapping: 'appointmenttime' }
    ]
});


var districtsore = new Ext.data.Store({
			url: 'data/getdistrictcode.php',          
	        reader: new Ext.data.JsonReader({			
                fields: ['dist_id', 'dist_nameeng'],
                root: 'rows'
            }),autoLoad:true
        });

var gridDataStore = new Ext.data.JsonStore({
    url: 'data/search-inquiry-grid-data.php',
    root: 'griddata',
    totalProperty: 'total',
    id: 'inquiry_id',
    fields: [
		{ name: 'inquiry_id', mapping: 'inquiry_id' },
		{ name: 'inquierorg_name', mapping: 'inquierorg_name' },
		{ name: 'contactperson', mapping: 'contactperson' },
		{ name: 'inquier_phoneno', mapping: 'inquier_phoneno' },
		{ name: 'inquier_mobileno', mapping: 'inquier_mobileno' },
		{ name: 'inquier_faxno', mapping: 'inquier_faxno' },
		{ name: 'inquier_email', mapping: 'inquier_email' },
		{ name: 'inquier_url', mapping: 'inquier_url' },
		{ name: 'inquier_address', mapping: 'inquier_address' },
        { name: 'appointmentdate', mapping: 'appointmentdate' },
        { name: 'appointmenttime', mapping: 'appointmenttime' }
    ]
});

// Custom rendering Template
var movieTpl = new Ext.XTemplate(
    '<tpl for="."><div class="movie-item">',
        '<h2><span>Date: {appointmentdate}<br/>Time: {appointmenttime}</span>{inquierorg_name}</h2>',
        '{inquier_address}',
    '</div></tpl>'
);
var inquiry_id="";
var district_id="";

var titleSearch = {
    xtype: 'combo',
    fieldLabel:'Inquier Organization ',
    store: moviesDataStore,
	labelWidth: 200,
    displayField: 'inquier_address',
	valueField: 'inquiry_id',
	id:'organization',
	name:'organization',
    typeAhead: false,
    loadingText: 'Searching...',
    emptyText: 'Type three or more letters of the Organization Name',
    minChars: 3,
    anchor: '100%',
    pageSize: 15,
    //hideTrigger: true,
    triggerAction: 'all',
    tpl: movieTpl,
    itemSelector: 'div.movie-item',
    onSelect: function(record) {
        this.setValue(record.data.inquierorg_name);
        this.collapse();
		inquiry_id=record.data.inquiry_id;
		Ext.getCmp('dist_id').setValue();
		gridDataStore.load({
                        params:{inquiry_id:inquiry_id}
                    });
		
    }
}
var createColModel = function (finish, start) {
        var columns = [
		 {header: "Follow Up",width:60,renderer: function(value, metaData, record, rowIndex, colIndex, store) {
	    	   		return '<a href="addedit-followup.php?ID='+ record.get('inquiry_id') +'">Add</a>';}
				  },
		{
            dataIndex: 'inquiry_id',
			header: 'Inquiry ID',
			hidden:true,
			filterable: true
        }, {
            dataIndex: 'inquierorg_name',
			header: 'Inquier Organization',
			width: 120,
			filterable: true
        }, {
            dataIndex: 'contactperson',
			header: 'Contact Person',
			width: 120
        }, {
            dataIndex: 'inquier_phoneno',
			header: 'Phone No',
			width: 120
        },{
            dataIndex: 'appointmentdate',
			header: 'Date'
        }
		, {
            dataIndex: 'appointmenttime',
			header: 'Time'
        }/*,
		{
            dataIndex: 'inquier_mobileno',
			header: 'Mobile No'
        }
		, {
            dataIndex: 'inquier_faxno',
			header: 'Fax No'
        }, {
            dataIndex: 'inquier_email',
			header: 'Email Id'
        }, {
            dataIndex: 'inquier_url',
			header: 'URL'
        }*/];
		return new Ext.grid.ColumnModel({
            columns: columns.slice(start || 0, finish),
            defaults: {
                sortable: true
            }
        });
    };

var grid = new Ext.grid.GridPanel({
        store: gridDataStore,
        colModel: createColModel(40), 
        loadMask: true,
		frame: false,
		enableColLock:false,
		height: 200,    
        width: 590,
        layout: 'fit',
		columnLines:true,
    	selModel: new Ext.grid.RowSelectionModel({singleSelect:false})
    });

Ext.onReady(function() {
var district = new Ext.form.ComboBox(
{
   xtype: 'combo',
   name: 'dist_id',
   id:'dist_id',
   hiddenName: 'dist_code1',
   tabIndex:14,	
   fieldLabel :'District',
   displayField:'dist_nameeng',
   valueField: 'dist_id',
   width: 200,
   blankText: '',
   emptyText:'',
   allowBlank:false,
   triggerAction:'all',
   mode:'local',			
   selectOnFocus: true,	
   store: districtsore,
   forceSelection:true,
   editable:false,
   listeners:{select:{fn:function(combo, value) 
  {	
		district_id=combo.getValue('dist_id');
			if(inquiry_id!="")
			{
				   gridDataStore.load({
					params:{inquiry_id:inquiry_id,district_id:district_id}
				});
			}
			else
			{
				gridDataStore.load({
					params:{district_id:district_id}
				});
			}
	}}}
});

var startdate = new Ext.form.DateField({
    id: 'startdate',	
	fieldLabel :'Date' ,
	name: 'startdate',
	emptyText :'From',
	allowBlank:false,
	vtype: 'daterange',
	endDateField:'enddate'
});	


var enddate = new Ext.form.DateField({
    id: 'enddate',
	fieldLabel:'',
	name: 'enddate',
	emptyText :'To',
	allowBlank:false,
	vtype: 'daterange',
	startDateField: 'startdate',
   listeners:{select:{fn:function() 
   {
	var satartdate=Ext.getCmp('startdate').getValue();
	var enddate=Ext.getCmp('enddate').getValue();
	if(satartdate!="")
	{
		if(inquiry_id!="")
		{
			gridDataStore.load({
			params:{inquiry_id:inquiry_id,satartdate:satartdate,enddate:enddate}
			});
		}
		else if(district_id!="")
		{
			gridDataStore.load({
			params:{district_id:district_id,satartdate:satartdate,enddate:enddate}
			});
		}
		else if(inquiry_id!="" && district_id!="")
		{
			gridDataStore.load({
			params:{inquiry_id:inquiry_id,district_id:district_id,satartdate:satartdate,enddate:enddate}
			});
		}
		else
		{
			gridDataStore.load({
			params:{satartdate:satartdate,enddate:enddate}
			});
		}
		
	} 
	else
	{
		Ext.MessageBox.show({
										   title: 'Error',
										   msg: 'Please select start date first.',
										   buttons: Ext.MessageBox.OK,
										   icon:Ext.MessageBox.ERROR
										  });
	}
	}}}
});	

    var productInquiryform = new Ext.FormPanel({
        iconCls: 'panel-icon',
        frame: true,
		id: 'movies-form',
		labelWidth: 118,
         bodyStyle:'padding:6px 6px 0',
        width: 620,
        items: [titleSearch,district,
				{
						xtype       : 'container',
						autoEl      : {},
						border      : false,
						layout      : 'column',
						anchor      : '95%',
						items       : [{
										xtype  : 'container',
										autoEl : {},
										layout : 'form',
										columnWidth :.40,
										items  : [startdate]
									   },
									   {
										xtype       : 'container',
										autoEl      : {},
										layout      : 'form',
										columnWidth : .20,
										labelWidth  : 1,
										items       : [enddate]
									   },
									   {
										xtype       : 'container',
										autoEl      : {},
										layout      : 'form',
										columnWidth : .20,
										labelWidth  : 1,
										items       : [{buttons:[{
										    
											text: 'Reset',
											id:'reset',
											iconCls: 'icon-reset',

											handler: function () { 
											productInquiryform.getForm().reset();
											gridDataStore.removeAll();
											inquiry_id="";
											district_id="";
												//productInquiryform.getForm().findField('startdate').reset();
												//productInquiryform.getForm().findField('enddate').reset();
											}				
						       			}]
									 }]
									   }]},
									   grid]
    });
	
	gridDataStore.on({'load': {
				fn: function(store, options){ 
				      if(gridDataStore.getCount() <= 0){
					       Ext.MessageBox.show({
							   title: 'Error',
							   msg: 'There is no Result found for this Selecion.',
							   buttons: Ext.MessageBox.OK,
							   icon:Ext.MessageBox.ERROR
							   });
					 
					  } 
					}
		 }
		 });
	
	 var SchoolWindow = new Ext.Window({
        title: 'Product Inquiry Follow Up',
        height: 320,
        width: 620,
		modal:true,
		border: false,
        layout: 'fit',
		closeAction:'close',
		collapsible:'true',
        items: productInquiryform
    }).show();
	 
	var varWidth =  (screen.width - SchoolWindow.width)/2;
	SchoolWindow.setPosition(varWidth,((screen.height/4)-110));
	

});