<?php
require_once __DIR__ . '/../../app/core/bootstrap.php';
require_once __DIR__ . '/../../app/core/access_guards.php';
require_once __DIR__ . '/../../app/core/i18n.php';
auth_require_login();
$pdo = crm_pdo();
gp_require_staff_access($pdo);
$page_title = 'Employees';
$_lang = gp_lang($pdo);
$rows = $pdo->query("SELECT id, code, full_name, phone, email, role_title, hourly_rate, active FROM employees ORDER BY full_name ASC")->fetchAll();
$total = count($rows);
$activeCount = count(array_filter($rows, fn($r) => (int)$r['active'] === 1));
include __DIR__ . '/../includes/header.php';
?>
<div class="page-header">
  <div class="page-header-title">
    <h1><?php echo gp_tr($pdo,'employees'); ?></h1>
    <span><?php echo $_lang==='el'?'Διαχείριση Τιμής':'Team management'; ?> &middot; <?php echo $activeCount; ?> <?php echo gp_tr($pdo,'active'); ?> / <?php echo $total; ?> <?php echo gp_tr($pdo,'total'); ?></span>
  </div>
  <div class="page-header-actions">
    <a class="btn btn-primary btn-sm" href="edit.php">+ <?php echo $_lang==='el'?'Νέος Υπάλληλος':'New Employee'; ?></a>
  </div>
</div>

<div class="card">
  <div class="list-toolbar">
    <div class="list-toolbar-left">
      <div class="list-search-wrap">
        <span class="search-icon">🔍</span>
        <input type="text" class="list-search-input" data-table-search="employees-table" placeholder="<?php echo gp_tr($pdo,'search').' '.gp_tr($pdo,'employees').'...'; ?>" autocomplete="off">
      </div>
      <span class="list-search-count" data-search-count><?php echo $total; ?> <?php echo gp_tr($pdo,'total'); ?></span>
    </div>
  </div>
  <?php if (!$rows): ?>
    <div class="empty-state">
      <div class="empty-state-icon">👷</div>
      <div class="empty-state-title"><?php echo $_lang==='el'?'Δεν υπάρχουν υπάλληλοι ακόμα':'No employees yet'; ?></div>
      <div class="empty-state-desc"><?php echo $_lang==='el'?'Κάντε κλικ στο &quot;Νέος Υπάλληλος&quot; για να προσθέσετε μέλη':'Click &quot;+ New Employee&quot; to add team members'; ?></div>
    </div>
  <?php else: ?>
    <div class="table-responsive">
      <table class="table" id="employees-table">
        <thead><tr>
          <th><?php echo gp_tr($pdo,'code'); ?></th>
          <th><?php echo $_lang==='el'?'Όνομα':'Full Name'; ?></th>
          <th><?php echo gp_tr($pdo,'phone'); ?></th>
          <th><?php echo gp_tr($pdo,'role'); ?></th>
          <th><?php echo $_lang==='el'?'Τιμή (€/h)':'Rate (€/h)'; ?></th>
          <th><?php echo gp_tr($pdo,'status'); ?></th>
          <th></th>
        </tr></thead>
        <tbody>
          <?php foreach ($rows as $row): ?>
          <tr>
            <td><?php echo h($row['code']); ?></td>
            <td><strong><?php echo h($row['full_name']); ?></strong></td>
            <td><?php echo h($row['phone']); ?></td>
            <td><?php echo h($row['role_title']); ?></td>
            <td><?php echo $row['hourly_rate'] ? '€ ' . number_format((float)$row['hourly_rate'], 2) : '-'; ?></td>
            <td><span class="badge <?php echo (int)$row['active'] === 1 ? 'badge-success' : 'badge-muted'; ?>"><?php echo (int)$row['active'] === 1 ? gp_tr($pdo,'active') : gp_tr($pdo,'inactive'); ?></span></td>
            <td><a class="btn btn-sm btn-primary" href="edit.php?id=<?php echo (int)$row['id']; ?>" style="box-shadow:none;"><?php echo gp_tr($pdo,'open'); ?></a></td>
          </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    </div>
  <?php endif; ?>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
