<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Super Admin · Settings | WEPESI HOTEL MANAGEMENT SOULUTION</title>
    <!-- Font & icon library for clean look -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    
    <style>
        :root {
            --primary: #059669;
            --primary-dark: #047857;
            --primary-light: #10b981;
            --secondary: #64748b;
            --success: #10b981;
            --info: #3b82f6;
            --warning: #f59e0b;
            --danger: #ef4444;
            --gray-50: #f8fafc;
            --gray-100: #f1f5f9;
            --gray-200: #e2e8f0;
            --gray-300: #cbd5e1;
            --gray-600: #475569;
            --gray-700: #334155;
            --gray-800: #1e293b;
            --gray-900: #0f172a;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Inter', sans-serif;
        }

        body {
            background-color: var(--gray-50);
            min-height: 100vh;
            color: var(--gray-800);
        }

        /* Top Navigation */
        .top-nav {
            background: white;
            box-shadow: 0 4px 20px rgba(5, 150, 105, 0.1);
            padding: 1rem 2rem;
            position: sticky;
            top: 0;
            z-index: 1000;
            border-bottom: 3px solid var(--primary);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .nav-brand {
            font-size: 1.5rem;
            font-weight: 800;
            background: linear-gradient(135deg, var(--primary), var(--primary-light));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            display: flex;
            align-items: center;
            gap: 0.75rem;
        }

        .back-to-dashboard {
            background: linear-gradient(135deg, var(--primary), var(--primary-light));
            color: white !important;
            padding: 0.6rem 1.5rem;
            border-radius: 50px;
            text-decoration: none;
            font-weight: 600;
            font-size: 0.9rem;
            display: flex;
            align-items: center;
            gap: 0.5rem;
            transition: all 0.3s;
            box-shadow: 0 4px 12px rgba(5, 150, 105, 0.3);
        }

        .back-to-dashboard:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 16px rgba(5, 150, 105, 0.4);
        }

        .main-container {
            max-width: 1200px;
            margin: 2rem auto;
            padding: 0 2rem;
        }

        .settings-card {
            background: white;
            border-radius: 24px;
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.05);
            border: 1px solid var(--gray-200);
            overflow: hidden;
        }

        .card-header {
            padding: 2rem;
            border-bottom: 1px solid var(--gray-200);
            background: linear-gradient(135deg, #f0fdf4, white);
            display: flex;
            align-items: center;
            gap: 1rem;
        }

        .card-header i {
            font-size: 2rem;
            color: var(--primary);
            background: white;
            padding: 0.75rem;
            border-radius: 16px;
            box-shadow: 0 4px 12px rgba(5, 150, 105, 0.1);
        }

        .card-header h1 {
            font-size: 1.75rem;
            font-weight: 700;
            color: var(--gray-900);
        }

        .settings-section {
            padding: 2rem;
            border-bottom: 1px solid var(--gray-100);
        }

        .settings-section:last-of-type {
            border-bottom: none;
        }

        .section-title {
            display: flex;
            align-items: center;
            gap: 0.75rem;
            font-size: 1.25rem;
            font-weight: 600;
            color: var(--primary-dark);
            margin-bottom: 2rem;
        }

        .section-title i {
            font-size: 1.25rem;
            width: 32px;
            height: 32px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: #dcfce7;
            border-radius: 8px;
            color: var(--primary);
        }

        .settings-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 2rem;
        }

        .setting-item {
            display: flex;
            flex-direction: column;
            gap: 0.5rem;
        }

        .setting-item label {
            font-weight: 600;
            color: var(--gray-700);
            font-size: 0.9rem;
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }

        .setting-item input, 
        .setting-item select, 
        .setting-item textarea {
            background: var(--gray-50);
            border: 2px solid var(--gray-200);
            border-radius: 12px;
            padding: 0.75rem 1rem;
            font-size: 0.95rem;
            transition: all 0.2s;
            color: var(--gray-900);
            outline: none;
        }

        .setting-item input:focus, 
        .setting-item select:focus, 
        .setting-item textarea:focus {
            border-color: var(--primary);
            background: white;
            box-shadow: 0 0 0 4px rgba(5, 150, 105, 0.1);
        }

        .full-width {
            grid-column: 1 / -1;
        }

        .form-actions {
            padding: 2rem;
            background: var(--gray-50);
            display: flex;
            justify-content: flex-end;
            gap: 1rem;
        }

        .btn {
            padding: 0.75rem 2rem;
            border-radius: 50px;
            font-weight: 600;
            font-size: 1rem;
            cursor: pointer;
            transition: all 0.2s;
            display: flex;
            align-items: center;
            gap: 0.5rem;
            border: none;
        }

        .btn-primary {
            background: linear-gradient(135deg, var(--primary), var(--primary-light));
            color: white !important;
            box-shadow: 0 4px 12px rgba(5, 150, 105, 0.2);
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 16px rgba(5, 150, 105, 0.3);
        }

        .btn-secondary {
            background: white;
            border: 2px solid var(--gray-200);
            color: var(--gray-700) !important;
        }

        .btn-secondary:hover {
            background: var(--gray-100);
            border_color: var(--gray-300);
        }

        .alert {
            padding: 1rem 1.5rem;
            border-radius: 12px;
            margin-bottom: 2rem;
            display: flex;
            align-items: center;
            gap: 0.75rem;
            font-weight: 500;
        }

        .alert-success {
            background: #dcfce7;
            color: #166534;
            border-left: 5px solid #22c55e;
        }

        .alert-danger {
            background: #fee2e2;
            color: #991b1b;
            border-left: 5px solid #ef4444;
        }

        @media (max-width: 768px) {
            .main-container {
                padding: 1rem;
            }
            .settings-grid {
                grid-template-columns: 1fr;
            }
        }
        /* 2026 elegance refresh */
        body {
            background:
                radial-gradient(circle at 10% 3%, rgba(16, 185, 129, 0.1), transparent 35%),
                radial-gradient(circle at 96% 8%, rgba(5, 150, 105, 0.08), transparent 32%),
                #f4f8f5;
        }

        .top-nav {
            background: rgba(255, 255, 255, 0.92);
            backdrop-filter: blur(8px);
            box-shadow: 0 10px 28px rgba(5, 150, 105, 0.18);
        }

        .settings-card {
            border-radius: 22px;
            border: 1px solid rgba(5, 150, 105, 0.12);
            box-shadow: 0 16px 34px rgba(5, 150, 105, 0.1);
        }

        .settings-section {
            padding-top: 2.2rem;
            padding-bottom: 2.2rem;
        }

        .setting-item input,
        .setting-item select,
        .setting-item textarea {
            border-radius: 14px;
            border-color: #cfeadf;
        }

        .form-actions {
            border-top: 1px solid rgba(5, 150, 105, 0.1);
        }

        .btn {
            border-radius: 999px;
            font-weight: 600;
        }
    </style>
    <link rel="stylesheet" href="{{ asset('css/admin-elegant-theme.css') }}">
