<?php

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

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

include(PATH_TO_ROOT.'_plugins/Ext.ux.FileUploadField.js');


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

$gridName = "addDiagnosticGrid";
$ajax = CRM_RPATH."_forms/prisotnost/_ajax/ajax.DiagnosticList.php";
$windowId = "addDiagnosticWin";

?>
var <?php echo $gridName;?> = new Ext.form.FormPanel({
	id: '<?php echo $gridName;?>',
	bodyStyle:'padding: 20px',
    border: false,
    fileUpload: true,
    labelAlign: 'left',
    autoScroll: true,
    items:[
    	new Ext.ux.form.FileUploadField({
    		fieldLabel: '<?php echo $LangPack->GetLabel("add_activities_window", "file_upload_text","Izberi datoteko");?>',
    		name: 'uploadFile',
    		buttonText: '<?php echo $LangPack->GetLabel("add_activities_window", "upload_button_text","Brskanje...");?>',
    		width: 180
    	}),
    {
    	anchor:'100%',
    	fieldLabel: '<?php echo $LangPack->GetLabel("add_activities_window", "comment_text","Opomba");?>',
    	xtype: 'textarea',
    	name:'comment',
    	height: 100
    }
    <?php 
    
    	$sql = "SELECT diagnostic_list_added, diagnostic_list_file, diagnostic_list_comment
    			 	FROM ".WARPIT_CRM."._PresentatorPermissions
				WHERE 
						cati_location 	= 	".$_POST['cati_location']."
					AND	cati_day		= 	".$_POST['dan']."
					AND cati_hour		=	".$_POST['ura']."
					AND id_project		=	".$_POST['projectId'];
					  
		//echo "/*".$sql."*/";	
		$res = $db->SQLexecute($sql);
		$zad = $db->fetchAssoc($res);		  
		if($zad['diagnostic_list_file']) :
			
    ?>
    , 
    {
    	anchor:'100%',
    	fieldLabel: '<?php echo $LangPack->GetLabel("add_diagnostic_window", "file_link","Datoteka");?>',
    	xtype: 'label',
    	html: '<a target=_blank href="<?php echo ACTIVITE_WWW_PATH."diagnostic_lists/".$zad['diagnostic_list_file'];?>"><?php echo $zad['diagnostic_list_added']." - ".str_replace("\n","<br>",str_replace("\r","",$zad['diagnostic_list_comment'])); ?></a>',
    	height: 20
    }
    <?php endif; ?>
    ],
    buttons:[{
    	text:'Submit',
    	handler: function(){
    		var form = Ext.getCmp('<?php echo $gridName;?>').getForm();
    		if(form.isValid()){
    			form.submit({
    				url: '<?php echo $ajax;?>',
    				waitTitle : '<?php echo $LangPack->GetLabel("add_diagnostic_window", "wait_title","Diagnostični list");?>',
     				waitMsg : '<?php echo $LangPack->GetLabel("add_diagnostic_window", "wait_msg","Datoteka diagnostičnega lista se nalaga na strežnik");?>',
     				
    				params:{
    					action			: 	'submit',
    					cati_location	: 	<?php echo $_POST['cati_location']; ?>,
						dan				:	<?php echo $_POST['dan']; ?>,
						ura				:	<?php echo $_POST['ura']; ?>,
						projectId		:	<?php echo $_POST['projectId']; ?>
    				},
    				success: function(form, resp, opt){
    					var res = Ext.decode(resp.response.responseText);
    					if(res.success){
    						Ext.Msg.alert("<?php echo $LangPack->GetLabel("add_diagnostic_window", "alert_title","Diagnostični list");?>", "<?php echo $LangPack->GetLabel("add_diagnostic_window", "alert_msg","Diagnostični list shranjen");?>");
    						Ext.getCmp('<?php echo $windowId;?>').close();
    					}else{
    						Ext.Msg.alert('<?php echo $LangPack->GetLabel("add_activities_window", "error_alert_title","Prišlo je do napake");?>', '<?php echo $LangPack->GetLabel("add_activities_window", "error_alert_msg","Prišlo je do sledeče napake:");?>' + res.error);
    					}
    				}
    			})
    		}
    	}
    }]
});

var <?php echo $windowId;?> = new Ext.Window({
   	title:'<?php echo $LangPack->GetLabel("add_diagnostic_window", "window_title","Diagnostični list");?>',
    id		: '<?php echo $windowId;?>',
    width	: 550,
    height	: 250,
    plain	: true,
    layout: 'fit',
    frame: true,
    modal: true,
    items:[<?php echo $gridName;?>]
   
}).show();