<?php $pageTitle=$record?'Edit Border Crossing':'New Border Crossing'; ?>
<div class="page-heading"><div><p class="eyebrow">International Operations</p><h1><?= e($pageTitle) ?></h1></div><a class="btn btn-light" href="<?= url('/borders') ?>">Back</a></div>
<form class="panel form-stack" method="post" action="<?= url($record?'/borders/update':'/borders/store') ?>"><?= csrf_field() ?><?php if($record):?><input type="hidden" name="id" value="<?= e((string)$record['id']) ?>"><?php endif;?>
<div class="form-grid three"><label><span>Crossing Number</span><input name="crossing_number" value="<?= e($record['crossing_number']??'') ?>" placeholder="Auto if blank"></label><label><span>Load</span><select name="load_id" required><option value="">Select load</option><?php foreach($loads as$l):?><option value="<?= e((string)$l['id']) ?>" <?= (int)($record['load_id']??0)===(int)$l['id']?'selected':'' ?>><?= e($l['load_number']) ?></option><?php endforeach;?></select></label><label><span>Customs Broker</span><select name="customs_broker_id"><option value="">Not assigned</option><?php foreach($brokers as$b):?><option value="<?= e((string)$b['id']) ?>" <?= (int)($record['customs_broker_id']??0)===(int)$b['id']?'selected':'' ?>><?= e($b['company_name']) ?></option><?php endforeach;?></select></label><label><span>Crossing / Port Name</span><input name="crossing_name" value="<?= e($record['crossing_name']??'') ?>" required></label><label><span>Port Code</span><input name="port_code" value="<?= e($record['port_code']??'') ?>"></label><label><span>Planned Date & Time</span><input type="datetime-local" name="planned_at" value="<?= !empty($record['planned_at'])?e(date('Y-m-d\TH:i',strtotime($record['planned_at']))):'' ?>"></label><label><span>Origin Country</span><input name="origin_country" value="<?= e($record['origin_country']??'United States') ?>" required></label><label><span>Destination Country</span><input name="destination_country" value="<?= e($record['destination_country']??'Canada') ?>" required></label><label><span>Direction</span><input name="direction" value="<?= e($record['direction']??'') ?>" placeholder="Northbound / Southbound"></label><label><span>Status</span><select name="status"><?php foreach($statuses as$s):?><option value="<?= e($s) ?>" <?= ($record['status']??'not_started')===$s?'selected':'' ?>><?= e(readable_status($s)) ?></option><?php endforeach;?></select></label><label><span>Reference Number</span><input name="reference_number" value="<?= e($record['reference_number']??'') ?>"></label><label><span>Entry Number</span><input name="entry_number" value="<?= e($record['entry_number']??'') ?>"></label><label><span>Seal Number</span><input name="seal_number" value="<?= e($record['seal_number']??'') ?>"></label><label><span>Commodity</span><input name="commodity" value="<?= e($record['commodity']??'') ?>"></label><label><span>Packages</span><input type="number" name="package_count" value="<?= e((string)($record['package_count']??'')) ?>"></label><label><span>Weight</span><input type="number" step="0.01" name="weight" value="<?= e((string)($record['weight']??'')) ?>"></label><label><span>Latitude</span><input name="latitude" value="<?= e((string)($record['latitude']??'')) ?>"></label><label><span>Longitude</span><input name="longitude" value="<?= e((string)($record['longitude']??'')) ?>"></label><label><span>Geofence Radius (m)</span><input type="number" name="geofence_radius_m" value="<?= e((string)($record['geofence_radius_m']??1500)) ?>"></label><label><span>Detention Rate / Hour</span><input type="number" step="0.01" name="detention_rate" value="<?= e((string)($record['detention_rate']??0)) ?>"></label></div>
<label><span>Driver Instructions</span><textarea name="driver_instructions" rows="3"><?= e($record['driver_instructions']??'') ?></textarea></label><label><span>Dispatcher Notes</span><textarea name="dispatcher_notes" rows="3"><?= e($record['dispatcher_notes']??'') ?></textarea></label>
<?php if(!$record):?><label><span>Document Checklist (one item per line)</span><textarea name="checklist_items[]" rows="8" disabled>Default border checklist will be created automatically.</textarea></label><?php endif;?>
<div class="form-actions"><button class="btn btn-primary"><?= $record?'Save Changes':'Create Crossing' ?></button></div></form>
<?php if(!$record):?><div class="panel" style="margin-top:18px"><h2>Quick Add Customs Broker</h2><form class="form-grid three" method="post" action="<?= url('/borders/brokers/store') ?>"><?= csrf_field() ?><label><span>Company</span><input name="company_name" required></label><label><span>Contact</span><input name="contact_name"></label><label><span>Phone</span><input name="phone"></label><label><span>Email</span><input type="email" name="email"></label><label><span>License</span><input name="license_number"></label><button class="btn btn-light" style="align-self:end">Add Broker</button></form></div><?php endif;?>
