<div class="tab-pane fade" id="email" role="tabpanel">
    <div class="card">
        <div class="card-body">
            <div class="row mb-2 align-items-center">
                <label for="disable_email_sending" class="col-sm-4 text-end col-form-label">{{ __('admin/settings.fields_disable_email_sending') }}</label>
                <div class="col-sm-8">
                    <div class="form-check form-switch d-flex align-items-center gap-2">
                        <input class="form-check-input" type="checkbox" id="disable_email_sending" name="disable_email_sending" value="1" {{ old('disable_email_sending', $settings['disable_email_sending'] ?? false) ? 'checked' : '' }}>
                        <span class="form-text mb-0">{{ __('admin/settings.fields_disable_email_sending_description') }}</span>
                    </div>
                </div>
            </div>
            <div class="row mb-2 align-items-center">
                <label for="mailProviderSelect" class="col-sm-4 text-end col-form-label">{{ __('admin/settings.fields_mail_provider') }}</label>
                <div class="col-sm-8">
                    @if($emailModules->isNotEmpty())
                        @php
                            $currentMailProvider = $settings['mail_provider'] ?? 'smtp';
                            $selectedEmailModule = $emailModules->firstWhere('name', $currentMailProvider);
                        @endphp
                        <select name="mail_provider" id="mailProviderSelect" class="form-select w-50 d-inline-block">
                            @if(! $selectedEmailModule)
                                
                                <option value="{{ $currentMailProvider }}" disabled selected>{{ $currentMailProvider }} ({{ __('admin/settings.provider_disabled_suffix') }})</option>
                            @endif
                            @foreach($emailModules as $module)
                                <option value="{{ $module->name }}" data-module-id="{{ $module->id }}" @selected($selectedEmailModule && $selectedEmailModule->name === $module->name)>{{ $module->label }}</option>
                            @endforeach
                        </select>
                        @if(! $selectedEmailModule)
                            <div class="alert alert-warning py-2 mt-2 mb-0">
                                {{ __('admin/settings.selected_provider_disabled_warning') }}
                                <a href="{{ route('admin.email-modules.index') }}">{{ __('admin/settings.go_to_module_page') }}</a>
                            </div>
                        @endif
                        <div class="form-text small">
                            <a href="{{ $selectedEmailModule ? route('admin.email-modules.edit', $selectedEmailModule->id) : route('admin.email-modules.index') }}" id="mailProviderEditLink">{{ __('admin/settings.manage_provider_settings') }}</a>
                        </div>
                    @else
                        <div class="alert alert-warning mb-0 py-2">
                            {{ __('admin/settings.no_enabled_email_modules') }}
                            <a href="{{ route('admin.email-modules.index') }}">{{ __('admin/settings.go_to_module_page') }}</a>
                        </div>
                    @endif
                </div>
            </div>
            <hr class="my-3">
            <h6 class="text-muted mb-3">{{ __('admin/settings.section_client_email_notifications') }}</h6>

            @foreach([
                'enable_welcome_emails' => 'enable_welcome_emails',
                'enable_order_confirmation_emails' => 'enable_order_confirmation_emails',
                'enable_payment_confirmation_emails' => 'enable_payment_confirmation_emails',
                'enable_service_activation_emails' => 'enable_service_activation_emails',
            ] as $key => $setting)
            <div class="row mb-2 align-items-center">
                <label for="{{ $key }}" class="col-sm-4 text-end col-form-label">{{ __("admin/settings.{$key}") }}</label>
                <div class="col-sm-8">
                    <div class="form-check form-switch d-flex align-items-center gap-2">
                        <input class="form-check-input" type="checkbox" id="{{ $key }}" name="{{ $key }}" value="1" {{ old($key, $settings[$key] ?? true) ? 'checked' : '' }}>
                        <span class="form-text mb-0">{{ __("admin/settings.{$key}_desc") }}</span>
                    </div>
                </div>
            </div>
            @endforeach

            <hr class="my-3">
            <h6 class="text-muted mb-3">{{ __('admin/settings.section_admin_email_notifications') }}</h6>

            @foreach([
                'enable_admin_order_notifications' => 'enable_admin_order_notifications',
            ] as $key => $setting)
            <div class="row mb-2 align-items-center">
                <label for="{{ $key }}" class="col-sm-4 text-end col-form-label">{{ __("admin/settings.{$key}") }}</label>
                <div class="col-sm-8">
                    <div class="form-check form-switch d-flex align-items-center gap-2">
                        <input class="form-check-input" type="checkbox" id="{{ $key }}" name="{{ $key }}" value="1" {{ old($key, $settings[$key] ?? true) ? 'checked' : '' }}>
                        <span class="form-text mb-0">{{ __("admin/settings.{$key}_desc") }}</span>
                    </div>
                </div>
            </div>
            @endforeach
            
            @php($key = 'notify_product_provisioning_success_admin')
            <div class="row mb-2 align-items-center">
                <label for="{{ $key }}" class="col-sm-4 text-end col-form-label">{{ __("admin/settings.{$key}") }}</label>
                <div class="col-sm-8">
                    <div class="form-check form-switch d-flex align-items-center gap-2">
                        <input class="form-check-input" type="checkbox" id="{{ $key }}" name="{{ $key }}" value="1" {{ old($key, $settings[$key] ?? false) ? 'checked' : '' }}>
                        <span class="form-text mb-0">{{ __("admin/settings.{$key}_desc") }}</span>
                    </div>
                </div>
            </div>
            <div class="row mb-3 align-items-center">
                <label class="col-sm-4 text-end col-form-label">{{ __('admin/settings.fields_send_test_email') }}</label>
                <div class="col-sm-8">
                    <div class="input-group" style="max-width: 450px;">
                        <input type="email" class="form-control" id="testEmailAddress" placeholder="{{ __('admin/settings.fields_test_email_placeholder') }}" value="{{ auth()->user()->email ?? '' }}">
                        <button type="button" class="btn btn-outline-primary" id="sendTestEmailBtn">
                            <i class="fas fa-paper-plane me-1"></i>{{ __('admin/settings.buttons_send_test') }}
                        </button>
                    </div>
                    <small class="form-text text-muted">{{ __('admin/settings.fields_test_email_description') }}</small>
                </div>
            </div>
            <hr class="my-3">
            <div class="row mb-2">
                <label for="global_email_signature" class="col-sm-4 text-end col-form-label">{{ __('admin/settings.fields_global_email_signature') }}</label>
                <div class="col-sm-8">
                    <textarea class="form-control" id="global_email_signature" name="global_email_signature">{{ $settings['global_email_signature'] ?? '' }}</textarea>
                </div>
            </div>
            <div class="row mb-2">
                <label for="global_email_css_styling" class="col-sm-4 text-end col-form-label">{{ __('admin/settings.fields_global_email_css_styling') }}</label>
                <div class="col-sm-8">
                    <textarea class="form-control" id="global_email_css_styling" name="global_email_css_styling">{{ $settings['global_email_css_styling'] ?? '' }}</textarea>
                </div>
            </div>
            <!-- Email Header & Footer alanı -->
            <div class="row mb-2 align-items-center">
                <label class="col-sm-4 text-end col-form-label">{{ __('admin/settings.fields_email_header_footer') }}</label>
                <div class="col-sm-8 d-flex align-items-center gap-2">
                    <button type="button" class="btn btn-outline-primary btn-sm" data-bs-toggle="modal" data-bs-target="#emailHeaderFooterModal">
                        {{ __('admin/settings.buttons_edit') }}
                    </button>
                </div>
            </div>
            <div class="row mb-2">
                <label for="system_emails_from_name" class="col-sm-4 text-end col-form-label">{{ __('admin/settings.fields_system_emails_from_name') }}</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control w-50 d-inline-block" id="system_emails_from_name" name="system_emails_from_name" value="{{ old('system_emails_from_name', $settings['system_emails_from_name'] ?? '') }}">
                </div>
            </div>
            <div class="row mb-2">
                <label for="system_emails_from_email" class="col-sm-4 text-end col-form-label">{{ __('admin/settings.fields_system_emails_from_email') }}</label>
                <div class="col-sm-8">
                    <input type="email" class="form-control w-50 d-inline-block" id="system_emails_from_email" name="system_emails_from_email" value="{{ old('system_emails_from_email', $settings['system_emails_from_email'] ?? '') }}">
                </div>
            </div>
            <div class="row mb-2">
                <label for="bcc_messages" class="col-sm-4 text-end col-form-label">{{ __('admin/settings.fields_bcc_messages') }}</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control w-50 d-inline-block" id="bcc_messages" name="bcc_messages" value="{{ old('bcc_messages', $settings['bcc_messages'] ?? '') }}">
                    <div class="form-text">{{ __('admin/settings.descriptions_bcc_messages') }}</div>
                </div>
            </div>
            <div class="row mb-2">
                <label for="presales_contact_form_email" class="col-sm-4 text-end col-form-label">{{ __('admin/settings.fields_presales_contact_form_email') }}</label>
                <div class="col-sm-8">
                    <input type="email" class="form-control w-50 d-inline-block" id="presales_contact_form_email" name="presales_contact_form_email" value="{{ old('presales_contact_form_email', $settings['presales_contact_form_email'] ?? '') }}">
                </div>
            </div>
        </div>
    </div>

    <!-- Email Header & Footer Modal -->
    <div class="modal fade" id="emailHeaderFooterModal" tabindex="-1" aria-labelledby="emailHeaderFooterModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="emailHeaderFooterModalLabel">{{ __('admin/settings.modals_edit_email_header_footer') }}</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">

                        <div class="mb-3">
                            <label for="client_email_header_content" class="form-label">{{ __('admin/settings.fields_client_email_header_content') }}</label>
                            <textarea class="form-control w-50 d-inline-block" id="client_email_header_content" name="client_email_header_content" rows="3">{{ $settings['client_email_header_content'] ?? '' }}</textarea>
                        </div>
                        <div class="mb-3">
                            <label for="client_email_footer_content" class="form-label">{{ __('admin/settings.fields_client_email_footer_content') }}</label>
                            <textarea class="form-control w-50 d-inline-block" id="client_email_footer_content" name="client_email_footer_content" rows="3">{{ $settings['client_email_footer_content'] ?? '' }}</textarea>
                        </div>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{{ __('admin/common.close') }}</button>
                    <button type="button" class="btn btn-primary" id="saveEmailHeaderFooterBtn">{{ __('admin/common.save') }}</button>
                </div>
            </div>
        </div>
    </div>


</div>

@push('scripts')
<script>
window.emailSettingsConfig = {
    routes: {
        sendTest: '{{ route("api.admin.email-providers.send-test") }}',
        moduleEdit: '{{ route("admin.email-modules.edit", ":id") }}'
    },
    translations: {
        success: '{{ __("admin/common.success") }}',
        error: '{{ __("admin/common.error") }}',
        testSuccess: '{{ __("admin/settings.test_success") }}',
        testFailed: '{{ __("admin/settings.test_failed") }}',
        testError: '{{ __("admin/settings.test_error") }}',
        testSending: '{{ __("admin/settings.test_sending") }}',
        sendTest: '{{ __("admin/settings.buttons_send_test") }}'
    }
};
</script>
@vite('resources/js/admin/settings/email.js')
@endpush
