<x-panel title="Email Configuration" subtitle="SMTP and sender settings — credentials are stored encrypted">
    <form method="POST" action="{{ route('system.email-settings.update') }}" enctype="multipart/form-data">
        @csrf @method('PUT')

        <div class="form-grid-2" style="margin-bottom: 20px;">
            <div class="form-group">
                <label class="form-label">Provider</label>
                <select class="select w-full" name="provider">
                    @foreach (['smtp' => 'SMTP', 'gmail' => 'Gmail SMTP', 'outlook' => 'Outlook SMTP', 'mailgun' => 'Mailgun', 'sendgrid' => 'SendGrid', 'ses' => 'Amazon SES', 'custom' => 'Custom'] as $val => $label)
                        <option value="{{ $val }}" @selected(old('provider', $emailConfig['provider']) === $val)>{{ $label }}</option>
                    @endforeach
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">Encryption</label>
                <select class="select w-full" name="encryption">
                    @foreach (['tls' => 'TLS', 'ssl' => 'SSL', 'none' => 'None'] as $val => $label)
                        <option value="{{ $val }}" @selected(old('encryption', $emailConfig['encryption']) === $val)>{{ $label }}</option>
                    @endforeach
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">SMTP Host</label>
                <input class="input" name="smtp_host" value="{{ old('smtp_host', $emailConfig['smtp_host']) }}">
            </div>
            <div class="form-group">
                <label class="form-label">SMTP Port</label>
                <input class="input" type="number" name="smtp_port" value="{{ old('smtp_port', $emailConfig['smtp_port']) }}">
            </div>
            <div class="form-group">
                <label class="form-label">SMTP Username</label>
                <input class="input" name="smtp_username" value="{{ old('smtp_username', $emailConfig['smtp_username']) }}" autocomplete="off">
            </div>
            <div class="form-group">
                <label class="form-label">SMTP Password</label>
                <input class="input" type="password" name="smtp_password" placeholder="{{ $emailConfig['has_password'] ? '•••••••• (leave blank to keep)' : 'Enter password' }}" autocomplete="new-password">
            </div>
            <div class="form-group">
                <label class="form-label">API Key / Secret</label>
                <input class="input" type="password" name="api_key" placeholder="{{ $emailConfig['has_api_key'] ? '•••••••• (leave blank to keep)' : 'For Mailgun/SendGrid/SES' }}" autocomplete="new-password">
            </div>
            <div class="form-group">
                <label class="form-label">API Domain (Mailgun)</label>
                <input class="input" name="api_domain" value="{{ old('api_domain', $emailConfig['api_domain']) }}">
            </div>
            <div class="form-group">
                <label class="form-label">API Region (SES)</label>
                <input class="input" name="api_region" value="{{ old('api_region', $emailConfig['api_region']) }}" placeholder="us-east-1">
            </div>
            <div class="form-group">
                <label class="form-label">Sender Name</label>
                <input class="input" name="from_name" value="{{ old('from_name', $emailConfig['from_name']) }}" required>
            </div>
            <div class="form-group">
                <label class="form-label">Sender Email</label>
                <input class="input" type="email" name="from_address" value="{{ old('from_address', $emailConfig['from_address']) }}" required>
            </div>
            <div class="form-group">
                <label class="form-label">Reply-To Email</label>
                <input class="input" type="email" name="reply_to" value="{{ old('reply_to', $emailConfig['reply_to']) }}">
            </div>
            <div class="form-group">
                <label class="form-label">Company Logo</label>
                <input class="input" type="file" name="logo" accept="image/*">
                @if ($emailConfig['logo_url'])
                    <img src="{{ $emailConfig['logo_url'] }}" alt="Logo" style="max-height:48px; margin-top:8px;">
                @endif
            </div>
        </div>

        <div class="form-group">
            <label class="form-label">Default Header (HTML)</label>
            <textarea class="input" name="default_header" rows="3">{{ old('default_header', $emailConfig['default_header']) }}</textarea>
        </div>
        <div class="form-group">
            <label class="form-label">Email Signature</label>
            <textarea class="input" name="signature" rows="3">{{ old('signature', $emailConfig['signature']) }}</textarea>
        </div>
        <div class="form-group">
            <label class="form-label">Default Footer (HTML)</label>
            <textarea class="input" name="default_footer" rows="3">{{ old('default_footer', $emailConfig['default_footer']) }}</textarea>
        </div>

        <div style="display:flex; gap:20px; flex-wrap:wrap; margin: 16px 0;">
            <label style="display:flex; align-items:center; gap:8px;">
                <input type="checkbox" name="is_enabled" value="1" @checked(old('is_enabled', $emailConfig['is_enabled']))>
                Enable email sending
            </label>
            <label style="display:flex; align-items:center; gap:8px;">
                <input type="checkbox" name="automated_enabled" value="1" @checked(old('automated_enabled', $emailConfig['automated_enabled']))>
                Enable automated emails
            </label>
        </div>

        <div style="display:flex; gap:10px; flex-wrap:wrap;">
            <button type="submit" class="btn btn-primary">Save Email Configuration</button>
            <a href="{{ route('communication.emails.index') }}" class="btn btn-outline">Open Email Center</a>
        </div>
    </form>

    <form method="POST" action="{{ route('system.email-settings.test') }}" style="margin-top:24px; padding-top:24px; border-top:1px solid var(--glass-border);">
        @csrf
        <h4 style="margin-bottom:12px;">Send Test Email</h4>
        <div style="display:flex; gap:10px; flex-wrap:wrap; align-items:end;">
            <div class="form-group" style="flex:1; min-width:220px; margin:0;">
                <label class="form-label">Test recipient</label>
                <input class="input" type="email" name="test_recipient" required placeholder="you@example.com">
            </div>
            <button type="submit" class="btn btn-outline">Send Test</button>
        </div>
    </form>
</x-panel>
