@extends('layouts.admin', ['heading' => 'Settings', 'subheading' => 'System configuration'])

@section('content')
    <form method="POST" action="{{ route('admin.settings.update') }}" enctype="multipart/form-data" class="space-y-6">
        @csrf

        <div class="admin-card p-5">
            <div class="mb-4 text-sm font-semibold text-slate-700">Company Profile</div>
            <div class="grid grid-cols-1 gap-4 md:grid-cols-2">
                <div>
                    <label class="mb-1 block text-sm font-medium">Company Name</label>
                    <input name="company_name" value="{{ setting('company_name') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Company Email</label>
                    <input name="company_email" value="{{ setting('company_email') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Company Phone</label>
                    <input name="company_phone" value="{{ setting('company_phone') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Company Address</label>
                    <input name="company_address" value="{{ setting('company_address') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Currency</label>
                    <input name="currency" value="{{ setting('currency') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Timezone</label>
                    <input name="timezone" value="{{ setting('timezone', 'Asia/Kolkata') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Language</label>
                    <input name="language" value="{{ setting('language', 'en') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Company Logo</label>
                    <input type="file" name="company_logo" class="w-full rounded border p-2" />
                    @if(setting('company_logo'))
                        <div class="mt-2 text-xs text-slate-500">Current: {{ setting('company_logo') }}</div>
                    @endif
                </div>
            </div>
        </div>

        <div class="admin-card p-5">
            <div class="mb-4 text-sm font-semibold text-slate-700">Admin Controls</div>
            <div class="grid grid-cols-1 gap-4 md:grid-cols-2">
            <div>
                <label class="mb-1 block text-sm font-medium">Admin Secret Slug</label>
                <input name="admin_slug" value="{{ setting('admin_slug', 'admin') }}" class="w-full rounded border p-2" />
                <div class="mt-1 text-xs text-slate-500">Admin login URL: {{ url('/'.setting('admin_slug', 'admin')) }}</div>
            </div>
            <div>
                <label class="mb-1 block text-sm font-medium">Root User ID</label>
                <input name="company_root_user_id" value="{{ setting('company_root_user_id') }}" class="w-full rounded border p-2" />
                <div class="mt-1 text-xs text-slate-500">Dashboard left/right metrics start from this user.</div>
            </div>
            <div>
                <label class="mb-1 block text-sm font-medium">KYC Required</label>
                <select name="kyc_required" class="w-full rounded border p-2">
                    <option value="1" @selected(setting('kyc_required', '1') == '1')>Enabled</option>
                    <option value="0" @selected(setting('kyc_required', '1') == '0')>Disabled</option>
                </select>
            </div>
            </div>
        </div>

        <div id="theme" class="admin-card p-5">
            <div class="mb-4 text-sm font-semibold text-slate-700">Theme Settings</div>
            <div class="grid grid-cols-1 gap-4 md:grid-cols-3">
                <div>
                    <label class="mb-1 block text-sm font-medium">Admin Panel Default</label>
                    <select name="admin_theme_default" class="w-full rounded border p-2">
                        <option value="light" @selected(setting('admin_theme_default', 'light') === 'light')>Light</option>
                        <option value="dark" @selected(setting('admin_theme_default', 'light') === 'dark')>Dark</option>
                        <option value="auto" @selected(setting('admin_theme_default', 'light') === 'auto')>Auto (System)</option>
                    </select>
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">User Panel Default</label>
                    <select name="user_theme_default" class="w-full rounded border p-2">
                        <option value="light" @selected(setting('user_theme_default', 'light') === 'light')>Light</option>
                        <option value="dark" @selected(setting('user_theme_default', 'light') === 'dark')>Dark</option>
                        <option value="auto" @selected(setting('user_theme_default', 'light') === 'auto')>Auto (System)</option>
                    </select>
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Franchise Panel Default</label>
                    <select name="franchise_theme_default" class="w-full rounded border p-2">
                        <option value="light" @selected(setting('franchise_theme_default', 'light') === 'light')>Light</option>
                        <option value="dark" @selected(setting('franchise_theme_default', 'light') === 'dark')>Dark</option>
                        <option value="auto" @selected(setting('franchise_theme_default', 'light') === 'auto')>Auto (System)</option>
                    </select>
                </div>
            </div>
            <div class="mt-2 text-xs text-slate-500">Panel defaults apply when a user has not selected a personal theme preference.</div>
        </div>

        <div class="admin-card p-5">
            <div class="mb-4 text-sm font-semibold text-slate-700">Matching & Capping</div>
            <div class="grid grid-cols-1 gap-4 md:grid-cols-4">
                <div>
                    <label class="mb-1 block text-sm font-medium">Joining Pair Value</label>
                    <input name="joining_pair_value" value="{{ setting('joining_pair_value') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Pair Rate</label>
                    <input name="pair_rate" value="{{ setting('pair_rate') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Daily Capping</label>
                    <input name="daily_capping" value="{{ setting('daily_capping') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Pair Income (Admin Plan)</label>
                    <input name="pair_income" value="{{ setting('pair_income', setting('joining_pair_value')) }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Binary Percentage (Admin Plan)</label>
                    <input name="binary_percentage" value="{{ setting('binary_percentage', setting('pair_rate')) }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Daily Cap (Admin Plan)</label>
                    <input name="daily_cap" value="{{ setting('daily_cap', setting('daily_capping')) }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Weekly Cap (Admin Plan)</label>
                    <input name="weekly_cap" value="{{ setting('weekly_cap') }}" class="w-full rounded border p-2" />
                </div>
            </div>
        </div>

        <div class="admin-card p-5">
            <div class="mb-4 text-sm font-semibold text-slate-700">Withdrawal Rules</div>
            <div class="grid grid-cols-1 gap-4 md:grid-cols-4">
                <div>
                    <label class="mb-1 block text-sm font-medium">Withdrawal Min</label>
                    <input name="withdrawal_min" value="{{ setting('withdrawal_min') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Daily Limit</label>
                    <input name="withdrawal_daily_limit" value="{{ setting('withdrawal_daily_limit') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Weekly Limit</label>
                    <input name="withdrawal_weekly_limit" value="{{ setting('withdrawal_weekly_limit') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Fixed Charge</label>
                    <input name="withdrawal_charge_fixed" value="{{ setting('withdrawal_charge_fixed') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">TDS Percent</label>
                    <input name="tds_percent" value="{{ setting('tds_percent') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Charge Percent</label>
                    <input name="charge_percent" value="{{ setting('charge_percent') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Hold Days</label>
                    <input name="hold_days" value="{{ setting('hold_days') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Auto Approve</label>
                    <select name="withdrawal_auto_approve" class="w-full rounded border p-2">
                        <option value="0" @selected(setting('withdrawal_auto_approve', '0') == '0')>Manual</option>
                        <option value="1" @selected(setting('withdrawal_auto_approve', '0') == '1')>Auto</option>
                    </select>
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Payout Gateway</label>
                    <input name="payout_gateway" value="{{ setting('payout_gateway', 'manual') }}" class="w-full rounded border p-2" />
                </div>
            </div>
        </div>

        <div class="admin-card p-5">
            <div class="mb-4 text-sm font-semibold text-slate-700">Identity & UPI Controls</div>
            <div class="grid grid-cols-1 gap-4 md:grid-cols-4">
                <div>
                    <label class="mb-1 block text-sm font-medium">Max IDs per Mobile</label>
                    <input name="max_ids_mobile" value="{{ setting('max_ids_mobile') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Max IDs per Email</label>
                    <input name="max_ids_email" value="{{ setting('max_ids_email') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Max IDs per PAN</label>
                    <input name="max_ids_pan" value="{{ setting('max_ids_pan') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Max IDs per Bank</label>
                    <input name="max_ids_bank" value="{{ setting('max_ids_bank') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">UPI Amount Tolerance</label>
                    <input name="upi_amount_tolerance" value="{{ setting('upi_amount_tolerance') }}" class="w-full rounded border p-2" />
                </div>
            </div>
        </div>

        <div class="admin-card p-5">
            <div class="mb-4 text-sm font-semibold text-slate-700">API & Notifications</div>
            <div class="grid grid-cols-1 gap-4 md:grid-cols-3">
                <div>
                    <label class="mb-1 block text-sm font-medium">API Rate Limit / Minute</label>
                    <input name="api_rate_limit_per_minute" value="{{ setting('api_rate_limit_per_minute') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">API Rate Limit / Day</label>
                    <input name="api_rate_limit_per_day" value="{{ setting('api_rate_limit_per_day') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">SMS Notifications</label>
                    <select name="notification_sms_enabled" class="w-full rounded border p-2">
                        <option value="1" @selected(setting('notification_sms_enabled', '1') == '1')>Enabled</option>
                        <option value="0" @selected(setting('notification_sms_enabled', '1') == '0')>Disabled</option>
                    </select>
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Email Notifications</label>
                    <select name="notification_email_enabled" class="w-full rounded border p-2">
                        <option value="1" @selected(setting('notification_email_enabled', '1') == '1')>Enabled</option>
                        <option value="0" @selected(setting('notification_email_enabled', '1') == '0')>Disabled</option>
                    </select>
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">WhatsApp Notifications</label>
                    <select name="notification_whatsapp_enabled" class="w-full rounded border p-2">
                        <option value="1" @selected(setting('notification_whatsapp_enabled', '0') == '1')>Enabled</option>
                        <option value="0" @selected(setting('notification_whatsapp_enabled', '0') == '0')>Disabled</option>
                    </select>
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Push Notifications</label>
                    <select name="notification_push_enabled" class="w-full rounded border p-2">
                        <option value="1" @selected(setting('notification_push_enabled', '1') == '1')>Enabled</option>
                        <option value="0" @selected(setting('notification_push_enabled', '1') == '0')>Disabled</option>
                    </select>
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Login OTP (Global)</label>
                    <select name="login_otp_enabled" class="w-full rounded border p-2">
                        <option value="1" @selected(setting('login_otp_enabled', '0') == '1')>Enabled</option>
                        <option value="0" @selected(setting('login_otp_enabled', '0') == '0')>Disabled</option>
                    </select>
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Force OTP For All Roles</label>
                    <select name="login_otp_force_all" class="w-full rounded border p-2">
                        <option value="1" @selected(setting('login_otp_force_all', '0') == '1')>Enabled</option>
                        <option value="0" @selected(setting('login_otp_force_all', '0') == '0')>Disabled</option>
                    </select>
                </div>
                <div class="md:col-span-2">
                    <label class="mb-1 block text-sm font-medium">WhatsApp OTP Webhook URL</label>
                    <input name="whatsapp_otp_webhook_url" value="{{ setting('whatsapp_otp_webhook_url') }}" class="w-full rounded border p-2" placeholder="https://your-provider.example/send-otp" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">WhatsApp OTP Webhook Token</label>
                    <input name="whatsapp_otp_webhook_token" value="{{ setting('whatsapp_otp_webhook_token') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">SMTP Mailer</label>
                    <select name="smtp_mailer" class="w-full rounded border p-2">
                        <option value="smtp" @selected(setting('smtp_mailer', 'smtp') === 'smtp')>SMTP</option>
                        <option value="sendmail" @selected(setting('smtp_mailer', 'smtp') === 'sendmail')>Sendmail</option>
                        <option value="log" @selected(setting('smtp_mailer', 'smtp') === 'log')>Log</option>
                    </select>
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">SMTP Host</label>
                    <input name="smtp_host" value="{{ setting('smtp_host') }}" class="w-full rounded border p-2" placeholder="smtp.gmail.com" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">SMTP Port</label>
                    <input name="smtp_port" value="{{ setting('smtp_port', '587') }}" class="w-full rounded border p-2" placeholder="587" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">SMTP Encryption</label>
                    <select name="smtp_encryption" class="w-full rounded border p-2">
                        <option value="tls" @selected(setting('smtp_encryption', 'tls') === 'tls')>TLS</option>
                        <option value="ssl" @selected(setting('smtp_encryption') === 'ssl')>SSL</option>
                        <option value="starttls" @selected(setting('smtp_encryption') === 'starttls')>STARTTLS</option>
                        <option value="none" @selected(setting('smtp_encryption') === '' || setting('smtp_encryption') === null || setting('smtp_encryption') === 'none')>None</option>
                    </select>
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">SMTP Username</label>
                    <input name="smtp_username" value="{{ setting('smtp_username') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">SMTP Password</label>
                    <input type="password" name="smtp_password" class="w-full rounded border p-2" placeholder="Leave blank to keep current password" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">SMTP From Address</label>
                    <input name="smtp_from_address" value="{{ setting('smtp_from_address', setting('company_email')) }}" class="w-full rounded border p-2" placeholder="no-reply@company.com" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">SMTP From Name</label>
                    <input name="smtp_from_name" value="{{ setting('smtp_from_name', setting('company_name')) }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">SMTP Reply-To</label>
                    <input name="smtp_reply_to" value="{{ setting('smtp_reply_to') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">SMTP Timeout (seconds)</label>
                    <input name="smtp_timeout" value="{{ setting('smtp_timeout', '10') }}" class="w-full rounded border p-2" />
                </div>
                <div class="md:col-span-2">
                    <label class="mb-1 block text-sm font-medium">WhatsApp API URL</label>
                    <input name="whatsapp_api_url" value="{{ setting('whatsapp_api_url', setting('whatsapp_otp_webhook_url')) }}" class="w-full rounded border p-2" placeholder="https://provider.example/send" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">WhatsApp API Token</label>
                    <input name="whatsapp_api_token" value="{{ setting('whatsapp_api_token', setting('whatsapp_otp_webhook_token')) }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">WhatsApp Sender ID</label>
                    <input name="whatsapp_sender_id" value="{{ setting('whatsapp_sender_id') }}" class="w-full rounded border p-2" placeholder="LWELLNESS" />
                </div>
                <div class="md:col-span-2">
                    <label class="mb-1 block text-sm font-medium">WhatsApp OTP Template</label>
                    <input name="whatsapp_template_otp" value="{{ setting('whatsapp_template_otp', 'Your OTP is {otp}. Valid for {minutes} minutes. Purpose: {purpose}.') }}" class="w-full rounded border p-2" />
                </div>
                <div class="md:col-span-3">
                    <label class="mb-1 block text-sm font-medium">WhatsApp Notification Template</label>
                    <input name="whatsapp_template_notification" value="{{ setting('whatsapp_template_notification', '{title}: {message} {link}') }}" class="w-full rounded border p-2" />
                </div>
                <div class="md:col-span-2">
                    <label class="mb-1 block text-sm font-medium">SMS API URL</label>
                    <input name="sms_api_url" value="{{ setting('sms_api_url') }}" class="w-full rounded border p-2" placeholder="https://sms-provider.example/send" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">SMS API Token</label>
                    <input name="sms_api_token" value="{{ setting('sms_api_token') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">SMS Sender ID</label>
                    <input name="sms_sender_id" value="{{ setting('sms_sender_id') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Default Country Code</label>
                    <input name="default_country_code" value="{{ setting('default_country_code', '+91') }}" class="w-full rounded border p-2" placeholder="+91" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">Notification HTTP Timeout</label>
                    <input name="notification_http_timeout" value="{{ setting('notification_http_timeout', '10') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">OTP TTL (minutes)</label>
                    <input name="otp_ttl_minutes" value="{{ setting('otp_ttl_minutes', '5') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">OTP Length</label>
                    <input name="otp_length" value="{{ setting('otp_length', '6') }}" class="w-full rounded border p-2" />
                </div>
                <div>
                    <label class="mb-1 block text-sm font-medium">OTP Max Attempts</label>
                    <input name="otp_max_attempts" value="{{ setting('otp_max_attempts', '5') }}" class="w-full rounded border p-2" />
                </div>
            </div>
        </div>

        <button class="rounded bg-emerald-600 px-4 py-2 font-semibold text-white">Save Settings</button>
    </form>

    <div class="mt-6 grid grid-cols-1 gap-6 xl:grid-cols-2">
        <div class="admin-card p-5">
            <div class="mb-4 text-sm font-semibold text-slate-700">SMTP Test</div>
            <form method="POST" action="{{ route('admin.settings.test-email') }}" class="space-y-3">
                @csrf
                <input type="email" name="test_email" class="w-full rounded border p-2" placeholder="recipient@example.com" required>
                <button class="rounded bg-slate-900 px-4 py-2 text-sm text-white">Send Test Email</button>
            </form>
        </div>
        <div class="admin-card p-5">
            <div class="mb-4 text-sm font-semibold text-slate-700">WhatsApp Test</div>
            <form method="POST" action="{{ route('admin.settings.test-whatsapp') }}" class="space-y-3">
                @csrf
                <input type="text" name="test_mobile" class="w-full rounded border p-2" placeholder="+919876543210" required>
                <button class="rounded bg-emerald-600 px-4 py-2 text-sm text-white">Send WhatsApp Test</button>
            </form>
        </div>
    </div>
@endsection
