<?php //echo "mode=".$_REQUEST['mode'];
if(file_exists('../../../'.$_REQUEST['tenant'].'/config.php')){require_once('../../../'.$_REQUEST['tenant'].'/config.php');}
if(file_exists('/config.php')){require_once('/config.php');}
switch ($_REQUEST['FCN']){
default:break;

case "save":
  
$files=json_decode($_REQUEST['files']);
foreach($files as $file){
$path_parts = pathinfo($file);
$oname=$path_parts['filename'];
$oext=$path_parts['extension'];
$source='../../../'.TENANT_URL.'/output/reports/'.$_REQUEST['client'].'/'.$file;
$dest='../../../'.TENANT_URL.'/output/reports/'.$_REQUEST['client'].'/saved/'.$file;
$dir='../../../'.TENANT_URL.'/output/reports/'.$_REQUEST['client'].'/saved/';
if(!is_dir($dir)){mkdir($dir);}
copy($source,$dest);

if(!empty($_REQUEST['name'])){
$_REQUEST['name']=str_replace(' ','_',$_REQUEST['name']);
$_REQUEST['name']=str_replace("'","",$_REQUEST['name']);
$newname=$dir.$_REQUEST['name'].'.'.$oext;
rename($dest, $newname);
$newname=str_replace('../../../'.TENANT_URL.'/',"",$newname);
$dir=str_replace('../../../'.TENANT_URL.'/',"",$dir);

$check=mysqli_query($GLOBALS['con'],"SELECT * FROM `delivery_save` WHERE `cid`='".$_REQUEST['client']."' AND `files`='".$newname."' AND `reps`='".$_REQUEST['reps']."'")or die(mysqli_error($GLOBALS['con']));
$num=mysqli_num_rows($check);
if($num<1){
$save=mysqli_query($GLOBALS['con'],"INSERT INTO `delivery_save` VALUES(
'',
'".$_REQUEST['client']."',
'".$newname."',
'".$_REQUEST['reps']."'
)")or die(mysqli_error($GLOBALS['con']));
}

echo "saved|".$_REQUEST['name']."|".$newname."|".$dir;
}
else{
    $dest=str_replace('../../../'.TENANT_URL.'/',"",$dest);
    $dir=str_replace('../../../'.TENANT_URL.'/',"",$dir);
    $check=mysqli_query($GLOBALS['con'],"SELECT * FROM `delivery_save` WHERE `cid`='".$_REQUEST['client']."' AND `files`='".$path_parts['filename']."' AND `reps`='".$_REQUEST['reps']."'")or die(mysqli_error($GLOBALS['con']));
    $num=mysqli_num_rows($check);
    if($num<1){
    $save=mysqli_query($GLOBALS['con'],"INSERT INTO `delivery_save` VALUES(
    '',
    '".$_REQUEST['client']."',
    '".$dest."',
    '".$_REQUEST['reps']."'
    )")or die(mysqli_error($GLOBALS['con']));
    }


echo "saved|".$file."|".$dest."|".$dir;
}
}




//echo $_REQUEST['name'];
break;


case "delete":
$check=mysqli_query($GLOBALS['con'],"DELETE FROM `delivery_save` WHERE `cid`='".$_REQUEST['client']."' AND `files`='".$_REQUEST['files']."' AND `reps`='".$_REQUEST['reps']."'")or die(mysqli_error($GLOBALS['con']));
unlink('../../../'.TENANT_URL.'/'.$_REQUEST['files']);
echo "removed";
break;

case "rename":
$_REQUEST['newName']=str_replace(' ','_',$_REQUEST['newName']);
$source='../../../'.TENANT_URL.'/output/reports/'.$_REQUEST['client'].'/'.$_REQUEST['file'];
$newname='../../../'.TENANT_URL.'/output/reports/'.$_REQUEST['client'].'/'.$_REQUEST['newName'];
$link='output/reports/'.$_REQUEST['client'].'/'.$_REQUEST['newName'];  

rename($source, $newname.".zip");
echo "renamed|".$_REQUEST['newName']."|".$link;
break;



}