<?php
include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."init.php");

include_once(PATH_TO_ROOT."CRM/class/class.checkString.php");
	
$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);

include_once (PATH_TO_ROOT."CRM/_CRM_config.php");
include_once(PATH_TO_ROOT."CRM/class/class.Language-Pack.php");

$LangPack = new LanguagePack($db, $aLanguage);

$ajax = "CRM/_forms/nastavitve/_ajax/ajax.tipPlacila.php";

$compName = str_replace(' ', '_', StringChecker::Check($_POST['tabName']));
?>
Ext.ns('Ext.ux.grid');
Ext.ux.grid.CheckColumn = Ext.extend(Ext.grid.Column, {
    processEvent : function(name, e, grid, rowIndex, colIndex){
        if (name == 'mousedown') {
            var record = grid.store.getAt(rowIndex);
            record.set(this.dataIndex, !record.data[this.dataIndex]);
            Ext.Ajax.request({
            	url:'<?php echo $ajax;?>',
   				params: { 
   					action: 'update' ,
					id		: record.data.id,
				    field	: 'odpri_narocilo',
			    	oldValue: !record.data.odpri_narocilo,
					value 	: record.data.odpri_narocilo == true ? 1 : 0,
					table: '<?php echo WARPIT_CRM."._tipiPlacila";?>'					
   				}
            });
            record.commit();
            return false; // Cancel row selection.
        } else {
            return Ext.grid.ActionColumn.superclass.processEvent.apply(this, arguments);
        }
    },

    renderer : function(v, p, record){
        p.css += ' x-grid3-check-col-td'; 
        return String.format('<div class="x-grid3-check-col{0}">&#160;</div>', v ? '-on' : '');
    },

    init: Ext.emptyFn
});
Ext.preg('checkcolumn', Ext.ux.grid.CheckColumn);
Ext.grid.CheckColumn = Ext.ux.grid.CheckColumn;
Ext.grid.Column.types.checkcolumn = Ext.ux.grid.CheckColumn;
var sm = new Ext.grid.RowSelectionModel();
var <?php echo $compName;?> = new Ext.grid.EditorGridPanel({
		id:'<?php echo $compName;?>',
		store: new Ext.data.Store({
			url: '<?php echo $ajax;?>',			
			baseParams:{
				action: 'get'
			},
			reader: new Ext.data.JsonReader({		
					root:'rows',
					totalProperty : "results"
				},
				new Ext.data.Record.create([
					{name:'id', type:'id'},
					{name:'naziv_placila', type:'string'},
					{name:'orderBy', type:'int'}
				])
			)
		}),
		sm: sm,
		title: '<?php echo str_replace('_', ' ',$compName);?>',
		closable: true,
		plugins:[new Ext.ux.dd.GridDragDropRowOrder(
	    {
	        scrollable: true, // enable scrolling support (default is false)
	        listeners: {
	        	afterrowmove: function(a,b,c,e){
	        		Ext.Ajax.request({
	        			url: '<?php echo $ajax;?>',
	        			params:{
	        				action: 'reorder',
	        				oldIndex: b,
	        				newIndex: c	
	        			},
	        			success: function(resp, opt){
	        				Ext.getCmp('<?php echo $compName;?>').getStore().reload();
	        			}
	        		});
	        	}
	        }
	    })],
		columns:[{
			header: '<?php echo $LangPack->GetLabel("CRM_settings_panel", "payment_type_title","Naziv plačila"); ?>',
			dataIndex: 'naziv_placila',
			editor: new Ext.form.TextField()
		},{
			header: '<?php echo $LangPack->GetLabel("CRM_settings_panel", "order_by_title","Vrsni Red"); ?>',
			dataIndex: 'orderBy',
			editor: new Ext.form.NumberField()
		}],
  		viewConfig:{
  			forceFit: true
  		},
		tbar:[{
			text:'<?php echo $LangPack->GetLabel("CRM_all", "CRM_add_button","Dodaj"); ?>',
			iconCls: 'icon-add-record',
			handler: function(){
				CallScript('CRM/_forms/nastavitve/tabs/window.addPlacila.php', { name: '<?php echo $compName; ?>'});
			}
		},'->',{
			text: '<?php echo $LangPack->GetLabel("CRM_all", "CRM_delete_button","Izbriši"); ?>',
			iconCls: 'icon-delete-record',
			handler: function(){
				var sel = <?php echo $compName;?>.getSelectionModel().selections.items;
        		if(sel[0] != null){
        			var d = sel[0].data;
        			Ext.Msg.confirm('<?php echo $LangPack->GetLabel("CRM_settings_panel", "CRM_delete_message_title","Brisanje"); ?>', '<?php echo $LangPack->GetLabel("CRM_settings_panel", "CRM_delete_message_msg","Ali res želite izbrisati ta predment?"); ?> ('+d.naziv_placila+')', function(btn){
        				if(btn == "yes"){
        					Ext.Ajax.request({
		        				url: '<?php echo $ajax;?>',
		        				params:{
		        					action: 'delete',
		        					id: d.id
		        				},
		        				success: function(resp,opt){
		        					Ext.getCmp('<?php echo $compName;?>').getStore().reload();
		        				}
		        			});
        				}
        			});
        		}
			}
		}],
		listeners: {
       		afteredit : function(e) {
       			Ext.Ajax.request({
       				url:'<?php echo $ajax;?>',
       				params: { 
       					action: 'update',
						id		: e.record.id,
					    field	: e.field,
						value 	: e.value,
						oldValue: e.originalValue,
						table 	:'<?php echo WARPIT_CRM."._tipiPlacila";?>'					
       				},
					success : function(resp,opt){ 
						e.record.commit();
					}							  							
				})
			}	
		}   
	});
	<?php echo $compName;?>.getStore().reload();
	Ext.getCmp('NastavitveTabPanel').add(<?php echo $compName;?>);
	Ext.getCmp('NastavitveTabPanel').setActiveTab(<?php echo $compName;?>);