<div class="row">
    <div class="col-md-12">
        <h4>Configuration</h4>
        <hr>
    </div>
    <div class="col-md-12">
        <form action="javascript:void(0)" method="POST" id="emailform">
            <?php echo csrf_field(); ?>
            <?php echo method_field('PUT'); ?>
            <div class="form-row">
                
                <div class="form-group col-md-12">
                    <h5 class="text-<?php echo e($text); ?>">Mail Server</h5>
                    <div class="selectgroup">
                        <label class="selectgroup-item">
                            <input type="radio" name="server" id="sendmailserver" value="sendmail" class="selectgroup-input">
                            <span class="selectgroup-button">Sendmail</span>
                        </label>
                        <label class="selectgroup-item">
                            <input type="radio" name="server" id="smtpserver" value="smtp" class="selectgroup-input">
                            <span class="selectgroup-button">SMTP</span>
                        </label>
                    </div>
                </div>

                
                <div class="form-group col-md-6">
                    <h5 class="text-<?php echo e($text); ?>">Email From</h5>
                    <input type="email" name="emailfrom" class="form-control bg-<?php echo e($bg); ?> text-<?php echo e($text); ?>" value="<?php echo e($settings->emailfrom); ?>" required>
                </div>
                <div class="form-group col-md-6">
                    <h5 class="text-<?php echo e($text); ?>">Email From Name</h5>
                    <input type="text" name="emailfromname" class="form-control bg-<?php echo e($bg); ?> text-<?php echo e($text); ?>" value="<?php echo e($settings->emailfromname); ?>" required>
                </div>

                
                <div class="form-group col-md-6 smtp d-none">
                    <h5 class="text-<?php echo e($text); ?>">SMTP Host</h5>
                    <input type="text" name="smtp_host" class="form-control bg-<?php echo e($bg); ?> text-<?php echo e($text); ?> smtpinput"
                        value="<?php echo e($settings->smtp_host); ?>"
                        placeholder="mail.yourdomain.com">
                    <small class="text-muted">e.g. mail.yourdomain.com — no https://</small>
                </div>
                <div class="form-group col-md-6 smtp d-none">
                    <h5 class="text-<?php echo e($text); ?>">SMTP Port</h5>
                    <select name="smtp_port" class="form-control bg-<?php echo e($bg); ?> text-<?php echo e($text); ?> smtpinput" id="smtp_port_select">
                        <option value="587" <?php echo e(($settings->smtp_port ?? '587') == '587' ? 'selected' : ''); ?>>587 — STARTTLS (Recommended)</option>
                        <option value="465" <?php echo e(($settings->smtp_port ?? '') == '465' ? 'selected' : ''); ?>>465 — SSL/TLS</option>
                        <option value="25"  <?php echo e(($settings->smtp_port ?? '') == '25'  ? 'selected' : ''); ?>>25 — No encryption</option>
                    </select>
                </div>
                <div class="form-group col-md-6 smtp d-none">
                    <h5 class="text-<?php echo e($text); ?>">SMTP Encryption</h5>
                    <select name="smtp_encrypt" class="form-control bg-<?php echo e($bg); ?> text-<?php echo e($text); ?> smtpinput" id="smtp_encrypt_select">
                        <option value="tls" <?php echo e(($settings->smtp_encrypt ?? 'tls') == 'tls' ? 'selected' : ''); ?>>TLS (STARTTLS)</option>
                        <option value="ssl" <?php echo e(($settings->smtp_encrypt ?? '') == 'ssl' ? 'selected' : ''); ?>>SSL</option>
                        <option value=""   <?php echo e(($settings->smtp_encrypt ?? '') == ''   ? 'selected' : ''); ?>>None</option>
                    </select>
                </div>
                <div class="form-group col-md-6 smtp d-none">
                    <h5 class="text-<?php echo e($text); ?>">SMTP Username</h5>
                    <input type="text" name="smtp_user" class="form-control bg-<?php echo e($bg); ?> text-<?php echo e($text); ?> smtpinput"
                        value="<?php echo e($settings->smtp_user); ?>"
                        placeholder="noreply@yourdomain.com">
                    <small class="text-muted">Your full cPanel email address</small>
                </div>
                <div class="form-group col-md-6 smtp d-none">
                    <h5 class="text-<?php echo e($text); ?>">SMTP Password</h5>
                    <input type="password" name="smtp_password" class="form-control bg-<?php echo e($bg); ?> text-<?php echo e($text); ?> smtpinput"
                        value="<?php echo e($settings->smtp_password); ?>"
                        placeholder="Your cPanel email password">
                </div>

                
                <div class="form-group col-md-6 smtp d-none">
                    <h5 class="text-<?php echo e($text); ?>">Test Withdraw OTP Alert</h5>
                    <button type="button" class="btn btn-warning" id="testEmailBtn">
                        Send Test OTP Email
                    </button>
                    <small class="text-muted d-block mt-1">Sends a test OTP to the "Email From" address above</small>
                    <div id="testEmailResult" class="mt-2"></div>
                </div>
            </div>

            <hr>
            <div class="form-row">
                <div class="col-md-12">
                    <h4>Google Login Credentials</h4>
                    <hr>
                </div>
            </div>
            <div class="form-row">
                <div class="form-group col-md-6">
                    <h5 class="text-<?php echo e($text); ?>">Client ID</h5>
                    <input type="text" name="google_id" class="form-control bg-<?php echo e($bg); ?> text-<?php echo e($text); ?>" value="<?php echo e($settings->google_id); ?>">
                    <small class="text-<?php echo e($text); ?>">From console.cloud.google.com</small>
                </div>
                <div class="form-group col-md-6">
                    <h5 class="text-<?php echo e($text); ?>">Client Secret</h5>
                    <input type="text" name="google_secret" class="form-control bg-<?php echo e($bg); ?> text-<?php echo e($text); ?>" value="<?php echo e($settings->google_secret); ?>">
                    <small class="text-<?php echo e($text); ?>">From console.cloud.google.com</small>
                </div>
                <div class="form-group col-md-6">
                    <h5 class="text-<?php echo e($text); ?>">Redirect URL</h5>
                    <input type="text" name="google_redirect" class="form-control bg-<?php echo e($bg); ?> text-<?php echo e($text); ?>" value="<?php echo e($settings->google_redirect); ?>">
                    <small class="text-<?php echo e($text); ?>">Set this to your Valid OAuth Redirect URI in console.cloud.google.com</small>
                </div>
            </div>

            <div class="mt-4 form-row">
                <div class="col-md-12">
                    <h4>Google Captcha Credentials</h4>
                    <hr>
                </div>
            </div>
            <div class="form-row">
                <div class="form-group col-md-6">
                    <h5 class="text-<?php echo e($text); ?>">Captcha Secret</h5>
                    <input type="text" name="capt_secret" class="form-control bg-<?php echo e($bg); ?> text-<?php echo e($text); ?>" value="<?php echo e($settings->capt_secret); ?>">
                    <small class="text-<?php echo e($text); ?>">From https://www.google.com/recaptcha/admin/create</small>
                </div>
                <div class="form-group col-md-6">
                    <h5 class="text-<?php echo e($text); ?>">Captcha Site-Key</h5>
                    <input type="text" name="capt_sitekey" class="form-control bg-<?php echo e($bg); ?> text-<?php echo e($text); ?>" value="<?php echo e($settings->capt_sitekey); ?>">
                    <small class="text-<?php echo e($text); ?>">From https://www.google.com/recaptcha/admin/create</small>
                </div>
                <div class="form-group col-md-12">
                    <input type="submit" class="px-5 btn btn-primary btn-lg" value="Save">
                </div>
            </div>
        </form>
    </div>
