<?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 = 'Partner history';

$id = (int)($_GET['id'] ?? 0);
if ($id <= 0) die('Missing partner id.');

$stmt = $pdo->prepare("SELECT * FROM external_partners WHERE id = :id LIMIT 1");
$stmt->execute([':id' => $id]);
$partner = $stmt->fetch();
if (!$partner) die('Partner not found.');

$stmt = $pdo->prepare("
    SELECT pp.*, p.code, p.title AS project_title
    FROM project_partners pp
    LEFT JOIN projects p ON p.id = pp.project_id
    WHERE pp.external_partner_id = :id
    ORDER BY pp.work_date DESC, pp.id DESC
");
$stmt->execute([':id' => $id]);
$rows = $stmt->fetchAll();

$totalAgreed = 0.0;
$totalPaid = 0.0;
foreach ($rows as $r) {
    $totalAgreed += (float)($r['agreed_cost'] ?? 0);
    $totalPaid += (float)($r['paid_amount'] ?? 0);
}
$balance = $totalAgreed - $totalPaid;

include __DIR__ . '/../includes/header.php';
?>
<div class="page-header">
  <div class="page-header-title">
    <h1>Partner history</h1>
    <span><?php echo h($partner['company_name']); ?></span>
  </div>
  <div class="page-header-actions">
    <a class="btn btn-secondary btn-sm" href="/public/partners/edit.php?id=<?php echo (int)$id; ?>">Back to partner</a>
  </div>
</div>

<div class="grid-4" style="margin-bottom:14px">
  <div class="card"><div class="small">Total agreed</div><div style="font-size:1.6rem;font-weight:700">€ <?php echo h(number_format($totalAgreed, 2)); ?></div></div>
  <div class="card"><div class="small">Total paid</div><div style="font-size:1.6rem;font-weight:700">€ <?php echo h(number_format($totalPaid, 2)); ?></div></div>
  <div class="card"><div class="small">Balance</div><div style="font-size:1.6rem;font-weight:700">€ <?php echo h(number_format($balance, 2)); ?></div></div>
  <div class="card"><div class="small">Specialty</div><div style="font-size:1.1rem;font-weight:700"><?php echo h($partner['specialty']); ?></div></div>
</div>

<div class="card">
  <div class="table-responsive">
    <table class="table">
      <thead><tr><th>Project</th><th>Date</th><th>Description</th><th>Agreed</th><th>Paid</th><th>Status</th><th>Notes</th></tr></thead>
      <tbody>
      <?php foreach ($rows as $r): ?>
        <tr>
          <td><?php echo h(($r['code'] ?? '') . ' · ' . ($r['project_title'] ?? '')); ?></td>
          <td><?php echo h($r['work_date']); ?></td>
          <td><?php echo h($r['description']); ?></td>
          <td>€ <?php echo h(number_format((float)($r['agreed_cost'] ?? 0), 2)); ?></td>
          <td>€ <?php echo h(number_format((float)($r['paid_amount'] ?? 0), 2)); ?></td>
          <td><?php echo h($r['status']); ?></td>
          <td><?php echo h($r['notes']); ?></td>
        </tr>
      <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
