<?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 = 'Customers';
$_lang = gp_lang($pdo);
$customerModel = new CustomerModel($pdo);
$rows = $customerModel->getActive();
$total = count($rows);
include __DIR__ . '/../includes/header.php';
?>
<div class="page-header">
  <div class="page-header-title">
    <h1><?php echo gp_tr($pdo,'customers'); ?></h1>
    <span><?php echo $_lang==='el'?'Διαχείριση Πελατών':'Client management'; ?> · <?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 Customer'; ?></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="customers-table" placeholder="<?php echo gp_tr($pdo,'search').' '.gp_tr($pdo,'customers').'...'; ?>" 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 customers yet'; ?></div>
      <div class="empty-state-desc"><?php echo $_lang==='el'?'Κάντε κλικ στο &quot;Νέος Πελάτης&quot; για να προσθέσετε τον πρώτο σας πελάτη':'Click &quot;+ New Customer&quot; to add your first client'; ?></div>
    </div>
  <?php else: ?>
    <div class="table-responsive">
      <table class="table" id="customers-table">
        <thead><tr><th>ID</th><th><?php echo gp_tr($pdo,'name'); ?></th><th><?php echo $_lang==='el'?'Επαφή':'Contact Person'; ?></th><th><?php echo gp_tr($pdo,'phone'); ?></th><th><?php echo gp_tr($pdo,'email'); ?></th><th><?php echo gp_tr($pdo,'city'); ?></th><th></th></tr></thead>
        <tbody>
          <?php foreach ($rows as $row): ?>
          <tr>
            <td>#<?php echo (int)$row['id']; ?></td>
            <td><strong><?php echo h($row['name']); ?></strong></td>
            <td><?php echo h($row['contact_person']); ?></td>
            <td><?php echo h($row['phone']); ?></td>
            <td><?php echo h($row['email'] ?? ''); ?></td>
            <td><?php echo h($row['city']); ?></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'; ?>