</div>

<?php if($settings->mail_server == 'smtp'): ?>
<script>
    document.getElementById("smtpserver").checked = true;
    document.querySelectorAll('.smtp').forEach(el => el.classList.remove('d-none'));
</script>
<?php else: ?>
<script>
    document.getElementById("sendmailserver").checked = true;
</script>
<?php endif; ?>

<script>
    // Toggle SMTP fields on radio change
    document.querySelectorAll('input[name="server"]').forEach(function(radio) {
        radio.addEventListener('change', function() {
            const isSmtp = this.value === 'smtp';
            document.querySelectorAll('.smtp').forEach(function(el) {
                el.classList.toggle('d-none', !isSmtp);
            });
        });
    });

    // Auto-sync encryption when port changes
    document.getElementById('smtp_port_select').addEventListener('change', function() {
        const enc = document.getElementById('smtp_encrypt_select');
        if (this.value === '465') enc.value = 'ssl';
        else if (this.value === '587') enc.value = 'tls';
        else enc.value = '';
    });

    // Test withdraw OTP email
    document.getElementById('testEmailBtn').addEventListener('click', function() {
        const btn = this;
        const result = document.getElementById('testEmailResult');

        result.innerHTML = '<div class="alert alert-info py-1 px-2 mb-0">Sending request...</div>';
        btn.disabled = true;
        btn.textContent = 'Sending...';

        fetch('<?php echo e(route("admin.settings.test-email")); ?>', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRF-TOKEN': '<?php echo e(csrf_token()); ?>'
            },
            body: JSON.stringify({ type: 'withdraw_otp' })
        })
        .then(function(res) {
            return res.text().then(function(text) {
                try {
                    return JSON.parse(text);
                } catch(e) {
                    throw new Error('Server error (status ' + res.status + '): ' + text.substring(0, 800));
                }
            });
        })
        .then(function(data) {
            result.innerHTML = data.success
                ? '<div class="alert alert-success py-1 px-2 mb-0">Test OTP email sent! Check your inbox.</div>'
                : '<div class="alert alert-danger py-1 px-2 mb-0">Failed: ' + data.message + '</div>';
        })
        .catch(function(err) {
            result.innerHTML = '<div class="alert alert-danger py-1 px-2 mb-0" style="word-break:break-all">' + err.message + '</div>';
        })
        .finally(function() {
            btn.disabled = false;
            btn.textContent = 'Send Test OTP Email';
        });
    });
</script><?php /**PATH /home3/infinexc/public_html/ix/resources/views/admin/Settings/AppSettings/email.blade.php ENDPATH**/ ?>