<?php
require_once('config.php');
if(!isset($_SESSION['user'])||!has_permission('faxdashboard')){header("Location:login.php");exit;}
$spoolDir=rtrim($applicationSpoolDir,'/').'/';
$incomingFaxes=array_filter(glob($spoolDir.'fax*.tif')?:[],'is_file');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title><?=ns(APP_NAME)?> - <?php _nsl('Fax Dashboard');?></title>
<link rel="stylesheet" href="include/css/style.css">
<?php include('include/web/header.php');?>
<script>
const tAlertTitle = <?=json_encode(__('Alert'), JSON_UNESCAPED_UNICODE)?>;
window.FAX_LANG={
ok:"<?=ns(__('Ok'))?>",
cancel:"<?=ns(__('Cancel'))?>",
faxMemo:"<?=ns(__('Fax Memo'))?>",
alert:"<?=ns(__('Alert'))?>",
pushTitle:"<?=ns(__('Push Notification'))?>",
pushDesc:"<?=ns(__('You can register or test push notifications'))?>",
pushRegister:"<?=ns(__('Subscribe'))?>",
pushTest:"<?=ns(__('Test'))?>",
pushNotSupported:"<?=ns(__('Push is not supported in this browser'))?>",
pushNeedPermission:"<?=ns(__('Notification permission is required'))?>",
pushAlready:"<?=ns(__('Already subscribed'))?>",
pushSuccess:"<?=ns(__('Push subscription completed'))?>",
pushTestSuccess:"<?=ns(__('Test notification sent'))?>",
pushTestFail:"<?=ns(__('Test failed'))?>"
};
window.PUSH_CONFIG={publicKey:"<?=PUSH_PUBLIC_KEY?>"};
</script>
<script src="include/js/base.js" defer></script>
<script src="include/js/modal.js" defer></script>
</head>
<body>
<div id="menuToggle">≡ <?php _nsl('Menu');?></div>
<div id="container">
<?php include('menu.php');?>
<div id="main">
<h2><?php _nsl('Fax Dashboard');?></h2>
<input type="hidden" id="csrf" value="<?=ns($_SESSION['csrf']??'')?>">
<div class="table-wrapper">
<table>
<thead><tr><th colspan="3"><?php _nsl('Incoming Queue');?></th></tr></thead>
<tbody>
<?php if(!$incomingFaxes):?>
<tr class="row-alternate"><td colspan="3" class="web-msg"><?php _nsl('No faxes in the incoming queue');?></td></tr>
<?php else:foreach($incomingFaxes as $i=>$f):
$realFile=realpath($f);
$realSpool=realpath($spoolDir);
if(!$realFile||strpos($realFile,$realSpool)!==0)continue;
$mtime=@filemtime($f);
$timeStr=$mtime?date('Y-m-d H:i',$mtime):'-';
?>
<tr class="<?=($i%2)?'row-classic':'row-alternate'?>">
<td><?=ns($timeStr)?></td>
<td><?=ns(basename($f))?></td>
<td>
<button type="button"class="btn-action"onclick="convertToPdf('<?=ns(basename($f))?>')">
<?php _nsl('Convert to PDF');?>
</button>
</td>
</tr>
<?php endforeach;endif;?>
</tbody>
<tfoot>
<tr><th colspan="3"><input type="button" value="<?php _nsl('Back');?>" class="send-btn" onclick="location.href='index.php';"></th></tr>
</tfoot>
</table>
</div></div></div>
<?php include('include/web/modal.php');?>
<?php include('include/web/footer.php');?>
<script>
async function convertToPdf(filename){
const csrf=document.getElementById('csrf')?.value;
if(!csrf){location.href='login.php';return;}
const formData=new FormData();
formData.append('fax',filename);
formData.append('csrf',csrf);
try{
const res=await fetch('include/service/converter.php',{method:'POST',body:formData});
if(!res.ok){if(res.status===403){location.href='login.php';return;}throw new Error('Server error');}
let result;
try{result=await res.json();
}catch{throw new Error('Invalid server response');}
if(result.success){location.href='fax_inbox.php';return;}
openAlertDialog(result.message||'Conversion failed');
}catch(e){console.error(e);
openAlertDialog('An error occurred during conversion');
}}
</script>
</body>
</html>
