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

$entries = billing_get_project_time_entries((int) $user['id'], (int) $organization['id']);
$billingActiveNav = 'timesheet';
$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>Timesheet</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;">Timesheet</h1>
                        <p class="panel-subtitle">Recovered timesheet route with working navigation.</p>
                    </div>
                    <div class="catalog-detail-actions">
                        <a class="customer-btn" href="timesheet_weekly.php">Weekly View</a>
                        <a class="customer-btn" href="timesheet_custom_view.php">Custom View</a>
                    </div>
                </div>
                <div class="expenses-table-wrap">
                    <table class="expenses-table">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Project</th>
                                <th>Customer</th>
                                <th>Task</th>
                                <th>Hours</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if ($entries === []): ?>
                                <tr><td class="expenses-empty" colspan="6">No time entries found.</td></tr>
                            <?php else: ?>
                                <?php foreach ($entries as $entry): ?>
                                    <tr>
                                        <td><?php echo billing_escape((string) ($entry['entry_date'] ?? '')); ?></td>
                                        <td><?php echo billing_escape((string) ($entry['project_name'] ?? '')); ?></td>
                                        <td><?php echo billing_escape((string) ($entry['customer_name'] ?? '')); ?></td>
                                        <td><?php echo billing_escape((string) ($entry['task_name'] ?? '')); ?></td>
                                        <td><?php echo billing_escape(number_format(((int) ($entry['duration_seconds'] ?? 0)) / 3600, 2)); ?></td>
                                        <td><?php echo billing_escape((string) ($entry['billing_status'] ?? '')); ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </section>
        </main>
    </div>
</div>
<script src="assets/billing.js"></script>
</body>
</html>

