<form method="post" action="<?= e($action) ?>" class="form-stack">
    <?= csrf_field() ?>
    <?php if (!empty($carrier['id'])): ?><input type="hidden" name="id" value="<?= e((string)$carrier['id']) ?>"><?php endif; ?>

    <section class="form-section">
        <div class="form-section-head"><div><p class="eyebrow">Carrier Profile</p><h3>Company & Contact</h3></div><span class="section-chip">Required onboarding details</span></div>
        <div class="form-grid three">
            <label><span>Legal Company Name *</span><input name="company_name" value="<?= e(old('company_name',$carrier['company_name']??'')) ?>" required></label>
            <label><span>DBA / Trade Name</span><input name="dba_name" value="<?= e(old('dba_name',$carrier['dba_name']??'')) ?>"></label>
            <label><span>Primary Contact</span><input name="contact_name" value="<?= e(old('contact_name',$carrier['contact_name']??'')) ?>"></label>
            <label><span>Email</span><input type="email" name="email" value="<?= e(old('email',$carrier['email']??'')) ?>"></label>
            <label><span>Phone</span><input name="phone" value="<?= e(old('phone',$carrier['phone']??'')) ?>"></label>
            <label><span>Tax ID / EIN</span><input name="tax_id" value="<?= e(old('tax_id',$carrier['tax_id']??'')) ?>"></label>
            <label class="field-full"><span>Address</span><input name="address" value="<?= e(old('address',$carrier['address']??'')) ?>"></label>
        </div>
    </section>

    <section class="form-section">
        <div class="form-section-head"><div><p class="eyebrow">Compliance</p><h3>Authority, Insurance & Safety</h3></div></div>
        <div class="form-grid three">
            <label><span>MC Number</span><input name="mc_number" value="<?= e(old('mc_number',$carrier['mc_number']??'')) ?>"></label>
            <label><span>DOT Number</span><input name="dot_number" value="<?= e(old('dot_number',$carrier['dot_number']??'')) ?>"></label>
            <label><span>Authority Status</span><select name="authority_status"><?php foreach(['active','pending','inactive','revoked'] as $value): ?><option value="<?= e($value) ?>" <?= old('authority_status',$carrier['authority_status']??'active')===$value?'selected':'' ?>><?= e(readable_status($value)) ?></option><?php endforeach; ?></select></label>
            <label><span>Auto Liability Expiry</span><input type="date" name="insurance_expiry" value="<?= e(old('insurance_expiry',$carrier['insurance_expiry']??'')) ?>"></label>
            <label><span>Cargo Insurance Expiry</span><input type="date" name="cargo_insurance_expiry" value="<?= e(old('cargo_insurance_expiry',$carrier['cargo_insurance_expiry']??'')) ?>"></label>
            <label><span>Safety Rating</span><input name="safety_rating" value="<?= e(old('safety_rating',$carrier['safety_rating']??'')) ?>" placeholder="Satisfactory, Conditional, Not Rated"></label>
        </div>
    </section>

    <section class="form-section">
        <div class="form-section-head"><div><p class="eyebrow">Dispatch Agreement</p><h3>Fee & Operating Status</h3></div></div>
        <div class="form-grid three">
            <label><span>Dispatch Fee Type</span><select name="dispatch_fee_type">
                <option value="percentage" <?= old('dispatch_fee_type',$carrier['dispatch_fee_type']??'percentage')==='percentage'?'selected':'' ?>>Percentage of load</option>
                <option value="fixed_per_load" <?= old('dispatch_fee_type',$carrier['dispatch_fee_type']??'')==='fixed_per_load'?'selected':'' ?>>Fixed per load</option>
                <option value="monthly" <?= old('dispatch_fee_type',$carrier['dispatch_fee_type']??'')==='monthly'?'selected':'' ?>>Monthly retainer</option>
            </select></label>
            <label><span>Fee Value</span><input type="number" step="0.01" name="dispatch_fee_value" value="<?= e(old('dispatch_fee_value',$carrier['dispatch_fee_value']??'0')) ?>"></label>
            <label><span>Settlement Terms</span><input name="payment_terms" value="<?= e(old('payment_terms',$carrier['payment_terms']??'Weekly')) ?>"></label>
            <label><span>Onboarding Stage</span><select name="onboarding_status"><?php foreach(['lead','documents_pending','review','approved','on_hold','inactive'] as $value): ?><option value="<?= e($value) ?>" <?= old('onboarding_status',$carrier['onboarding_status']??'documents_pending')===$value?'selected':'' ?>><?= e(readable_status($value)) ?></option><?php endforeach; ?></select></label>
            <label><span>Operating Status</span><select name="status"><?php foreach(['active','on_hold','inactive'] as $value): ?><option value="<?= e($value) ?>" <?= old('status',$carrier['status']??'active')===$value?'selected':'' ?>><?= e(readable_status($value)) ?></option><?php endforeach; ?></select></label>
            <label class="field-full"><span>Internal Notes</span><textarea name="notes" rows="4"><?= e(old('notes',$carrier['notes']??'')) ?></textarea></label>
        </div>
    </section>

    <div class="form-actions sticky-form-actions">
        <button class="btn btn-primary" type="submit">Save Carrier</button>
        <a class="btn btn-light" href="<?= url('/carriers') ?>">Cancel</a>
    </div>
</form>
