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

billing_require_auth();
billing_ensure_project_support();

$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');
}

$projects = billing_get_projects((int) $user['id'], (int) $organization['id']);
$billingActiveNav = 'projects';
$billingTopbarOrganizationName = (string) ($organization['organization_name'] ?? 'Billing');
$billingTopbarUserLabel = (string) ($user['email'] ?? '');
?>
<!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>Projects</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">
</head>
<body>
<div class="dashboard-shell">
    <?php include dirname(__DIR__) . '/includes/app_sidebar.php'; ?>
    <div class="dashboard-main">
        <?php include dirname(__DIR__) . '/includes/topbar.php'; ?>
        <main class="dashboard-content catalog-page">
            <section class="panel">
                <div class="catalog-list-head">
                    <div>
                        <h1 style="margin:0;">Projects</h1>
                        <p class="panel-subtitle">Recovered project listing with valid request URLs.</p>
                    </div>
                </div>
                <div class="expenses-table-wrap">
                    <table class="expenses-table">
                        <thead>
                            <tr>
                                <th>Project</th>
                                <th>Customer</th>
                                <th>Status</th>
                                <th>Logged Hours</th>
                                <th>Unbilled Hours</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if ($projects === []): ?>
                                <tr><td class="expenses-empty" colspan="5">No projects found.</td></tr>
                            <?php else: ?>
                                <?php foreach ($projects as $project): ?>
                                    <tr>
                                        <td><?php echo billing_escape((string) ($project['project_name'] ?? '')); ?></td>
                                        <td><?php echo billing_escape((string) ($project['customer_name'] ?? '')); ?></td>
                                        <td><?php echo billing_escape((string) ($project['status_label'] ?? '')); ?></td>
                                        <td><?php echo billing_escape(number_format(((int) ($project['logged_seconds'] ?? 0)) / 3600, 2)); ?> hrs</td>
                                        <td><?php echo billing_escape(number_format(((int) ($project['unbilled_seconds'] ?? 0)) / 3600, 2)); ?> hrs</td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </section>
        </main>
    </div>
</div>
<script src="assets/billing.js"></script>
</body>
</html>

