<?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.artikliNastavitve.php";

$compName = str_replace(' ', '_', StringChecker::Check($_POST['tabName']));
?>
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:'product_code', type: 'string'},
					{name:'product_name', type:'string'},
					{name:'cena1', type:'string'},
					{name:'cena2', type:'string'},
					{name:'cena3', type:'string'},
					{name:'cena4', type:'string'},
                    {name:'cena5', 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", "item_code_title","Koda"); ?>',
			dataIndex: 'product_code',
			editor: new Ext.form.TextField()
		},{
			header: '<?php echo $LangPack->GetLabel("CRM_settings_panel", "item__title","Naziv"); ?>',
			dataIndex: 'product_name',
			editor: new Ext.form.TextField()
		},{
			header: '<?php echo $LangPack->GetLabel("CRM_settings_panel", "item_price1_title","Cena gotovine"); ?>',
			dataIndex: 'cena1',
			renderer:function(a){
				return a + ' <?php echo MONEY_VALUE;?>';
			},
			editor: new Ext.form.TextField()
		},{
			header: '<?php echo $LangPack->GetLabel("CRM_settings_panel", "price2_title","Cena kartice"); ?>',
			dataIndex: 'cena2',
			renderer:function(a){
				return a + ' <?php echo MONEY_VALUE;?>';
			},
			editor: new Ext.form.TextField()
		},{
			header: '<?php echo $LangPack->GetLabel("CRM_settings_panel", "item_price3_title","Cena za TRR"); ?>',
			dataIndex: 'cena3',
			renderer:function(a){
				return a + ' <?php echo MONEY_VALUE;?>';
			},
			editor: new Ext.form.TextField()
		},{
            header: '<?php echo $LangPack->GetLabel("CRM_settings_panel", "item_price4_title","Cena lastni kredit"); ?>',
            dataIndex: 'cena4',
            renderer:function(a){
				return a + ' <?php echo MONEY_VALUE;?>';
			},
            editor: new Ext.form.TextField()
        },{
            header: '<?php echo $LangPack->GetLabel("CRM_settings_panel", "item_price5_title","Obročno plačilo s kartico"); ?>',
            dataIndex: 'cena5',
            renderer:function(a){
				return a + ' <?php echo MONEY_VALUE;?>';
			},
            editor: new Ext.form.TextField()
        },{
			header: '<?php echo $LangPack->GetLabel("CRM_settings_panel", "order_by_title","Vrsni Red"); ?>',
			dataIndex: 'orderBy',
			editor: new Ext.form.TextField()
		}],
		tbar:[{
			text:'<?php echo $LangPack->GetLabel("CRM_all", "CRM_add_button","Dodaj"); ?>',
			iconCls: 'icon-add-record',
			handler: function(){
				CallScript('CRM/_forms/nastavitve/tabs/window.addArtikle.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_izdelka+')', 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();
		        				}
		        			});
        				}
        			});
        		}
			}
		},{
		    text: "<?php echo $LangPack->GetLabel("CRM_all", "CRM_help_button","Pomoč"); ?>",
		    handler: function()
		    {
		        CallScript('CRM/_forms/nastavitve/tabs/window.artikel.help.php');
		    }
		}],
		viewConfig:{
  			forceFit: true
  		},
		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					
       				},
					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;?>);