<?php $pageTitle = 'Certificate — ' . e($cert['cert_number']); include __DIR__ . '/../shared/header.php'; ?>

<div class="page-header">
  <div>
    <div class="page-title"><i class="bi bi-award me-2"></i>Calibration Certificate</div>
    <div class="page-subtitle"><code><?= e($cert['cert_number']) ?></code></div>
  </div>
  <div class="d-flex gap-2 flex-wrap">
    <a href="<?= APP_URL ?>/index.php?page=certificates&action=download&id=<?= $cert['cert_id'] ?>&type=pdf"
       class="btn btn-danger">
      <i class="bi bi-file-earmark-pdf me-1"></i>Download PDF
    </a>
    <a href="<?= APP_URL ?>/index.php?page=certificates&action=download&id=<?= $cert['cert_id'] ?>&type=xml"
       class="btn btn-outline-secondary">
      <i class="bi bi-file-earmark-code me-1"></i>Download DCC XML
    </a>
    <button onclick="window.print()" class="btn btn-outline-primary">
      <i class="bi bi-printer me-1"></i>Print
    </button>
    <a href="<?= APP_URL ?>/index.php?page=certificates" class="btn btn-outline-secondary">
      <i class="bi bi-arrow-left me-1"></i>Back
    </a>
  </div>
</div>

