<div class="page-heading hos-heading">
    <div><p class="eyebrow">Safety & Compliance</p><h1>Driver Logbooks & HOS</h1><p>Review duty status, remaining clocks, certification gaps, and log corrections across United States and Canadian operations.</p></div>
</div>

<div class="notice notice-warning hos-compliance-notice"><strong>ELD readiness notice:</strong> FleetLume stores operational records of duty status and compliance clocks. It is not represented as a registered U.S. ELD or a Transport Canada-certified ELD unless it is connected to and validated with an approved device implementation.</div>

<div class="stats-grid hos-stats">
    <article class="stat-card"><span>Driving Now</span><strong><?= (int)$summary['driving'] ?></strong><small>Active driving status</small></article>
    <article class="stat-card"><span>Near a Limit</span><strong><?= (int)$summary['near_limit'] ?></strong><small>Two hours or less remaining</small></article>
    <article class="stat-card"><span>Warnings</span><strong><?= (int)$summary['violations'] ?></strong><small>Automated HOS warnings</small></article>
    <article class="stat-card"><span>Edit Reviews</span><strong><?= (int)$summary['pending_edits'] ?></strong><small>Original records preserved</small></article>
</div>

<section class="panel">
    <div class="panel-header"><div><h2>Driver Compliance Board</h2><p>Current duty status and calculated clocks based on recorded events.</p></div></div>
    <div class="hos-driver-grid">
        <?php foreach($drivers as $driver): $c=$driver['clocks']; $danger=!empty($c['violations']); $near=($c['drive_remaining']??9999)<=120||($c['shift_remaining']??9999)<=120||($c['cycle_remaining']??9999)<=300; ?>
        <article class="hos-driver-card <?= $danger?'has-violation':($near?'near-limit':'') ?>">
            <div class="hos-driver-card-head"><div><h3><?= e($driver['name']) ?></h3><p><?= e($driver['carrier_name'] ?: 'No carrier assigned') ?></p></div><span class="hos-duty-pill duty-<?= e($c['current_status']) ?>"><?= e(hos_status_label((string)$c['current_status'],(string)($c['special_status']??''))) ?></span></div>
            <div class="hos-mini-clocks">
                <div><small>Drive</small><strong><?= hos_minutes($c['drive_remaining']) ?></strong><span>remaining</span></div>
                <div><small><?= str_starts_with($c['rule_set'],'canada')?'On duty':'Window' ?></small><strong><?= hos_minutes($c['shift_remaining']) ?></strong><span>remaining</span></div>
                <div><small>Cycle</small><strong><?= hos_minutes($c['cycle_remaining']) ?></strong><span>remaining</span></div>
            </div>
            <div class="hos-driver-meta"><span><?= e(hos_rule_label((string)$c['rule_set'])) ?></span><span><?= e($c['cycle_label']) ?></span></div>
            <?php if($danger): ?><div class="hos-warning-list"><?php foreach($c['violations'] as $warning): ?><span><?= e($warning) ?></span><?php endforeach; ?></div><?php endif; ?>
            <div class="card-actions"><a class="btn btn-primary btn-sm" href="<?= url('/hos/driver?driver_id='.(int)$driver['id']) ?>">Open Logbook</a><?php if(can('hos.manage')): ?><a class="btn btn-light btn-sm" href="<?= url('/hos/profile?driver_id='.(int)$driver['id']) ?>">HOS Profile</a><?php endif; ?></div>
        </article>
        <?php endforeach; ?>
        <?php if(!$drivers): ?><p class="empty">No active drivers found.</p><?php endif; ?>
    </div>
</section>

<?php if(can('hos.manage') && $pendingEdits): ?>
<section class="panel">
    <div class="panel-header"><div><h2>Pending Log Corrections</h2><p>Approve or reject driver-requested changes. Approved changes create a new event version and preserve the original.</p></div></div>
    <div class="hos-edit-list">
        <?php foreach($pendingEdits as $request): ?>
        <article class="hos-edit-card">
            <div><strong><?= e($request['driver_name']) ?></strong><span><?= e(datetime_display($request['original_event_at'])) ?> · <?= e(hos_status_label($request['original_duty_status'],$request['original_special_status'])) ?></span><p><?= e($request['reason']) ?></p></div>
            <div class="hos-edit-change"><small>Requested change</small><strong><?= e(datetime_display($request['proposed_event_at'])) ?></strong><span><?= e(hos_status_label($request['proposed_duty_status'],$request['proposed_special_status'])) ?></span></div>
            <form method="post" action="<?= url('/hos/edits/review') ?>" class="hos-review-form"><?= csrf_field() ?><input type="hidden" name="request_id" value="<?= (int)$request['id'] ?>"><input name="review_notes" placeholder="Review note (optional)"><button class="btn btn-primary btn-sm" name="decision" value="approved">Approve</button><button class="btn btn-light btn-sm" name="decision" value="rejected">Reject</button></form>
        </article>
        <?php endforeach; ?>
    </div>
</section>
<?php endif; ?>
