@csrf

@php
    $languagesValue = old('languages', is_array($candidate->languages ?? null) ? implode(', ', $candidate->languages) : ($candidate->languages ?? ''));
    $countriesWorkedValue = old('countries_worked', is_array($candidate->countries_worked ?? null) ? implode(', ', $candidate->countries_worked) : ($candidate->countries_worked ?? ''));

    $languagesValue = is_string($languagesValue) ? $languagesValue : '';
    $countriesWorkedValue = is_string($countriesWorkedValue) ? $countriesWorkedValue : '';

    $ageBandValue = old('age_band', $candidate->age_band ?? '');
    $isActiveChecked = old('is_active', $candidate->is_active ?? true) ? true : false;
@endphp

<div class="row g-4">
    <div class="col-lg-6">
        <label class="form-label">Code</label>
        <input type="text" name="code" value="{{ old('code', $candidate->code ?? '') }}" class="form-control @error('code') is-invalid @enderror">
        @error('code')
            <div class="invalid-feedback d-block">{{ $message }}</div>
        @enderror
    </div>

    <div class="col-lg-6">
        <label class="form-label">Name</label>
        <input type="text" name="name" value="{{ old('name', $candidate->name ?? '') }}" class="form-control @error('name') is-invalid @enderror">
        @error('name')
            <div class="invalid-feedback d-block">{{ $message }}</div>
        @enderror
    </div>

    <div class="col-lg-6">
        <label class="form-label">Country</label>
        <input type="text" name="country" value="{{ old('country', $candidate->country ?? '') }}" class="form-control @error('country') is-invalid @enderror" placeholder="Philippines / Ethiopia / Uganda ...">
        @error('country')
            <div class="invalid-feedback d-block">{{ $message }}</div>
        @enderror
    </div>

    <div class="col-lg-3">
        <label class="form-label">Source</label>
        <select name="source" class="form-select @error('source') is-invalid @enderror">
            <option value="inside" {{ (string) old('source', $candidate->source ?? '') === 'inside' ? 'selected' : '' }}>inside</option>
            <option value="outside" {{ (string) old('source', $candidate->source ?? '') === 'outside' ? 'selected' : '' }}>outside</option>
        </select>
        @error('source')
            <div class="invalid-feedback d-block">{{ $message }}</div>
        @enderror
    </div>

    <div class="col-lg-3">
        <label class="form-label">Sponsorship</label>
        <select name="sponsorship" class="form-select @error('sponsorship') is-invalid @enderror">
            <option value="tadbeer" {{ (string) old('sponsorship', $candidate->sponsorship ?? '') === 'tadbeer' ? 'selected' : '' }}>tadbeer</option>
            <option value="family" {{ (string) old('sponsorship', $candidate->sponsorship ?? '') === 'family' ? 'selected' : '' }}>family</option>
        </select>
        @error('sponsorship')
            <div class="invalid-feedback d-block">{{ $message }}</div>
        @enderror
    </div>

    <div class="col-lg-3">
        <label class="form-label">Age</label>
        <input type="number" name="age" min="0" max="120" value="{{ old('age', $candidate->age ?? '') }}" class="form-control @error('age') is-invalid @enderror">
        @error('age')
            <div class="invalid-feedback d-block">{{ $message }}</div>
        @enderror
    </div>

    <div class="col-lg-3">
        <label class="form-label">Age Band (optional)</label>
        <input type="text" name="age_band" value="{{ old('age_band', $candidate->age_band ?? '') }}" class="form-control @error('age_band') is-invalid @enderror" placeholder="36-45 / 18-25 / 46+">
        @error('age_band')
            <div class="invalid-feedback d-block">{{ $message }}</div>
        @enderror
    </div>

    <div class="col-lg-6">
        <label class="form-label">Religion</label>
        <input type="text" name="religion" value="{{ old('religion', $candidate->religion ?? '') }}" class="form-control @error('religion') is-invalid @enderror">
        @error('religion')
            <div class="invalid-feedback d-block">{{ $message }}</div>
        @enderror
    </div>

    <div class="col-lg-6">
        <label class="form-label">Languages (comma separated)</label>
        <input type="text" name="languages" value="{{ $languagesValue }}" class="form-control @error('languages') is-invalid @enderror" placeholder="English, Arabic">
        @error('languages')
            <div class="invalid-feedback d-block">{{ $message }}</div>
        @enderror
    </div>

    <div class="col-lg-6">
        <label class="form-label">Marital Status</label>
        <input type="text" name="marital_status" value="{{ old('marital_status', $candidate->marital_status ?? '') }}" class="form-control @error('marital_status') is-invalid @enderror" placeholder="Single / Married / Widowed">
        @error('marital_status')
            <div class="invalid-feedback d-block">{{ $message }}</div>
        @enderror
    </div>

    <div class="col-lg-3">
        <label class="form-label">Children</label>
        <input type="number" name="children" min="0" max="30" value="{{ old('children', $candidate->children ?? '') }}" class="form-control @error('children') is-invalid @enderror">
        @error('children')
            <div class="invalid-feedback d-block">{{ $message }}</div>
        @enderror
    </div>

    <div class="col-lg-3">
        <label class="form-label">Experience Years</label>
        <input type="number" name="experience_years" min="0" max="60" value="{{ old('experience_years', $candidate->experience_years ?? '') }}" class="form-control @error('experience_years') is-invalid @enderror">
        @error('experience_years')
            <div class="invalid-feedback d-block">{{ $message }}</div>
        @enderror
    </div>

    <div class="col-lg-6">
        <label class="form-label">Experience Band</label>
        <input type="text" name="experience_band" value="{{ old('experience_band', $candidate->experience_band ?? '') }}" class="form-control @error('experience_band') is-invalid @enderror" placeholder="United Arab Emirates / Others / Saudi Arabia">
        @error('experience_band')
            <div class="invalid-feedback d-block">{{ $message }}</div>
        @enderror
    </div>

    <div class="col-lg-12">
        <label class="form-label">Countries Worked (comma separated)</label>
        <input type="text" name="countries_worked" value="{{ $countriesWorkedValue }}" class="form-control @error('countries_worked') is-invalid @enderror" placeholder="UAE, Qatar">
        @error('countries_worked')
            <div class="invalid-feedback d-block">{{ $message }}</div>
        @enderror
    </div>

    <div class="col-lg-6">
        <label class="form-label">Salary</label>
        <input type="text" name="salary" value="{{ old('salary', $candidate->salary ?? '') }}" class="form-control @error('salary') is-invalid @enderror" placeholder="1,440 AED / per month">
        @error('salary')
            <div class="invalid-feedback d-block">{{ $message }}</div>
        @enderror
    </div>

    <div class="col-lg-6">
        <label class="form-label">Video URL</label>
        <input type="text" name="video" value="{{ old('video', $candidate->video ?? '') }}" class="form-control @error('video') is-invalid @enderror" placeholder="https://.../video.mp4">
        @error('video')
            <div class="invalid-feedback d-block">{{ $message }}</div>
        @enderror
    </div>

    <div class="col-lg-12">
        <label class="form-label">CV URL</label>
        <input type="text" name="cv" value="{{ old('cv', $candidate->cv ?? '') }}" class="form-control @error('cv') is-invalid @enderror" placeholder="https://... or #">
        @error('cv')
            <div class="invalid-feedback d-block">{{ $message }}</div>
        @enderror
    </div>

    <div class="col-lg-12">
        <label class="form-label">Image</label>
        <input type="file" name="image" class="form-control @error('image') is-invalid @enderror">
        @error('image')
            <div class="invalid-feedback d-block">{{ $message }}</div>
        @enderror

        @if(isset($candidate) && $candidate->image)
            <div class="mt-3">
                @php
                    $isHttp = is_string($candidate->image) && str_starts_with($candidate->image, 'http');
                    $src = $isHttp ? $candidate->image : asset('storage/' . $candidate->image);
                @endphp
                <img src="{{ $src }}" alt="{{ $candidate->name }}" width="180" class="rounded border img-fluid" style="object-fit: cover;">
            </div>
        @endif
    </div>

    <div class="col-lg-12">
        <div class="form-check form-switch mt-2">
            <input type="hidden" name="is_active" value="0">
            <input type="checkbox" class="form-check-input" id="is_active" name="is_active" value="1" {{ $isActiveChecked ? 'checked' : '' }}>
            <label class="form-check-label" for="is_active">Active</label>
        </div>
    </div>
</div>

<div class="d-flex justify-content-end gap-2 mt-4">
    <a href="{{ route('admin.candidates.index') }}" class="btn btn-light border">Cancel</a>
    <button type="submit" class="btn btn-primary">{{ $submitButtonText }}</button>
</div>

