<?php
session_start();
require_once dirname(__DIR__) . '/includes/functions.php';

billing_require_auth();

$user = billing_current_user();
$organization = billing_current_organization();

if (!$user) {
    billing_redirect('signin.php');
}

if (!$organization || empty($organization['onboarding_completed_at'])) {
    billing_redirect('onboarding.php?step=1');
}

$billingActiveNav = 'documents';
$billingTopbarOrganizationName = (string) ($organization['organization_name'] ?? 'Billing');
$billingTopbarUserLabel = (string) ($user['email'] ?? '');

$documentDir = dirname(__DIR__) . '/assets/uploads/documents';
$documentFiles = [];
if (is_dir($documentDir)) {
    $items = glob($documentDir . '/*');
    if (is_array($items)) {
        foreach ($items as $item) {
            if (!is_file($item)) {
                continue;
            }

            $basename = basename($item);
            $displayName = preg_replace('/^\d{14}-[a-z0-9]+-/i', '', $basename) ?: $basename;
            $timestamp = filemtime($item) ?: time();
            $documentFiles[] = [
                'name' => $displayName,
                'ext' => strtoupper((string) pathinfo($displayName, PATHINFO_EXTENSION)),
                'size' => filesize($item) ?: 0,
                'uploaded_by' => 'Me',
                'uploaded_ts' => $timestamp,
                'uploaded_on' => date('d/m/Y h:i A', $timestamp),
            ];
        }
    }
}

usort($documentFiles, static function (array $left, array $right): int {
    return ($right['uploaded_ts'] ?? 0) <=> ($left['uploaded_ts'] ?? 0);
});

$formatBytes = static function (int $bytes): string {
    if ($bytes >= 1024 * 1024) {
        return number_format($bytes / (1024 * 1024), 2) . ' MB';
    }
    if ($bytes >= 1024) {
        return number_format($bytes / 1024, 1) . ' KB';
    }

    return $bytes . ' B';
};
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<base href="<?php echo billing_escape(billing_base_href()); ?>">
<title>Documents</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Manrope:wght@400;500;700;800&display=swap">
<link rel="stylesheet" href="Payslip_Project/assets/fontawesome/css/all.min.css">
<link rel="stylesheet" href="assets/billing.css?v=20260507-layout-fix-2">
</head>
<body class="billing-documents-page">
<div class="dashboard-shell customer-shell">
    <?php include dirname(__DIR__) . '/includes/app_sidebar.php'; ?>
    <div class="dashboard-main customer-main">
        <?php include dirname(__DIR__) . '/includes/topbar.php'; ?>
        <main class="dashboard-content billing-app-page">
            <div class="billing-maintenance-strip">
                <span>Planned India Data Center Maintenance on 10th May 2026 and 17th May 2026, between 06.30AM to 09.30AM IST.</span>
                <a href="reports.php">Know more</a>
            </div>

            <section class="app-screen app-screen--documents">
                <aside class="app-doc-sidebar">
                    <a class="app-doc-back" href="dashboard.php"><i class="fa-solid fa-angle-left"></i> Back</a>
                    <h1>Documents</h1>

                    <nav class="app-doc-nav">
                        <a href="#" class="app-doc-nav-link"><i class="fa-regular fa-file-lines"></i><span>All Documents</span></a>
                        <a href="#" class="app-doc-nav-link is-active"><i class="fa-regular fa-folder-open"></i><span>Inbox</span><strong><?php echo count($documentFiles); ?></strong></a>
                    </nav>

                    <div class="app-doc-folders">
                        <div class="app-doc-folders-head">
                            <span>FOLDERS</span>
                            <div>
                                <button type="button" aria-label="Search folders"><i class="fa-solid fa-magnifying-glass"></i></button>
                                <button type="button" aria-label="Add folder"><i class="fa-solid fa-plus"></i></button>
                            </div>
                        </div>
                        <div class="app-doc-folder-item"><i class="fa-regular fa-folder"></i><span>Inbox</span></div>
                    </div>

                    <a href="#" class="app-doc-trash"><i class="fa-regular fa-trash-can"></i><span>Trash</span></a>
                </aside>

                <div class="app-doc-content">
                    <div class="app-page-heading app-page-heading--docs">
                        <h1>Files</h1>
                        <div class="app-doc-actions">
                            <a class="app-soft-btn" href="#"><i class="fa-regular fa-copy"></i> Available Autoscans: <?php echo count($documentFiles); ?></a>
                            <a class="app-soft-btn" href="#"><i class="fa-regular fa-envelope"></i> Configure</a>
                            <button class="app-primary-btn app-primary-btn--split" type="button">Upload File <i class="fa-solid fa-angle-down"></i></button>
                            <button class="app-icon-btn" type="button" aria-label="More actions"><i class="fa-solid fa-ellipsis-vertical"></i></button>
                        </div>
                    </div>

                    <div class="app-doc-filterbar">
                        <span>Filter By :</span>
                        <button type="button">Status: All <i class="fa-solid fa-angle-down"></i></button>
                    </div>

                    <div class="app-data-card">
                        <div class="app-table-wrap">
                            <table class="app-table app-table--documents">
                                <thead>
                                    <tr>
                                        <th><input type="checkbox" aria-label="Select all files"></th>
                                        <th>FILE NAME</th>
                                        <th>DETAILS</th>
                                        <th>UPLOADED BY</th>
                                        <th>UPLOADED ON</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php if ($documentFiles === []): ?>
                                        <tr><td class="app-table-empty" colspan="5">No documents available.</td></tr>
                                    <?php else: ?>
                                        <?php foreach ($documentFiles as $file): ?>
                                            <tr>
                                                <td><input type="checkbox" aria-label="Select file"></td>
                                                <td>
                                                    <div class="app-file-name">
                                                        <span class="app-file-badge"><?php echo billing_escape($file['ext']); ?></span>
                                                        <span><?php echo billing_escape($file['name']); ?></span>
                                                    </div>
                                                </td>
                                                <td><?php echo billing_escape($formatBytes((int) $file['size'])); ?></td>
                                                <td><?php echo billing_escape($file['uploaded_by']); ?></td>
                                                <td><?php echo billing_escape($file['uploaded_on']); ?></td>
                                            </tr>
                                        <?php endforeach; ?>
                                    <?php endif; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </section>
        </main>
    </div>
</div>
<script src="assets/billing.js?v=20260507-layout-fix-2"></script>
</body>
</html>
