@php
$color = $options['color'] ?? 'transparent';
$padding = $options['padding'] ?? 'lg';
$showPhone = $options['show_phone'] ?? false;
$showSubject = $options['show_subject'] ?? true;
$showDepartments = $options['show_departments'] ?? true;
$layout = $options['layout'] ?? 'stacked';

$departments = [];
if ($showDepartments) {
    try {
        $departments = \App\Models\TicketDepartment::query()
            ->where('status', 1)
            ->where('hidden', 0)
            ->where('client_only', 0)
            ->orderBy('name')
            ->get(['id', 'name']);
    } catch (\Throwable $e) {
        $departments = [];
    }
}

$action = \Illuminate\Support\Facades\Route::has('store.contact.store') ? lroute('store.contact.store') : '/contact';
@endphp
<section class="page-block-contact-form py-{{ $padding }} @if($color !== 'transparent') {{ pb_bg_class($color) }} @endif">
    <div class="container">
        @if(! empty($data['title']))<h2 class="text-center mb-3">{{ $data['title'] }}</h2>@endif
        @if(! empty($data['subtitle']))<p class="text-center text-muted mb-5">{!! e($data['subtitle']) !!}</p>@endif

        @if(session('success'))
            <div class="alert alert-success mx-auto" style="max-width:600px;">
                {{ $data['success_message'] ?: __('store.contact_success') }}
            </div>
        @endif

        <form action="{{ $action }}" method="POST" class="mx-auto" style="max-width:600px;">
            @csrf
            <div class="row g-3">
                <div class="{{ $layout === 'split' ? 'col-md-6' : 'col-12' }}">
                    <label class="form-label small text-muted">{{ __('store.contact_name') }} <span class="text-danger">*</span></label>
                    <input type="text" name="name" class="form-control @error('name') is-invalid @enderror" value="{{ old('name') }}" required>
                    @error('name')<div class="invalid-feedback">{{ $message }}</div>@enderror
                </div>
                <div class="{{ $layout === 'split' ? 'col-md-6' : 'col-12' }}">
                    <label class="form-label small text-muted">{{ __('store.contact_email') }} <span class="text-danger">*</span></label>
                    <input type="email" name="email" class="form-control @error('email') is-invalid @enderror" value="{{ old('email') }}" required>
                    @error('email')<div class="invalid-feedback">{{ $message }}</div>@enderror
                </div>

                @if($showPhone)
                    <div class="{{ $layout === 'split' ? 'col-md-6' : 'col-12' }}">
                        <label class="form-label small text-muted">{{ __('store.contact_phone') }}</label>
                        <input type="tel" name="phone" class="form-control" value="{{ old('phone') }}">
                    </div>
                @endif

                @if($showDepartments && count($departments))
                    <div class="{{ $layout === 'split' ? 'col-md-6' : 'col-12' }}">
                        <label class="form-label small text-muted">{{ __('store.contact_department') }}</label>
                        <select name="department_id" class="form-select">
                            <option value="">{{ __('store.contact_department_placeholder') }}</option>
                            @foreach($departments as $dept)
                                <option value="{{ $dept->id }}" @selected(old('department_id') == $dept->id)>{{ $dept->name }}</option>
                            @endforeach
                        </select>
                    </div>
                @endif

                @if($showSubject)
                    <div class="col-12">
                        <label class="form-label small text-muted">{{ __('store.contact_subject') }}</label>
                        <input type="text" name="subject" class="form-control" value="{{ old('subject') }}">
                    </div>
                @endif

                <div class="col-12">
                    <label class="form-label small text-muted">{{ __('store.contact_message') }} <span class="text-danger">*</span></label>
                    <textarea name="message" class="form-control @error('message') is-invalid @enderror" rows="5" required>{{ old('message') }}</textarea>
                    @error('message')<div class="invalid-feedback">{{ $message }}</div>@enderror
                </div>

                <div class="col-12">
                    <x-captcha form="contact_form" />
                </div>

                <div class="col-12">
                    <button type="submit" class="btn btn-primary w-100">{{ $data['submit_text'] ?: __('store.contact_send') }}</button>
                </div>
            </div>
        </form>
    </div>
</section>
