<?php
declare(strict_types=1);

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

$conn = iscms_init();
$pageTitle = 'Import and export permits';
$activeNav = 'import_export';

$sql = "SELECT p.id, p.permit_number, p.permit_type, p.status, p.quantity, p.quantity_unit,
               p.declared_value, p.currency, p.hs_code, p.port_of_entry, p.country_of_origin,
               p.destination_country, p.issue_date, p.expiry_date, p.clearance_status,
               o.org_name, pr.product_name,
               a.application_number
        FROM import_export_permits p
        INNER JOIN organizations o ON p.org_id = o.id
        INNER JOIN products pr ON p.product_id = pr.id
        LEFT JOIN applications a ON p.application_id = a.id
        ORDER BY p.updated_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>Import and export permits</h1>
        <p>Trade permits with quantities, routing, and clearance tracking.</p>
    </div>

    <div class="section-card mb-4">
        <div class="section-header">
            <h2><i class="ri-links-line"></i> Related registers</h2>
        </div>
        <div class="quick-actions">
            <a class="qa-btn" href="shipment_verifications.php"><i class="ri-ship-line"></i> Shipment verifications</a>
            <a class="qa-btn" href="permit_documents.php"><i class="ri-attachment-2"></i> Permit documents</a>
            <a class="qa-btn" href="certifications.php"><i class="ri-shield-star-line"></i> Certifications</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-ship-line"></i> Permits</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>Permit</th>
                        <th>Type</th>
                        <th>Organisation</th>
                        <th>Product</th>
                        <th>Application</th>
                        <th>Qty</th>
                        <th>Declared</th>
                        <th>HS</th>
                        <th>Status</th>
                        <th>Clearance</th>
                        <th>Issue</th>
                        <th>Expiry</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="12"><i class="ri-inbox-line me-2"></i>No permits</td></tr>
                    <?php else: ?>
                        <?php foreach ($rows as $r): ?>
                        <?php
                            $qty = isset($r['quantity']) ? (string) $r['quantity'] : '';
                            $unit = (string) ($r['quantity_unit'] ?? '');
                            $qtyDisp = trim($qty . ' ' . $unit);
                            $val = isset($r['declared_value']) ? (float) $r['declared_value'] : 0;
                            $cur = (string) ($r['currency'] ?? 'USD');
                            $valDisp = $val > 0 ? $cur . ' ' . number_format($val, 2) : '-';
                        ?>
                        <tr>
                            <td class="iscms-mono" style="font-weight:600; color:var(--ssbs-blue)"><?= iscms_h($r['permit_number']) ?></td>
                            <td><small><?= iscms_h(str_replace('_', ' ', (string) $r['permit_type'])) ?></small></td>
                            <td><small><?= iscms_h($r['org_name']) ?></small></td>
                            <td><small><?= iscms_h($r['product_name']) ?></small></td>
                            <td class="iscms-mono"><small><?= iscms_val($r['application_number'] ?? null) ?></small></td>
                            <td><small><?= iscms_h($qtyDisp) ?></small></td>
                            <td><small><?= iscms_h($valDisp) ?></small></td>
                            <td class="iscms-mono"><small><?= iscms_val($r['hs_code'] ?? null) ?></small></td>
                            <td><?= iscms_status_badge((string) ($r['status'] ?? '')) ?></td>
                            <td><small><?= iscms_val($r['clearance_status'] ?? null) ?></small></td>
                            <td><small class="text-muted"><?= iscms_val($r['issue_date'] ?? null) ?></small></td>
                            <td><small class="text-muted"><?= iscms_val($r['expiry_date'] ?? null) ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
