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

$compName = str_replace(' ', '_', StringChecker::Check($_POST['tabName']));
?>
var <?php echo $compName;?> = new Ext.form.FormPanel({
    bodyStyle:'padding: 15px',
	closable: true,
    labelAlign: 'left',
	labelWidth: 100,
	id: '<?php echo $compName;?>',
	title: '<?php echo $compName;?>',
	items:[
	new Ext.form.ComboBox({
        fieldLabel: '<?php echo $LangPack->GetLabel("presentation_permission_machine", "project_text","Project");?>', 
	    mode: 'remote',
	    triggerAction: 'all',
	    editable: false,
	    width: 250,
	    allowBlank: false,
	    hiddenName: 'projectId',
	    name: 'projectId',
	    id: 'projectId',
	    store: new Ext.data.JsonStore({
            url : '<?php echo $ajax; ?>',
            root : 'rows',
            baseParams: {
                action: 'get'
            },
            fields:[
	             { name:"id", type: 'int' },
	             { name:"proj_name", type: 'string' }
            ]
        }),
        listeners:{
        	expand: function(comp,a,b){
        		comp.lastQuery = null;
        	},
        	select: function(comp, a, b){
        		Ext.Ajax.request({
        			url: '<?php echo $ajax;?>',
        			params:{
        				action: 'getDateTime',
        				projectId: a.data.id
        			},
        			success: function(resp, opt){
        				var res = Ext.decode(resp.responseText);
        				if(res.success){
        					var i = 2;
        					Ext.each(res.days, function(d){
        						Ext.getCmp('<?php echo $compName;?>').insert(i++,{
									xtype: 'textfield',
									readOnly: true,
									name: 'd_'+d.id,
									width: 250,
									fieldLabel: '<?php echo $LangPack->GetLabel("presentation_permission_machine", "day_number_text", "Dan");?> ' + d.id,
									value: d.days
								});
        					});
        					Ext.each(res.hours, function(h){
        						Ext.getCmp('<?php echo $compName;?>').insert(i++,{
									xtype: 'compositefield',
									defaults:{
										style: 'margin-right: 5px'
									},
									fieldLabel: '<?php echo $LangPack->GetLabel("presentation_permission_machine", "hour_text", "Ura");?> ' + h.id,
									items:[{
										xtype:'textfield',
										value: h.time,
										name: 'h_'+h.id
									},{
										xtype:'textfield',
										name: 'hT_'+h.id
									}]
								});
        					});
        					
        					Ext.each(res.hotel, function(h){
        						Ext.getCmp('<?php echo $compName;?>').insert(i++,{
									xtype: 'compositefield',
									defaults:{
							    		style: 'margin-right: 5px'
							    	},
							    	fieldLabel: '<?php echo $LangPack->GetLabel("presentation_permission_machine", "location_text", "Lokacija");?> ' + h.smpl_var,
							    	items:[{
							    		xtype: 'textfield',
							    		value: h.location,
								    	name: 'l_'+h.smpl_var,
							    	},{
							    		xtype: 'textfield',
							    		value: h.city,
								    	name: 'c_'+h.smpl_var,
							    	}]
								});
        					});
        					
							Ext.getCmp('<?php echo $compName;?>').doLayout();
        					Ext.getCmp('DateTimeLabel').update(res.msg);
        					Ext.getCmp('selectFirstDateLabel').update('<b><?php echo $LangPack->GetLabel("presentation_permission_machine", "select_first_date_text", "Izberi datum za prvi termin");?></b><br>');
        				}
        			}
        		});
        	}
        },
	    lazyRender: true,
	    valueField: 'id',
	    displayField: 'proj_name' 
	}),{
		xtype: 'label',
		id: 'DateTimeLabel'
	},{
		xtype: 'datefield',
		format: 'd.m.Y',
		hiddenName: 'date',
		name: 'date',
		allowBlank: false,
		fieldLabel: '<?php echo $LangPack->GetLabel("presentation_permission_machine", "date_datefiled_text", "Prvi datum");?>'
	},{
		xtype: 'label',
		id: 'selectFirstDateLabel'
	},
	new Ext.form.ComboBox({
	    editable: false,
	    width: 250,
	    hiddenName: 'presentation_team',
	    triggerAction: 'all',
	    allowBlank: false,
	    fieldLabel: '<?php echo $LangPack->GetLabel("presentation_permission_machine", "presentation_team_text", "Ekipa");?>',
	    mode: 'remote',
	    store: new Ext.data.JsonStore({
            url : '<?php echo $ajax; ?>',
            root : 'rows',
            baseParams: {
                action: 'getTeams'
            },
            fields:[
	             { name:"id", type: 'int' },
	             { name:"team", type: 'string' }
            ]
        }),
	    lazyRender: true,
	    valueField: 'id',
	    displayField: 'team' 
	}),{
		xtype: 'button',
		text: 'Do some magic',
		handler: function(){
			if(Ext.getCmp('<?php echo $compName;?>').getForm().isValid()){
				Ext.getCmp('<?php echo $compName;?>').getForm().submit({
					url: '<?php echo $ajax;?>',
					params: {
						action: 'submit'
					},
					success: function(form,resp){
						var res = Ext.decode(resp.response.responseText);
						if(res.success){
							Ext.Msg.alert('<?php echo $LangPack->GetLabel("presentation_permission_machine", "alert_success_title_text", "Uspešno");?>', '<?php echo $LangPack->GetLabel("presentation_permission_machine", "alert_success_message_text", "Termin se je uspešno dodal");?>');
							
							//Ext.getCmp('<?php echo $compName;?>').getForm().reset();
							CallScript('CRM/_forms/nastavitve/tabs/forms.presentatorPermissions.php', { tabName: 'Presentations', projectId: Ext.getCmp('projectId').getValue() });
						}
					}
				});
			}
		}
	}]
});
Ext.getCmp('NastavitveTabPanel').add(<?php echo $compName;?>);
Ext.getCmp('NastavitveTabPanel').setActiveTab(<?php echo $compName;?>);