<!-- On-screen certificate preview -->
<div class="cert-preview" id="certPrintArea">

  <!-- Header -->
  <div style="display:flex;align-items:flex-start;justify-content:space-between;border-bottom:3px solid #003366;padding-bottom:14px;margin-bottom:18px;">
    <div>
      <div style="font-size:22px;font-weight:800;color:#003366;">Ghana Standards Authority</div>
      <div style="font-size:13px;color:#555;margin-top:2px;"><?= e($request['lab_name']) ?> — <?= e($request['lab_code']) ?></div>
      <div style="font-size:11px;color:#888;margin-top:3px;">P.O. Box MB 245, Shiashie, Accra, Ghana &bull; scientificmetrology@gsa.gov.gh &bull; +233-302-500065</div>
    </div>
    <div style="text-align:right;">
      <div style="font-size:11px;color:#888;">Accredited Calibration Laboratory</div>
      <div style="font-size:11px;color:#888;">ISO/IEC 17025</div>
    </div>
  </div>

  <!-- Title -->
  <div style="text-align:center;font-size:18px;font-weight:700;text-transform:uppercase;color:#003366;letter-spacing:3px;padding:12px 0;border-top:1px solid #003366;border-bottom:1px solid #003366;margin-bottom:18px;">
    Calibration Certificate
  </div>
  <div style="text-align:center;margin-bottom:18px;">
    <span style="font-size:13px;font-weight:600;">Certificate No.: </span>
    <span style="font-size:14px;font-weight:800;color:#003366;"><?= e($cert['cert_number']) ?></span>
    <button id="copyCertNum" data-cert="<?= e($cert['cert_number']) ?>" class="btn btn-xs btn-outline-secondary btn-sm ms-2 py-0 px-2" style="font-size:11px;">Copy</button>
  </div>

  <!-- 2-col info -->
  <div class="row g-3 mb-3">
    <div class="col-md-6">
      <div style="background:#f8f9fa;border-radius:8px;padding:14px;">
        <div style="font-size:11px;font-weight:700;text-transform:uppercase;color:#003366;border-bottom:1px solid #dee2e6;padding-bottom:5px;margin-bottom:10px;">Customer</div>
        <table class="table table-sm mb-0" style="font-size:12.5px;">
          <tr><td style="color:#888;width:40%">Company</td><td class="fw-semibold"><?= e($cert['company_name']) ?></td></tr>
          <tr><td style="color:#888">Address</td><td><?= e($cert['co_address'] ?? '—') ?></td></tr>
          <tr><td style="color:#888">Request No.</td><td><code><?= e($cert['request_number']) ?></code></td></tr>
          <tr><td style="color:#888">Date Received</td><td><?= date('d M Y', strtotime($request['request_date'])) ?></td></tr>
          <tr><td style="color:#888">Date Issued</td><td><?= date('d M Y', strtotime($cert['issue_date'])) ?></td></tr>
        </table>
      </div>
    </div>
    <div class="col-md-6">
      <div style="background:#f8f9fa;border-radius:8px;padding:14px;">
        <div style="font-size:11px;font-weight:700;text-transform:uppercase;color:#003366;border-bottom:1px solid #dee2e6;padding-bottom:5px;margin-bottom:10px;">Instrument Under Calibration</div>
        <table class="table table-sm mb-0" style="font-size:12.5px;">
          <tr><td style="color:#888;width:40%">Description</td><td class="fw-semibold"><?= e($cert['equipment_name']) ?></td></tr>
          <tr><td style="color:#888">Manufacturer</td><td><?= e($request['manufacturer'] ?? '—') ?></td></tr>
          <tr><td style="color:#888">Model</td><td><?= e($cert['model'] ?? '—') ?></td></tr>
          <tr><td style="color:#888">Serial No.</td><td><code><?= e($cert['serial_number']) ?></code></td></tr>
          <tr><td style="color:#888">Range</td><td><?= e($request['range_capacity'] ?? '—') ?></td></tr>
        </table>
      </div>
    </div>
  </div>

  <!-- Environmental Conditions -->
  <div style="background:#f8f9fa;border-radius:8px;padding:14px;margin-bottom:14px;">
    <div style="font-size:11px;font-weight:700;text-transform:uppercase;color:#003366;border-bottom:1px solid #dee2e6;padding-bottom:5px;margin-bottom:10px;">Environmental Conditions</div>
    <div class="row" style="font-size:12.5px;">
      <div class="col-md-4"><span style="color:#888;">Place: </span><?= e($form['place_of_test'] ?? '—') ?></div>
      <div class="col-md-3"><span style="color:#888;">Temperature: </span><?= e($form['temperature_min']) ?> – <?= e($form['temperature_max']) ?> °C</div>
      <div class="col-md-3"><span style="color:#888;">Humidity: </span><?= e($form['humidity_min']) ?> – <?= e($form['humidity_max']) ?> %RH</div>
      <div class="col-md-2"><span style="color:#888;">Pressure: </span><?= e($form['pressure_min']) ?> – <?= e($form['pressure_max']) ?> hPa</div>
    </div>
  </div>

  <!-- Measurement Results -->
  <div style="margin-bottom:14px;">
    <div style="font-size:11px;font-weight:700;text-transform:uppercase;color:#003366;border-bottom:2px solid #003366;padding-bottom:5px;margin-bottom:10px;">Measurement Results</div>
    <div class="table-responsive">
      <table class="table table-bordered mb-0" style="font-size:12.5px;">
        <thead style="background:#003366;color:#fff;">
          <tr>
            <th>#</th>
            <th>Measurement / Point</th>
            <th>Nominal Value</th>
            <th>Measured Value</th>
            <th>Error</th>
            <th>Expanded Uncertainty</th>
            <th>Unit</th>
          </tr>
        </thead>
        <tbody>
        <?php if (!empty($measData['rows'])): ?>
          <?php foreach ($measData['rows'] as $i => $row): ?>
          <tr>
            <td><?= $i + 1 ?></td>
            <td><?= e($row['name'] ?? '') ?></td>
            <td class="text-end"><?= e($row['nominal'] ?? '') ?></td>
            <td class="text-end"><?= e($row['measured'] ?? '') ?></td>
            <td class="text-end"><?= e($row['error'] ?? '') ?></td>
            <td class="text-end"><?= e($row['uncertainty'] ?? '') ?></td>
            <td><?= e($row['unit'] ?? '') ?></td>
          </tr>
          <?php endforeach; ?>
        <?php else: ?>
          <tr><td colspan="7" class="text-center text-muted">No measurement data recorded.</td></tr>
        <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>

  <!-- Uncertainty Statement -->
  <div style="background:#fffbf0;border:1px solid #fde68a;border-radius:8px;padding:14px;margin-bottom:14px;font-size:12px;">
    <strong>Uncertainty Statement: </strong>
    <?= nl2br(e($form['uncertainty_statement'] ?? '')) ?>
    <div class="mt-1"><strong>Coverage factor k = <?= e($form['coverage_factor']) ?></strong>, confidence level <?= e($form['confidence_level']) ?>.</div>
  </div>

  <?php if (!empty($form['remarks'])): ?>
  <div style="background:#f8f9fa;border-radius:8px;padding:14px;margin-bottom:14px;font-size:12px;">
    <strong>Remarks: </strong><?= nl2br(e($form['remarks'])) ?>
  </div>
  <?php endif; ?>

  <!-- Traceability Statement -->
  <div style="font-size:11px;color:#555;font-style:italic;border-top:1px solid #dee2e6;padding-top:10px;margin-bottom:20px;">
    This calibration certificate documents the metrological traceability to national standards, which realize the units of measurement according to the International System of Units (SI).<br>
    This calibration certificate may not be reproduced other than in full except with the prior written approval of Ghana Standards Authority.
  </div>

  <!-- Signature -->
  <div style="display:flex;justify-content:flex-end;">
    <div style="text-align:center;width:240px;">
      <?php if (!empty($cert['signature']) && file_exists(SIGNATURE_PATH . $cert['signature'])): ?>
        <img src="data:image/png;base64,<?= base64_encode(file_get_contents(SIGNATURE_PATH . $cert['signature'])) ?>" style="max-height:70px;max-width:200px;" alt="Signature">
      <?php else: ?>
        <div style="height:60px;border-bottom:1px solid #000;"></div>
      <?php endif; ?>
      <div style="padding-top:6px;font-size:12px;">
        <strong><?= e($cert['issued_by_name']) ?></strong><br>
        <span style="color:#888;">Laboratory Officer<br><?= e($cert['lab_name']) ?></span>
      </div>
    </div>
  </div>

  <!-- Footer -->
  <div style="border-top:2px solid #003366;margin-top:20px;padding-top:8px;text-align:center;font-size:10px;color:#888;">
    Ghana Standards Authority &mdash; <?= e($cert['lab_name']) ?> &bull;
    Certificate No. <?= e($cert['cert_number']) ?> &bull;
    Issued: <?= date('d M Y', strtotime($cert['issue_date'])) ?>
  </div>
</div>

<style>
@media print {
  .sidebar, .topbar, .page-header, .btn, .alert { display: none !important; }
  .main-content { margin-left: 0 !important; }
  .content-area { padding: 0 !important; }
  .cert-preview { border: none !important; padding: 10mm !important; }
}
</style>

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