Ext.QuickTips.init();

var orgDataStore = 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 gridDataStore = new Ext.data.JsonStore({
	url: 'data/view-quotation-grid-data.php',
    root: 'griddata',
    totalProperty: 'total',
    id: 'inquiry_id',
    fields: [
		{ name: 'quotation_id', 		 mapping: 'quotation_id' },
		{ name: 'quotation_referenceby', mapping: 'quotation_referenceby' },
		{ name: 'quotation_subject',     mapping: 'quotation_subject' },
		{ name: 'quotation_total',       mapping: 'quotation_total' },
		{ name: 'quotation_date',        mapping: 'quotation_date' }
    ]
});

// Custom rendering Template
var quotation = 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: orgDataStore,
	labelWidth:20,
    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: '80%',
    pageSize: 15,
    triggerAction: 'all',
    tpl: quotation,
    itemSelector: 'div.movie-item',
    onSelect: function(record) {
        this.setValue(record.data.inquierorg_name);
        this.collapse();
		inquiry_id=record.data.inquiry_id;
		gridDataStore.load({
       		params:{inquiry_id:inquiry_id}
     	});
	}
}

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 createColModel = function (finish, start){
	var columns = [
		{
			header: "View",
			width:40,
			renderer: function(value, metaData, record, rowIndex, colIndex, store) {
	    		//return '<a href="addedit-fellowup.php?ID='+ record.get('quotation_id') +'">View</a>';
				//return '<a href="convertpdf/quotation-create-pdf.php?ID='+ record.get('quotation_id') +'">View</a>';
				return '<a href="report-form-quotation-result.php?ID='+ record.get('quotation_id') +'">View</a>';
				
			}
		},{
			header: "Edit",
			width:40,
			renderer: function(value, metaData, record, rowIndex, colIndex, store) {
	    		return '<a href="addedit-quotation.php?ID='+ record.get('quotation_id') +'">Edit</a>';
			}
		},{
            dataIndex: 'quotation_id',
			header: 'Quotation ID',
			hidden:true,
			filterable: true
        },{
            dataIndex: 'quotation_referenceby',
			header: 'Reference By',
			width: 150,
			filterable: true
        },{
            dataIndex: 'quotation_subject',
			header: 'Subject',
			width: 150,
			filterable: true
        },{
            dataIndex: 'quotation_total',
			header: 'Total',
			width: 80
        },{
            dataIndex: 'quotation_date',
			header: 'Date',
			width: 70,
			renderer: Ext.util.Format.dateRenderer('d/m/Y')
        }
	];
	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: 550,
    layout: 'fit',
	columnLines:true,
    selModel: new Ext.grid.RowSelectionModel({singleSelect:false})
});

Ext.onReady(function() {
	var productInquiryform = new Ext.FormPanel({
    	iconCls: 'panel-icon',
        frame: true,
		id: 'movies-form',
		labelWidth: 120,
		bodyStyle: 'padding:5px',
        width: 620,
        items: [titleSearch,{ height:25 },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 QuotationWindow = new Ext.Window({
        title: 'View Quotation Information',
        height: 300,
        width: 590,
		modal:true,
		border: false,
        layout: 'fit',
		closeAction:'close',
		collapsible:'true',
        items: productInquiryform
    }).show();
	 
	var varWidth =  (screen.width - QuotationWindow.width)/2;
	QuotationWindow.setPosition(varWidth,((screen.height/4)-110));
});