<?php
include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."init.php");
include_once (PATH_TO_ROOT."config.php");
include_once (PATH_TO_ROOT."CRM/_CRM_config.php");
	
$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);
$ajax = CRM_RPATH."_forms/_ajax/ajax.selectProject.php";

//ini_set("display_errors",1);
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);
$windowId = "WindowPresenceSelect";	
?>
var <?php echo $windowId;?> = new Ext.Window({
	title: '<?php echo $LangPack->GetLabel("window_presence_select", "window_title","Izbira termina"); ?>',
    id		: '<?php echo $windowId;?>',
    width	:500,
    height	: 160,
    plain	: true,
    layout	: 'fit',
    frame: true,
    modal: true,
    items:[new Ext.form.FormPanel({
    	bodyStyle: 'padding: 20px',
		border: false,
		id: 'addPlacForm',
		items:[
			new Ext.form.ComboBox({
			    typeAhead: true,
			    editable: false,
			    emptyText:'Select week...',
			    fieldLabel: '<?php echo $LangPack->GetLabel("window_presence_select", "week_select_combo","Izbira tedna");?>',
			    width: 150,
			    name: 'week_select',
			    hiddenName: 'week_select',
			    id: 'week',
			    triggerAction: 'all',
			    lazyRender: true,
			    mode: 'local',
			    store: new Ext.data.ArrayStore({
			        fields: [ 'id', 'week' ],
			        data: [
			        <?php
			        	$weeks = array();
			        	if ( $_SESSION['UserInfo']['user_type'] == 0 ) {

			        		$sql = "SELECT c.* FROM  ".DB_WARPIT_WEBCATI_BASE."._group_link as a
			        					LEFT JOIN  ".WARPIT_CRM."._PresentatorPermissions as b ON a.id_group = b.presentation_team
			        					LEFT JOIN  ".DB_WARPIT_WEBCATI_BASE."._Projects  as c ON b.id_project = c.id 
			        					WHERE a.id = ".$_SESSION['UserInfo']['id']." AND c.id IS NOT NULL GROUP BY c.id ";
			        		
			        			
			        	} else {
			        		$sql = "SELECT * 
			        				FROM ".DB_WARPIT_WEBCATI_BASE."._Projects AS c
			        				WHERE c.id 
			        					IN (SELECT id_project 
			        						FROM ".WARPIT_CRM."._PresentatorPermissions 
			        						GROUP BY  id_project ) ";
						}
			        	$sql.= " ORDER BY c.id DESC";
						
						$res = $db->SQLexecute($sql);
						
						while($row = $db->fetchAssoc($res)){
							array_push($weeks, "['".$row['id']."', '".$row['proj_name']."']");
						}
						echo implode(", ", $weeks); 
			        ?>
			        ]
			    }),
			    listeners: {
			    	select: function(comp, a, b){
			    		Ext.getCmp('projects').getStore().baseParams.idProject = a.data.id;
			    		Ext.getCmp('projects').clearValue();
			    		
			    		if(Ext.getCmp('projects').getValue() == ""){
							Ext.getCmp('select').setDisabled(true);
						}
						
						Ext.Ajax.request({ 
							url: '<?php echo $ajax;?>', 
							params: { action: 'getCount', idProject: a.data.id }, 
							success: function(resp, opt){ 
								var data = Ext.decode(resp.responseText);
								if(data.count == 1){
									data = data.rows[0];
									CallScript("_forms/prisotnost/forms.mainPanel.php",
							   		{ 				
										dan: data.dan,
										projectId: data.idProj,
										uniqueTabId: 'webPrisotnost_'+data.idProj+'_'+data.zipId+'_'+data.dan+'_'+data.ura,
										ura: data.ura,
										zipId: data.zipId,
										cati_location: data.zipId,
										CRM: true 
							   		});
							   		Ext.getCmp('<?php echo $windowId;?>').close();
								} 
							}
						});
			    	}
			    },
			    valueField: 'id',
			    displayField: 'week'
			}),
			new Ext.form.ComboBox({
			    typeAhead: true,
			    editable: false,
			    emptyText:'Select time...',
			    fieldLabel: '<?php echo $LangPack->GetLabel("window_presence_select", "project_select_combo","Izbira termina");?>',
			    anchor: '95%',
			    name: 'projects',
			    id: 'projects',
			    hiddenName: 'projects',
			    triggerAction: 'all',
			    mode: 'remote',
			    store: new Ext.data.Store({
					url: '<?php echo $ajax;?>',			
					baseParams:{
						action: 'getProjects'
					},
					reader: new Ext.data.JsonReader({		
							root: 'rows',
							totalProperty : "results"
						},
						new Ext.data.Record.create([
							{ name: 'id', type: 'id'},
							{ name: 'idProj', type: 'int'},
							{ name: 'project', type: 'string' },
							{ name: 'dan', type: 'int' },
							{ name: 'ura', type: 'int' },
							{ name: 'zipId', type: 'int' }
						])
					),
					listeners:{
						load: function() {
							Ext.getCmp('projects').lastQuery = null;
						}
					}
			    }),
			    listeners: {
			    	select: function(comp, a, b){
			    		if(Ext.getCmp('projects').getValue() != ""){
							Ext.getCmp('select').setDisabled(false);
						}
			    	}
			    },
			    valueField: 'id',
			    displayField: 'project'
			})
		]
    })],
    listeners: {
		afterRender: function(grid){
			var X = (screen.width/2) - ((Ext.getCmp('<?php echo $windowId;?>').getSize().width)/2);
			var Y = (screen.height/2) - ((Ext.getCmp('<?php echo $windowId;?>').getSize().height)/2);
			Ext.getCmp('<?php echo $windowId;?>').setPosition(X, Y);
		}
	},
	buttons:[{
		text: '<?php echo $LangPack->GetLabel("CRM_all", "CRM_cancle_button","Prekliči"); ?>',
		handler: function(){
			<?php echo $windowId;?>.close();
		}
	},{
		text:'<?php echo $LangPack->GetLabel("CRM_all", "CRM_select_button","Izberi"); ?>',
		id: 'select',
		disabled: 'true',
		handler: function(){
			var combo = Ext.getCmp('projects');
			var comboStore = combo.getStore();
			var data = comboStore.getAt(comboStore.findExact('id', combo.getValue())).data;

			CallScript("_forms/prisotnost/forms.mainPanel.php",
	   		{ 				
				dan: data.dan,
				projectId: data.idProj,
				uniqueTabId: 'webPrisotnost_'+data.idProj+'_'+data.zipId+'_'+data.dan+'_'+data.ura,
				ura: data.ura,
				zipId: data.zipId,
				cati_location: data.zipId,
				CRM: true 
	   		});
	   		if(Ext.getCmp('west-panel').collapsed){
				Ext.getCmp('west-panel').expand();
			}	
	   		Ext.getCmp('StatisticGrid').getEl().mask('<?php echo $LangPack->GetLabel("CRM_all", "loading_mask_text","Posodabljanje..."); ?>');
			Ext.getCmp('StatisticGrid').getStore().load({
				params: { 
					action: 'get',
					project_id: data.idProj,
					dan: data.dan,
					ura: data.ura,
					cati_location: data.zipId
				},
				callback: function(store, opt, success){
					if(success){
						Ext.getCmp('StatisticGrid').getEl().unmask();
					}
				}
			});
	   		<?php echo $windowId;?>.close();
		}
	}]
}).show();
