<?php
declare(strict_types=1);

require_once __DIR__ . '/includes/iscms_layout.php';

$conn = iscms_init();
$pageTitle = 'Shipment verifications';
$activeNav = 'import_export';

$sql = "SELECT v.id, v.permit_id, v.verification_number, v.vessel_or_flight, v.arrival_date,
               v.inspection_date, v.actual_quantity, v.quantity_unit, v.conformity_status,
               v.clearance_cert_issued, v.verified_at,
               p.permit_number,
               CONCAT(TRIM(u.first_name),' ',TRIM(u.last_name)) AS verified_by_name
        FROM shipment_verifications v
        INNER JOIN import_export_permits p ON v.permit_id = p.id
        INNER JOIN users u ON v.verified_by = u.id
        ORDER BY v.verified_at DESC
        LIMIT 400";
$result = $conn->query($sql);
$rows = $result ? $result->fetch_all(MYSQLI_ASSOC) : [];
$dbError = $result ? null : $conn->error;

iscms_render_head($pageTitle);
iscms_render_sidebar($activeNav);
iscms_render_topbar($pageTitle);
?>
    <div class="page-header">
        <h1>Shipment verifications</h1>
        <p>Physical verification and conformity outcomes against permits.</p>
    </div>

    <div class="section-card mb-4">
        <div class="section-header">
            <h2><i class="ri-links-line"></i> Quick links</h2>
        </div>
        <div class="quick-actions">
            <a class="qa-btn" href="import_export_permits.php"><i class="ri-ship-line"></i> Permits</a>
            <a class="qa-btn" href="permit_documents.php"><i class="ri-attachment-2"></i> Documents</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-checkbox-circle-line"></i> Verifications</h2>
            <span class="text-muted" style="font-size:.75rem"><?= count($rows) ?> shown</span>
        </div>
        <div class="table-responsive">
            <?php if ($dbError): ?>
                <div class="alert alert-danger m-3" style="font-size:.85rem">Unable to load data: <?= iscms_h($dbError) ?></div>
            <?php else: ?>
            <table class="ssbs-table">
                <thead>
                    <tr>
                        <th>Verification</th>
                        <th>Permit</th>
                        <th>Vessel / flight</th>
                        <th>Arrival</th>
                        <th>Inspection</th>
                        <th>Actual qty</th>
                        <th>Conformity</th>
                        <th>Clearance cert</th>
                        <th>Verified by</th>
                        <th>Recorded</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="10"><i class="ri-inbox-line me-2"></i>No verifications</td></tr>
                    <?php else: ?>
                        <?php foreach ($rows as $r): ?>
                        <?php
                            $aq = $r['actual_quantity'] ?? null;
                            $qu = (string) ($r['quantity_unit'] ?? '');
                            $qtyDisp = $aq !== null && $aq !== '' ? trim((string) $aq . ' ' . $qu) : '';
                        ?>
                        <tr>
                            <td class="iscms-mono" style="font-weight:600; color:var(--ssbs-blue)"><?= iscms_h($r['verification_number']) ?></td>
                            <td class="iscms-mono"><small><?= iscms_h($r['permit_number']) ?></small></td>
                            <td><small><?= iscms_val($r['vessel_or_flight'] ?? null) ?></small></td>
                            <td><small class="text-muted"><?= iscms_val($r['arrival_date'] ?? null) ?></small></td>
                            <td><small class="text-muted"><?= iscms_val($r['inspection_date'] ?? null) ?></small></td>
                            <td><small><?= iscms_val($qtyDisp !== '' ? $qtyDisp : null) ?></small></td>
                            <td><?= iscms_status_badge((string) ($r['conformity_status'] ?? '')) ?></td>
                            <td><?= !empty($r['clearance_cert_issued']) ? '<span class="badge bg-success-subtle text-success" style="font-size:.65rem">Yes</span>' : '<span class="badge bg-secondary-subtle text-secondary" style="font-size:.65rem">No</span>' ?></td>
                            <td><small><?= iscms_h(trim((string) ($r['verified_by_name'] ?? ''))) ?></small></td>
                            <td><small class="text-muted"><?= !empty($r['verified_at']) ? iscms_h(substr((string) $r['verified_at'], 0, 19)) : '-' ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
