<?php $pageTitle='Carriers'; ?>
<div class="panel">
    <?php $title='Carrier Companies';$eyebrow='Dispatch Network';$searchAction=url('/carriers');$createUrl=url('/carriers/create');$createText='Add Carrier';require app_path('Views/components/search_header.php'); ?>
    <div class="carrier-summary-strip">
        <div><strong><?= count($carriers) ?></strong><span>Carrier accounts</span></div>
        <div><strong><?= count(array_filter($carriers,fn($c)=>$c['status']==='active')) ?></strong><span>Active</span></div>
        <div><strong><?= array_sum(array_map(fn($c)=>(int)$c['active_load_count'],$carriers)) ?></strong><span>Open loads</span></div>
        <div><strong><?= money(array_sum(array_map(fn($c)=>(float)$c['dispatch_revenue'],$carriers))) ?></strong><span>Recorded dispatch fees</span></div>
    </div>
    <div class="table-wrap">
        <table>
            <thead><tr><th>Carrier</th><th>Authority</th><th>Dispatch Terms</th><th>Resources</th><th>Onboarding</th><th>Status</th><th class="text-right">Actions</th></tr></thead>
            <tbody>
            <?php foreach($carriers as $carrier): ?>
                <tr>
                    <td><strong><?= e($carrier['company_name']) ?></strong><?php if($carrier['dba_name']): ?><br><small>DBA <?= e($carrier['dba_name']) ?></small><?php endif; ?><br><small><?= e($carrier['contact_name']?:$carrier['email']?:$carrier['phone']?:'No contact') ?></small></td>
                    <td><?= e($carrier['mc_number']?:'No MC') ?><br><small><?= e($carrier['dot_number']?:'No DOT') ?> · <?= e(readable_status($carrier['authority_status'])) ?></small></td>
                    <td><?= $carrier['dispatch_fee_type']==='percentage'?e((string)$carrier['dispatch_fee_value']).'%':money($carrier['dispatch_fee_value']) ?><br><small><?= e(readable_status($carrier['dispatch_fee_type'])) ?> · <?= e($carrier['payment_terms']) ?></small></td>
                    <td><strong><?= (int)$carrier['driver_count'] ?></strong> drivers · <strong><?= (int)$carrier['truck_count'] ?></strong> trucks<br><small><?= (int)$carrier['trailer_count'] ?> trailers · <?= (int)$carrier['active_load_count'] ?> open loads</small></td>
                    <td><span class="badge badge-<?= status_class($carrier['onboarding_status']) ?>"><?= e(readable_status($carrier['onboarding_status'])) ?></span><br><small>Insurance <?= e(date_display($carrier['insurance_expiry'])) ?></small></td>
                    <td><span class="badge badge-<?= status_class($carrier['status']) ?>"><?= e(readable_status($carrier['status'])) ?></span></td>
                    <td class="text-right table-actions"><a class="btn btn-light btn-sm" href="<?= url('/carriers/show?id='.$carrier['id']) ?>">Open</a><a class="btn btn-light btn-sm" href="<?= url('/carriers/edit?id='.$carrier['id']) ?>">Edit</a></td>
                </tr>
            <?php endforeach; ?>
            <?php if(!$carriers): ?><tr><td colspan="7" class="empty">No carrier companies found.</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
