<?php
require_once __DIR__ . '/../../app/core/bootstrap.php';
require_once __DIR__ . '/../../app/core/portal_auth.php';
auth_require_employee_portal();
$pdo = crm_pdo();
$page_title = 'Employee Portal';

$employeeId = auth_employee_id();

if (auth_is_admin_like() && $employeeId <= 0) {
    $employees = $pdo->query("SELECT id, full_name FROM employees WHERE active = 1 ORDER BY full_name ASC")->fetchAll();
    include __DIR__ . '/../includes/header.php';
    ?>
    <div class="page-header">
      <div class="page-header-title">
        <h1>Employee Portal</h1>
        <span>Admin access — choose employee</span>
      </div>
    </div>
    <div class="card">
      <div class="table-responsive">
        <table class="table">
          <thead><tr><th>Name</th><th></th></tr></thead>
          <tbody>
            <?php foreach ($employees as $e): ?>
              <tr>
                <td><?php echo h($e['full_name']); ?></td>
                <td><a class="btn btn-sm btn-outline-light" href="/public/employees/my_jobs.php?employee_id=<?php echo (int)$e['id']; ?>">Open portal</a></td>
              </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      </div>
    </div>
    <?php include __DIR__ . '/../includes/footer.php'; exit; }

if ($employeeId <= 0) { http_response_code(403); exit('Employee record not linked.'); }

$stmt = $pdo->prepare("
    SELECT pa.*, p.title, p.code, p.address, p.city, c.name AS customer_name, c.phone AS customer_phone
    FROM project_assignments pa
    JOIN projects p ON p.id = pa.project_id
    LEFT JOIN customers c ON c.id = p.customer_id
    WHERE pa.employee_id = :employee_id
    ORDER BY pa.assigned_date ASC, pa.assigned_time ASC, pa.id DESC
");
$stmt->execute([':employee_id' => $employeeId]);
$rows = $stmt->fetchAll();

include __DIR__ . '/../includes/header.php';
?>
<div class="page-header">
  <div class="page-header-title">
    <h1>Employee Portal</h1>
    <span><?php echo auth_is_admin_like() ? 'Admin view' : 'My jobs'; ?></span>
  </div>
  <div class="page-header-actions">
    <?php if (file_exists(__DIR__ . '/timeclock.php')): ?><a class="btn btn-primary btn-sm" href="/public/employees/timeclock.php<?php echo auth_is_admin_like() ? '?employee_id=' . (int)$employeeId : ''; ?>">Time Clock</a><?php endif; ?>
  </div>
</div>

<?php if (!$rows): ?>
  <div class="card"><p class="small">No assigned jobs.</p></div>
<?php else: ?>
  <div class="grid-2">
    <?php foreach ($rows as $r): ?>
      <div class="card">
        <div class="page-header-title">
          <h1 style="font-size:1.05rem"><?php echo h($r['title']); ?></h1>
          <span><?php echo h((string)$r['assigned_date'] . ' ' . substr((string)$r['assigned_time'], 0, 5)); ?></span>
        </div>
        <p><strong>Project:</strong> <?php echo h($r['code']); ?></p>
        <p><strong>Customer:</strong> <?php echo h($r['customer_name']); ?></p>
        <p><strong>Phone:</strong> <?php echo h($r['customer_phone']); ?></p>
        <p><strong>Address:</strong> <?php echo h(trim(($r['address'] ?? '') . ' ' . ($r['city'] ?? ''))); ?></p>
        <p><strong>Role:</strong> <?php echo h($r['role_label']); ?></p>
        <p><strong>Status:</strong> <?php echo h($r['status']); ?></p>
      </div>
    <?php endforeach; ?>
  </div>
<?php endif; ?>

<?php include __DIR__ . '/../includes/footer.php'; ?>
