<?php
require_once __DIR__ . '/../../app/core/bootstrap.php';
require_once __DIR__ . '/../../app/core/project_permissions.php';
require_once __DIR__ . '/../../app/core/access_guards.php';
auth_require_login();
$pdo = crm_pdo();
gp_require_staff_access($pdo);
$page_title = 'External partners';

$specialty = trim((string)($_GET['specialty'] ?? ''));
$city = trim((string)($_GET['city'] ?? ''));
$active = trim((string)($_GET['active'] ?? ''));

$where = [];
$params = [];

if ($specialty !== '') {
    $where[] = 'specialty = :specialty';
    $params[':specialty'] = $specialty;
}
if ($city !== '') {
    $where[] = 'city = :city';
    $params[':city'] = $city;
}
if ($active !== '') {
    $where[] = 'active = :active';
    $params[':active'] = (int)$active;
}

$whereSql = $where ? ('WHERE ' . implode(' AND ', $where)) : '';

$stmt = $pdo->prepare("SELECT * FROM external_partners $whereSql ORDER BY company_name ASC, contact_person ASC");
$stmt->execute($params);
$rows = $stmt->fetchAll();

$specialties = $pdo->query("SELECT DISTINCT specialty FROM external_partners WHERE specialty IS NOT NULL AND specialty <> '' ORDER BY specialty ASC")->fetchAll(PDO::FETCH_COLUMN);
$cities = $pdo->query("SELECT DISTINCT city FROM external_partners WHERE city IS NOT NULL AND city <> '' ORDER BY city ASC")->fetchAll(PDO::FETCH_COLUMN);

include __DIR__ . '/../includes/header.php';
?>
<div class="page-header">
  <div class="page-header-title">
    <h1>External partners</h1>
    <span>Suppliers, subcontractors, external crews</span>
  </div>
  <div class="page-header-actions">
    <a class="btn btn-primary btn-sm" href="/public/partners/edit.php">+ New partner</a>
  </div>
</div>

<div class="card" style="margin-bottom:14px">
  <form method="get">
    <div class="form-grid">
      <div class="form-group">
        <label>Specialty</label>
        <select name="specialty">
          <option value="">All</option>
          <?php foreach ($specialties as $s): ?>
            <option value="<?php echo h($s); ?>" <?php echo $specialty === $s ? 'selected' : ''; ?>><?php echo h($s); ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="form-group">
        <label>City</label>
        <select name="city">
          <option value="">All</option>
          <?php foreach ($cities as $c): ?>
            <option value="<?php echo h($c); ?>" <?php echo $city === $c ? 'selected' : ''; ?>><?php echo h($c); ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="form-group">
        <label>Active</label>
        <select name="active">
          <option value="">All</option>
          <option value="1" <?php echo $active === '1' ? 'selected' : ''; ?>>Active</option>
          <option value="0" <?php echo $active === '0' ? 'selected' : ''; ?>>Inactive</option>
        </select>
      </div>
    </div>
    <div class="form-actions">
      <button class="btn btn-primary" type="submit">Filter</button>
      <a class="btn btn-secondary" href="/public/partners/index.php">Clear</a>
    </div>
  </form>
</div>

<div class="card">
  <div class="table-responsive">
    <table class="table">
      <thead>
        <tr>
          <th>Company</th>
          <th>Contact</th>
          <th>Phone</th>
          <th>Specialty</th>
          <th>City</th>
          <th>Rate</th>
          <th>Status</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
      <?php foreach ($rows as $row): ?>
        <tr>
          <td><?php echo h($row['company_name']); ?></td>
          <td><?php echo h($row['contact_person']); ?></td>
          <td><?php echo h($row['phone']); ?></td>
          <td><?php echo h($row['specialty']); ?></td>
          <td><?php echo h($row['city']); ?></td>
          <td>
            <?php
              $rate = $row['rate_amount'] !== null ? '€ ' . number_format((float)$row['rate_amount'], 2) . ' / ' . str_replace('_', ' ', (string)$row['rate_type']) : '-';
              echo h($rate);
            ?>
          </td>
          <td><span class="badge"><?php echo ((int)$row['active'] === 1) ? 'Active' : 'Inactive'; ?></span></td>
          <td>
            <div class="form-actions" style="margin-top:0">
              <a class="btn btn-sm btn-outline-light" href="/public/partners/edit.php?id=<?php echo (int)$row['id']; ?>">Open</a>
              <a class="btn btn-sm btn-outline-light" href="/public/partners/history.php?id=<?php echo (int)$row['id']; ?>">History</a>
            </div>
          </td>
        </tr>
      <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
