@extends('clientarea.profile.layout')

@section('profile-content')
<div class="card">
    <div class="card-header">
        <h5 class="mb-0">{{ __('store.clientarea_profile_navigation_communication_preferences') }}</h5>
    </div>
    <div class="card-body">
        <p class="text-muted mb-4">{{ __('store.clientarea_profile_communication_preferences_description') }}</p>

        <form method="POST" action="{{ route('store.auth.profile.communication-preferences.update') }}">
            @csrf

            <div class="row">
                <div class="col-md-6">
                    <h6 class="mb-3">{{ __('store.clientarea_profile_communication_preferences_email_preferences') }}</h6>

                    <div class="form-check mb-3">
                        <input class="form-check-input" type="checkbox" name="email_notifications" value="1" id="email_notifications"
                               {{ old('email_notifications', $client->email_notifications ?? true) ? 'checked' : '' }}>
                        <label class="form-check-label" for="email_notifications">
                            <strong>{{ __('store.clientarea_profile_communication_preferences_email_notifications') }}</strong>
                            <div class="form-text">{{ __('store.clientarea_profile_communication_preferences_email_notifications_desc') }}</div>
                        </label>
                    </div>

                    <div class="form-check mb-3">
                        <input class="form-check-input" type="checkbox" name="invoice_reminders" value="1" id="invoice_reminders"
                               {{ old('invoice_reminders', $client->invoice_reminders ?? true) ? 'checked' : '' }}>
                        <label class="form-check-label" for="invoice_reminders">
                            <strong>{{ __('store.clientarea_profile_communication_preferences_invoice_reminders') }}</strong>
                            <div class="form-text">{{ __('store.clientarea_profile_communication_preferences_invoice_reminders_desc') }}</div>
                        </label>
                    </div>

                    <div class="form-check mb-3">
                        <input class="form-check-input" type="checkbox" name="service_notifications" value="1" id="service_notifications"
                               {{ old('service_notifications', $client->service_notifications ?? true) ? 'checked' : '' }}>
                        <label class="form-check-label" for="service_notifications">
                            <strong>{{ __('store.clientarea_profile_communication_preferences_service_notifications') }}</strong>
                            <div class="form-text">{{ __('store.clientarea_profile_communication_preferences_service_notifications_desc') }}</div>
                        </label>
                    </div>

                    <div class="form-check mb-4">
                        <input class="form-check-input" type="checkbox" name="marketing_emails" value="1" id="marketing_emails"
                               {{ old('marketing_emails', $client->marketing_emails ?? false) ? 'checked' : '' }}>
                        <label class="form-check-label" for="marketing_emails">
                            <strong>{{ __('store.clientarea_profile_communication_preferences_marketing_emails') }}</strong>
                            <div class="form-text">{{ __('store.clientarea_profile_communication_preferences_marketing_emails_desc') }}</div>
                        </label>
                    </div>
                </div>

                <div class="col-md-6">
                    <h6 class="mb-3">{{ __('store.clientarea_profile_communication_preferences_sms_preferences') }}</h6>

                    <div class="form-check mb-3">
                        <input class="form-check-input" type="checkbox" name="sms_notifications" value="1" id="sms_notifications"
                               {{ old('sms_notifications', $client->sms_notifications ?? false) ? 'checked' : '' }}>
                        <label class="form-check-label" for="sms_notifications">
                            <strong>{{ __('store.clientarea_profile_communication_preferences_sms_notifications') }}</strong>
                            <div class="form-text">{{ __('store.clientarea_profile_communication_preferences_sms_notifications_desc') }}</div>
                        </label>
                    </div>

                    @if($client->phone)
                        <div class="alert alert-info">
                            <i class="fas fa-info-circle me-2"></i>
                            <strong>{{ __('store.clientarea_profile_communication_preferences_current_phone') }}:</strong> {{ $client->phone }}
                            <br>
                            <small>{{ __('store.clientarea_profile_communication_preferences_update_phone_note') }}</small>
                        </div>
                    @else
                        <div class="alert alert-warning">
                            <i class="fas fa-exclamation-triangle me-2"></i>
                            {{ __('store.clientarea_profile_communication_preferences_no_phone_warning') }}
                        </div>
                    @endif

                    <div class="mt-4">
                        <h6 class="mb-3">{{ __('store.clientarea_profile_communication_preferences_contact_method') }}</h6>
                        <div class="form-check">
                            <input class="form-check-input" type="radio" name="preferred_contact_method" value="email" id="contact_email"
                                   {{ old('preferred_contact_method', $client->preferred_contact_method ?? 'email') === 'email' ? 'checked' : '' }}>
                            <label class="form-check-label" for="contact_email">
                                {{ __('store.clientarea_profile_communication_preferences_prefer_email') }}
                            </label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" type="radio" name="preferred_contact_method" value="sms" id="contact_sms"
                                   {{ old('preferred_contact_method', $client->preferred_contact_method ?? 'email') === 'sms' ? 'checked' : '' }}>
                            <label class="form-check-label" for="contact_sms">
                                {{ __('store.clientarea_profile_communication_preferences_prefer_sms') }}
                            </label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" type="radio" name="preferred_contact_method" value="both" id="contact_both"
                                   {{ old('preferred_contact_method', $client->preferred_contact_method ?? 'email') === 'both' ? 'checked' : '' }}>
                            <label class="form-check-label" for="contact_both">
                                {{ __('store.clientarea_profile_communication_preferences_prefer_both') }}
                            </label>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pt-3 border-top">
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-save me-2"></i>{{ __('store.clientarea_profile_communication_preferences_save_preferences') }}
                </button>
            </div>
        </form>
    </div>
</div>


<div class="card border-0 shadow-sm mt-4">
    <div class="card-body">
        <h5>{{ __('store.consents_section_title') }}</h5>
        <p class="text-muted small">{{ __('store.consents_section_subtitle') }}</p>

        <div class="list-group list-group-flush" id="consentChannelsList">
            @foreach (\App\Enums\ConsentChannel::cases() as $channel)
                <div class="list-group-item d-flex justify-content-between align-items-center px-0">
                    <span>{{ $channel->label() }}</span>
                    <div class="form-check form-switch m-0">
                        <input
                            class="form-check-input consent-toggle"
                            type="checkbox"
                            role="switch"
                            data-channel="{{ $channel->value }}"
                            data-grant-url="{{ route('store.auth.profile.consents.grant', $channel->value) }}"
                            data-withdraw-url="{{ route('store.auth.profile.consents.withdraw', $channel->value) }}"
                            {{ ($consents[$channel->value] ?? false) ? 'checked' : '' }}>
                    </div>
                </div>
            @endforeach
        </div>

        <div class="mt-3">
            <a href="{{ route('store.auth.profile.consents.history') }}" class="text-decoration-none small">
                {{ __('store.consents_history_title') }} →
            </a>
        </div>
    </div>
</div>

@push('scripts')
<script>
window.consentsConfig = {
    translations: {
        grant_failed: '{{ __("store.consent_grant_failed") }}',
        withdraw_failed: '{{ __("store.consent_withdraw_failed") }}',
    }
};
</script>
@vite('resources/js/store/profile/consents.js')
@endpush
@endsection