@extends('layout')

@section('title', __('store/reseller.apply_title'))

@section('content')
<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-lg-8">
            <h1 class="mb-3"><i class="fas fa-crown me-2 text-warning"></i>{{ __('store/reseller.apply_title') }}</h1>
            <p class="text-muted">{{ __('store/reseller.apply_subtitle') }}</p>

            @if(session('success'))
                <div class="alert alert-success">{{ session('success') }}</div>
            @endif
            @if(session('error'))
                <div class="alert alert-danger">{{ session('error') }}</div>
            @endif

            @if($application)
                @php
                    $statusMap = [
                        'pending' => ['badge' => 'warning', 'label' => __('store/reseller.status_pending')],
                        'needs_info' => ['badge' => 'info', 'label' => __('store/reseller.status_needs_info')],
                        'rejected' => ['badge' => 'danger', 'label' => __('store/reseller.status_rejected')],
                        'suspended' => ['badge' => 'secondary', 'label' => __('store/reseller.status_suspended')],
                        'terminated' => ['badge' => 'dark', 'label' => __('store/reseller.status_terminated')],
                    ];
                    $st = $statusMap[$application->status] ?? ['badge' => 'secondary', 'label' => $application->status];
                @endphp
                <div class="card border-0 shadow-sm mb-4">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center">
                            <div>
                                <div class="fw-semibold">{{ $application->brand_name }}</div>
                                <small class="text-muted">{{ __('store/reseller.applied_at') }}: {{ $application->created_at->format($dateFormat ?? 'd.m.Y') }}</small>
                            </div>
                            <span class="badge bg-{{ $st['badge'] }}">{{ $st['label'] }}</span>
                        </div>
                        @if($application->admin_note && in_array($application->status, ['needs_info', 'rejected']))
                            <div class="alert alert-{{ $application->status === 'rejected' ? 'danger' : 'info' }} mt-3 mb-0">
                                <strong>{{ __('store/reseller.admin_note') }}:</strong> {{ $application->admin_note }}
                            </div>
                        @endif
                    </div>
                </div>
            @endif

            @if(!$application || $application->canResubmit())
                <div class="card border-0 shadow-sm">
                    <div class="card-body">
                        <form method="POST" action="{{ route('store.auth.reseller.apply.submit') }}">
                            @csrf
                            <div class="mb-3">
                                <label class="form-label">{{ __('store/reseller.field_brand_name') }} <span class="text-danger">*</span></label>
                                <input type="text" class="form-control @error('brand_name') is-invalid @enderror" name="brand_name"
                                       value="{{ old('brand_name', $application->brand_name ?? '') }}" maxlength="255" required>
                                @error('brand_name') <div class="invalid-feedback">{{ $message }}</div> @enderror
                            </div>
                            <div class="mb-3">
                                <label class="form-label">{{ __('store/reseller.field_desired_domain') }}</label>
                                <input type="text" class="form-control @error('desired_domain') is-invalid @enderror" name="desired_domain"
                                       value="{{ old('desired_domain') }}" placeholder="mysite.com" maxlength="255">
                                @error('desired_domain') <div class="invalid-feedback">{{ $message }}</div> @enderror
                                <div class="form-text small">{{ __('store/reseller.field_desired_domain_help') }}</div>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">{{ __('store/reseller.field_message') }}</label>
                                <textarea class="form-control @error('application_message') is-invalid @enderror" name="application_message"
                                          rows="4" maxlength="2000">{{ old('application_message', $application->application_message ?? '') }}</textarea>
                                @error('application_message') <div class="invalid-feedback">{{ $message }}</div> @enderror
                            </div>
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-paper-plane me-1"></i>
                                {{ $application ? __('store/reseller.apply_resubmit') : __('store/reseller.apply_submit') }}
                            </button>
                        </form>
                    </div>
                </div>
            @endif
        </div>
    </div>
</div>
@endsection
