@csrf

<div class="row g-3">
    <div class="col-md-6">
        <label for="name" class="form-label fw-semibold">Company Name</label>
        <input id="name" name="name" type="text" value="{{ old('name', $company->name) }}" required
               class="form-control @error('name') is-invalid @enderror">
        @error('name')<div class="invalid-feedback">{{ $message }}</div>@enderror
    </div>

    <div class="col-md-6">
        <label for="slug" class="form-label fw-semibold">Slug</label>
        <input id="slug" name="slug" type="text" value="{{ old('slug', $company->slug) }}" required
               class="form-control @error('slug') is-invalid @enderror">
        @error('slug')<div class="invalid-feedback">{{ $message }}</div>@enderror
    </div>

    <div class="col-md-6">
        <label for="code" class="form-label fw-semibold">Company Code</label>
        <input id="code" name="code" type="text" value="{{ old('code', $company->code) }}" required
               class="form-control @error('code') is-invalid @enderror">
        <div class="form-text">The code this company's users type on the login screen. Code 123 is reserved for the Super Admin.</div>
        @error('code')<div class="invalid-feedback">{{ $message }}</div>@enderror
    </div>

    <div class="col-12">
        <label for="address" class="form-label fw-semibold">Address</label>
        <input id="address" name="address" type="text" value="{{ old('address', $company->address) }}"
               class="form-control @error('address') is-invalid @enderror">
        @error('address')<div class="invalid-feedback">{{ $message }}</div>@enderror
    </div>

    <div class="col-md-4">
        <label for="contact_person" class="form-label fw-semibold">Contact Person</label>
        <input id="contact_person" name="contact_person" type="text" value="{{ old('contact_person', $company->contact_person) }}"
               class="form-control @error('contact_person') is-invalid @enderror">
        @error('contact_person')<div class="invalid-feedback">{{ $message }}</div>@enderror
    </div>

    <div class="col-md-4">
        <label for="contact_phone" class="form-label fw-semibold">Contact Phone</label>
        <input id="contact_phone" name="contact_phone" type="text" value="{{ old('contact_phone', $company->contact_phone) }}"
               class="form-control @error('contact_phone') is-invalid @enderror">
        @error('contact_phone')<div class="invalid-feedback">{{ $message }}</div>@enderror
    </div>

    <div class="col-md-4">
        <label for="contact_email" class="form-label fw-semibold">Contact Email</label>
        <input id="contact_email" name="contact_email" type="email" value="{{ old('contact_email', $company->contact_email) }}"
               class="form-control @error('contact_email') is-invalid @enderror">
        @error('contact_email')<div class="invalid-feedback">{{ $message }}</div>@enderror
    </div>

    <div class="col-md-4">
        <label for="country_id" class="form-label fw-semibold">Country</label>
        <select id="country_id" name="country_id" class="form-select @error('country_id') is-invalid @enderror">
            <option value="">— Select —</option>
            @foreach ($countries as $country)
                <option value="{{ $country->id }}" @selected(old('country_id', $company->country_id) == $country->id)>{{ $country->name }}</option>
            @endforeach
        </select>
        @error('country_id')<div class="invalid-feedback">{{ $message }}</div>@enderror
    </div>

    <div class="col-md-4">
        <label for="currency_id" class="form-label fw-semibold">Currency</label>
        <select id="currency_id" name="currency_id" class="form-select @error('currency_id') is-invalid @enderror">
            <option value="">— Select —</option>
            @foreach ($currencies as $currency)
                <option value="{{ $currency->id }}" @selected(old('currency_id', $company->currency_id) == $currency->id)>
                    {{ $currency->name }} ({{ $currency->code }})
                </option>
            @endforeach
        </select>
        @error('currency_id')<div class="invalid-feedback">{{ $message }}</div>@enderror
    </div>

    <div class="col-md-4">
        <label for="distance_unit" class="form-label fw-semibold">Distance Unit</label>
        <select id="distance_unit" name="distance_unit" class="form-select @error('distance_unit') is-invalid @enderror">
            <option value="mi" @selected(old('distance_unit', $company->distance_unit) == 'mi')>Miles</option>
            <option value="km" @selected(old('distance_unit', $company->distance_unit) == 'km')>Kilometers</option>
        </select>
        @error('distance_unit')<div class="invalid-feedback">{{ $message }}</div>@enderror
    </div>

    <div class="col-md-4">
        <label for="timezone" class="form-label fw-semibold">Timezone</label>
        <input id="timezone" name="timezone" type="text" value="{{ old('timezone', $company->timezone) }}" required
               class="form-control @error('timezone') is-invalid @enderror">
        @error('timezone')<div class="invalid-feedback">{{ $message }}</div>@enderror
    </div>

    <div class="col-md-4">
        <label for="valid_until" class="form-label fw-semibold">Valid Until</label>
        <input id="valid_until" name="valid_until" type="date" value="{{ old('valid_until', optional($company->valid_until)->format('Y-m-d')) }}"
               class="form-control @error('valid_until') is-invalid @enderror">
        @error('valid_until')<div class="invalid-feedback">{{ $message }}</div>@enderror
    </div>

    <div class="col-12">
        <div class="form-check form-switch">
            <input class="form-check-input" type="checkbox" name="is_active" id="is_active" value="1"
                   @checked(old('is_active', $company->is_active)) >
            <label class="form-check-label fw-semibold" for="is_active">Active</label>
        </div>
    </div>

    <div class="col-12">
        <label class="form-label fw-semibold d-block">Features</label>
        <div class="form-text mb-2">Turn a capability off for this company only — groundwork for tiered plans. Everything defaults to on.</div>
        @foreach (\App\Models\Company::FEATURES as $key => $default)
            <div class="form-check form-switch">
                <input class="form-check-input" type="checkbox" name="feature_flags[{{ $key }}]" id="feature_{{ $key }}" value="1"
                       @checked(old("feature_flags.$key", $company->feature_flags[$key] ?? $default))>
                <label class="form-check-label" for="feature_{{ $key }}">{{ str($key)->headline() }}</label>
            </div>
        @endforeach
    </div>
</div>

<div class="d-flex gap-2 mt-4">
    <button type="submit" class="btn btn-dark">Save Company</button>
    <a href="{{ route('admin.companies.index') }}" class="btn btn-outline-secondary">Cancel</a>
</div>

<script>
    document.getElementById('name')?.addEventListener('input', function (e) {
        const slugField = document.getElementById('slug');
        if (!slugField.dataset.touched) {
            slugField.value = e.target.value.toLowerCase().trim()
                .replace(/[^a-z0-9]+/g, '-')
                .replace(/(^-|-$)/g, '');
        }
    });
    document.getElementById('slug')?.addEventListener('input', function (e) {
        e.target.dataset.touched = '1';
    });
</script>