</head>
<body class="super-admin-elegant">
    <nav class="top-nav">
        <a href="{{ route('super.admin.dashboard') }}" class="back-to-dashboard">
            <i class="fas fa-arrow-left"></i>
            <span>Back to Dashboard</span>
        </a>
    </nav>

    <div class="main-container">
        @if(session('success'))
            <div class="alert alert-success">
                <i class="fas fa-check-circle"></i>
                {{ session('success') }}
            </div>
        @endif

        @if(session('error'))
            <div class="alert alert-danger">
                <i class="fas fa-exclamation-circle"></i>
                {{ session('error') }}
            </div>
        @endif

        @if ($errors->any())
            <div class="alert alert-danger">
                <ul class="mb-0">
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

        <div class="settings-card">
            <div class="card-header">
                <i class="fas fa-cog"></i>
                <h1>System Settings</h1>
            </div>

            <form action="{{ route('super.admin.settings.update') }}" method="POST">
                @csrf
                
                <!-- General Section -->
                <div class="settings-section">
                    <div class="section-title">
                        <i class="fas fa-sliders-h"></i>
                        <span>General Settings</span>
                    </div>
                    <div class="settings-grid">
                        <div class="setting-item">
                            <label><i class="fas fa-globe"></i> Site Name</label>
                            <input type="text" name="site_name" value="{{ old('site_name', $settings['site_name'] ?? '') }}" required placeholder="e.g. Deluxe System">
                        </div>
                        <div class="setting-item">
                            <label><i class="fas fa-clock"></i> Timezone</label>
                            <select name="timezone" required>
                                @foreach(timezone_identifiers_list() as $tz)
                                    <option value="{{ $tz }}" {{ old('timezone', $settings['timezone'] ?? 'Africa/Nairobi') == $tz ? 'selected' : '' }}>
                                        {{ $tz }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                        <div class="setting-item">
                            <label><i class="fas fa-dollar-sign"></i> Default Currency</label>
                            <input type="text" name="currency" value="{{ old('currency', $settings['currency'] ?? 'UGX') }}" required maxlength="3">
                        </div>
                        <div class="setting-item">
                            <label><i class="fas fa-calendar-alt"></i> Date Format</label>
                            <select name="date_format" required>
                                <option value="Y-m-d" {{ old('date_format', $settings['date_format'] ?? '') == 'Y-m-d' ? 'selected' : '' }}>YYYY-MM-DD (2025-03-24)</option>
                                <option value="d/m/Y" {{ old('date_format', $settings['date_format'] ?? '') == 'd/m/Y' ? 'selected' : '' }}>DD/MM/YYYY (24/03/2025)</option>
                                <option value="m/d/Y" {{ old('date_format', $settings['date_format'] ?? '') == 'm/d/Y' ? 'selected' : '' }}>MM/DD/YYYY (03/24/2025)</option>
                                <option value="M d, Y" {{ old('date_format', $settings['date_format'] ?? '') == 'M d, Y' ? 'selected' : '' }}>Mar 24, 2025</option>
                            </select>
                        </div>
                        <div class="setting-item">
                            <label><i class="fas fa-clock"></i> Time Format</label>
                            <select name="time_format" required>
                                <option value="H:i" {{ old('time_format', $settings['time_format'] ?? '') == 'H:i' ? 'selected' : '' }}>24-hour (14:30)</option>
                                <option value="h:i A" {{ old('time_format', $settings['time_format'] ?? '') == 'h:i A' ? 'selected' : '' }}>12-hour (02:30 PM)</option>
                            </select>
                        </div>
                        <div class="setting-item">
                            <label><i class="fas fa-hourglass-start"></i> Default Trial Days</label>
                            <input type="number" name="trial_days" value="{{ old('trial_days', $settings['trial_days'] ?? 14) }}" min="0" max="90" required>
                        </div>
                        <div class="setting-item full-width">
                            <label><i class="fas fa-info-circle"></i> Site Description</label>
                            <textarea name="site_description" rows="3" placeholder="Describe the system...">{{ old('site_description', $settings['site_description'] ?? '') }}</textarea>
                        </div>
                    </div>
                </div>

                <!-- Contact Section -->
                <div class="settings-section">
                    <div class="section-title">
                        <i class="fas fa-envelope"></i>
                        <span>Contact & Support</span>
                    </div>
                    <div class="settings-grid">
                        <div class="setting-item">
                            <label><i class="fas fa-at"></i> Contact Email</label>
                            <input type="email" name="contact_email" value="{{ old('contact_email', $settings['contact_email'] ?? '') }}" required placeholder="admin@example.com">
                            <small class="text-muted">Primary system email for notifications</small>
                        </div>
                        <div class="setting-item">
                            <label><i class="fas fa-headset"></i> Support Email</label>
                            <input type="email" name="support_email" value="{{ old('support_email', $settings['support_email'] ?? '') }}" placeholder="support@example.com">
                            <small class="text-muted">Email displayed to users for support</small>
                        </div>
                    </div>
                </div>

                <!-- PesaPal Gateway -->
                <div class="settings-section">
                    <div class="section-title">
                        <i class="fas fa-credit-card"></i>
                        <span>PesaPal Gateway</span>
                    </div>
                    <div class="settings-grid">
                        <div class="setting-item">
                            <label><i class="fas fa-link"></i> PesaPal Base URL</label>
                            <input
                                type="url"
                                name="pesapal_base_url"
                                value="{{ old('pesapal_base_url', $settings['pesapal_base_url'] ?? 'https://cybqa.pesapal.com/pesapalv3') }}"
                                placeholder="https://cybqa.pesapal.com/pesapalv3"
                            >
                            <small class="text-muted">Sandbox: cybqa URL, Production: pay URL.</small>
                        </div>
                        <div class="setting-item">
                            <label><i class="fas fa-key"></i> Consumer Key</label>
                            <input
                                type="password"
                                name="pesapal_consumer_key"
                                value="{{ old('pesapal_consumer_key', $settings['pesapal_consumer_key'] ?? '') }}"
                                placeholder="Enter PesaPal consumer key"
                                autocomplete="off"
                            >
                        </div>
                        <div class="setting-item">
                            <label><i class="fas fa-user-shield"></i> Consumer Secret</label>
                            <input
                                type="password"
                                name="pesapal_consumer_secret"
                                value="{{ old('pesapal_consumer_secret', $settings['pesapal_consumer_secret'] ?? '') }}"
                                placeholder="Enter PesaPal consumer secret"
                                autocomplete="off"
                            >
                        </div>
                        <div class="setting-item">
                            <label><i class="fas fa-coins"></i> PesaPal Currency</label>
                            <input
                                type="text"
                                name="pesapal_currency"
                                value="{{ old('pesapal_currency', $settings['pesapal_currency'] ?? 'UGX') }}"
                                maxlength="3"
                                placeholder="UGX"
                            >
                        </div>
                    </div>
                </div>

                <div class="form-actions">
                    <button type="reset" class="btn btn-secondary">
                        <i class="fas fa-undo"></i>
                        Reset Changes
                    </button>
                    <button type="button" class="btn btn-secondary" onclick="testPesapalConnection()">
                        <i class="fas fa-plug"></i>
                        Test PesaPal Connection
                    </button>
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-save"></i>
                        Save Settings
                    </button>
                </div>
            </form>
        </div>
    </div>

    <script>
        async function testPesapalConnection() {
            const token = document.querySelector('input[name="_token"]')?.value;

            try {
                const resp = await fetch('{{ route('super.admin.settings.test-pesapal') }}', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-CSRF-TOKEN': token,
                        'Accept': 'application/json',
                    },
                    body: JSON.stringify({}),
                });

                const data = await resp.json();
                if (!resp.ok || !data.success) {
                    Swal.fire({
                        icon: 'error',
                        title: 'Connection Failed',
                        text: data.message || 'Could not connect to PesaPal.',
                    });
                    return;
                }

                Swal.fire({
                    icon: 'success',
                    title: 'Connected',
                    text: data.message || 'PesaPal connection successful.',
                });
            } catch (error) {
                Swal.fire({
                    icon: 'error',
                    title: 'Connection Failed',
                    text: 'Could not reach server for connection test.',
                });
            }
        }
    </script>
</body>
</html>