@extends('layouts.app')

@section('content')
<div class="container-fluid mt-4 px-3 px-md-5">
    <div class="row mb-4">
        <div class="col">
            <h2>Pengaturan Sistem</h2>
            <p class="text-muted">Kelola konfigurasi dan pengaturan aplikasi</p>
        </div>
    </div>

    @if ($message = Session::get('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <i class="bi bi-check-circle"></i> {{ $message }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    @endif

    @if ($message = Session::get('error'))
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            <i class="bi bi-exclamation-circle"></i> {{ $message }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    @endif

    <div class="row">
        <div class="col-lg-3 mb-4">
            <!-- Navigation Tabs -->
            <div class="list-group sticky-top" style="top: 70px;">
                <a href="#application" class="list-group-item list-group-item-action active" data-bs-toggle="list" role="tab">
                    <i class="bi bi-app"></i> Aplikasi
                </a>
                <a href="#contact" class="list-group-item list-group-item-action" data-bs-toggle="list" role="tab">
                    <i class="bi bi-telephone"></i> Kontak
                </a>
                <a href="#security" class="list-group-item list-group-item-action" data-bs-toggle="list" role="tab">
                    <i class="bi bi-shield-lock"></i> Keamanan
                </a>
                <a href="#backup" class="list-group-item list-group-item-action" data-bs-toggle="list" role="tab">
                    <i class="bi bi-cloud-download"></i> Backup
                </a>
                <a href="#logging" class="list-group-item list-group-item-action" data-bs-toggle="list" role="tab">
                    <i class="bi bi-file-text"></i> Logging
                </a>
                <a href="#integration" class="list-group-item list-group-item-action" data-bs-toggle="list" role="tab">
                    <i class="bi bi-puzzle"></i> Integrasi
                </a>
                <a href="#maintenance" class="list-group-item list-group-item-action" data-bs-toggle="list" role="tab">
                    <i class="bi bi-hammer"></i> Maintenance
                </a>
            </div>
        </div>

        <div class="col-lg-9">
            <!-- Settings Form -->
            <form action="{{ route('superadmin.settings.update') }}" method="POST" id="settingsForm">
                @csrf

                <!-- Application Settings -->
                <div class="tab-pane fade show active" id="application" role="tabpanel">
                    <div class="card shadow-sm mb-4">
                        <div class="card-header bg-primary text-white">
                            <h5 class="mb-0"><i class="bi bi-app"></i> Pengaturan Aplikasi</h5>
                        </div>
                        <div class="card-body">
                            <div class="mb-3">
                                <label for="app_name" class="form-label">Nama Aplikasi</label>
                                <input type="text" class="form-control @error('app_name') is-invalid @enderror" 
                                       id="app_name" name="app_name" 
                                       value="{{ old('app_name', $settings->get('application')[0]->value ?? 'AppSuper PKLK') }}"
                                       placeholder="Masukkan nama aplikasi">
                                @error('app_name')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>

                            <div class="mb-3">
                                <label for="app_description" class="form-label">Deskripsi Aplikasi</label>
                                <textarea class="form-control @error('app_description') is-invalid @enderror" 
                                          id="app_description" name="app_description" rows="3"
                                          placeholder="Masukkan deskripsi aplikasi">{{ old('app_description', $settings->get('application')[1]->value ?? '') }}</textarea>
                                @error('app_description')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>

                            <div class="mb-3">
                                <label for="app_version" class="form-label">Versi Aplikasi</label>
                                <input type="text" class="form-control @error('app_version') is-invalid @enderror" 
                                       id="app_version" name="app_version" 
                                       value="{{ old('app_version', $settings->get('application')[2]->value ?? '1.0.0') }}"
                                       placeholder="Contoh: 1.0.0">
                                @error('app_version')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>

                            <div class="alert alert-info">
                                <i class="bi bi-info-circle"></i> Pengaturan ini mempengaruhi tampilan dan identitas aplikasi Anda.
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Contact Settings -->
                <div class="tab-pane fade" id="contact" role="tabpanel">
                    <div class="card shadow-sm mb-4">
                        <div class="card-header bg-info text-white">
                            <h5 class="mb-0"><i class="bi bi-telephone"></i> Informasi Kontak</h5>
                        </div>
                        <div class="card-body">
                            <div class="mb-3">
                                <label for="support_email" class="form-label">Email Support</label>
                                <input type="email" class="form-control @error('support_email') is-invalid @enderror" 
                                       id="support_email" name="support_email" 
                                       value="{{ old('support_email', $settings->get('contact')[0]->value ?? '') }}"
                                       placeholder="support@example.com">
                                @error('support_email')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>

                            <div class="mb-3">
                                <label for="support_phone" class="form-label">Nomor Telepon Support</label>
                                <input type="tel" class="form-control @error('support_phone') is-invalid @enderror" 
                                       id="support_phone" name="support_phone" 
                                       value="{{ old('support_phone', $settings->get('contact')[1]->value ?? '') }}"
                                       placeholder="Contoh: +62812345678">
                                @error('support_phone')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>

                            <div class="alert alert-info">
                                <i class="bi bi-info-circle"></i> Informasi ini akan ditampilkan kepada pengguna untuk menghubungi support.
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Security Settings -->
                <div class="tab-pane fade" id="security" role="tabpanel">
                    <div class="card shadow-sm mb-4">
                        <div class="card-header bg-danger text-white">
                            <h5 class="mb-0"><i class="bi bi-shield-lock"></i> Pengaturan Keamanan</h5>
                        </div>
                        <div class="card-body">
                            <div class="mb-3">
                                <div class="form-check form-switch">
                                    <input class="form-check-input" type="checkbox" id="maintenance_mode" 
                                           name="maintenance_mode" value="1"
                                           @if(old('maintenance_mode', $settings->get('security')[0]->value ?? false)) checked @endif>
                                    <label class="form-check-label" for="maintenance_mode">
                                        Mode Pemeliharaan
                                    </label>
                                </div>
                                <small class="text-muted d-block mt-2">Aktifkan mode pemeliharaan untuk menutup akses aplikasi sementara.</small>
                            </div>

                            <hr>

                            <div class="mb-3">
                                <label for="max_login_attempts" class="form-label">Maksimal Percobaan Login</label>
                                <input type="number" class="form-control @error('max_login_attempts') is-invalid @enderror" 
                                       id="max_login_attempts" name="max_login_attempts" 
                                       value="{{ old('max_login_attempts', $settings->get('security')[1]->value ?? 5) }}"
                                       min="1" max="100">
                                @error('max_login_attempts')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                                <small class="text-muted">Jumlah percobaan login yang diizinkan sebelum akun dikunci sementara.</small>
                            </div>

                            <div class="mb-3">
                                <label for="session_timeout" class="form-label">Session Timeout (menit)</label>
                                <input type="number" class="form-control @error('session_timeout') is-invalid @enderror" 
                                       id="session_timeout" name="session_timeout" 
                                       value="{{ old('session_timeout', $settings->get('security')[2]->value ?? 30) }}"
                                       min="5" max="1440">
                                @error('session_timeout')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                                <small class="text-muted">Waktu session sebelum user otomatis logout (dalam menit).</small>
                            </div>

                            <div class="alert alert-warning">
                                <i class="bi bi-exclamation-triangle"></i> Pengaturan keamanan sangat penting untuk melindungi aplikasi dan data pengguna.
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Backup Settings -->
                <div class="tab-pane fade" id="backup" role="tabpanel">
                    <div class="card shadow-sm mb-4">
                        <div class="card-header bg-success text-white">
                            <h5 class="mb-0"><i class="bi bi-cloud-download"></i> Pengaturan Backup</h5>
                        </div>
                        <div class="card-body">
                            <div class="mb-3">
                                <div class="form-check form-switch">
                                    <input class="form-check-input" type="checkbox" id="backup_enabled" 
                                           name="backup_enabled" value="1"
                                           @if(old('backup_enabled', $settings->get('backup')[0]->value ?? true)) checked @endif>
                                    <label class="form-check-label" for="backup_enabled">
                                        Aktifkan Backup Otomatis
                                    </label>
                                </div>
                                <small class="text-muted d-block mt-2">Sistem akan secara otomatis membuat backup database dan file penting.</small>
                            </div>

                            <div class="mb-3">
                                <label for="backup_frequency" class="form-label">Frekuensi Backup</label>
                                <select class="form-select @error('backup_frequency') is-invalid @enderror" 
                                        id="backup_frequency" name="backup_frequency">
                                    <option value="">-- Pilih Frekuensi --</option>
                                    <option value="daily" @selected(old('backup_frequency', $settings->get('backup')[1]->value ?? 'daily') === 'daily')>Harian</option>
                                    <option value="weekly" @selected(old('backup_frequency', $settings->get('backup')[1]->value ?? 'daily') === 'weekly')>Mingguan</option>
                                    <option value="monthly" @selected(old('backup_frequency', $settings->get('backup')[1]->value ?? 'daily') === 'monthly')>Bulanan</option>
                                </select>
                                @error('backup_frequency')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>

                            <div class="alert alert-info">
                                <i class="bi bi-info-circle"></i> Backup otomatis sangat penting untuk pemulihan data dalam situasi darurat.
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Logging Settings -->
                <div class="tab-pane fade" id="logging" role="tabpanel">
                    <div class="card shadow-sm mb-4">
                        <div class="card-header bg-warning text-dark">
                            <h5 class="mb-0"><i class="bi bi-file-text"></i> Pengaturan Logging</h5>
                        </div>
                        <div class="card-body">
                            <div class="mb-3">
                                <label for="log_level" class="form-label">Level Logging</label>
                                <select class="form-select @error('log_level') is-invalid @enderror" 
                                        id="log_level" name="log_level">
                                    <option value="">-- Pilih Level --</option>
                                    <option value="debug" @selected(old('log_level', $settings->get('logging')[0]->value ?? 'info') === 'debug')>Debug</option>
                                    <option value="info" @selected(old('log_level', $settings->get('logging')[0]->value ?? 'info') === 'info')>Info</option>
                                    <option value="notice" @selected(old('log_level', $settings->get('logging')[0]->value ?? 'info') === 'notice')>Notice</option>
                                    <option value="warning" @selected(old('log_level', $settings->get('logging')[0]->value ?? 'info') === 'warning')>Warning</option>
                                    <option value="error" @selected(old('log_level', $settings->get('logging')[0]->value ?? 'info') === 'error')>Error</option>
                                </select>
                                @error('log_level')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                                <small class="text-muted">Level logging yang lebih tinggi akan mencatat lebih banyak informasi.</small>
                            </div>

                            <div class="alert alert-info">
                                <i class="bi bi-info-circle"></i> Log membantu dalam debugging dan monitoring sistem.
                            </div>

                            <form action="{{ route('superadmin.settings.clearLogs') }}" method="POST" class="d-inline">
                                @csrf
                                <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Yakin ingin menghapus semua log?');">
                                    <i class="bi bi-trash"></i> Hapus Semua Log
                                </button>
                            </form>
                        </div>
                    </div>
                </div>

                <!-- Integration Settings -->
                <div class="tab-pane fade" id="integration" role="tabpanel">
                    <div class="card shadow-sm mb-4">
                        <div class="card-header bg-secondary text-white">
                            <h5 class="mb-0"><i class="bi bi-puzzle"></i> Pengaturan Integrasi</h5>
                        </div>
                        <div class="card-body">
                            <div class="mb-3">
                                <label for="sms_gateway" class="form-label">Provider Gateway SMS</label>
                                <select class="form-select @error('sms_gateway') is-invalid @enderror" 
                                        id="sms_gateway" name="sms_gateway">
                                    <option value="">-- Pilih Provider --</option>
                                    <option value="twilio" @selected(old('sms_gateway', $settings->get('integration')[0]->value ?? '') === 'twilio')>Twilio</option>
                                    <option value="nexmo" @selected(old('sms_gateway', $settings->get('integration')[0]->value ?? '') === 'nexmo')>Nexmo/Vonage</option>
                                    <option value="aws_sns" @selected(old('sms_gateway', $settings->get('integration')[0]->value ?? '') === 'aws_sns')>AWS SNS</option>
                                </select>
                                @error('sms_gateway')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>

                            <div class="mb-3">
                                <div class="form-check form-switch">
                                    <input class="form-check-input" type="checkbox" id="email_notification" 
                                           name="email_notification" value="1"
                                           @if(old('email_notification', $settings->get('integration')[1]->value ?? true)) checked @endif>
                                    <label class="form-check-label" for="email_notification">
                                        Aktifkan Notifikasi Email
                                    </label>
                                </div>
                                <small class="text-muted d-block mt-2">Sistem akan mengirim email notifikasi untuk event penting.</small>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Maintenance -->
                <div class="tab-pane fade" id="maintenance" role="tabpanel">
                    <div class="card shadow-sm mb-4">
                        <div class="card-header bg-dark text-white">
                            <h5 class="mb-0"><i class="bi bi-hammer"></i> Maintenance & Utilities</h5>
                        </div>
                        <div class="card-body">
                            <p class="text-muted mb-3">Tools untuk maintenance dan optimasi sistem aplikasi.</p>

                            <form action="{{ route('superadmin.settings.clearCache') }}" method="POST" class="d-inline">
                                @csrf
                                <button type="submit" class="btn btn-warning btn-sm" onclick="return confirm('Yakin ingin menghapus cache aplikasi?');">
                                    <i class="bi bi-arrow-clockwise"></i> Clear Cache
                                </button>
                            </form>

                            <div class="alert alert-info mt-3">
                                <i class="bi bi-info-circle"></i> 
                                <strong>Cache:</strong> Menghapus cache dapat membantu mengatasi masalah performa atau bug. Aplikasi akan membuat cache baru secara otomatis.
                            </div>

                            <div class="alert alert-success mt-3">
                                <strong>Versi:</strong> 
                                <code>{{ old('app_version', $settings->get('application')[2]->value ?? '1.0.0') }}</code>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Submit Button -->
                <div class="card shadow-sm">
                    <div class="card-body d-flex justify-content-end gap-2">
                        <a href="{{ route('dashboard') }}" class="btn btn-secondary">
                            <i class="bi bi-arrow-left"></i> Kembali
                        </a>
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-check-circle"></i> Simpan Pengaturan
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    // Tab navigation
    document.querySelectorAll('[role="tab"]').forEach(tab => {
        tab.addEventListener('click', function() {
            // Remove active from all tabs
            document.querySelectorAll('[role="tab"]').forEach(t => t.classList.remove('active'));
            // Hide all panes
            document.querySelectorAll('[role="tabpanel"]').forEach(p => {
                p.classList.remove('show', 'active');
            });
            
            // Add active to clicked tab
            this.classList.add('active');
            // Show corresponding pane
            const target = this.getAttribute('href').substring(1);
            document.getElementById(target).classList.add('show', 'active');
        });
    });
</script>
@endsection
